Created
April 17, 2012 15:08
-
-
Save cescoffier/2406634 to your computer and use it in GitHub Desktop.
Unit Test reproducing FELIX-3461
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import org.apache.felix.ipojo.annotations.*; | |
import test.ipojo.ExternalHandler; | |
import java.util.List; | |
@Component | |
@Instantiate | |
public class PlentyOfAnnotations { | |
@Requires | |
List list; | |
private String m_prop; | |
private Runnable m_runnable; | |
private String m_stuff; | |
@ExternalHandler | |
private String stuff2; | |
PlentyOfAnnotations(@Property String prop, @Requires Runnable runnable, @ExternalHandler String stuff) { | |
m_prop = prop; | |
m_runnable = runnable; | |
m_stuff = stuff; | |
} | |
@Validate | |
public void start() { | |
//... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.apache.felix.ipojo.manipulation; | |
import junit.framework.Assert; | |
import junit.framework.TestCase; | |
import org.apache.felix.ipojo.manipulator.Reporter; | |
import org.apache.felix.ipojo.manipulator.ResourceStore; | |
import org.apache.felix.ipojo.manipulator.ResourceVisitor; | |
import org.apache.felix.ipojo.manipulator.metadata.AnnotationMetadataProvider; | |
import org.apache.felix.ipojo.manipulator.util.Streams; | |
import org.apache.felix.ipojo.manipulator.util.Strings; | |
import org.apache.felix.ipojo.metadata.Element; | |
import test.AnnotatedComponent; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import static org.mockito.Mockito.mock; | |
/** | |
* Test Case for FELIX-3461. | |
* Checks the consistency of multiple manipulation. | |
*/ | |
public class RemanipulationTest extends TestCase { | |
/** | |
* Tests checking that the consecutive manipulation does still returns valid metadata (from annotations), | |
* and valid manipulation metadata. | |
* | |
* @throws IOException | |
* @throws ClassNotFoundException | |
*/ | |
public void testDoubleManipulationWithAnnotations() throws IOException, ClassNotFoundException { | |
Reporter reporter = mock(Reporter.class); | |
// Step 1 - First collection and manipulation | |
//1.1 Metadata collection | |
MiniStore store = new MiniStore() | |
.addClassToStore("test.PlentyOfAnnotations", | |
ManipulatorTest.getBytesFromFile(new File("target/test-classes/test/PlentyOfAnnotations.class"))); | |
AnnotationMetadataProvider provider = new AnnotationMetadataProvider(store, reporter); | |
List<Element> originalMetadata = provider.getMetadatas(); | |
// 1.2 Manipulation | |
Manipulator manipulator = new Manipulator(); | |
byte[] clazz = manipulator.manipulate( | |
ManipulatorTest.getBytesFromFile(new File("target/test-classes/test/PlentyOfAnnotations.class"))); | |
Element originalManipulationMetadata = manipulator.getManipulationMetadata(); | |
// 1.3 Check that the class is valid | |
TestClassLoader classloader = new TestClassLoader("test.PlentyOfAnnotations", clazz); | |
Class cl = classloader.findClass("test.PlentyOfAnnotations"); | |
Assert.assertNotNull(cl); | |
// --------------- | |
// Step 2 - Second collection and manipulation | |
// We use the output class as entry. | |
// 2.1 Metadata collection | |
store = new MiniStore().addClassToStore("test.PlentyOfAnnotations", clazz); | |
provider = new AnnotationMetadataProvider(store, reporter); | |
List<Element> metadataAfterOneManipulation = provider.getMetadatas(); | |
// 2.2 Manipulation | |
manipulator = new Manipulator(); | |
byte[] clazz2 = manipulator.manipulate( | |
ManipulatorTest.getBytesFromFile(new File("target/test-classes/test/PlentyOfAnnotations.class"))); | |
Element manipulationMetadataAfterSecondManipulation = manipulator.getManipulationMetadata(); | |
// 2.3 Check that the class is valid | |
classloader = new TestClassLoader("test.PlentyOfAnnotations", clazz); | |
cl = classloader.findClass("test.PlentyOfAnnotations"); | |
Assert.assertNotNull(cl); | |
// --------------- | |
// Step 3 - Third collection and manipulation | |
// We use the output class of 2 as entry. | |
// 3.1 Metadata collection | |
store = new MiniStore().addClassToStore("test.PlentyOfAnnotations", clazz2); | |
provider = new AnnotationMetadataProvider(store, reporter); | |
List<Element> metadataAfterTwoManipulation = provider.getMetadatas(); | |
// 3.2 Manipulation | |
manipulator = new Manipulator(); | |
byte[] clazz3 = manipulator.manipulate( | |
ManipulatorTest.getBytesFromFile(new File("target/test-classes/test/PlentyOfAnnotations.class"))); | |
Element manipulationMetadataAfterThirdManipulation = manipulator.getManipulationMetadata(); | |
// 3.3 Check that the class is valid | |
classloader = new TestClassLoader("test.PlentyOfAnnotations", clazz); | |
cl = classloader.findClass("test.PlentyOfAnnotations"); | |
Assert.assertNotNull(cl); | |
// --------------- | |
// Verification | |
// Unchanged metadata | |
Assert.assertEquals(originalMetadata, metadataAfterOneManipulation); | |
Assert.assertEquals(originalMetadata, metadataAfterTwoManipulation); | |
// Unchanged manipulation metadata | |
Assert.assertEquals(originalManipulationMetadata, manipulationMetadataAfterSecondManipulation); | |
Assert.assertEquals(originalManipulationMetadata, manipulationMetadataAfterThirdManipulation); | |
} | |
private class MiniStore implements ResourceStore { | |
private Map<String, byte[]> resources; | |
public MiniStore addClassToStore(String qualifiedName, byte[] bytes) { | |
if (this.resources == null) { | |
this.resources = new HashMap<String, byte[]>(); | |
} | |
this.resources.put(Strings.asResourcePath(qualifiedName), bytes); | |
return this; | |
} | |
public byte[] read(String path) throws IOException { | |
return resources.get(path); | |
} | |
public void accept(ResourceVisitor visitor) { | |
for (Map.Entry<String, byte[]> entry : resources.entrySet()) { | |
visitor.visit(entry.getKey()); | |
} | |
} | |
public void open() throws IOException { | |
} | |
public void writeMetadata(Element metadata) { | |
} | |
public void write(String resourcePath, byte[] resource) throws IOException { | |
} | |
public void close() throws IOException { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment