You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by bd...@apache.org on 2005/07/13 19:09:31 UTC

svn commit: r216190 - in /myfaces: api/trunk/src/test/ api/trunk/src/test/javax/ api/trunk/src/test/javax/faces/ impl/trunk/src/test/ tomahawk/trunk/src/test/

Author: bdudney
Date: Wed Jul 13 10:09:30 2005
New Revision: 216190

URL: http://svn.apache.org/viewcvs?rev=216190&view=rev
Log:
initial crack at testing

Added:
    myfaces/api/trunk/src/test/
    myfaces/api/trunk/src/test/javax/
    myfaces/api/trunk/src/test/javax/faces/
    myfaces/api/trunk/src/test/javax/faces/FacesExceptionTest.java
    myfaces/api/trunk/src/test/javax/faces/FactoryFinderTest.java
    myfaces/api/trunk/src/test/javax/faces/Mock2ApplicationFactory.java
    myfaces/api/trunk/src/test/javax/faces/MockApplicationFactory.java
    myfaces/impl/trunk/src/test/
    myfaces/tomahawk/trunk/src/test/

Added: myfaces/api/trunk/src/test/javax/faces/FacesExceptionTest.java
URL: http://svn.apache.org/viewcvs/myfaces/api/trunk/src/test/javax/faces/FacesExceptionTest.java?rev=216190&view=auto
==============================================================================
--- myfaces/api/trunk/src/test/javax/faces/FacesExceptionTest.java (added)
+++ myfaces/api/trunk/src/test/javax/faces/FacesExceptionTest.java Wed Jul 13 10:09:30 2005
@@ -0,0 +1,67 @@
+package javax.faces;
+
+import junit.framework.TestCase;
+
+public class FacesExceptionTest extends TestCase {
+
+	public static void main(String[] args) {
+		junit.textui.TestRunner.run(FacesExceptionTest.class);
+	}
+
+	public FacesExceptionTest(String name) {
+		super(name);
+	}
+
+	protected void setUp() throws Exception {
+		super.setUp();
+	}
+
+	protected void tearDown() throws Exception {
+		super.tearDown();
+	}
+
+	/*
+	 * Test method for 'javax.faces.FacesException.FacesException()'
+	 */
+	public void testFacesException() {
+		FacesException e = new FacesException();
+		assertNull(e.getCause());
+	}
+
+	/*
+	 * Test method for 'javax.faces.FacesException.FacesException(Throwable)'
+	 */
+	public void testFacesExceptionThrowable() {
+		Throwable t = new Throwable();
+		FacesException fe = new FacesException(t);
+		assertEquals(t, fe.getCause());
+	}
+
+	/*
+	 * Test method for 'javax.faces.FacesException.FacesException(String)'
+	 */
+	public void testFacesExceptionString() {
+		String m = "Message";
+		FacesException e = new FacesException(m);
+		assertEquals(e.getMessage(), m);
+	}
+
+	/*
+	 * Test method for 'javax.faces.FacesException.FacesException(String, Throwable)'
+	 */
+	public void testFacesExceptionStringThrowable() {
+		String m = "Message";
+		Throwable t = new Throwable();
+		FacesException fe = new FacesException(m, t);
+		assertEquals(t, fe.getCause());
+		assertEquals(fe.getMessage(), m);
+	}
+
+	/*
+	 * Test method for 'javax.faces.FacesException.getCause()'
+	 */
+	public void testGetCause() {
+
+	}
+
+}

