felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject svn commit: r1640977 - /felix/trunk/framework/src/test/java/org/apache/felix/framework/BundleWiringImplTest.java
Date Fri, 21 Nov 2014 19:01:38 GMT
Author: bob
Date: Fri Nov 21 19:01:38 2014
New Revision: 1640977

URL: http://svn.apache.org/r1640977
Log:
FELIX-4582 Add additional tests to BundleWiringImpl.  Cover class exists and class does not
exist.

Modified:
    felix/trunk/framework/src/test/java/org/apache/felix/framework/BundleWiringImplTest.java

Modified: felix/trunk/framework/src/test/java/org/apache/felix/framework/BundleWiringImplTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/test/java/org/apache/felix/framework/BundleWiringImplTest.java?rev=1640977&r1=1640976&r2=1640977&view=diff
==============================================================================
--- felix/trunk/framework/src/test/java/org/apache/felix/framework/BundleWiringImplTest.java
(original)
+++ felix/trunk/framework/src/test/java/org/apache/felix/framework/BundleWiringImplTest.java
Fri Nov 21 19:01:38 2014
@@ -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;
+	
+	private BundleRevisionImpl mockRevisionImpl;
 	
-	@Before
-	public void setUp() throws Exception 
+	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{
+		
+	}
 }



Mime
View raw message