ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1527522 [3/3] - in /ace/trunk: org.apache.ace.client.repository.itest/src/org/apache/ace/it/repositoryadmin/ org.apache.ace.client.repository/src/org/apache/ace/client/repository/helper/ org.apache.ace.client.repository/src/org/apache/ace/...
Date Mon, 30 Sep 2013 12:00:27 GMT
Modified: ace/trunk/org.apache.ace.deployment/test/org/apache/ace/deployment/servlet/DeploymentServletTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.deployment/test/org/apache/ace/deployment/servlet/DeploymentServletTest.java?rev=1527522&r1=1527521&r2=1527522&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.deployment/test/org/apache/ace/deployment/servlet/DeploymentServletTest.java
(original)
+++ ace/trunk/org.apache.ace.deployment/test/org/apache/ace/deployment/servlet/DeploymentServletTest.java
Mon Sep 30 12:00:26 2013
@@ -21,12 +21,16 @@ package org.apache.ace.deployment.servle
 import static org.apache.ace.test.utils.TestUtils.UNIT;
 import static org.apache.ace.test.utils.TestUtils.configureObject;
 import static org.apache.ace.test.utils.TestUtils.createMockObjectAdapter;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -38,9 +42,9 @@ import javax.servlet.http.HttpServletRes
 import org.apache.ace.deployment.provider.ArtifactData;
 import org.apache.ace.deployment.provider.DeploymentProvider;
 import org.apache.ace.deployment.streamgenerator.StreamGenerator;
