KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > util > FactoryUtilTest


1 package info.magnolia.cms.util;
2
3 import junit.framework.TestCase;
4
5 public class FactoryUtilTest extends TestCase {
6
7     public void testConfiguredImplementation(){
8        FactoryUtil.clear();
9        FactoryUtil.setDefaultImplementation(FactoryUtilTestInterface.class, FactoryUtilTestImplementation.class);
10        Object JavaDoc obj = FactoryUtil.getSingleton(FactoryUtilTestInterface.class);
11        assertTrue(obj instanceof FactoryUtilTestImplementation);
12    }
13
14     public void testDontRedefineTheDefaultImplementation(){
15         FactoryUtil.clear();
16         FactoryUtil.setDefaultImplementation(FactoryUtilTestInterface.class, FactoryUtilTestImplementation.class);
17         FactoryUtil.setDefaultImplementation(FactoryUtilTestInterface.class, "a.wrong.class.not.set");
18         Object JavaDoc obj = FactoryUtil.getSingleton(FactoryUtilTestInterface.class);
19         assertTrue(obj instanceof FactoryUtilTestImplementation);
20     }
21
22    public void testDefaultImplementation(){
23        FactoryUtil.clear();
24        Object JavaDoc obj = FactoryUtil.getSingleton(FactoryUtilTestImplementation.class);
25        assertTrue(obj instanceof FactoryUtilTestImplementation);
26    }
27
28    public void testSingleton(){
29        FactoryUtil.clear();
30        assertEquals(FactoryUtil.getSingleton(FactoryUtilTestImplementation.class), FactoryUtil.getSingleton(FactoryUtilTestImplementation.class));
31    }
32    
33    public void testNewInstance(){
34        FactoryUtil.clear();
35        assertNotSame(FactoryUtil.newInstance(FactoryUtilTestImplementation.class), FactoryUtil.newInstance(FactoryUtilTestImplementation.class));
36    }
37
38    public void testSetSingletonInstance(){
39        FactoryUtil.clear();
40        FactoryUtilTestImplementation instance = new FactoryUtilTestImplementation();
41        FactoryUtil.setInstance(FactoryUtilTestInterface.class, instance);
42        assertSame(instance, FactoryUtil.getSingleton(FactoryUtilTestInterface.class));
43    }
44
45    public void testInstanceFactory(){
46        FactoryUtil.clear();
47        FactoryUtil.setInstanceFactory(FactoryUtilTestInterface.class, new FactoryUtil.InstanceFactory(){
48            public Object JavaDoc newInstance() {
49                return new FactoryUtilTestOtherImplementation();
50            }
51        });
52        
53        assertTrue(FactoryUtil.getSingleton(FactoryUtilTestInterface.class) instanceof FactoryUtilTestOtherImplementation);
54    }
55
56    
57
58 }
59
Popular Tags