    001    package org.nanocontainer.script;
    002
    003    import java.io.File;
    004    import java.net.URL;
    005    import java.util.Arrays;
    006    import java.util.List;
    007    import java.util.Set;
    008    import java.util.TreeSet;
    009    import junit.framework.TestCase;
    010
    011    /**
    012     * Name/Builder Resolution Test Cases.
    013     * @author Michael Rimov
    014     */
    015    public class ScriptBuilderResolverTestCase extends TestCase {
    016        private ScriptBuilderResolver scriptBuilderResolver = null;
    017
    018        protected void setUp() throws Exception {
    019            super.setUp();
    020            scriptBuilderResolver = new ScriptBuilderResolver();
    021        }
    022
    023        protected void tearDown() throws Exception {
    024            scriptBuilderResolver = null;
    025            super.tearDown();
    026        }
    027
    028
    029        public void testGetAllSupportedExtensions() {
    030            Set allExtensions = new TreeSet();
    031
    032            allExtensions.add(ScriptBuilderResolver.XML);
    033
    034            String[] actualReturn = scriptBuilderResolver.getAllSupportedExtensions();
    035            assertNotNull(actualReturn);
    036
    037            List returnAsList = Arrays.asList(actualReturn);
    038            boolean someMerged = allExtensions.removeAll(returnAsList);
    039            assertTrue(someMerged);
    040            assertTrue(allExtensions.size() == 0);
    041        }
    042
    043        public void testGetBuilderClassNameForFile() {
    044            File compositionFile = new File("test.xml");
    045            String expected = ScriptBuilderResolver.DEFAULT_XML_BUILDER;
    046            String actual = scriptBuilderResolver.getBuilderClassName(compositionFile);
    047            assertEquals("return value", expected, actual);
    048        }
    049
    050        public void testGetBuilderClassNameForResource() {
    051            final String resourceName = "/org/nanocontainer/nanocontainer.xml";
    052            URL compositionURL = this.getClass().getResource(resourceName);
    053            if (compositionURL == null) {
    054                fail("This test depended on resource '"+ resourceName + "' which appears to have been moved");
    055            }
    056            String expected = ScriptBuilderResolver.DEFAULT_XML_BUILDER;
    057            String actual = scriptBuilderResolver.getBuilderClassName(compositionURL);
    058            assertEquals("return value", expected, actual);
    059        }
    060
    061        public void testGetBuilderClassNameForExtension() throws UnsupportedScriptTypeException {
    062            String expectedReturn = ScriptBuilderResolver.DEFAULT_XML_BUILDER;
    063            String actualReturn = scriptBuilderResolver.getBuilderClassName(".xml");
    064            assertEquals("return value", expectedReturn, actualReturn);
    065        }
    066
    067        public void testGetBuilderForExtensionThrowsExceptionForUnknownBuilderType() {
    068            try {
    069                scriptBuilderResolver.getBuilderClassName(".foo");
    070                fail("Retrieving extension of type .foo should have thrown exception");
    071            } catch (UnsupportedScriptTypeException ex) {
    072                assertEquals(".foo",ex.getRequestedExtension());
    073            }
    074        }
    075
    076        public void testRegisterBuilder() {
    077            scriptBuilderResolver.registerBuilder(".foo","org.example.FooBar");
    078            assertEquals("org.example.FooBar", scriptBuilderResolver.getBuilderClassName(".foo"));
    079        }
    080
    081        public void testResetBuilders() {
    082            scriptBuilderResolver.registerBuilder(".foo","org.example.FooBar");
    083            scriptBuilderResolver.resetBuilders();
    084            try {
    085                scriptBuilderResolver.getBuilderClassName(".foo");
    086                fail("Retrieving extension of type .foo should have thrown exception");
    087            } catch (UnsupportedScriptTypeException ex) {
    088                assertEquals(".foo",ex.getRequestedExtension());
    089            }
    090        }
    091
    092    }