Added: myfaces/api/trunk/src/test/javax/faces/FactoryFinderTest.java
URL: http://svn.apache.org/viewcvs/myfaces/api/trunk/src/test/javax/faces/FactoryFinderTest.java?rev=216190&view=auto
==============================================================================
--- myfaces/api/trunk/src/test/javax/faces/FactoryFinderTest.java (added)
+++ myfaces/api/trunk/src/test/javax/faces/FactoryFinderTest.java Wed Jul 13 10:09:30 2005
@@ -0,0 +1,218 @@
+package javax.faces;
+
+import java.lang.reflect.Field;
+import java.util.List;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+public class FactoryFinderTest extends TestCase {
+
+	public static void main(String[] args) {
+		junit.textui.TestRunner.run(FactoryFinderTest.class);
+	}
+
+	public FactoryFinderTest(String name) {
+		super(name);
+	}
+
+	protected void setUp() throws Exception {
+		super.setUp();
+	}
+
+	protected void tearDown() throws Exception {
+		super.tearDown();
+		FactoryFinder.releaseFactories();
+		releaseRegisteredFactoryNames();
+	}
+
+    private void releaseRegisteredFactoryNames() throws Exception {
+        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+        Map _registeredFactoryNames = getRegisteredFactoryNames();
+		_registeredFactoryNames.remove(classLoader);
+	}
+
+	private List registeredFactoryNames(String factoryName) throws Exception {
+        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+        Map _registeredFactoryNames = getRegisteredFactoryNames();
+		Map map = (Map)_registeredFactoryNames.get(classLoader);
+		return (List)map.get(factoryName);
+	}
+
+	/*
+	 * This method allows us access to the _registeredFactoryNames field so we can test
+	 * the content of that map during the running of this test.
+	 *  
+	 * @return Returns the _registeredFactoryNames Map from the FactoryFinder class. 
+	 * @throws NoSuchFieldException
+	 * @throws IllegalAccessException
+	 */
+	private Map getRegisteredFactoryNames() throws NoSuchFieldException, IllegalAccessException {
+		Class factoryFinderClass = FactoryFinder.class;
+		Field fields[] = factoryFinderClass.getDeclaredFields();
+		Field field = null;
+		for(int i = 0;i < fields.length;i++) {
+			if(fields[i].getName().equals("_registeredFactoryNames")) {
+				field = fields[i];
+				field.setAccessible(true);
+				break;
+			}
+		}
+        Map _registeredFactoryNames = (Map)field.get(null);
+		return _registeredFactoryNames;
+	}
+
+
+	/*
+	 * Test method for 'javax.faces.FactoryFinder.getFactory(String)'
+	 */
+	public void testGetFactory() throws Exception {
+		// no catch because if this fails the test fails, i.e. not trying to test setFactory here
+		FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, MockApplicationFactory.class.getName());
+		try {
+			Object factory = FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
+			assertNotNull(factory);
+			assertTrue(factory.getClass().equals(MockApplicationFactory.class));
+		} catch (IllegalStateException e) {
+			fail("Should not throw an illegal state exception");
+		}
+	}
+
+	/*
+	 * Test method for 'javax.faces.FactoryFinder.getFactory(String)'
+	 */
+	public void testGetFactoryTwice() throws Exception {
+		// this test just makes sure that things work when the get has been called more than once
+		FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, MockApplicationFactory.class.getName());
+		try {
+			Object factory1 = FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
+			assertNotNull(factory1);
+			assertTrue(factory1.getClass().equals(MockApplicationFactory.class));
+			Object factory2 = FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
+			assertNotNull(factory2);
+			assertTrue(factory2.getClass().equals(MockApplicationFactory.class));
+			assertEquals(factory1, factory2);
+		} catch (IllegalStateException e) {
+			fail("Should not throw an illegal state exception");
+		}
+	}
+
+	/*
+	 * Test method for 'javax.faces.FactoryFinder.getFactory(String)'
+	 */
+	public void testGetFactoryNoFactory() throws Exception {
+		// no catch because if this fails the test fails, i.e. not trying to test setFactory here
+		FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, MockApplicationFactory.class.getName());
+		try {
+			FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
+			fail("Should have thrown an illegal state exception");
+		} catch (IllegalStateException e) {
+			assertNotNull(e.getMessage());
+		}
+	}
+
+	/*
+	 * No configuration test, this should throw and deliver a useful message
+	 * Test method for 'javax.faces.FactoryFinder.getFactory(String)'
+	 */
+	public void testGetFactoryNoConfiguration() throws Exception {
+		try {
+			FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
+			fail("Should have thrown an illegal state exception");
+		} catch (IllegalStateException e) {
+			assertNotNull(e.getMessage());
+			assertTrue(e.getMessage().startsWith("No Factories configured for this Application"));
+		}
+	}
+
+	/*
+	 * Bogus factory name test
+	 * Test method for 'javax.faces.FactoryFinder.setFactory(String, String)'
+	 */
+	public void testSetFactoryBogusName() {
+		try {
+			FactoryFinder.setFactory("BogusFactoryName", MockApplicationFactory.class.getName());
+			fail("Should have thrown an illegal argument exception");
+		} catch (IllegalArgumentException e) {
+			assertNotNull(e.getMessage());
+		}
+	}
+
+	/*
+	 * Test method for 'javax.faces.FactoryFinder.setFactory(String, String)'
+	 */
+	public void testSetFactory() throws Exception {
+		try {
+			FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
+					MockApplicationFactory.class.getName());
+			assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY)
+					.contains(
+					MockApplicationFactory.class.getName()));
+		} catch (IllegalArgumentException e) {
+			fail("Should not throw an illegal argument exception");
+		}
+	}
+
+	/*
+	 * If a factory has ever been handed out then setFactory is not supposed to change the factory layout.
+	 * This test checks to see if that is true.
+	 * Test method for 'javax.faces.FactoryFinder.setFactory(String, String)'
+	 */
+	public void testSetFactoryNoEffect() throws Exception {
+		try {
+			FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
+					MockApplicationFactory.class.getName());
+			assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY)
+					.contains(MockApplicationFactory.class.getName()));
+			assertFalse(registeredFactoryNames(
+					FactoryFinder.APPLICATION_FACTORY).contains(
+					Mock2ApplicationFactory.class.getName()));
+			// getFactory should cause setFactory to stop changing the
+			// registered classes
+			FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
+			// this should essentially be a no-op
+			FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
+					Mock2ApplicationFactory.class.getName());
+			assertFalse(registeredFactoryNames(
+					FactoryFinder.APPLICATION_FACTORY).contains(
+					Mock2ApplicationFactory.class.getName()));
+			assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY)
+					.contains(MockApplicationFactory.class.getName()));
+		} catch (IllegalArgumentException e) {
+			fail("Should not throw an illegal argument exception");
+		}
+	}
+
+	/*
+	 * Adding factories should add the class name to the list of avalable class
+	 * names Test method for 'javax.faces.FactoryFinder.setFactory(String,
+	 * String)'
+	 */
+	public void testSetFactoryAdditiveClassNames() throws Exception {
+		try {
+			FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
+					MockApplicationFactory.class.getName());
+			assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY)
+					.contains(MockApplicationFactory.class.getName()));
+			assertFalse(registeredFactoryNames(
+					FactoryFinder.APPLICATION_FACTORY).contains(
+					Mock2ApplicationFactory.class.getName()));
+			FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
+					Mock2ApplicationFactory.class.getName());
+			assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY)
+					.contains(Mock2ApplicationFactory.class.getName()));
+			assertTrue(registeredFactoryNames(FactoryFinder.APPLICATION_FACTORY)
+					.contains(MockApplicationFactory.class.getName()));
+		} catch (IllegalArgumentException e) {
+			fail("Should not throw an illegal argument exception");
+		}
+	}
+
+	/*
+	 * Test method for 'javax.faces.FactoryFinder.releaseFactories()'
+	 */
+	public void testReleaseFactories() {
+
+	}
+
+}

Added: myfaces/api/trunk/src/test/javax/faces/Mock2ApplicationFactory.java
URL: http://svn.apache.org/viewcvs/myfaces/api/trunk/src/test/javax/faces/Mock2ApplicationFactory.java?rev=216190&view=auto
==============================================================================
--- myfaces/api/trunk/src/test/javax/faces/Mock2ApplicationFactory.java (added)
+++ myfaces/api/trunk/src/test/javax/faces/Mock2ApplicationFactory.java Wed Jul 13 10:09:30 2005
@@ -0,0 +1,25 @@
+package javax.faces;
+
+import javax.faces.application.Application;
+import javax.faces.application.ApplicationFactory;
+
+public class Mock2ApplicationFactory extends ApplicationFactory {
+	private Application app = null;
+	private ApplicationFactory factory = null;
+
+	public Mock2ApplicationFactory() {
+	}
+
+	public Mock2ApplicationFactory(ApplicationFactory factory) {
+		this.factory = factory;
+	}
+
+	public Application getApplication() {
+		return app;
+	}
+
+	public void setApplication(Application application) {
+		this.app = application;
+	}
+
+}

Added: myfaces/api/trunk/src/test/javax/faces/MockApplicationFactory.java
URL: http://svn.apache.org/viewcvs/myfaces/api/trunk/src/test/javax/faces/MockApplicationFactory.java?rev=216190&view=auto
==============================================================================
--- myfaces/api/trunk/src/test/javax/faces/MockApplicationFactory.java (added)
+++ myfaces/api/trunk/src/test/javax/faces/MockApplicationFactory.java Wed Jul 13 10:09:30 2005
@@ -0,0 +1,25 @@
+package javax.faces;
+
+import javax.faces.application.Application;
+import javax.faces.application.ApplicationFactory;
+
+public class MockApplicationFactory extends ApplicationFactory {
+	private Application app = null;
+	private ApplicationFactory factory;
+
+	public MockApplicationFactory() {
+	}
+
+	public MockApplicationFactory(ApplicationFactory factory) {
+		this.factory = factory;
+	}
+	
+	public Application getApplication() {
+		return app;
+	}
+
+	public void setApplication(Application application) {
+		this.app = application;
+	}
+
+}