KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > script > ScriptBuilderResolverTestCase


1 package org.nanocontainer.script;
2
3 import java.io.File JavaDoc;
4 import java.net.URL JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Set JavaDoc;
8 import java.util.TreeSet JavaDoc;
9 import junit.framework.TestCase;
10
11 /**
12  * Name/Builder Resolution Test Cases.
13  * @author Michael Rimov
14  */

15 public class ScriptBuilderResolverTestCase extends TestCase {
16     private ScriptBuilderResolver scriptBuilderResolver = null;
17
18     protected void setUp() throws Exception JavaDoc {
19         super.setUp();
20         scriptBuilderResolver = new ScriptBuilderResolver();
21     }
22
23     protected void tearDown() throws Exception JavaDoc {
24         scriptBuilderResolver = null;
25         super.tearDown();
26     }
27
28
29     public void testGetAllSupportedExtensions() {
30         Set JavaDoc allExtensions = new TreeSet JavaDoc();
31
32         allExtensions.add(ScriptBuilderResolver.XML);
33
34         String JavaDoc[] actualReturn = scriptBuilderResolver.getAllSupportedExtensions();
35         assertNotNull(actualReturn);
36
37         List JavaDoc returnAsList = Arrays.asList(actualReturn);
38         boolean someMerged = allExtensions.removeAll(returnAsList);
39         assertTrue(someMerged);
40         assertTrue(allExtensions.size() == 0);
41     }
42
43     public void testGetBuilderClassNameForFile() {
44         File JavaDoc compositionFile = new File JavaDoc("test.xml");
45         String JavaDoc expected = ScriptBuilderResolver.DEFAULT_XML_BUILDER;
46         String JavaDoc actual = scriptBuilderResolver.getBuilderClassName(compositionFile);
47         assertEquals("return value", expected, actual);
48     }
49
50     public void testGetBuilderClassNameForResource() {
51         final String JavaDoc resourceName = "/org/nanocontainer/nanocontainer.xml";
52         URL JavaDoc compositionURL = this.getClass().getResource(resourceName);
53         if (compositionURL == null) {
54             fail("This test depended on resource '"+ resourceName + "' which appears to have been moved");
55         }
56         String JavaDoc expected = ScriptBuilderResolver.DEFAULT_XML_BUILDER;
57         String JavaDoc actual = scriptBuilderResolver.getBuilderClassName(compositionURL);
58         assertEquals("return value", expected, actual);
59     }
60
61     public void testGetBuilderClassNameForExtension() throws UnsupportedScriptTypeException {
62         String JavaDoc expectedReturn = ScriptBuilderResolver.DEFAULT_XML_BUILDER;
63         String JavaDoc actualReturn = scriptBuilderResolver.getBuilderClassName(".xml");
64         assertEquals("return value", expectedReturn, actualReturn);
65     }
66
67     public void testGetBuilderForExtensionThrowsExceptionForUnknownBuilderType() {
68         try {
69             scriptBuilderResolver.getBuilderClassName(".foo");
70             fail("Retrieving extension of type .foo should have thrown exception");
71         } catch (UnsupportedScriptTypeException ex) {
72             assertEquals(".foo",ex.getRequestedExtension());
73         }
74     }
75
76     public void testRegisterBuilder() {
77         scriptBuilderResolver.registerBuilder(".foo","org.example.FooBar");
78         assertEquals("org.example.FooBar", scriptBuilderResolver.getBuilderClassName(".foo"));
79     }
80
81     public void testResetBuilders() {
82         scriptBuilderResolver.registerBuilder(".foo","org.example.FooBar");
83         scriptBuilderResolver.resetBuilders();
84         try {
85             scriptBuilderResolver.getBuilderClassName(".foo");
86             fail("Retrieving extension of type .foo should have thrown exception");
87         } catch (UnsupportedScriptTypeException ex) {
88             assertEquals(".foo",ex.getRequestedExtension());
89         }
90     }
91
92 }
93
Popular Tags