ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject svn commit: r1513422 [2/2] - in /ace/sandbox/bramk: cnf/localrepo/ cnf/localrepo/org.apache.ace.builder/ cnf/localrepo/osgi.cmpn/ org.apache.ace.agent.launcher/src/org/apache/ace/agent/launcher/ org.apache.ace.agent/ org.apache.ace.agent/.settings/ org...
Date Tue, 13 Aug 2013 10:22:02 GMT
Added: ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/deployment/impl/DeploymentHandlerTest.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/deployment/impl/DeploymentHandlerTest.java?rev=1513422&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/deployment/impl/DeploymentHandlerTest.java
(added)
+++ ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/deployment/impl/DeploymentHandlerTest.java
Tue Aug 13 10:22:01 2013
@@ -0,0 +1,168 @@
+package org.apache.ace.agent.deployment.impl;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.notNull;
+import static org.easymock.EasyMock.replay;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.ace.agent.AgentConstants;
+import org.apache.ace.agent.connection.impl.ConnectionHandlerImpl;
+import org.apache.ace.agent.discovery.DiscoveryHandler;
+import org.apache.ace.agent.identification.IdentificationHandler;
+import org.apache.ace.agent.impl.AgentContext;
+import org.apache.ace.agent.testutil.BaseWebServerTest;
+import org.osgi.framework.Version;
+import org.osgi.service.deploymentadmin.DeploymentAdmin;
+import org.osgi.service.deploymentadmin.DeploymentPackage;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+public class DeploymentHandlerTest extends BaseWebServerTest {
+
+    static class TestDeploymentServlet extends HttpServlet {
+        private static final long serialVersionUID = 1L;
+
+        private Map<String, File> m_packages = new HashMap<String, File>();
+
+        @Override
+        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
+
+            String pathInfo = req.getPathInfo();
+            if (pathInfo != null && pathInfo.startsWith("/"))
+                pathInfo = pathInfo.substring(1);
+
+            if (pathInfo != null && m_packages.containsKey(pathInfo)) {
+                File file = m_packages.get(pathInfo);
+                resp.setHeader(AgentConstants.HEADER_DPSIZE, "" + file.length());
+                FileInputStream fis = new FileInputStream(file);
+                OutputStream os = resp.getOutputStream();
+                int b;
+                while ((b = fis.read()) != -1) {
+                    os.write(b);
+                }
+                fis.close();
+                os.close();
+            }
+            else {
+                PrintWriter writer = resp.getWriter();
+                for (String version : m_packages.keySet()) {
+                    writer.println(version);
+                }
+                writer.close();
+            }
+            resp.setStatus(200, "voila");
+        }
+
+        void addPackage(String version, File file) {
+            m_packages.put(version, file);
+        }
+    }
+
+    DeploymentHandlerImpl m_deploymentHandler;
+
+    Version version1 = Version.parseVersion("1.0.0");
+    Version version2 = Version.parseVersion("2.0.0");
+    Version version3 = Version.parseVersion("3.0.0");
+
+    long m_remotePackageSize = 0l;
+
+    @BeforeClass
+    public void setUpOnceAgain() throws Exception {
+
+        int port = 8888;
+        String identification = "agent";
+        URL serverURL = new URL("http://localhost:" + port + "/");
+
+        TestDeploymentServlet servlet = new TestDeploymentServlet();
+        servlet.addPackage(version1.toString(), m_200file);
+        servlet.addPackage(version2.toString(), m_200file);
+        servlet.addPackage(version3.toString(), m_200file);
+
+        m_remotePackageSize = m_200file.length();
+        m_webserver.addServlet(servlet, "/deployment/" + identification + "/versions/*");
+
+        DeploymentPackage deploymentPackage1 = createMock("dp1", DeploymentPackage.class);
+        expect(deploymentPackage1.getName()).andReturn(identification).anyTimes();
+        expect(deploymentPackage1.getVersion()).andReturn(Version.parseVersion("1.0.0")).anyTimes();
+
+        DeploymentPackage deploymentPackage2 = createMock("dp2", DeploymentPackage.class);
+        expect(deploymentPackage2.getName()).andReturn(identification).anyTimes();
+        expect(deploymentPackage2.getVersion()).andReturn(Version.parseVersion("2.0.0")).anyTimes();
+
+        DeploymentPackage deploymentPackage3 = createMock("dp2", DeploymentPackage.class);
+        expect(deploymentPackage3.getName()).andReturn(identification).anyTimes();
+        expect(deploymentPackage3.getVersion()).andReturn(Version.parseVersion("3.0.0")).anyTimes();
+
+        IdentificationHandler identificationHandler = createMock(IdentificationHandler.class);
+        expect(identificationHandler.getIdentification()).andReturn(identification).anyTimes();
+
+        DiscoveryHandler discoveryHandler = createMock(DiscoveryHandler.class);
+        expect(discoveryHandler.getServerUrls()).andReturn(new URL[] { serverURL }).anyTimes();
+
+        DeploymentAdmin deploymentAdmin = createMock(DeploymentAdmin.class);
+        expect(deploymentAdmin.listDeploymentPackages()).andReturn(
+            new DeploymentPackage[] { deploymentPackage2, deploymentPackage1 }).anyTimes();
+        expect(deploymentAdmin.installDeploymentPackage(notNull(InputStream.class)
+            )).andReturn(deploymentPackage3).once();
+
+        AgentContext agentContext = createMock("ctx", AgentContext.class);
+        expect(agentContext.getIdentificationHandler()).andReturn(identificationHandler).anyTimes();
+        expect(agentContext.getDiscoveryHandler()).andReturn(discoveryHandler).anyTimes();
+        expect(agentContext.getConnectionHandler()).andReturn(new ConnectionHandlerImpl(agentContext)).anyTimes();
+
+        replay(deploymentPackage1, deploymentPackage2, deploymentAdmin, agentContext, identificationHandler,
discoveryHandler);
+
+        m_deploymentHandler = new DeploymentHandlerImpl(agentContext, deploymentAdmin);
+    }
+
+    @Test
+    public void testCurrentVersion() throws Exception {
+        SortedSet<Version> installed = m_deploymentHandler.getInstalledVersions();
+        assertNotNull(installed);
+        assertFalse(installed.isEmpty());
+        assertEquals(installed.last(), version2);
+    }
+
+    @Test
+    public void testAvailableVersions() throws Exception {
+        SortedSet<Version> expected = new TreeSet<Version>();
+        expected.add(version1);
+        expected.add(version2);
+        expected.add(version3);
+        SortedSet<Version> available = m_deploymentHandler.getAvailableVersions();
+        assertNotNull(available);
+        assertFalse(available.isEmpty());
+        assertEquals(available, expected);
+    }
+
+    @Test
+    public void testPackageSize() throws Exception {
+        long packageSize = m_deploymentHandler.getPackageSize(version1, true);
+        assertEquals(packageSize, m_remotePackageSize);
+    }
+
+    @Test
+    public void testDeployPackage() throws Exception {
+        m_deploymentHandler.deployPackage(version3, true);
+    }
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/download/impl/DownloadHandlerTest.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/download/impl/DownloadHandlerTest.java?rev=1513422&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/download/impl/DownloadHandlerTest.java
(added)
+++ ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/download/impl/DownloadHandlerTest.java
Tue Aug 13 10:22:01 2013
@@ -0,0 +1,181 @@
+package org.apache.ace.agent.download.impl;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.*;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertSame;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.math.BigInteger;
+import java.security.DigestInputStream;
+import java.security.MessageDigest;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.ace.agent.download.DownloadHandle;
+import org.apache.ace.agent.download.DownloadHandle.CompletedListener;
+import org.apache.ace.agent.download.DownloadHandle.ProgressListener;
+import org.apache.ace.agent.download.DownloadResult;
+import org.apache.ace.agent.download.DownloadState;
+import org.apache.ace.agent.impl.AgentContext;
+import org.apache.ace.agent.testutil.BaseWebServerTest;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+public class DownloadHandlerTest extends BaseWebServerTest {
+
+    static class TestErrorServlet extends HttpServlet {
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        public void service(ServletRequest req, ServletResponse res) throws ServletException,
IOException {
+            String retry = req.getParameter("retry");
+            if (retry != null)
+                ((HttpServletResponse) res).setHeader("Retry-After", retry);
+            int code = 500;
+            String status = req.getParameter("status");
+            if (status != null)
+                code = Integer.parseInt(status);
+            ((HttpServletResponse) res).sendError(code, "You asked for it");
+        }
+    }
+
+    protected DownloadHandlerImpl m_handler;
+
+    @BeforeClass
+    public void setUpOnceAgain() throws Exception {
+
+        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
+        AgentContext agentContext = createMock("ctx", AgentContext.class);
+        expect(agentContext.getExecutorService()).andReturn(executorService).anyTimes();
+        replay(agentContext);
+
+        m_webserver.addServlet(new TestErrorServlet(), "/error");
+        m_handler = new DownloadHandlerImpl(agentContext);
+    }
+
+    @Test
+    public void testSuccessful_noresume_result() throws Exception {
+        final DownloadHandle handle = m_handler.getHandle(m_200url).start();
+        final DownloadResult result = handle.result();
+        assertSuccessFul(result, 200, m_200digest);
+    }
+
+    @Test
+    public void testSuccessful_noresume_listener() throws Exception {
+        final CountDownLatch latch = new CountDownLatch(1);
+        final List<DownloadResult> holder = new ArrayList<DownloadResult>();
+        final DownloadHandle handle = m_handler.getHandle(m_200url)
+            .setCompletionListener(new CompletedListener() {
+                @Override
+                public void completed(DownloadResult result) {
+                    holder.add(result);
+                    latch.countDown();
+                }
+            }).start();
+        latch.await();
+        assertSuccessFul(holder.get(0), 200, m_200digest);
+        assertSame(handle.result(), holder.get(0), "Await should return same result given
to the completion handler.");
+    }
+
+    @Test
+    public void testSuccessful_resume_result() throws Exception {
+        final DownloadHandle handle = m_handler.getHandle(m_200url);
+        handle.setProgressListener(new ProgressListener() {
+            @Override
+            public void progress(long contentLength, long progress) {
+                handle.stop();
+            }
+        }).start();
+        assertStopped(handle.result(), 200);
+        assertStopped(handle.start().result(), 206);
+        assertSuccessFul(handle.setProgressListener(null).start().result(), 206, m_200digest);
+    }
+
+    @Test
+    public void testFailedIO_nostatus_result() throws Exception {
+        DownloadHandle handle = m_handler.getHandle(m_200url, 2048);
+
+        DownloadResult result = ((DownloadHandleImpl) handle).start(DownloadCallableImpl.FAIL_OPENCONNECTION).result();
+        assertFailed(result, 0);
+        assertNull(result.getHeaders());
+
+        result = ((DownloadHandleImpl) handle).start(DownloadCallableImpl.FAIL_OPENINPUTSTREAM).result();
+        assertFailed(result, 200);
+        assertNotNull(result.getHeaders());
+
+        result = ((DownloadHandleImpl) handle).start(DownloadCallableImpl.FAIL_OPENOUTPUTSTREAM).result();
+        assertFailed(result, 200);
+        assertNotNull(result.getHeaders());
+
+        result = ((DownloadHandleImpl) handle).start(DownloadCallableImpl.FAIL_AFTERFIRSTWRITE).result();
+        assertFailed(result, 200);
+        assertNotNull(result.getHeaders());
+
+        result = ((DownloadHandleImpl) handle).start(DownloadCallableImpl.FAIL_AFTERFIRSTWRITE).result();
+        assertFailed(result, 206);
+        assertNotNull(result.getHeaders());
+
+        result = handle.start().result();
+        assertSuccessFul(result, 206, m_200digest);
+    }
+
+    @Test
+    public void testFailed404_noresume_result() throws Exception {
+        final DownloadResult result = m_handler.getHandle(m_404url).start().result();
+        assertFailed(result, 404);
+    }
+
+    @Test
+    public void testFailed503_noresume_result() throws Exception {
+        DownloadResult result = m_handler.getHandle(m_503url).start().result();
+        assertFailed(result, 503);
+        assertNotNull(result.getHeaders().get("Retry-After"), "Expected a Retry-After header
from error servlet");
+        assertNotNull(result.getHeaders().get("Retry-After").get(0), "Expected a Retry-After
header from error servlet");
+        assertEquals(result.getHeaders().get("Retry-After").get(0), "500", "Expected a Retry-After
header from error servlet");
+    }
+
+    private static void assertSuccessFul(final DownloadResult result, int statusCode, String
digest) throws Exception {
+        assertEquals(result.getState(), DownloadState.SUCCESSFUL, "Expected state SUCCESSFUL
after succesful completion");
+        assertEquals(result.getCode(), statusCode, "Expected statusCode " + statusCode +
" after successful completion");
+        assertNotNull(result.getFile(), "Expected non null file after successful completion");
+        assertNotNull(result.getHeaders(), "Expected non null headers after successful completion");
+        assertNull(result.getCause(), "Excpected null cause after successful completion");
+        assertEquals(getDigest(result.getFile()), digest, "Expected same digest after successful
completion");
+    }
+
+    private static void assertFailed(final DownloadResult result, int statusCode) throws
Exception {
+        assertEquals(result.getState(), DownloadState.FAILED, "DownloadState must be FAILED
after failed completion");
+        assertEquals(result.getCode(), statusCode, "Expected statusCode " + statusCode +
" after failed completion");
+        assertNull(result.getFile(), "File must not be null after failed completion");
+    }
+
+    private static void assertStopped(final DownloadResult result, int statusCode) throws
Exception {
+        assertEquals(result.getState(), DownloadState.STOPPED, "DownloadState must be STOPPED
after stopped completion");
+        assertEquals(result.getCode(), statusCode, "Expected statusCode " + statusCode +
" after stopped completion");
+        assertNotNull(result.getHeaders(), "Expected headers not to be null after stopped
completion");
+        assertNull(result.getFile(), "File must not be null after failed download");
+        assertNull(result.getCause(), "Excpected cause to null null after stopped completion");
+    }
+
+    private static String getDigest(File file) throws Exception {
+        DigestInputStream dis = new DigestInputStream(new FileInputStream(file), MessageDigest.getInstance("MD5"));
+        while (dis.read() != -1) {
+        }
+        dis.close();
+        return new BigInteger(dis.getMessageDigest().digest()).toString();
+    }
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/impl/CustomControllerTest.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/impl/CustomControllerTest.java?rev=1513422&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/impl/CustomControllerTest.java
(added)
+++ ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/impl/CustomControllerTest.java
Tue Aug 13 10:22:01 2013
@@ -0,0 +1,123 @@
+package org.apache.ace.agent.impl;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.testng.Assert.assertTrue;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.ace.agent.AgentConstants;
+import org.apache.ace.agent.AgentControl;
+import org.apache.ace.agent.connection.ConnectionHandler;
+import org.apache.ace.agent.connection.impl.ConnectionHandlerImpl;
+import org.apache.ace.agent.deployment.DeploymentHandler;
+import org.apache.ace.agent.deployment.impl.DeploymentHandlerImpl;
+import org.apache.ace.agent.discovery.DiscoveryHandler;
+import org.apache.ace.agent.download.DownloadHandler;
+import org.apache.ace.agent.download.impl.DownloadHandlerImpl;
+import org.apache.ace.agent.identification.IdentificationHandler;
+import org.apache.ace.agent.testutil.TestWebServer;
+import org.osgi.service.deploymentadmin.DeploymentAdmin;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+public class CustomControllerTest {
+
+    static class TestDeploymentServlet extends HttpServlet {
+        private static final long serialVersionUID = 1L;
+
+        private Map<String, File> m_packages = new HashMap<String, File>();
+
+        @Override
+        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
+
+            String pathInfo = req.getPathInfo();
+            if (pathInfo != null && pathInfo.startsWith("/"))
+                pathInfo = pathInfo.substring(1);
+
+            if (pathInfo != null && m_packages.containsKey(pathInfo)) {
+                File file = m_packages.get(pathInfo);
+                resp.setHeader(AgentConstants.HEADER_DPSIZE, "" + file.length());
+                FileInputStream fis = new FileInputStream(file);
+                OutputStream os = resp.getOutputStream();
+                int b;
+                while ((b = fis.read()) != -1) {
+                    os.write(b);
+                }
+                fis.close();
+                os.close();
+            }
+            else {
+                PrintWriter writer = resp.getWriter();
+                for (String version : m_packages.keySet()) {
+                    writer.println(version);
+                }
+                writer.close();
+            }
+            resp.setStatus(200, "voila");
+        }
+
+        void addPackage(String version, File file) {
+            m_packages.put(version, file);
+        }
+    }
+
+    TestWebServer m_webServer;
+    AgentControl m_agentControl;
+
+    @BeforeClass
+    public void setUpOnceAgain() throws Exception {
+
+        String identification = "agent007";
+        int port = 8888;
+
+        m_webServer = new TestWebServer(port, "/", "generated");
+        m_webServer.addServlet(new TestDeploymentServlet(), "/deployment/" + identification
+ "/versions/*");
+
+        AgentContext agentContext = createMock("ctx", AgentContext.class);
+
+        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
+        ConnectionHandler connectionHandler = new ConnectionHandlerImpl(agentContext);
+        DownloadHandler downloadHandler = new DownloadHandlerImpl(agentContext);
+
+        DeploymentAdmin deploymentAdmin = createNiceMock("da", DeploymentAdmin.class);
+        DeploymentHandler deploymentHandler = new DeploymentHandlerImpl(agentContext, deploymentAdmin);
+
+        IdentificationHandler identificationHandler = createMock(IdentificationHandler.class);
+        expect(identificationHandler.getIdentification()).andReturn(identification).anyTimes();
+
+        DiscoveryHandler discoveryHandler = createMock(DiscoveryHandler.class);
+        expect(discoveryHandler.getServerUrls()).andReturn(new URL[] { new URL("http://localhost:"
+ port) }).anyTimes();
+
+        expect(agentContext.getExecutorService()).andReturn(executorService).anyTimes();
+        expect(agentContext.getIdentificationHandler()).andReturn(identificationHandler).anyTimes();
+        expect(agentContext.getDiscoveryHandler()).andReturn(discoveryHandler).anyTimes();
+        expect(agentContext.getDownloadHandler()).andReturn(downloadHandler).anyTimes();
+        expect(agentContext.getDeploymentHandler()).andReturn(deploymentHandler).anyTimes();
+        expect(agentContext.getConnectionHandler()).andReturn(connectionHandler).anyTimes();
+
+        replay(agentContext, identificationHandler, discoveryHandler, deploymentAdmin);
+        m_agentControl = new AgentControlImpl(agentContext);
+    }
+
+    @Test
+    public void testTrue() throws Exception {
+        assertTrue(true);
+    }
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseWebServerTest.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseWebServerTest.java?rev=1513422&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseWebServerTest.java
(added)
+++ ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/BaseWebServerTest.java
Tue Aug 13 10:22:01 2013
@@ -0,0 +1,49 @@
+package org.apache.ace.agent.testutil;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.math.BigInteger;
+import java.net.URL;
+import java.security.DigestOutputStream;
+import java.security.MessageDigest;
+
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+
+public abstract class BaseWebServerTest {
+
+    protected TestWebServer m_webserver;
+
+    protected URL m_200url;
+    protected File m_200file;
+    protected String m_200digest;
+
+    protected URL m_404url;
+    protected URL m_503url;
+
+    @BeforeClass
+    public void setUpOnce() throws Exception {
+
+        m_webserver = new TestWebServer(8888, "/", "generated");
+        m_webserver.start();
+
+        m_200url = new URL("http://localhost:8888/testfile.txt");
+        m_404url = new URL("http://localhost:8888/error?status=404");
+        m_503url = new URL("http://localhost:8888/error?status=503&retry=500");
+
+        m_200file = new File(new File("generated"), "testfile.txt");
+        DigestOutputStream dos = new DigestOutputStream(new FileOutputStream(m_200file),
MessageDigest.getInstance("MD5"));
+        for (int i = 0; i < 10000; i++) {
+            dos.write(String.valueOf(System.currentTimeMillis()).getBytes());
+            dos.write(" Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum\n".getBytes());
+        }
+        dos.close();
+        m_200digest = new BigInteger(dos.getMessageDigest().digest()).toString();
+    }
+
+    @AfterClass
+    public void tearDownOnce() throws Exception {
+        m_webserver.stop();
+    }
+
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/TestWebServer.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/TestWebServer.java?rev=1513422&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/TestWebServer.java
(added)
+++ ace/sandbox/bramk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/TestWebServer.java
Tue Aug 13 10:22:01 2013
@@ -0,0 +1,138 @@
+package org.apache.ace.agent.testutil;
+
+import java.io.IOException;
+import java.util.Date;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletResponseWrapper;
+
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.servlet.DefaultServlet;
+import org.eclipse.jetty.servlet.FilterHolder;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.servlet.ServletHolder;
+
+public class TestWebServer {
+
+    private Server m_server;
+    private ServletContextHandler m_contextHandler;
+
+    public TestWebServer(int port, String contextPath, String basePath) throws Exception
{
+
+        m_server = new Server(port);
+
+        m_contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
+        m_contextHandler.setContextPath("/");
+
+        ServletHolder holder = new ServletHolder(new DefaultServlet());
+        holder.setInitParameter("resourceBase", basePath);
+        holder.setInitParameter("pathInfoOnly", "true");
+        holder.setInitParameter("acceptRanges", "true");
+        holder.setInitParameter("dirAllowed", "true");
+
+        m_contextHandler.addFilter(new FilterHolder(new HttpDumpFilter()), "/*", null);
+        m_contextHandler.addServlet(holder, contextPath.concat(contextPath.endsWith("/")
? "*" : "/*"));
+        m_server.setHandler(m_contextHandler);
+    }
+
+    public void start() throws Exception {
+        m_server.start();
+    }
+
+    public void stop() throws Exception {
+        m_server.stop();
+        m_server.join();
+    }
+
+    public void addServlet(Servlet servlet, String pathPsec) {
+        m_contextHandler.addServlet(new ServletHolder(servlet), pathPsec);
+    }
+
+    static class HttpDumpFilter implements Filter {
+
+        @Override
+        public void init(FilterConfig arg0) throws ServletException {
+        }
+
+        @Override
+        public void destroy() {
+        }
+
+        @Override
+        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
+            HttpServletRequest hreq = (HttpServletRequest) req;
+            HttpServletResponse hres = (HttpServletResponse) res;
+
+            Enumeration<String> attrs = hreq.getHeaderNames();
+            System.out.println("> " + hreq.getMethod() + " " + hreq.getRequestURI() +
" " + req.getProtocol());
+            while (attrs.hasMoreElements()) {
+                String attr = attrs.nextElement();
+                System.out.println("> " + attr + ":" + hreq.getHeader(attr));
+            }
+            ResponseInfoCollector coll = new ResponseInfoCollector(hres);
+            chain.doFilter(req, coll);
+
+            // servlet API 3.0
+            // System.out.println("< " + res.getStatus());
+            // for (String headerName : res.getHeaderNames())
+            // System.out.println("< " + headerName + ":" + res.getHeader(headerName));
+            System.out.println("< " + coll.statusCode + " " + coll.statusMessage);
+            for (String headerName : coll.headers.keySet()) {
+                System.out.println("< " + headerName + ":" + coll.headers.get(headerName));
+            }
+        }
+    }
+
+    static class ResponseInfoCollector extends HttpServletResponseWrapper {
+
+        long statusCode;
+        String statusMessage;
+        Map<String, String> headers = new HashMap<String, String>();
+
+        public ResponseInfoCollector(HttpServletResponse response) {
+            super(response);
+        }
+
+        @Override
+        public void setHeader(String name, String value) {
+            headers.put(name, value);
+            super.setHeader(name, value);
+        }
+
+        @Override
+        public void setDateHeader(String name, long date) {
+            headers.put(name, new Date(date).toString());
+            super.setDateHeader(name, date);
+        }
+
+        @Override
+        public void setIntHeader(String name, int value) {
+            headers.put(name, "" + value);
+            super.setIntHeader(name, value);
+        }
+
+        @Override
+        public void setStatus(int sc, String sm) {
+            statusCode = sc;
+            statusMessage = sm;
+            super.setStatus(sc, sm);
+        }
+
+        @Override
+        public void setStatus(int sc) {
+            statusCode = sc;
+            super.setStatus(sc);
+        }
+    }
+}



Mime
View raw message