aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1386716 - in /aries/trunk/spi-fly: spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/ spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/ spi-fly-static-tool/src/test/java/org/apache/aries/spifly/static...
Date Mon, 17 Sep 2012 16:55:04 GMT
Author: davidb
Date: Mon Sep 17 16:55:03 2012
New Revision: 1386716

URL: http://svn.apache.org/viewvc?rev=1386716&view=rev
Log:
Add some tests to the static weaving code.

Added:
    aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/ConsumerTest.java
    aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/
    aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/Test2Class.java
    aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/TestClass.java
Modified:
    aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/Main.java
    aries/trunk/spi-fly/spi-fly-weaver/src/main/java/org/apache/aries/spifly/weaver/TCCLSetterVisitor.java

Modified: aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/Main.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/Main.java?rev=1386716&r1=1386715&r2=1386716&view=diff
==============================================================================
--- aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/Main.java
(original)
+++ aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/Main.java
Mon Sep 17 16:55:03 2012
@@ -41,7 +41,6 @@ import org.apache.aries.spifly.WeavingDa
 import org.apache.aries.spifly.api.SpiFlyConstants;
 import org.apache.aries.spifly.weaver.TCCLSetterVisitor;
 import org.objectweb.asm.ClassReader;
-import org.objectweb.asm.ClassVisitor;
 import org.objectweb.asm.ClassWriter;
 import org.osgi.framework.Constants;
 import org.osgi.framework.Version;
