1 package org.nanocontainer.script; 2 3 import java.io.File ; 4 import java.net.URL ; 5 import java.util.Arrays ; 6 import java.util.List ; 7 import java.util.Set ; 8 import java.util.TreeSet ; 9 import junit.framework.TestCase; 10 11 15 public class ScriptBuilderResolverTestCase extends TestCase { 16 private ScriptBuilderResolver scriptBuilderResolver = null; 17 18 protected void setUp() throws Exception { 19 super.setUp(); 20 scriptBuilderResolver = new ScriptBuilderResolver(); 21 } 22 23 protected void tearDown() throws Exception { 24 scriptBuilderResolver = null; 25 super.tearDown(); 26 } 27 28 29 public void testGetAllSupportedExtensions() { 30 Set allExtensions = new TreeSet (); 31 32 allExtensions.add(ScriptBuilderResolver.XML); 33 34 String [] actualReturn = scriptBuilderResolver.getAllSupportedExtensions(); 35 assertNotNull(actualReturn); 36 37 List 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 compositionFile = new File ("test.xml"); 45 String expected = ScriptBuilderResolver.DEFAULT_XML_BUILDER; 46 String actual = scriptBuilderResolver.getBuilderClassName(compositionFile); 47 assertEquals("return value", expected, actual); 48 } 49 50 public void testGetBuilderClassNameForResource() { 51 final String resourceName = "/org/nanocontainer/nanocontainer.xml"; 52 URL 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 expected = ScriptBuilderResolver.DEFAULT_XML_BUILDER; 57 String actual = scriptBuilderResolver.getBuilderClassName(compositionURL); 58 assertEquals("return value", expected, actual); 59 } 60 61 public void testGetBuilderClassNameForExtension() throws UnsupportedScriptTypeException { 62 String expectedReturn = ScriptBuilderResolver.DEFAULT_XML_BUILDER; 63 String 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 |