incubator-ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r942361 - in /incubator/ace/trunk/ace-obr-servlet: ./ src/test/java/org/apache/ace/obr/ src/test/java/org/apache/ace/obr/servlet/
Date Sat, 08 May 2010 11:23:00 GMT
Author: marrs
Date: Sat May  8 11:23:00 2010
New Revision: 942361

URL: http://svn.apache.org/viewvc?rev=942361&view=rev
Log:
Added unit tests.

Added:
    incubator/ace/trunk/ace-obr-servlet/src/test/java/org/apache/ace/obr/
    incubator/ace/trunk/ace-obr-servlet/src/test/java/org/apache/ace/obr/servlet/
    incubator/ace/trunk/ace-obr-servlet/src/test/java/org/apache/ace/obr/servlet/BundleServletTest.java
    incubator/ace/trunk/ace-obr-servlet/src/test/java/org/apache/ace/obr/servlet/MockBundleStore.java
Modified:
    incubator/ace/trunk/ace-obr-servlet/pom.xml

Modified: incubator/ace/trunk/ace-obr-servlet/pom.xml
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-obr-servlet/pom.xml?rev=942361&r1=942360&r2=942361&view=diff
==============================================================================
--- incubator/ace/trunk/ace-obr-servlet/pom.xml (original)
+++ incubator/ace/trunk/ace-obr-servlet/pom.xml Sat May  8 11:23:00 2010
@@ -78,6 +78,10 @@
             <groupId>javax.servlet</groupId>
             <artifactId>servlet-api</artifactId>
         </dependency>
+        <dependency>
+            <groupId>${groupId}</groupId>
+            <artifactId>ace-util</artifactId>
+            <version>${version}</version>
+        </dependency>
     </dependencies>
-
 </project>
\ No newline at end of file

Added: incubator/ace/trunk/ace-obr-servlet/src/test/java/org/apache/ace/obr/servlet/BundleServletTest.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-obr-servlet/src/test/java/org/apache/ace/obr/servlet/BundleServletTest.java?rev=942361&view=auto
==============================================================================
--- incubator/ace/trunk/ace-obr-servlet/src/test/java/org/apache/ace/obr/servlet/BundleServletTest.java
(added)
+++ incubator/ace/trunk/ace-obr-servlet/src/test/java/org/apache/ace/obr/servlet/BundleServletTest.java
Sat May  8 11:23:00 2010
@@ -0,0 +1,240 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.obr.servlet;
+
+import static org.apache.ace.test.utils.TestUtils.UNIT;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Random;
+
+import javax.servlet.ServletInputStream;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.ace.obr.storage.BundleStore;
+import org.apache.ace.test.constants.TestConstants;
+import org.apache.ace.test.utils.FileUtils;
+import org.apache.ace.test.utils.TestUtils;
+import org.osgi.service.log.LogService;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class BundleServletTest {
+    private HttpServletRequest m_request;
+    private HttpServletResponse m_response;
+    private ByteArrayOutputStream m_byteStream = new ByteArrayOutputStream();
+    protected int m_status;
+    private MockBundleStore m_store;
+    private BundleServlet m_bundleServlet;
+    private File m_testFile;
+    private String m_requestFile;
+
+    @BeforeMethod(alwaysRun = true)
+    protected void setUp() throws IOException {
+        m_testFile = createRandomFileWithContent();
+        m_store = new MockBundleStore(new FileInputStream(m_testFile));
+        m_bundleServlet = new BundleServlet();
+
+        TestUtils.configureObject(m_bundleServlet, LogService.class);
+        TestUtils.configureObject(m_bundleServlet, BundleStore.class, m_store);
+
+        m_request = TestUtils.createMockObjectAdapter(HttpServletRequest.class, new Object()
{
+            @SuppressWarnings("unused")
+            public String getParameter(String param) {
+                return m_requestFile;
+            }
+
+            @SuppressWarnings("unused")
+            public String getPathInfo() {
+                return "/" + m_requestFile;
+            }
+
+            @SuppressWarnings("unused")
+            public StringBuffer getRequestURL() {
+                return new StringBuffer("http://localhost:" + TestConstants.PORT + "/obr/"
+ m_requestFile);
+            }
+
+            @SuppressWarnings("unused")
+            public ServletInputStream getInputStream() {
+                return new ServletInputStream() {
+                    int i = 0;
+                    @Override
+                    public int read() throws IOException {
+                        if (i == 0) {
+                            i++;
+                            return 'a';
+                        }
+                        else {
+                            return -1;
+                        }
+                    }
+
+                };
+            }
+        });
+
+        // create a HttpServletResponse mock object
+        m_response = TestUtils.createMockObjectAdapter(HttpServletResponse.class, new Object()
{
+            @SuppressWarnings("unused")
+            public ServletOutputStream getOutputStream() {
+                return new ServletOutputStream() {
+                    @Override
+                    public void write(int b) throws IOException {
+                        m_byteStream.write(b);
+                    }
+
+                    @Override
+                    public void println(String s) throws IOException {
+                        for (int i = 0; i < s.length(); i++) {
+                            m_byteStream.write(s.charAt(i));
+                        }
+                    }
+                };
+            }
+
+            @SuppressWarnings("unused")
+            public void sendError(int status) {
+                m_status = status;
+            }
+
+            @SuppressWarnings("unused")
+            public void sendError(int status, String description) {
+                m_status = status;
+            }
+        });
+        m_status = HttpServletResponse.SC_OK;
+    }
+
+    private File createRandomFileWithContent() throws IOException {
+        OutputStream fileOut = null;
+        File file = null;
+        try {
+            file = FileUtils.createTempFile(null);
+            fileOut = new FileOutputStream(file);
+            byte[] byteArray = new byte[12345];
+            Random randomContentCreator = new Random();
+            randomContentCreator.nextBytes(byteArray);
+            fileOut.write(byteArray);
+
+            return file;
+        }
+        finally {
+            try {
+                if (fileOut != null) {
+                    fileOut.close();
+                }
+            }
+            catch (IOException e) {
+                throw e;
+            }
+        }
+    }
+
+    @Test(groups = { UNIT })
+    public void testGetValidResource() throws Exception {
+        m_requestFile = m_testFile.getName();
+        m_bundleServlet.doGet(m_request, m_response);
+
+        assert m_status == HttpServletResponse.SC_OK : "We should have got response code
" + HttpServletResponse.SC_OK + " and we got " + m_status;
+
+        boolean checkStream = checkOutputStreamForFile();
+        assert checkStream : "One stream stopped before the other one did.";
+    }
+
+    @Test(groups = { UNIT })
+    public void testGetInValidResource() throws Exception {
+        m_requestFile = "UnknownFile";
+        m_bundleServlet.doGet(m_request, m_response);
+
+        assert m_status == HttpServletResponse.SC_NOT_FOUND : "We should have got response
code " + HttpServletResponse.SC_NOT_FOUND + " and we got " + m_status;
+    }
+
+    @Test(groups = { UNIT })
+    public void testPostResource() throws Exception {
+        m_requestFile = "NewFile";
+        m_bundleServlet.doPost(m_request, m_response);
+        assert m_status == HttpServletResponse.SC_OK;
+        m_requestFile = "ExistingFile";
+        m_bundleServlet.doPost(m_request, m_response);
+        assert m_status == HttpServletResponse.SC_CONFLICT;
+        m_requestFile = "";
+        m_bundleServlet.doPost(m_request, m_response);
+        assert m_status == HttpServletResponse.SC_BAD_REQUEST;
+    }
+
+    @Test(groups = { UNIT })
+    public void testRemoveResource() throws Exception {
+        m_requestFile = "RemoveMe";
+        m_bundleServlet.doDelete(m_request, m_response);
+        assert m_status == HttpServletResponse.SC_OK;
+        m_requestFile = "NonExistingFile";
+        m_bundleServlet.doDelete(m_request, m_response);
+        assert m_status == HttpServletResponse.SC_NOT_FOUND;
+    }
+
+    /**
+     * Check if the output from the server is the configured file
+     */
+    private boolean checkOutputStreamForFile() throws Exception {
+        assert m_testFile.length() == m_byteStream.size() : "Different filesize";
+
+        InputStream inStream = null;
+        InputStream outStream = null;
+        try {
+            // ok, the length is the same, now compare the content.
+            inStream = new BufferedInputStream(m_testFile.toURI().toURL().openStream());
+            outStream = new BufferedInputStream(new ByteArrayInputStream(m_byteStream.toByteArray()));
+            int inByte = inStream.read();
+            int outByte = outStream.read();
+            while ((inByte != -1) && (outByte != -1)) {
+                assert inByte == outByte : "Unexpected Stream content found";
+                inByte = inStream.read();
+                outByte = outStream.read();
+            }
+            return inByte == outByte;
+        }
+        finally {
+            try {
+                if (inStream != null) {
+                    inStream.close();
+                }
+            } finally {
+                if (outStream != null) {
+                    outStream.close();
+                }
+            }
+        }
+    }
+
+    @AfterMethod(alwaysRun = true)
+    protected void tearDown() {
+        m_byteStream = new ByteArrayOutputStream();
+        m_testFile.delete();
+    }
+}

Added: incubator/ace/trunk/ace-obr-servlet/src/test/java/org/apache/ace/obr/servlet/MockBundleStore.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-obr-servlet/src/test/java/org/apache/ace/obr/servlet/MockBundleStore.java?rev=942361&view=auto
==============================================================================
--- incubator/ace/trunk/ace-obr-servlet/src/test/java/org/apache/ace/obr/servlet/MockBundleStore.java
(added)
+++ incubator/ace/trunk/ace-obr-servlet/src/test/java/org/apache/ace/obr/servlet/MockBundleStore.java
Sat May  8 11:23:00 2010
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.obr.servlet;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Dictionary;
+
+import org.apache.ace.obr.storage.BundleStore;
+import org.osgi.service.cm.ConfigurationException;
+
+public class MockBundleStore implements BundleStore {
+
+    private InputStream m_outFile;
+
+    public MockBundleStore(InputStream outFile) {
+        m_outFile = outFile;
+    }
+
+    public InputStream get(String fileName) throws IOException {
+        if (fileName.equals("UnknownFile")) {
+            return null;
+        }
+        return m_outFile;
+    }
+
+    public void put(String fileName, OutputStream data) throws IOException {
+        // TODO does nothing yet
+    }
+
+    public boolean put(String fileName, InputStream data) throws IOException {
+        if (fileName.equals("NewFile")) {
+            return true;
+        }
+        return false;
+    }
+
+    public boolean remove(String fileName) throws IOException {
+        if (fileName.equals("RemoveMe")) {
+            return true;
+        }
+        return false;
+    }
+
+    @SuppressWarnings("unchecked")
+    public void updated(Dictionary arg0) throws ConfigurationException {
+        // TODO does nothing yet
+    }
+}



Mime
View raw message