Package org.apache.tiles.definition

Examples of org.apache.tiles.definition.DefinitionsFactory


     * Tests reloading definitions impl.
     *
     * @throws Exception If something goes wrong.
     */
    public void testReloadableDefinitionsFactory() throws Exception {
        DefinitionsFactory factory = new UrlDefinitionsFactory();

        // Set up multiple data sources.
        URL url = this.getClass().getClassLoader().getResource(
                "org/apache/tiles/config/temp-defs.xml");

        URI uri = null;
        String urlPath = null;

        // The following madness is necessary b/c of the way Windows hanndles URLs.
        // We must add a slash to the protocol if Windows does not.  But we cannot
        // add a slash to Unix paths b/c they already have one.
        if (url.getPath().startsWith("/")) {
            urlPath = "file:" + url.getPath();
        } else {
            urlPath = "file:/" + url.getPath();
        }

        // The following second madness is necessary b/c sometimes spaces
        // are encoded as '%20', sometimes they are not. For example in
        // Windows 2000 under Eclipse they are encoded, under the prompt of
        // Windows 2000 they are not.
        // It seems to be in the different behaviour of
        // sun.misc.Launcher$AppClassLoader (called under Eclipse) and
        // java.net.URLClassLoader (under maven).
        // And an URL accepts spaces while URIs need '%20'.
        try {
            uri = new URI(urlPath);
        } catch (URISyntaxException e) {
            uri = new URI(urlPath.replaceAll(" ", "%20"));
        }

        String xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n" +
                "<!DOCTYPE tiles-definitions PUBLIC " +
                "\"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN\" " +
                "\"http://tiles.apache.org/dtds/tiles-config_2_0.dtd\">\n\n" +
                "<tiles-definitions>" +
                "<definition name=\"rewrite.test\" template=\"/test.jsp\">" +
                "<put-attribute name=\"testparm\" value=\"testval\"/>" +
                "</definition>" +
                "</tiles-definitions>";

        File file = new File(uri);
        FileOutputStream fileOut = new FileOutputStream(file);
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(fileOut));
        writer.write(xml);
        writer.close();

        factory.init(new HashMap<String, String>());
        factory.addSource(url);

        // Parse files.
        Definitions definitions = factory.readDefinitions();

        assertNotNull("rewrite.test definition not found.",
                definitions.getDefinition("rewrite.test"));
        assertEquals("Incorrect initial template value", "/test.jsp",
                definitions.getDefinition("rewrite.test").getTemplate());

        ReloadableDefinitionsFactory reloadable = (ReloadableDefinitionsFactory) factory;
        assertEquals("Factory should be fresh.", false,
                reloadable.refreshRequired());

        // Make sure the system actually updates the timestamp.
        Thread.sleep(30000);

        // Set up multiple data sources.
        xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n" +
                "<!DOCTYPE tiles-definitions PUBLIC " +
                "\"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN\" " +
                "\"http://tiles.apache.org/dtds/tiles-config_2_0.dtd\">\n\n" +
                "<tiles-definitions>" +
                "<definition name=\"rewrite.test\" template=\"/newtest.jsp\">" +
                "<put-attribute name=\"testparm\" value=\"testval\"/>" +
                "</definition>" +
                "</tiles-definitions>";

        file = new File(uri);
        fileOut = new FileOutputStream(file);
        writer = new BufferedWriter(new OutputStreamWriter(fileOut));
        writer.write(xml);
        writer.close();


        assertEquals("Factory should be stale.", true,
                reloadable.refreshRequired());
        definitions = factory.readDefinitions();
        assertNotNull("rewrite.test definition not found.",
                definitions.getDefinition("rewrite.test"));
        assertEquals("Incorrect initial template value", "/newtest.jsp",
                definitions.getDefinition("rewrite.test").getTemplate());
    }
