View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.context;
21  
22  import org.apache.myfaces.tobago.context.ThemeImpl;
23  import org.apache.myfaces.tobago.context.ThemeResources;
24  import org.apache.myfaces.tobago.internal.config.ThemeBuilder;
25  import org.apache.myfaces.tobago.internal.config.TobagoConfigParser;
26  import org.apache.myfaces.tobago.internal.config.TobagoConfigTestUtils;
27  import org.junit.Assert;
28  import org.junit.Test;
29  import org.xml.sax.SAXException;
30  
31  import javax.xml.parsers.ParserConfigurationException;
32  import java.io.IOException;
33  import java.net.URISyntaxException;
34  import java.net.URL;
35  import java.util.Enumeration;
36  
37  public class ThemeParserUnitTest {
38  
39    @Test
40    public void test() throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
41      final ThemeBuilder themeBuilder = TobagoConfigTestUtils.createThemeBuilder();
42      final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
43      Enumeration<URL> urls = classLoader.getResources("theme-config.xml");
44  
45      final TobagoConfigParser parser = new TobagoConfigParser();
46      ThemeImpl theme = null;
47      if  (urls.hasMoreElements()) {
48        final URL themeUrl = urls.nextElement();
49        theme = parser.parse(themeUrl).getThemeDefinitions().get(0);
50        Assert.assertEquals("test", theme.getName());
51        Assert.assertNotNull(theme.getResources());
52        Assert.assertNotNull(theme.getProductionResources());
53        final ThemeResources resources = theme.getResources();
54        final ThemeResources productionResources = theme.getProductionResources();
55  
56        Assert.assertEquals(3, resources.getScriptList().size());
57        Assert.assertEquals("script/tobago.js", resources.getScriptList().get(0).getName());
58        Assert.assertEquals("script/tobago-logging.js", resources.getScriptList().get(1).getName());
59        Assert.assertEquals("script/tobago-console.js", resources.getScriptList().get(2).getName());
60  
61        Assert.assertEquals(1, productionResources.getScriptList().size());
62        themeBuilder.addTheme(theme);
63      } else {
64        Assert.fail();
65      }
66  
67      urls = classLoader.getResources("theme-config2.xml");
68  
69      ThemeImpl theme2 = null;
70      if (urls.hasMoreElements()) {
71        final URL themeUrl = urls.nextElement();
72        theme2 = parser.parse(themeUrl).getThemeDefinitions().get(0);
73        Assert.assertEquals("test2", theme2.getName());
74        Assert.assertNotNull(theme2.getResources());
75        Assert.assertEquals(1, theme2.getResources().getScriptList().size());
76        Assert.assertEquals(1, theme2.getResources().getStyleList().size());
77        themeBuilder.addTheme(theme2);
78      } else {
79        Assert.fail();
80      }
81  
82      urls = classLoader.getResources("theme-config3.xml");
83  
84      ThemeImpl theme3 = null;
85      if (urls.hasMoreElements()) {
86        final URL themeUrl = urls.nextElement();
87        theme3 = parser.parse(themeUrl).getThemeDefinitions().get(0);
88        Assert.assertEquals("test3", theme3.getName());
89        Assert.assertEquals(0, theme3.getResources().getScriptList().size());
90        Assert.assertEquals(0, theme3.getResources().getStyleList().size());
91        themeBuilder.addTheme(theme3);
92      } else {
93        Assert.fail();
94      }
95  
96      urls = classLoader.getResources("theme-config4.xml");
97  
98      ThemeImpl theme4 = null;
99      if (urls.hasMoreElements()) {
100       final URL themeUrl = urls.nextElement();
101       theme4 = parser.parse(themeUrl).getThemeDefinitions().get(0);
102       Assert.assertEquals("test4", theme4.getName());
103       Assert.assertEquals(0, theme4.getResources().getScriptList().size());
104       Assert.assertEquals(0, theme4.getResources().getStyleList().size());
105       themeBuilder.addTheme(theme4);
106     } else {
107       Assert.fail();
108     }
109 
110     themeBuilder.resolveThemes();
111     Assert.assertEquals(3, theme.getResources().getScriptList().size());
112     Assert.assertEquals("script/tobago.js", theme.getResources().getScriptList().get(0).getName());
113     Assert.assertEquals("script/tobago-logging.js", theme.getResources().getScriptList().get(1).getName());
114     Assert.assertEquals("script/tobago-console.js", theme.getResources().getScriptList().get(2).getName());
115 
116     Assert.assertNotNull(theme2.getResources());
117     Assert.assertEquals(4, theme2.getResources().getScriptList().size());
118     Assert.assertEquals(1, theme2.getResources().getStyleList().size());
119     Assert.assertEquals("script/tobago.js", theme2.getResources().getScriptList().get(0).getName());
120     Assert.assertEquals("script/tobago-logging.js", theme2.getResources().getScriptList().get(1).getName());
121     Assert.assertEquals("script/tobago-console.js", theme2.getResources().getScriptList().get(2).getName());
122     Assert.assertEquals("script/test.js", theme2.getResources().getScriptList().get(3).getName());
123 
124     Assert.assertEquals(4, theme3.getResources().getScriptList().size());
125     Assert.assertEquals("script/tobago.js", theme3.getResources().getScriptList().get(0).getName());
126     Assert.assertEquals("script/tobago-logging.js", theme3.getResources().getScriptList().get(1).getName());
127     Assert.assertEquals("script/tobago-console.js", theme3.getResources().getScriptList().get(2).getName());
128     Assert.assertEquals("script/test.js", theme3.getResources().getScriptList().get(3).getName());
129 
130     Assert.assertEquals(4, theme4.getResources().getScriptList().size());
131     Assert.assertEquals("script/tobago.js", theme4.getResources().getScriptList().get(0).getName());
132     Assert.assertEquals("script/tobago-logging.js", theme4.getResources().getScriptList().get(1).getName());
133     Assert.assertEquals("script/tobago-console.js", theme4.getResources().getScriptList().get(2).getName());
134     Assert.assertEquals("script/test.js", theme4.getResources().getScriptList().get(3).getName());
135   }
136 }