Return-Path: Delivered-To: apmail-incubator-ace-commits-archive@minotaur.apache.org Received: (qmail 57244 invoked from network); 8 May 2010 19:32:35 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 8 May 2010 19:32:35 -0000 Received: (qmail 57787 invoked by uid 500); 8 May 2010 19:32:35 -0000 Delivered-To: apmail-incubator-ace-commits-archive@incubator.apache.org Received: (qmail 57766 invoked by uid 500); 8 May 2010 19:32:35 -0000 Mailing-List: contact ace-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: ace-dev@incubator.apache.org Delivered-To: mailing list ace-commits@incubator.apache.org Received: (qmail 57758 invoked by uid 99); 8 May 2010 19:32:35 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 08 May 2010 19:32:35 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED,NORMAL_HTTP_TO_IP X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 08 May 2010 19:32:32 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 5B1172388900; Sat, 8 May 2010 19:31:35 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r942451 - in /incubator/ace/trunk/ace-deployment-streamgenerator: ./ src/test/java/org/apache/ace/deployment/ src/test/java/org/apache/ace/deployment/streamgenerator/ src/test/java/org/apache/ace/deployment/streamgenerator/impl/ Date: Sat, 08 May 2010 19:31:35 -0000 To: ace-commits@incubator.apache.org From: marrs@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100508193135.5B1172388900@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: marrs Date: Sat May 8 19:31:34 2010 New Revision: 942451 URL: http://svn.apache.org/viewvc?rev=942451&view=rev Log: Added unit tests, modified them to run in the new Maven environment. Added: incubator/ace/trunk/ace-deployment-streamgenerator/src/test/java/org/apache/ace/deployment/ incubator/ace/trunk/ace-deployment-streamgenerator/src/test/java/org/apache/ace/deployment/streamgenerator/ incubator/ace/trunk/ace-deployment-streamgenerator/src/test/java/org/apache/ace/deployment/streamgenerator/impl/ incubator/ace/trunk/ace-deployment-streamgenerator/src/test/java/org/apache/ace/deployment/streamgenerator/impl/StreamTest.java Modified: incubator/ace/trunk/ace-deployment-streamgenerator/pom.xml Modified: incubator/ace/trunk/ace-deployment-streamgenerator/pom.xml URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-deployment-streamgenerator/pom.xml?rev=942451&r1=942450&r2=942451&view=diff ============================================================================== --- incubator/ace/trunk/ace-deployment-streamgenerator/pom.xml (original) +++ incubator/ace/trunk/ace-deployment-streamgenerator/pom.xml Sat May 8 19:31:34 2010 @@ -25,7 +25,7 @@ org.apache.ace ace-pom 0.8.0-SNAPSHOT - ../pom/ + ../pom/ org.apache.ace @@ -67,6 +67,20 @@ org.apache.felix org.apache.felix.dependencymanager + + ${project.groupId} + ace-util + ${project.version} + + + org.ops4j.pax.swissbox + pax-swissbox-tinybundles + 1.2.0 + + + commons-logging + commons-logging + 1.1.1 + - \ No newline at end of file Added: incubator/ace/trunk/ace-deployment-streamgenerator/src/test/java/org/apache/ace/deployment/streamgenerator/impl/StreamTest.java URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-deployment-streamgenerator/src/test/java/org/apache/ace/deployment/streamgenerator/impl/StreamTest.java?rev=942451&view=auto ============================================================================== --- incubator/ace/trunk/ace-deployment-streamgenerator/src/test/java/org/apache/ace/deployment/streamgenerator/impl/StreamTest.java (added) +++ incubator/ace/trunk/ace-deployment-streamgenerator/src/test/java/org/apache/ace/deployment/streamgenerator/impl/StreamTest.java Sat May 8 19:31:34 2010 @@ -0,0 +1,314 @@ +/* + * 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.deployment.streamgenerator.impl; + +import static org.apache.ace.test.utils.TestUtils.BROKEN; +import static org.apache.ace.test.utils.TestUtils.UNIT; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URL; +import java.util.HashSet; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.jar.Attributes; +import java.util.jar.JarEntry; +import java.util.jar.JarInputStream; +import java.util.jar.Manifest; + +import org.apache.ace.deployment.provider.DeploymentProvider; +import org.apache.ace.test.constants.TestConstants; +import org.apache.ace.test.utils.TestUtils; +import org.apache.ace.test.utils.deployment.TestProvider; +import org.osgi.service.log.LogService; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; +import static org.ops4j.pax.swissbox.tinybundles.core.TinyBundles.*; + +/** + * Unit tests for the deployment admin stream. + */ +public class StreamTest { + private static final int COPY_BUFFER_SIZE = 4096; + + private StreamGeneratorImpl m_generator; + + private TestProvider m_provider; + + @BeforeTest(alwaysRun = true) + protected void setUp() throws Exception { + m_generator = new StreamGeneratorImpl(); + m_provider = new TestProvider(); + InputStream is = newBundle().build(); // TODO this is a very trivial bundle, put more data in? + File temp = File.createTempFile("bundle", "jar"); + temp.deleteOnExit(); + FileOutputStream fos = new FileOutputStream(temp); + byte[] buf = new byte[4096]; + int b = is.read(buf); + while (b != -1) { + fos.write(buf, 0, b); + b = is.read(buf); + } + fos.close(); + is.close(); + URL url = temp.toURI().toURL(); + + m_provider.addData("A1.jar", "A1", url, "1.0.0", true); + m_provider.addData("A2.jar", "A2", url, "1.0.0", false); + m_provider.addData("A3.jar", "A3", url, "1.0.0", true); + TestUtils.configureObject(m_generator, DeploymentProvider.class, m_provider); + TestUtils.configureObject(m_generator, LogService.class); + } + + public static void main(String[] args) { + final InputStream[] streams = new InputStream[300]; + for (int i = 1; i <= 250; i++) { + final String id = "gateway-" + i; + try { + streams[i - 1] = new URL("http://127.0.0.1:" + TestConstants.PORT + "/data/" + id + "/versions/1.0.0").openStream(); + } + catch (IOException ex) { + ex.printStackTrace(); + } + } + new Thread() { + @Override + public void run() { + int done = 0; + while (done < 50) { + for (int i = 0; i < 50; i++) { + try { + int in = streams[i].read(); + if (in == -1) { + System.out.println(1); + done++; + } + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + } + }.start(); + new Thread() { + @Override + public void run() { + int done = 0; + while (done < 50) { + for (int i = 50; i < 100; i++) { + try { + int in = streams[i].read(); + if (in == -1) { + System.out.println(2); + done++; + } + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + } + }.start(); + new Thread() { + @Override + public void run() { + int done = 0; + while (done < 50) { + for (int i = 100; i < 150; i++) { + try { + int in = streams[i].read(); + if (in == -1) { + System.out.println(3); + done++; + } + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + } + }.start(); + new Thread() { + @Override + public void run() { + int done = 0; + while (done < 50) { + for (int i = 150; i < 200; i++) { + try { + int in = streams[i].read(); + if (in == -1) { + System.out.println(4); + done++; + } + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + } + }.start(); + new Thread() { + @Override + public void run() { + int done = 0; + while (done < 50) { + for (int i = 200; i < 250; i++) { + try { + int in = streams[i].read(); + if (in == -1) { + System.out.println(5); + done++; + } + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + } + }.start(); + new Thread() { + @Override + public void run() { + int done = 0; + while (done < 50) { + for (int i = 250; i < 300; i++) { + try { + if (streams[i] == null) { + streams[i] = new URL("http://127.0.0.1:" + TestConstants.PORT + "/data/gateway-" + (i + 1) + "/versions/1.0.0").openStream(); + } + int in = streams[i].read(); + if (in == -1) { + System.out.println(5); + done++; + } + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + } + }.start(); + } + + /** + * The specification requires the stream to be readable by JarInputStream (114.3) so make sure it is. + */ + @Test(groups = { UNIT }) + public void isJarInputStreamReadable() throws Exception { + isJarInputStreamReadable(new JarInputStream(m_generator.getDeploymentPackage("test", "1.0.0")), false); + isJarInputStreamReadable(new JarInputStream(m_generator.getDeploymentPackage("test", "0.0.0", "1.0.0")), true); + } + + private void isJarInputStreamReadable(JarInputStream jis, boolean fixPackage) throws Exception { + assert jis != null : "We should have got an input stream for this deployment package."; + Manifest m = jis.getManifest(); + assert m != null : "The stream should contain a valid manifest."; + Attributes att = m.getMainAttributes(); + assert att.getValue("DeploymentPackage-SymbolicName").equals("test"); + assert att.getValue("DeploymentPackage-Version").equals("1.0.0"); + assert (fixPackage && att.getValue("DeploymentPackage-FixPack").equals("[0.0.0,1.0.0)")) || (att.getValue("DeploymentPackage-FixPack") == null); + HashSet names = new HashSet(); + JarEntry e = jis.getNextJarEntry(); + while (e != null) { + String name = e.getName(); + names.add(name); + if (fixPackage && name.equals("A2.jar")) { + assert e.getAttributes().getValue("DeploymentPackage-Missing").equals("true"); + } + // we could check the name here against the manifest + // and make sure we actually get what was promised + Attributes a = m.getAttributes(name); + assert a != null : "The stream should contain a named section for " + name + " in the manifest."; + + byte[] buffer = new byte[COPY_BUFFER_SIZE]; + int bytes = jis.read(buffer); + long size = 0; + while (bytes != -1) { + size += bytes; + bytes = jis.read(buffer); + } + + // get the next entry, if any + e = jis.getNextJarEntry(); + } + if (!fixPackage) { + assert names.size() == 3 : "The stream should have contained three resources."; + } + else { + assert names.size() == 2 : "The stream should have contained three resources"; + } + assert names.contains("A1.jar") : "The stream should have contained a resource called A1.jar"; + assert fixPackage ^ names.contains("A2.jar") : "The stream should have contained a resource called A2.jar"; + assert names.contains("A3.jar") : "The stream should have contained a resource called A3.jar"; + } + + /** + * Test reading 100 streams sequentially. + */ + @Test(groups = { UNIT, BROKEN }) + public void hundredStreamsSequentially() throws Exception { + for (int i = 0; i < 100; i++) { + isJarInputStreamReadable(); + } + } + + private Exception m_failure; + + /** + * Test reading 100 streams concurrently. + */ + @Test(groups = { UNIT, BROKEN }) // marked broken after discussing it with Karl + public void hundredStreamsConcurrently() throws Exception { + ExecutorService e = Executors.newFixedThreadPool(5); + for (int i = 0; i < 10; i++) { + e.execute(new Runnable() { + public void run() { + for (int i = 0; i < 10; i++) { + try { + isJarInputStreamReadable(); + } + catch (Exception e) { + m_failure = e; + } + } + } + }); + } + e.shutdown(); + e.awaitTermination(10, TimeUnit.SECONDS); + + assert m_failure == null : "Test failed: " + m_failure.getLocalizedMessage(); + } +}