View Full Code Here


     *
     * @throws Exception If something goes wrong.
     */
    @SuppressWarnings("unchecked")
    public void testReadDefinitions() throws Exception {
        DefinitionsFactory factory = new UrlDefinitionsFactory();

        // Set up multiple data sources.
        URL url1 = this.getClass().getClassLoader().getResource(
                "org/apache/tiles/config/defs1.xml");
        assertNotNull("Could not load defs1 file.", url1);
        URL url2 = this.getClass().getClassLoader().getResource(
                "org/apache/tiles/config/defs2.xml");
        assertNotNull("Could not load defs2 file.", url2);
        URL url3 = this.getClass().getClassLoader().getResource(
                "org/apache/tiles/config/defs3.xml");
        assertNotNull("Could not load defs3 file.", url3);

        factory.init(Collections.EMPTY_MAP);
        factory.addSource(url1);
        factory.addSource(url2);
        factory.addSource(url3);

        // Parse files.
        Definitions definitions = factory.readDefinitions();

        assertNotNull("test.def1 definition not found.", definitions.getDefinition("test.def1"));
        assertNotNull("test.def2 definition not found.", definitions.getDefinition("test.def2"));
        assertNotNull("test.def3 definition not found.", definitions.getDefinition("test.def3"));
    }
View Full Code Here

     *
     * @throws Exception If something goes wrong.
     */
    @SuppressWarnings("unchecked")
    public void testGetDefinition() throws Exception {
        DefinitionsFactory factory = new UrlDefinitionsFactory();

        // Set up multiple data sources.
        URL url1 = this.getClass().getClassLoader().getResource(
                "org/apache/tiles/config/defs1.xml");
        assertNotNull("Could not load defs1 file.", url1);
        URL url2 = this.getClass().getClassLoader().getResource(
                "org/apache/tiles/config/defs2.xml");
        assertNotNull("Could not load defs2 file.", url2);
        URL url3 = this.getClass().getClassLoader().getResource(
                "org/apache/tiles/config/defs3.xml");
        assertNotNull("Could not load defs3 file.", url3);

        factory.init(Collections.EMPTY_MAP);
        factory.addSource(url1);
        factory.addSource(url2);
        factory.addSource(url3);

        // Parse files.
        factory.readDefinitions();
       
        TilesRequestContext emptyContext = new MockOnlyLocaleTilesContext(null);
        TilesRequestContext usContext = new MockOnlyLocaleTilesContext(Locale.US);
        TilesRequestContext frenchContext = new MockOnlyLocaleTilesContext(Locale.FRENCH);
        TilesRequestContext chinaContext = new MockOnlyLocaleTilesContext(Locale.CHINA);
        TilesRequestContext canadaFrenchContext = new MockOnlyLocaleTilesContext(Locale.CANADA_FRENCH);

        assertNotNull("test.def1 definition not found.", factory.getDefinition("test.def1", emptyContext));
        assertNotNull("test.def2 definition not found.", factory.getDefinition("test.def2", emptyContext));
        assertNotNull("test.def3 definition not found.", factory.getDefinition("test.def3", emptyContext));
        assertNotNull("test.common definition not found.", factory.getDefinition("test.common", emptyContext));
        assertNotNull("test.common definition in US locale not found.", factory.getDefinition("test.common", usContext));
        assertNotNull("test.common definition in FRENCH locale not found.", factory.getDefinition("test.common", frenchContext));
        assertNotNull("test.common definition in CHINA locale not found.", factory.getDefinition("test.common", chinaContext));
        assertNotNull("test.common.french definition in FRENCH locale not found.", factory.getDefinition("test.common.french", frenchContext));
        assertNotNull("test.common.french definition in CANADA_FRENCH locale not found.", factory.getDefinition("test.common.french", canadaFrenchContext));
        assertNotNull("test.def.toextend definition not found.", factory.getDefinition("test.def.toextend", emptyContext));
        assertNotNull("test.def.overridden definition not found.", factory.getDefinition("test.def.overridden", emptyContext));
        assertNotNull("test.def.overridden definition in FRENCH locale not found.", factory.getDefinition("test.def.overridden", frenchContext));

        assertEquals("Incorrect default country value", "default",
                factory.getDefinition("test.def1", emptyContext).getAttribute("country"));
        assertEquals("Incorrect US country value", "US",
                factory.getDefinition("test.def1", usContext).getAttribute("country"));
        assertEquals("Incorrect France country value", "France",
                factory.getDefinition("test.def1", frenchContext).getAttribute("country"));
        assertEquals("Incorrect Chinese country value (should be default)", "default",
                factory.getDefinition("test.def1", chinaContext).getAttribute("country"));
        assertEquals("Incorrect default country value", "default",
                factory.getDefinition("test.def.overridden", emptyContext).getAttribute("country"));
        assertEquals("Incorrect default title value", "Definition to be overridden",
                factory.getDefinition("test.def.overridden", emptyContext).getAttribute("title"));
        assertEquals("Incorrect France country value", "France",
                factory.getDefinition("test.def.overridden", frenchContext).getAttribute("country"));
        assertEquals("Incorrect France title value", "Definition to be extended",
                factory.getDefinition("test.def.overridden", frenchContext).getAttribute("title"));
    }
