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.ThemeParser;
25  import org.apache.myfaces.tobago.internal.config.TobagoConfigImpl;
26  import org.junit.Assert;
27  import org.junit.Test;
28  import org.xml.sax.SAXException;
29  
30  import java.io.IOException;
31  import java.net.URL;
32  import java.util.Enumeration;
33  
34  public class ThemeParserTest {
35  
36    @Test
37    public void test() throws IOException, SAXException {
38      TobagoConfigImpl config = new TobagoConfigImpl();
39      ThemeBuilder themeBuilder = new ThemeBuilder(config);
40      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
41      Enumeration<URL> urls = classLoader.getResources("theme-config.xml");
42  
43      ThemeParser parser = new ThemeParser();
44      ThemeImpl theme = null;
45      if  (urls.hasMoreElements()) {
46        URL themeUrl = urls.nextElement();
47        theme = parser.parse(themeUrl);
48        Assert.assertEquals("test", theme.getName());
49        Assert.assertNotNull(theme.getResources());
50        Assert.assertNotNull(theme.getProductionResources());
51        ThemeResources resources = theme.getResources();
52        ThemeResources productionResources = theme.getProductionResources();
53  
54        Assert.assertEquals(2, resources.getScriptList().size());
55        Assert.assertEquals("script/tobago.js", resources.getScriptList().get(0).getName());
56        Assert.assertEquals("script/tobago-logging.js", resources.getScriptList().get(1).getName());
57  
58        Assert.assertEquals(1, productionResources.getScriptList().size());
59        themeBuilder.addTheme(theme);
60      } else {
61        Assert.fail();
62      }
63  
64      urls = classLoader.getResources("theme-config2.xml");
65  
66      ThemeImpl theme2 = null;
67      if (urls.hasMoreElements()) {
68        URL themeUrl = urls.nextElement();
69        theme2 = parser.parse(themeUrl);
70        Assert.assertEquals("test2", theme2.getName());
71        Assert.assertNotNull(theme2.getResources());
72        Assert.assertEquals(1, theme2.getResources().getScriptList().size());
73        Assert.assertEquals(1, theme2.getResources().getStyleList().size());
74        themeBuilder.addTheme(theme2);
75      } else {
76        Assert.fail();
77      }
78  
79      urls = classLoader.getResources("theme-config3.xml");
80  
81      ThemeImpl theme3 = null;
82      if (urls.hasMoreElements()) {
83        URL themeUrl = urls.nextElement();
84        theme3 = parser.parse(themeUrl);
85        Assert.assertEquals("test3", theme3.getName());
86        Assert.assertNull(theme3.getResources());
87        themeBuilder.addTheme(theme3);
88      } else {
89        Assert.fail();
90      }
91  
92      urls = classLoader.getResources("theme-config4.xml");
93  
94      ThemeImpl theme4 = null;
95      if (urls.hasMoreElements()) {
96        URL themeUrl = urls.nextElement();
97        theme4 = parser.parse(themeUrl);
98        Assert.assertEquals("test4", theme4.getName());
99        Assert.assertNull(theme4.getResources());
100       themeBuilder.addTheme(theme4);
101     } else {
102       Assert.fail();
103     }
104 
105     themeBuilder.resolveThemes();
106     Assert.assertEquals(2, theme.getResources().getScriptList().size());
107     Assert.assertEquals("script/tobago.js", theme.getResources().getScriptList().get(0).getName());
108     Assert.assertEquals("script/tobago-logging.js", theme.getResources().getScriptList().get(1).getName());
109 
110     Assert.assertNotNull(theme2.getResources());
111     Assert.assertEquals(3, theme2.getResources().getScriptList().size());
112     Assert.assertEquals(1, theme2.getResources().getStyleList().size());
113     Assert.assertEquals("script/tobago.js", theme2.getResources().getScriptList().get(0).getName());
114     Assert.assertEquals("script/tobago-logging.js", theme2.getResources().getScriptList().get(1).getName());
115     Assert.assertEquals("script/test.js", theme2.getResources().getScriptList().get(2).getName());
116 
117     Assert.assertEquals(3, theme3.getResources().getScriptList().size());
118     Assert.assertEquals("script/tobago.js", theme3.getResources().getScriptList().get(0).getName());
119     Assert.assertEquals("script/tobago-logging.js", theme3.getResources().getScriptList().get(1).getName());
120     Assert.assertEquals("script/test.js", theme3.getResources().getScriptList().get(2).getName());
121 
122     Assert.assertEquals(3, theme4.getResources().getScriptList().size());
123     Assert.assertEquals("script/tobago.js", theme4.getResources().getScriptList().get(0).getName());
124     Assert.assertEquals("script/tobago-logging.js", theme4.getResources().getScriptList().get(1).getName());
125     Assert.assertEquals("script/test.js", theme4.getResources().getScriptList().get(2).getName());
126 
127 
128   }
129 }