FELIX-4582 Add additional tests to BundleWiringImpl.  Cover class exists and class does not exist.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1640977 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/test/java/org/apache/felix/framework/BundleWiringImplTest.java b/framework/src/test/java/org/apache/felix/framework/BundleWiringImplTest.java
index e408255..2558c45 100644
--- a/framework/src/test/java/org/apache/felix/framework/BundleWiringImplTest.java
+++ b/framework/src/test/java/org/apache/felix/framework/BundleWiringImplTest.java
@@ -19,35 +19,149 @@
 package org.apache.felix.framework;

 

 import static org.junit.Assert.*;

+import static org.mockito.Mockito.*;

 

+import java.io.ByteArrayOutputStream;

+import java.io.InputStream;

 import java.lang.reflect.Constructor;

+import java.util.ArrayList;

+import java.util.HashMap;

+import java.util.List;

+import java.util.Map;

 

 import org.apache.felix.framework.BundleWiringImpl.BundleClassLoader;

-import org.junit.Before;

+import org.apache.felix.framework.BundleWiringImpl.BundleClassLoaderJava5;

+import org.apache.felix.framework.cache.Content;

+

 import org.junit.Test;

 

+import org.osgi.framework.wiring.BundleRevision;

+import org.osgi.framework.wiring.BundleWire;

+

 public class BundleWiringImplTest 

 {

 

 	private BundleWiringImpl bundleWiring;

 	

-	private BundleClassLoader bundleClassLoader;

+	private StatefulResolver mockResolver;

 	

-	@Before

-	public void setUp() throws Exception 

+	private BundleRevisionImpl mockRevisionImpl;

+	

+	private BundleImpl mockBundle;

+	

+	public void initializeSimpleBundleWiring() throws Exception

 	{

+		

+		mockResolver = mock(StatefulResolver.class);

+		mockRevisionImpl = mock(BundleRevisionImpl.class);

+		mockBundle = mock(BundleImpl.class);

+		

 		Logger logger = new Logger();

-		Constructor ctor = BundleRevisionImpl.getSecureAction()

-                .getConstructor(BundleClassLoader.class, new Class[] { BundleWiringImpl.class, ClassLoader.class, Logger.class });

-            bundleClassLoader = (BundleClassLoader)

-                BundleRevisionImpl.getSecureAction().invoke(ctor,

-                new Object[] { bundleWiring, this.getClass().getClassLoader(), logger });

+		Map configMap = new HashMap();

+		List<BundleRevision> fragments = new ArrayList<BundleRevision>();

+		List<BundleWire> wires = new ArrayList<BundleWire>();

+		Map<String, BundleRevision> importedPkgs = new HashMap<String, BundleRevision>();

+		Map<String, List<BundleRevision>> requiredPkgs =

+				new HashMap<String, List<BundleRevision>>();

+		

+		when(mockRevisionImpl.getBundle()).thenReturn(mockBundle);

+		when(mockBundle.getBundleId()).thenReturn(Long.valueOf(1));

+		

+		bundleWiring = new BundleWiringImpl(logger, configMap, mockResolver, mockRevisionImpl,

+				fragments, wires, importedPkgs, requiredPkgs);

 	}

 

 	@Test

-	public void testBundleClassLoader() 

+	public void testBundleClassLoader() throws Exception

 	{

+		bundleWiring = mock(BundleWiringImpl.class);

+		BundleClassLoader bundleClassLoader = createBundleClassLoader(BundleClassLoader.class, bundleWiring);

 		assertNotNull(bundleClassLoader);

 	}

-

+	

+	@Test

+	public void testBundleClassLoaderJava5() throws Exception

+	{

+		bundleWiring = mock(BundleWiringImpl.class);

+		BundleClassLoader bundleClassLoader = createBundleClassLoader(BundleClassLoaderJava5.class, bundleWiring);

+		assertNotNull(bundleClassLoader);

+	}

+	

+	@Test

+	public void testFindClassNonExistant() throws Exception

+	{

+		initializeSimpleBundleWiring();

+		

+		BundleClassLoader bundleClassLoader = createBundleClassLoader(BundleClassLoaderJava5.class, bundleWiring);

+		assertNotNull(bundleClassLoader);

+		Class foundClass = null;

+		try {

+			foundClass = bundleClassLoader.findClass("org.apache.felix.test.NonExistant");

+		} 

+		catch (ClassNotFoundException e) 

+		{

+			fail("Class should not throw exception");

+		}

+		assertNull("Nonexistant Class Should be null", foundClass);

+	}

+	

+	@Test

+	public void testFindClassExistant() throws Exception

+	{

+		Felix mockFramework = mock(Felix.class);

+		Content mockContent = mock(Content.class);

+		Class testClass = TestClass.class;

+		String testClassName = testClass.getName();

+		String testClassAsPath = testClassName.replace('.', '/') + ".class";

+		InputStream testClassResourceStream = 

+				testClass.getClassLoader().getResourceAsStream(testClassAsPath);

+		

+		

+		ByteArrayOutputStream baos = new ByteArrayOutputStream();

+		int curByte;

+		while((curByte = testClassResourceStream.read()) != -1)

+		{

+			baos.write(curByte);

+		}

+		byte[] testClassBytes = baos.toByteArray();

+		

+		List<Content> contentPath = new ArrayList<Content>();

+		contentPath.add(mockContent);

+		initializeSimpleBundleWiring();

+		

+		when(mockBundle.getFramework()).thenReturn(mockFramework);

+		when(mockFramework.getBootPackages()).thenReturn(new String[0]);

+		

+		when(mockRevisionImpl.getContentPath()).thenReturn(contentPath);

+		when(mockContent.getEntryAsBytes(testClassAsPath)).thenReturn(testClassBytes);

+		

+		BundleClassLoader bundleClassLoader = createBundleClassLoader(BundleClassLoaderJava5.class, bundleWiring);

+		assertNotNull(bundleClassLoader);

+		Class foundClass = null;

+		try {

+			

+			foundClass = bundleClassLoader.findClass(TestClass.class.getName());

+		} 

+		catch (ClassNotFoundException e) 

+		{

+			fail("Class should not throw exception");

+		}

+		assertNotNull("Class Should be found in this classloader", foundClass);

+	}

+	

+	

+	private BundleClassLoader createBundleClassLoader(Class bundleClassLoaderClass, BundleWiringImpl bundleWiring) throws Exception

+	{

+		Logger logger = new Logger();

+		Constructor ctor = BundleRevisionImpl.getSecureAction()

+                .getConstructor(bundleClassLoaderClass, new Class[] { BundleWiringImpl.class, ClassLoader.class, Logger.class });

+		BundleClassLoader bundleClassLoader = (BundleClassLoader)

+                BundleRevisionImpl.getSecureAction().invoke(ctor,

+                new Object[] { bundleWiring, this.getClass().getClassLoader(), logger });

+        return bundleClassLoader;

+	}

+	

+	class TestClass{

+		

+	}

 }