@@ -81,6 +80,11 @@ public class Main {
         File tempDir = new File(System.getProperty("java.io.tmpdir") + File.separator + jarFile.getName()
+ "_" + System.currentTimeMillis());
         Manifest manifest = unJar(jarFile, tempDir);
         String consumerHeader = manifest.getMainAttributes().getValue(SpiFlyConstants.SPI_CONSUMER_HEADER);
+//        if (consumerHeader == null) {
+//            String reqCap = manifest.getMainAttributes().getValue(SpiFlyConstants.REQUIRE_CAPABILITY);
+//            sdasda
+//        }
+
         if (consumerHeader != null) {
             String bcp = manifest.getMainAttributes().getValue(Constants.BUNDLE_CLASSPATH);
             weaveDir(tempDir, consumerHeader, bcp);
@@ -107,8 +111,12 @@ public class Main {
         Version maxVersion = new Version(osgiVersion.getMajor(), osgiVersion.getMinor() +
1, 0);
 
         String ip = manifest.getMainAttributes().getValue(IMPORT_PACKAGE);
+        if (ip == null)
+            ip = "";
+
         StringBuilder sb = new StringBuilder(ip);
-        sb.append(",");
+        if (ip.length() > 0)
+            sb.append(",");
         sb.append(Util.class.getPackage().getName());
         sb.append(";version=\"[");
         sb.append(minVersion);
@@ -140,6 +148,9 @@ public class Main {
     }
 
     private static void weaveDir(File dir, String consumerHeader, String bundleClassPath)
throws Exception {
+        // TODO support Require-Capability in addition to SPI-Consumer
+        Set<WeavingData> wd = ConsumerHeaderProcessor.processHeader(SpiFlyConstants.SPI_CONSUMER_HEADER,
consumerHeader);
+
         String dirName = dir.getAbsolutePath();
 
         DirTree dt = new DirTree(dir);
@@ -153,16 +164,19 @@ public class Main {
             className = className.substring(0, className.length() - ".class".length());
             className = className.replace(File.separator, ".");
 
-            // TODO support Require-Capability in addition to SPI-Consumer
-            Set<WeavingData> wd = ConsumerHeaderProcessor.processHeader(SpiFlyConstants.SPI_CONSUMER_HEADER,
consumerHeader);
             InputStream is = new FileInputStream(f);
             byte[] b;
             try {
                 ClassReader cr = new ClassReader(is);
                 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
-                ClassVisitor cv = new TCCLSetterVisitor(cw, className, wd);
+                TCCLSetterVisitor cv = new TCCLSetterVisitor(cw, className, wd);
                 cr.accept(cv, ClassReader.SKIP_FRAMES);
-                b = cw.toByteArray();
+                if (cv.isWoven()) {
+                    b = cw.toByteArray();
+                } else {
+                    // if not woven, store the original bytes
+                    b = Streams.suck(new FileInputStream(f));
+                }
             } finally {
                 is.close();
             }

Added: aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/ConsumerTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/ConsumerTest.java?rev=1386716&view=auto
==============================================================================
--- aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/ConsumerTest.java
(added)
+++ aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/ConsumerTest.java
Mon Sep 17 16:55:03 2012
@@ -0,0 +1,99 @@
+/**
+ * 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.aries.spifly.statictool;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+
+import org.apache.aries.spifly.Streams;
+import org.apache.aries.spifly.api.SpiFlyConstants;
+import org.apache.aries.spifly.statictool.bundle.Test2Class;
+import org.apache.aries.spifly.statictool.bundle.TestClass;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ConsumerTest {
+    @Test
+    public void testConsumerBundle() throws Exception {
+        String testClassFileName = TestClass.class.getName().replace('.', '/') + ".class";
+        URL testClassURL = getClass().getResource("/" + testClassFileName);
+        String test2ClassFileName = Test2Class.class.getName().replace('.', '/') + ".class";
+        URL test2ClassURL = getClass().getResource("/" + test2ClassFileName);
+
+        File jarFile = new File(System.getProperty("java.io.tmpdir") + "/testjar_" + System.currentTimeMillis()
+ ".jar");
+        File expectedFile = null;
+        try {
+            // Create the jarfile to be used for testing
+            Manifest mf = new Manifest();
+            Attributes mainAttributes = mf.getMainAttributes();
+            mainAttributes.putValue("Manifest-Version", "1.0");
+            mainAttributes.putValue("Bundle-ManifestVersion", "2.0");
+            mainAttributes.putValue("Bundle-SymbolicName", "testbundle");
+            mainAttributes.putValue("Foo", "Bar Bar");
+            mainAttributes.putValue(SpiFlyConstants.SPI_CONSUMER_HEADER, Test2Class.class.getName()
+ "#getTCCL()");
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            mf.write(baos);
+            JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile), mf);
+            jos.putNextEntry(new ZipEntry(testClassFileName));
+            Streams.pump(testClassURL.openStream(), jos);
+            jos.putNextEntry(new ZipEntry(test2ClassFileName));
+            Streams.pump(test2ClassURL.openStream(), jos);
+            jos.close();
+
+            Main.main(jarFile.getCanonicalPath());
+
+            expectedFile = new File(jarFile.getParent(), jarFile.getName().replaceAll("[.]jar",
"_spifly.jar"));
+            Assert.assertTrue("A processed separate bundle should have been created", expectedFile.exists());
+
+            // Check manifest in generated bundle.
+            JarFile transformedJarFile = new JarFile(expectedFile);
+            Manifest expectedMF = transformedJarFile.getManifest();
+            Assert.assertEquals("1.0", expectedMF.getMainAttributes().getValue("Manifest-Version"));
+            Assert.assertEquals("2.0", expectedMF.getMainAttributes().getValue("Bundle-ManifestVersion"));
+            Assert.assertEquals("testbundle", expectedMF.getMainAttributes().getValue("Bundle-SymbolicName"));
+            Assert.assertEquals("Bar Bar", expectedMF.getMainAttributes().getValue("Foo"));
+            Assert.assertNull(expectedMF.getMainAttributes().get(SpiFlyConstants.SPI_CONSUMER_HEADER));
+
+            JarFile initialJarFile = new JarFile(jarFile);
+            byte[] orgBytes = Streams.suck(initialJarFile.getInputStream(new ZipEntry(testClassFileName)));
+            byte[] transBytes = Streams.suck(transformedJarFile.getInputStream(new ZipEntry(testClassFileName)));
+            Assert.assertFalse("The transformed class should be different", Arrays.equals(orgBytes,
transBytes));
+
+            byte[] orgBytes2 = Streams.suck(initialJarFile.getInputStream(new ZipEntry(test2ClassFileName)));
+            byte[] nonTransBytes = Streams.suck(transformedJarFile.getInputStream(new ZipEntry(test2ClassFileName)));
+            Assert.assertArrayEquals(orgBytes2, nonTransBytes);
+
+            initialJarFile.close();
+            transformedJarFile.close();
+        } finally {
+            jarFile.delete();
+
+            if (expectedFile != null)
+                expectedFile.delete();
+        }
+    }
+}

Added: aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/Test2Class.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/Test2Class.java?rev=1386716&view=auto
==============================================================================
--- aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/Test2Class.java
(added)
+++ aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/Test2Class.java
Mon Sep 17 16:55:03 2012
@@ -0,0 +1,25 @@
+/**
+ * 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.aries.spifly.statictool.bundle;
+
+public class Test2Class {
+    public static ClassLoader getTCCL() {
+        return Thread.currentThread().getContextClassLoader();
+    }
+}

Added: aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/TestClass.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/TestClass.java?rev=1386716&view=auto
==============================================================================
--- aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/TestClass.java
(added)
+++ aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/TestClass.java
Mon Sep 17 16:55:03 2012
@@ -0,0 +1,25 @@
+/**
+ * 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.aries.spifly.statictool.bundle;
+
+public class TestClass {
+    public void doit() {
+        Test2Class.getTCCL();
+    }
+}

Modified: aries/trunk/spi-fly/spi-fly-weaver/src/main/java/org/apache/aries/spifly/weaver/TCCLSetterVisitor.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-weaver/src/main/java/org/apache/aries/spifly/weaver/TCCLSetterVisitor.java?rev=1386716&r1=1386715&r2=1386716&view=diff
==============================================================================
--- aries/trunk/spi-fly/spi-fly-weaver/src/main/java/org/apache/aries/spifly/weaver/TCCLSetterVisitor.java
(original)
+++ aries/trunk/spi-fly/spi-fly-weaver/src/main/java/org/apache/aries/spifly/weaver/TCCLSetterVisitor.java
Mon Sep 17 16:55:03 2012
@@ -64,6 +64,10 @@ public class TCCLSetterVisitor extends C
         this.weavingData = weavingData;
     }
 
+    public boolean isWoven() {
+        return woven;
+    }
+
     @Override
     public MethodVisitor visitMethod(int access, String name, String desc,
             String signature, String[] exceptions) {



Mime
View raw message