+import org.easymock.IAnswer;
 import org.osgi.service.log.LogService;
 import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.BeforeTest;
 import org.testng.annotations.Test;
 
 public class DeploymentServletTest {
@@ -48,6 +52,8 @@ public class DeploymentServletTest {
     // the servlet under test
     private DeploymentServlet m_servlet;
 
+    private long m_artifactSize;
+    
     // request state
     private HttpServletRequest m_request;
     private String m_requestCurrentParameter;
@@ -62,7 +68,6 @@ public class DeploymentServletTest {
 
     // deployment provider state
     private DeploymentProvider m_provider;
-    private Map<String, List<String>> m_providerVersions;
 
     // stream generator state
     private StreamGenerator m_generator;
@@ -71,140 +76,58 @@ public class DeploymentServletTest {
     private String m_generatorToVersion;
     private InputStream m_generatorResultStream;
 
-    @BeforeTest
-    protected void setUpOnce() throws Exception {
-
-        List<String> existingTargetVersions = new ArrayList<String>();
-        existingTargetVersions.add("2.0.0");
-        m_providerVersions = new HashMap<String, List<String>>();
-        m_providerVersions.put("existing", existingTargetVersions);
-
-        m_provider = new DeploymentProvider() {
-            public List<ArtifactData> getBundleData(String targetId, String version)
throws IllegalArgumentException {
-                return null; // not used
-            }
-
-            public List<ArtifactData> getBundleData(String targetId, String versionFrom,
String versionTo) throws IllegalArgumentException {
-                return null; // not used
-            }
-
-            public List<String> getVersions(String targetId) throws IllegalArgumentException
{
-                if (m_providerVersions.containsKey(targetId)) {
-                    return m_providerVersions.get(targetId);
-                }
-                throw new IllegalArgumentException();
-            }
-        };
-
-        m_generator = new StreamGenerator() {
-            public InputStream getDeploymentPackage(String id, String version) throws IOException
{
-                if (m_generatorResultStream == null) {
-                    throw new IOException("No data for " + id + " " + version);
-                }
-                m_generatorId = id;
-                m_generatorToVersion = version;
-                return m_generatorResultStream;
-            }
-
-            public InputStream getDeploymentPackage(String id, String fromVersion, String
toVersion) throws IOException {
-                if (m_generatorResultStream == null) {
-                    throw new IOException("No delta for " + id + " " + fromVersion + " "
+ toVersion);
-                }
-                m_generatorId = id;
-                m_generatorFromVersion = fromVersion;
-                m_generatorToVersion = toVersion;
-                return m_generatorResultStream;
-            }
-        };
-
-        // create a HttpServletRequest mock object
-        m_request = createMockObjectAdapter(HttpServletRequest.class, new Object() {
-            @SuppressWarnings("unused")
-            public String getParameter(String param) {
-                if (param.equals(DeploymentServlet.CURRENT)) {
-                    return m_requestCurrentParameter;
-                }
-                return null;
-            }
-
+    @Test
+    public void getDataForBadURL() throws Exception {
+        HttpServletRequest garbage = createMockObjectAdapter(HttpServletRequest.class, new
Object() {
             @SuppressWarnings("unused")
             public String getPathInfo() {
-                return m_requestPathInfo;
-            }
-
-            @SuppressWarnings("unused")
-            public String getHeader(String name) {
-                if (name.equals("Range")) {
-                    return m_requestRangeHeader;
-                }
-                return null;
-            }
-        });
-
-        // create a HttpServletResponse mock object
-        m_response = createMockObjectAdapter(HttpServletResponse.class, new Object() {
-            @SuppressWarnings("unused")
-            public ServletOutputStream getOutputStream() {
-                return new ServletOutputStream() {
-                    @Override
-                    public void write(int b) throws IOException {
-                        m_responseOutputStream.write(b);
-                    }
-                };
-            }
-
-            public void sendError(int status) {
-                m_responseStatus = status;
-            }
-
-            @SuppressWarnings("unused")
-            public void sendError(int status, String desc) {
-                sendError(status);
-            }
-
-            public void setStatus(int status) {
-                m_responseStatus = status;
-            }
-
-            @SuppressWarnings("unused")
-            public void setStatus(int status, String desc) {
-                setStatus(status);
-            }
-
-            @SuppressWarnings("unused")
-            public void setHeader(String name, String value) {
-                m_responseHeaders.put(name, value);
+                return "/";
             }
         });
-
-        // create the instance to test
-        m_servlet = new DeploymentServlet();
-        configureObject(m_servlet, LogService.class);
-        configureObject(m_servlet, StreamGenerator.class, m_generator);
-        configureObject(m_servlet, DeploymentProvider.class, m_provider);
+        m_servlet.doGet(garbage, m_response);
+        assertResponseCode(HttpServletResponse.SC_BAD_REQUEST);
     }
 
-    @BeforeMethod
-    protected void setUp() throws Exception {
-        // set the default state
-        m_generatorResultStream = new ByteArrayInputStream(new byte[100]);
-        m_generatorId = null;
-        m_generatorFromVersion = null;
-        m_generatorToVersion = null;
-
-        m_responseStatus = HttpServletResponse.SC_OK;
-        m_responseHeaders = new HashMap<String, String>();
-        m_responseOutputStream = new ByteArrayOutputStream();
+    @Test
+    public void getDataForExistingTarget() throws Exception {
+        m_requestPathInfo = "/existing/versions/2.0.0";
+        m_servlet.doGet(m_request, m_response);
+        assertResponseCode(HttpServletResponse.SC_OK);
+        assertResponseOutputSize(100);
+        assertGeneratorTargetId("existing");
+        assertGeneratorToVersion("2.0.0");
     }
 
     @Test
-    public void getDataForExistingTarget() throws Exception {
+    public void getDataForNonExistingTarget() throws Exception {
+        m_requestPathInfo = "/nonexisting/versions/2.0.0";
+        m_servlet.doGet(m_request, m_response);
+        assertResponseCode(HttpServletResponse.SC_NOT_FOUND);
+    }
+
+    @Test(groups = { UNIT })
+    public void getFixPackageForExistingTarget() throws Exception {
         m_requestPathInfo = "/existing/versions/2.0.0";
+        m_requestCurrentParameter = "1.0.0";
         m_servlet.doGet(m_request, m_response);
         assertResponseCode(HttpServletResponse.SC_OK);
         assertResponseOutputSize(100);
         assertGeneratorTargetId("existing");
         assertGeneratorToVersion("2.0.0");
+        assertGeneratorFromVersion("1.0.0");
+    }
+
+    @Test
+    public void getRangeDataForExistingTarget_badHeaderValue() throws Exception {
+        // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.16
+        // If the server ignores a byte-range-spec because it is syntactically invalid, the
server SHOULD treat the
+        // request as if the invalid Range header field did not exist. (Normally, this means
return a 200 response
+        // containing the full entity).
+        m_requestPathInfo = "/existing/versions/2.0.0";
+        m_requestRangeHeader = "bytes=a-1";
+        m_servlet.doGet(m_request, m_response);
+        assertResponseCode(HttpServletResponse.SC_OK);
+        assertResponseOutputSize(100);
     }
 
     @Test
@@ -218,6 +141,21 @@ public class DeploymentServletTest {
         assertResponseHeaderValue("Content-Range", "bytes 0-10/100");
     }
 
+    @Test
+    public void getRangeDataForExistingTarget_firstOKlastANY() throws Exception {
+        // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.1
+        // If the last-byte-pos value is absent, or if the value is greater than or equal
to the current length of the
+        // entity-body, last-byte-pos is taken to be equal to one less than the current length
of the entity- body in
+        // bytes.
+        m_requestPathInfo = "/existing/versions/2.0.0";
+        m_requestRangeHeader = "bytes=2-";
+        m_servlet.doGet(m_request, m_response);
+        assertResponseCode(HttpServletResponse.SC_PARTIAL_CONTENT);
+        assertResponseHeaderValue("Content-Length", "98");
+        assertResponseHeaderValue("Content-Range", "bytes 2-99/100");
+        assertResponseOutputSize(98);
+    }
+
     @Test(groups = { UNIT })
     public void getRangeDataForExistingTarget_firstOKlastOK() throws Exception {
         // valid range not starting at 0
@@ -245,18 +183,20 @@ public class DeploymentServletTest {
     }
 
     @Test
-    public void getRangeDataForExistingTarget_firstOKlastANY() throws Exception {
+    public void getRangeDataForExistingTarget_firstOKlastTooSmall() throws Exception {
         // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.1
-        // If the last-byte-pos value is absent, or if the value is greater than or equal
to the current length of the
-        // entity-body, last-byte-pos is taken to be equal to one less than the current length
of the entity- body in
-        // bytes.
+        // If the last-byte-pos value is present, it MUST be greater than or equal to the
first-byte-pos in that
+        // byte-range-spec, or the byte- range-spec is syntactically invalid.
+
+        // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.16
+        // If the server ignores a byte-range-spec because it is syntactically invalid, the
server SHOULD treat the
+        // request as if the invalid Range header field did not exist. (Normally, this means
return a 200 response
+        // containing the full entity).
         m_requestPathInfo = "/existing/versions/2.0.0";
-        m_requestRangeHeader = "bytes=2-";
+        m_requestRangeHeader = "bytes=2-1";
         m_servlet.doGet(m_request, m_response);
-        assertResponseCode(HttpServletResponse.SC_PARTIAL_CONTENT);
-        assertResponseHeaderValue("Content-Length", "98");
-        assertResponseHeaderValue("Content-Range", "bytes 2-99/100");
-        assertResponseOutputSize(98);
+        assertResponseCode(HttpServletResponse.SC_OK);
+        assertResponseOutputSize(100);
     }
 
     @Test
@@ -271,64 +211,41 @@ public class DeploymentServletTest {
     }
 
     @Test
-    public void getRangeDataForExistingTarget_firstOKlastTooSmall() throws Exception {
-        // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.1
-        // If the last-byte-pos value is present, it MUST be greater than or equal to the
first-byte-pos in that
-        // byte-range-spec, or the byte- range-spec is syntactically invalid.
-
-        // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.16
-        // If the server ignores a byte-range-spec because it is syntactically invalid, the
server SHOULD treat the
-        // request as if the invalid Range header field did not exist. (Normally, this means
return a 200 response
-        // containing the full entity).
+    public void getSizeForExistingTargetWithKnownSize() throws Exception {
+        m_artifactSize = 10;
         m_requestPathInfo = "/existing/versions/2.0.0";
-        m_requestRangeHeader = "bytes=2-1";
-        m_servlet.doGet(m_request, m_response);
+        m_servlet.doHead(m_request, m_response);
         assertResponseCode(HttpServletResponse.SC_OK);
-        assertResponseOutputSize(100);
+        assertResponseHeaderValue("X-ACE-DPSize", "11"); // 10 + 10%
     }
 
     @Test
-    public void getRangeDataForExistingTarget_badHeaderValue() throws Exception {
-        // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.16
-        // If the server ignores a byte-range-spec because it is syntactically invalid, the
server SHOULD treat the
-        // request as if the invalid Range header field did not exist. (Normally, this means
return a 200 response
-        // containing the full entity).
+    public void getSizeForFixPackageExistingTargetWithKnownSize() throws Exception {
+        m_artifactSize = 10;
+        m_requestCurrentParameter = "2.0.0";
         m_requestPathInfo = "/existing/versions/2.0.0";
-        m_requestRangeHeader = "bytes=a-1";
-        m_servlet.doGet(m_request, m_response);
+        m_servlet.doHead(m_request, m_response);
         assertResponseCode(HttpServletResponse.SC_OK);
-        assertResponseOutputSize(100);
+        assertResponseHeaderValue("X-ACE-DPSize", "22"); // 20 + 10%
     }
 
-    @Test(groups = { UNIT })
-    public void getFixPackageForExistingTarget() throws Exception {
+    @Test
+    public void getSizeForExistingTargetWithUnknownSize() throws Exception {
+        m_artifactSize = -1;
         m_requestPathInfo = "/existing/versions/2.0.0";
-        m_requestCurrentParameter = "1.0.0";
-        m_servlet.doGet(m_request, m_response);
+        m_servlet.doHead(m_request, m_response);
         assertResponseCode(HttpServletResponse.SC_OK);
-        assertResponseOutputSize(100);
-        assertGeneratorTargetId("existing");
-        assertGeneratorToVersion("2.0.0");
-        assertGeneratorFromVersion("1.0.0");
+        assertResponseHeaderNotPresent("X-ACE-DPSize");
     }
 
     @Test
-    public void getDataForNonExistingTarget() throws Exception {
-        m_requestPathInfo = "/nonexisting/versions/2.0.0";
-        m_servlet.doGet(m_request, m_response);
-        assertResponseCode(HttpServletResponse.SC_NOT_FOUND);
-    }
+    public void getSizeForNonExistingTarget() throws Exception {
+        m_artifactSize = 10;
 
-    @Test
-    public void getDataForBadURL() throws Exception {
-        HttpServletRequest garbage = createMockObjectAdapter(HttpServletRequest.class, new
Object() {
-            @SuppressWarnings("unused")
-            public String getPathInfo() {
-                return "/";
-            }
-        });
-        m_servlet.doGet(garbage, m_response);
-        assertResponseCode(HttpServletResponse.SC_BAD_REQUEST);
+        m_requestPathInfo = "/existing/versions/1.0.0";
+        m_servlet.doHead(m_request, m_response);
+        assertResponseCode(HttpServletResponse.SC_OK);
+        assertResponseHeaderNotPresent("X-ACE-DPSize");
     }
 
     @Test
@@ -347,17 +264,151 @@ public class DeploymentServletTest {
         assertResponseOutputSize(0);
     }
 
-    private void assertResponseCode(int value) throws Exception {
-        assert m_responseStatus == value : "We should have got response code " + value +
" but got " + m_responseStatus;
-    }
+    @BeforeMethod(alwaysRun = true)
+    protected void setUp() throws Exception {
+        final Map<String, List<String>> providerVersions = new HashMap<String,
List<String>>();
+        providerVersions.put("existing", Arrays.asList("2.0.0"));
 
-    private void assertResponseHeaderValue(String name, String value) throws Exception {
-        assert m_responseHeaders.containsKey(name) : "Expected response " + name + " header
to be set";
-        assert m_responseHeaders.get(name).equals(value) : "Expected " + name + " header
with value '" + value + "' and got '" + m_responseHeaders.get(name) + "'";
+        final ArtifactData artifactData = createMock(ArtifactData.class);
+        expect(artifactData.getSize()).andAnswer(new IAnswer<Long>() {
+            @Override
+            public Long answer() throws Throwable {
+                return DeploymentServletTest.this.m_artifactSize;
+            }
+        }).anyTimes();
+        replay(artifactData);
+
+        m_provider = new DeploymentProvider() {
+            public List<ArtifactData> getBundleData(String targetId, String version)
throws IllegalArgumentException {
+                List<String> versions = providerVersions.get(targetId);
+                if (versions != null && versions.contains(version)) {
+                    return Arrays.asList(artifactData);
+                }
+                return Collections.emptyList();
+            }
+
+            public List<ArtifactData> getBundleData(String targetId, String versionFrom,
String versionTo) throws IllegalArgumentException {
+                List<String> versions = providerVersions.get(targetId);
+                if (versions != null && versions.contains(versionFrom) &&
versions.contains(versionTo)) {
+                    return Arrays.asList(artifactData, artifactData);
+                }
+                return Collections.emptyList();
+            }
+
+            public List<String> getVersions(String targetId) throws IllegalArgumentException
{
+                if (providerVersions.containsKey(targetId)) {
+                    return providerVersions.get(targetId);
+                }
+                throw new IllegalArgumentException();
+            }
+        };
+
+        m_generator = new StreamGenerator() {
+            public InputStream getDeploymentPackage(String id, String version) throws IOException
{
+                if (m_generatorResultStream == null) {
+                    throw new IOException("No data for " + id + " " + version);
+                }
+                m_generatorId = id;
+                m_generatorToVersion = version;
+                return m_generatorResultStream;
+            }
+
+            public InputStream getDeploymentPackage(String id, String fromVersion, String
toVersion) throws IOException {
+                if (m_generatorResultStream == null) {
+                    throw new IOException("No delta for " + id + " " + fromVersion + " "
+ toVersion);
+                }
+                m_generatorId = id;
+                m_generatorFromVersion = fromVersion;
+                m_generatorToVersion = toVersion;
+                return m_generatorResultStream;
+            }
+        };
+
+        // create a HttpServletRequest mock object
+        m_request = createMockObjectAdapter(HttpServletRequest.class, new Object() {
+            @SuppressWarnings("unused")
+            public String getHeader(String name) {
+                if (name.equals("Range")) {
+                    return m_requestRangeHeader;
+                }
+                return null;
+            }
+
+            @SuppressWarnings("unused")
+            public String getParameter(String param) {
+                if (param.equals(DeploymentServlet.CURRENT)) {
+                    return m_requestCurrentParameter;
+                }
+                return null;
+            }
+
+            @SuppressWarnings("unused")
+            public String getPathInfo() {
+                return m_requestPathInfo;
+            }
+        });
+
+        // create a HttpServletResponse mock object
+        m_response = createMockObjectAdapter(HttpServletResponse.class, new Object() {
+            @SuppressWarnings("unused")
+            public void addHeader(String name, String value) {
+                m_responseHeaders.put(name, value);
+            }
+
+            @SuppressWarnings("unused")
+            public ServletOutputStream getOutputStream() {
+                return new ServletOutputStream() {
+                    @Override
+                    public void write(int b) throws IOException {
+                        m_responseOutputStream.write(b);
+                    }
+                };
+            }
+
+            public synchronized void sendError(int status) {
+                m_responseStatus = status;
+            }
+
+            @SuppressWarnings("unused")
+            public void sendError(int status, String desc) {
+                sendError(status);
+            }
+
+            @SuppressWarnings("unused")
+            public void setHeader(String name, String value) {
+                m_responseHeaders.put(name, value);
+            }
+
+            public void setStatus(int status) {
+                m_responseStatus = status;
+            }
+
+            @SuppressWarnings("unused")
+            public void setStatus(int status, String desc) {
+                setStatus(status);
+            }
+        });
+
+        // create the instance to test
+        m_servlet = new DeploymentServlet();
+        configureObject(m_servlet, LogService.class);
+        configureObject(m_servlet, StreamGenerator.class, m_generator);
+        configureObject(m_servlet, DeploymentProvider.class, m_provider);
+
+        // set the default state
+        m_generatorResultStream = new ByteArrayInputStream(new byte[100]);
+        m_requestCurrentParameter = null;
+        m_generatorId = null;
+        m_generatorFromVersion = null;
+        m_generatorToVersion = null;
+
+        m_responseStatus = HttpServletResponse.SC_OK;
+        m_responseHeaders = new HashMap<String, String>();
+        m_responseOutputStream = new ByteArrayOutputStream();
     }
 
-    private void assertResponseOutputSize(long size) throws Exception {
-        assert m_responseOutputStream.size() == size : "We should have got a (dummy) deployment
package of " + size + " bytes but got " + m_responseOutputStream.size();
+    private void assertGeneratorFromVersion(String version) {
+        assert m_generatorFromVersion.equals(version) : "Wrong version.";
     }
 
     private void assertGeneratorTargetId(String id) {
@@ -368,7 +419,20 @@ public class DeploymentServletTest {
         assert m_generatorToVersion.equals(version) : "Wrong version.";
     }
 
-    private void assertGeneratorFromVersion(String version) {
-        assert m_generatorFromVersion.equals(version) : "Wrong version.";
+    private void assertResponseCode(int value) throws Exception {
+        assert m_responseStatus == value : "We should have got response code " + value +
" but got " + m_responseStatus;
+    }
+
+    private void assertResponseHeaderNotPresent(String name) throws Exception {
+        assert !m_responseHeaders.containsKey(name) : "Expected response " + name + " header
to NOT be set";
+    }
+
+    private void assertResponseHeaderValue(String name, String value) throws Exception {
+        assert m_responseHeaders.containsKey(name) : "Expected response " + name + " header
to be set";
+        assert m_responseHeaders.get(name).equals(value) : "Expected " + name + " header
with value '" + value + "' and got '" + m_responseHeaders.get(name) + "'";
+    }
+
+    private void assertResponseOutputSize(long size) throws Exception {
+        assert m_responseOutputStream.size() == size : "We should have got a (dummy) deployment
package of " + size + " bytes but got " + m_responseOutputStream.size();
     }
 }

Modified: ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/metadata/bindeximpl/BindexMetadataTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/metadata/bindeximpl/BindexMetadataTest.java?rev=1527522&r1=1527521&r2=1527522&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/metadata/bindeximpl/BindexMetadataTest.java
(original)
+++ ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/metadata/bindeximpl/BindexMetadataTest.java
Mon Sep 30 12:00:26 2013
@@ -34,7 +34,8 @@ import org.testng.annotations.Test;
 public class BindexMetadataTest {
 
     private ArtifactData generateBundle(File file, String symbolicName, String version) throws
Exception {
-        ArtifactData bundle = new ArtifactDataImpl(file.getName(), symbolicName, version,
file.toURI().toURL(), false);
+        // create a mock bundle, which is only used to generate the bundle on disk, and not
used for anything else...
+        ArtifactData bundle = new ArtifactDataImpl(file.getName(), symbolicName, -1L, version,
file.toURI().toURL(), false);
         BundleStreamGenerator.generateBundle(bundle);
         return bundle;
     }



Mime
View raw message