View Full Code Here

     * @throws Exception If something goes wrong.
     */
    @SuppressWarnings("unchecked")
    public void testBadSourceType() throws Exception {
        try {
            DefinitionsFactory factory = new UrlDefinitionsFactory();

            factory.init(Collections.EMPTY_MAP);
            factory.addSource("Bad object.");

            fail("Should've thrown exception.");
        } catch (DefinitionsFactoryException e) {
            // success.
        }
View Full Code Here

        params.put(DefinitionsFactory.READER_IMPL_PROPERTY,
                "org.apache.tiles.definition.MockDefinitionsReader");

        int instanceCount = MockDefinitionsReader.getInstanceCount();

        DefinitionsFactory factory = new UrlDefinitionsFactory();

        // Set up multiple data sources.
        URL url1 = this.getClass().getClassLoader().getResource(
                "org/apache/tiles/config/defs1.xml");
        assertNotNull("Could not load defs1 file.", url1);

        factory.init(params);
        factory.addSource(url1);

        assertEquals("MockDefinitionsReader not used.",
                instanceCount + 1,
                MockDefinitionsReader.getInstanceCount());
    }
View Full Code Here

                                              BasicTilesContainer container) throws TilesException {
        TilesContextFactory contextFactory =
            (TilesContextFactory) createFactory(configuration,
                CONTEXT_FACTORY_INIT_PARAM);

        DefinitionsFactory defsFactory =
            (DefinitionsFactory) createFactory(configuration,
                DEFINITIONS_FACTORY_INIT_PARAM);

        PreparerFactory prepFactory =
            (PreparerFactory) createFactory(configuration,
View Full Code Here

     * @return the definitions factory used by this container. If the key is not
     * valid, the default factory will be returned.
     * @param key The key of the needed definitions factory.
     */
    public DefinitionsFactory getDefinitionsFactory(String key) {
      DefinitionsFactory retValue = null;
     
      if (key != null) {
        retValue = key2definitionsFactory.get(key);
      }
      if (retValue == null) {
View Full Code Here

     * @return the definitions factory used by this container. If the key is not
     * valid, <code>null</code> will be returned.
     * @param key The key of the needed definitions factory.
     */
    public DefinitionsFactory getProperDefinitionsFactory(String key) {
        DefinitionsFactory retValue = null;
       
        if (key != null) {
            retValue = key2definitionsFactory.get(key);
        }
       
View Full Code Here

    protected Definition getDefinition(String definitionName,
            TilesRequestContext request) throws DefinitionsFactoryException {
        Definition retValue = null;
        String key = getDefinitionsFactoryKey(request);
        if (key != null) {
            DefinitionsFactory definitionsFactory =
                key2definitionsFactory.get(key);
            if (definitionsFactory != null) {
                retValue = definitionsFactory.getDefinition(definitionName,
                        request);
            }
        }
        if (retValue == null) {
            retValue = super.getDefinition(definitionName, request);
View Full Code Here

    public static void reloadDefinitionsFactory(Object context)
            throws DefinitionsFactoryException {
        TilesContainer container = TilesAccess.getContainer(context);
        if (container instanceof BasicTilesContainer) {
            BasicTilesContainer basic = (BasicTilesContainer) container;
            DefinitionsFactory factory = basic.getDefinitionsFactory();
            if (factory instanceof ReloadableDefinitionsFactory) {
                ReloadableDefinitionsFactory rFactory = (ReloadableDefinitionsFactory) factory;
                if (rFactory.refreshRequired()) {
                    rFactory.refresh();
                }
View Full Code Here

TOP

Related Classes of org.apache.tiles.definition.DefinitionsFactory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.