Author: struberg Date: Wed Dec 19 21:50:45 2012 New Revision: 1424159 URL: http://svn.apache.org/viewvc?rev=1424159&view=rev Log: add a unit test for a test Weaver
Added: commons/sandbox/privilizer/trunk/processor/src/test/ commons/sandbox/privilizer/trunk/processor/src/test/java/ commons/sandbox/privilizer/trunk/processor/src/test/java/org/ commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/ commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/ commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/ commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/ commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaveProcessorTest.java (with props) commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaverTestBase.java (with props) commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/ commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestAnnotation.java (with props) commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanWithClassAnnotation.java (with props) commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanWithMethodAnnotation.java (with props) commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/weaver/ commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java (with props) commons/sandbox/privilizer/trunk/processor/src/test/resources/ commons/sandbox/privilizer/trunk/processor/src/test/resources/META-INF/ commons/sandbox/privilizer/trunk/processor/src/test/resources/META-INF/services/ commons/sandbox/privilizer/trunk/processor/src/test/resources/META-INF/services/org.apache.commons.weaver.spi.Weaver Modified: commons/sandbox/privilizer/trunk/pom.xml commons/sandbox/privilizer/trunk/processor/pom.xml commons/sandbox/privilizer/trunk/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java Modified: commons/sandbox/privilizer/trunk/pom.xml URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/trunk/pom.xml?rev=1424159&r1=1424158&r2=1424159&view=diff ============================================================================== --- commons/sandbox/privilizer/trunk/pom.xml (original) +++ commons/sandbox/privilizer/trunk/pom.xml Wed Dec 19 21:50:45 2012 @@ -54,6 +54,12 @@ <email>mbenson AT apache DOT org</email> <organization>Apache</organization> </developer> + <developer> + <id>struberg</id> + <name>Mark Struberg</name> + <email>struberg AT apache DOT org</email> + <organization>Apache</organization> + </developer> </developers> <scm> @@ -70,16 +76,14 @@ </site> </distributionManagement> - <dependencyManagement> - <dependencies> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>4.10</version> - <scope>test</scope> - </dependency> - </dependencies> - </dependencyManagement> + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.10</version> + <scope>test</scope> + </dependency> + </dependencies> <build> <pluginManagement> Modified: commons/sandbox/privilizer/trunk/processor/pom.xml URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/trunk/processor/pom.xml?rev=1424159&r1=1424158&r2=1424159&view=diff ============================================================================== --- commons/sandbox/privilizer/trunk/processor/pom.xml (original) +++ commons/sandbox/privilizer/trunk/processor/pom.xml Wed Dec 19 21:50:45 2012 @@ -35,7 +35,7 @@ <dependencies> <dependency> <groupId>org.apache.xbean</groupId> - <artifactId>xbean-finder</artifactId> + <artifactId>xbean-finder-shaded</artifactId> <version>3.12</version> </dependency> Modified: commons/sandbox/privilizer/trunk/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/trunk/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java?rev=1424159&r1=1424158&r2=1424159&view=diff ============================================================================== --- commons/sandbox/privilizer/trunk/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java (original) +++ commons/sandbox/privilizer/trunk/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java Wed Dec 19 21:50:45 2012 @@ -103,7 +103,9 @@ public class WeaveProcessor { ClassLoader classLoader = new URLClassLoader(URLArray.fromFiles(classPathsToWeave)); - AnnotationFinder annotationFinder = new AnnotationFinder(new FileArchive(classLoader, target), false); + //X ORIGINAL AnnotationFinder annotationFinder = new AnnotationFinder(new FileArchive(classLoader, target), false); + //X TODO this is a hack for now! + AnnotationFinder annotationFinder = new AnnotationFinder(new FileArchive(classLoader, classPathsToWeave.get(0)), false); for (Class<? extends Annotation> annotation : interest) { List<Class<?>> annotatedClasses = annotationFinder.findAnnotatedClasses(annotation); Added: commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaveProcessorTest.java URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaveProcessorTest.java?rev=1424159&view=auto ============================================================================== --- commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaveProcessorTest.java (added) +++ commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaveProcessorTest.java Wed Dec 19 21:50:45 2012 @@ -0,0 +1,65 @@ +/* + * 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.commons.weaver.test; + +import java.util.Properties; + +import org.apache.commons.weaver.test.beans.TestBeanWithClassAnnotation; +import org.apache.commons.weaver.test.beans.TestBeanWithMethodAnnotation; +import org.apache.commons.weaver.test.weaver.TestWeaver; +import org.apache.commons.weaver.WeaveProcessor; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Test the {@link WeaveProcessor} + */ +public class WeaveProcessorTest extends WeaverTestBase +{ + + @Test + public void testWeaveVisiting() throws Exception { + addClassForScanning(TestBeanWithMethodAnnotation.class); + addClassForScanning(TestBeanWithClassAnnotation.class); + + WeaveProcessor wp = WeaveProcessor.getInstance(); + + Properties config = new Properties(); + config.put("configKey", "configValue"); + wp.configure(config); + + wp.addClassPath(getTargetFolder()); + + TestWeaver.postWeaveExecuted = false; + TestWeaver.preWeaveExecuted = false; + TestWeaver.wovenClasses.clear(); + TestWeaver.wovenMethods.clear(); + + wp.weave(); + + Assert.assertTrue(TestWeaver.preWeaveExecuted); + Assert.assertTrue(TestWeaver.postWeaveExecuted); + Assert.assertEquals(1, TestWeaver.wovenClasses.size()); + Assert.assertEquals(TestBeanWithClassAnnotation.class, TestWeaver.wovenClasses.get(0)); + + Assert.assertEquals(1, TestWeaver.wovenMethods.size()); + Assert.assertEquals(TestBeanWithMethodAnnotation.class, TestWeaver.wovenMethods.get(0)); + } +} Propchange: commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaveProcessorTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaverTestBase.java URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaverTestBase.java?rev=1424159&view=auto ============================================================================== --- commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaverTestBase.java (added) +++ commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaverTestBase.java Wed Dec 19 21:50:45 2012 @@ -0,0 +1,71 @@ +/* + * 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.commons.weaver.test; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; + +/** + * Base class for Weaver tests. + */ +public abstract class WeaverTestBase { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private static final String TARGET_FOLDER = "target"; + /** + * Add a class to the temporary folder. + * @param clazz + */ + protected void addClassForScanning(Class clazz) throws IOException{ + String clazzDirName = clazz.getPackage().getName().replace(".", "/"); + File targetDirFile = new File(getTargetFolder(), clazzDirName); + targetDirFile.mkdirs(); + + String clazzFileName = clazzDirName + "/" + clazz.getSimpleName() + ".class"; + URL clazzUrl = getClass().getClassLoader().getResource(clazzFileName); + File targetClazzFile = new File(targetDirFile, clazz.getSimpleName() + ".class"); + + byte[] buffer = new byte[0xFFFF]; + + FileOutputStream fos = new FileOutputStream(targetClazzFile); + + InputStream inputStream = clazzUrl.openStream(); + int len; + while ((len = inputStream.read(buffer)) > 0) { + fos.write(buffer, 0, len); + } + fos.flush(); + fos.close(); + } + + /** + * Resolves the 'target' folder where the classes should get placed + */ + protected File getTargetFolder() { + return new File(temporaryFolder.getRoot(), TARGET_FOLDER); + } +} Propchange: commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaverTestBase.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestAnnotation.java URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestAnnotation.java?rev=1424159&view=auto ============================================================================== --- commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestAnnotation.java (added) +++ commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestAnnotation.java Wed Dec 19 21:50:45 2012 @@ -0,0 +1,32 @@ +/* + * 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.commons.weaver.test.beans; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Test marker annotation to test the scanning + */ +@Target({ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.CLASS) +public @interface TestAnnotation { +} Propchange: commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestAnnotation.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanWithClassAnnotation.java URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanWithClassAnnotation.java?rev=1424159&view=auto ============================================================================== --- commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanWithClassAnnotation.java (added) +++ commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanWithClassAnnotation.java Wed Dec 19 21:50:45 2012 @@ -0,0 +1,30 @@ +/* + * 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.commons.weaver.test.beans; + +/** + * Simple test bean + */ +@TestAnnotation +public class TestBeanWithClassAnnotation +{ + private int someMethod() { + return 42; + } +} Propchange: commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanWithClassAnnotation.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanWithMethodAnnotation.java URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanWithMethodAnnotation.java?rev=1424159&view=auto ============================================================================== --- commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanWithMethodAnnotation.java (added) +++ commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanWithMethodAnnotation.java Wed Dec 19 21:50:45 2012 @@ -0,0 +1,34 @@ +/* + * 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.commons.weaver.test.beans; + +/** + * Simple test bean + */ +public class TestBeanWithMethodAnnotation +{ + @TestAnnotation + private int annotatedMethod() { + return 42; + } + + private int nonAnnotatedMethod() { + return 21; + } +} Propchange: commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/beans/TestBeanWithMethodAnnotation.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java?rev=1424159&view=auto ============================================================================== --- commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java (added) +++ commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java Wed Dec 19 21:50:45 2012 @@ -0,0 +1,82 @@ +/* + * 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.commons.weaver.test.weaver; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import org.apache.commons.weaver.test.beans.TestAnnotation; +import org.junit.Assert; + +import org.apache.commons.weaver.spi.Weaver; + +/** + */ +public class TestWeaver implements Weaver +{ + public static boolean preWeaveExecuted = false; + public static boolean postWeaveExecuted = false; + public static List<Method> wovenMethods = new ArrayList<Method>(); + public static List<Class> wovenClasses = new ArrayList<Class>(); + + @Override + public void configure(Properties config) + { + Assert.assertNotNull(config); + Assert.assertEquals(1, config.size()); + + String configValue = config.getProperty("configKey"); + Assert.assertEquals("configValue", configValue); + } + + @Override + public List<Class<? extends Annotation>> getInterest() + { + List<Class<? extends Annotation>> interests = new ArrayList<Class<? extends Annotation>>(); + interests.add(TestAnnotation.class); + return interests; + } + + @Override + public void preWeave() + { + preWeaveExecuted = true; + } + + @Override + public boolean weave(Class classToWeave) + { + return wovenClasses.add(classToWeave); + } + + @Override + public boolean weave(Method methodToWeave) + { + return wovenMethods.add(methodToWeave); + } + + @Override + public void postWeave() + { + postWeaveExecuted = true; + } +} Propchange: commons/sandbox/privilizer/trunk/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/sandbox/privilizer/trunk/processor/src/test/resources/META-INF/services/org.apache.commons.weaver.spi.Weaver URL: http://svn.apache.org/viewvc/commons/sandbox/privilizer/trunk/processor/src/test/resources/META-INF/services/org.apache.commons.weaver.spi.Weaver?rev=1424159&view=auto ============================================================================== --- commons/sandbox/privilizer/trunk/processor/src/test/resources/META-INF/services/org.apache.commons.weaver.spi.Weaver (added) +++ commons/sandbox/privilizer/trunk/processor/src/test/resources/META-INF/services/org.apache.commons.weaver.spi.Weaver Wed Dec 19 21:50:45 2012 @@ -0,0 +1,19 @@ +# 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. + +# this class gets picked up by the WeaveProcessor +org.apache.commons.weaver.test.weaver.TestWeaver