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.TobagoConfigParser;
25  import org.junit.Assert;
26  import org.junit.Test;
27  import org.xml.sax.SAXException;
28  
29  import javax.xml.parsers.ParserConfigurationException;
30  import java.io.IOException;
31  import java.net.URISyntaxException;
32  import java.net.URL;
33  import java.util.Enumeration;
34  
35  public class ThemeParserUnitTest {
36  
37    @Test
38    public void test() throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
39      final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
40      Enumeration<URL> urls = classLoader.getResources("theme-config.xml");
41  
42      final TobagoConfigParser parser = new TobagoConfigParser();
43      ThemeImpl theme = null;
44      if (urls.hasMoreElements()) {
45        final URL themeUrl = urls.nextElement();
46        theme = parser.parse(themeUrl).getThemeDefinitions().get(0);
47        Assert.assertEquals("test", theme.getName());
48        Assert.assertNotNull(theme.getResources());
49        Assert.assertNotNull(theme.getProductionResources());
50        final ThemeResources resources = theme.getResources();
51        final ThemeResources productionResources = theme.getProductionResources();
52  
53        Assert.assertEquals(1, resources.getScriptList().size());
54        Assert.assertEquals("script/tobago.js", resources.getScriptList().get(0).getName());
55  //      Assert.assertEquals("script/tobago-console.js", resources.getScriptList().get(1).getName());
56  
57        Assert.assertEquals(1, productionResources.getScriptList().size());
58      } else {
59        Assert.fail();
60      }
61  
62      urls = classLoader.getResources("theme-config2.xml");
63  
64      ThemeImpl theme2 = null;
65      if (urls.hasMoreElements()) {
66        final URL themeUrl = urls.nextElement();
67        theme2 = parser.parse(themeUrl).getThemeDefinitions().get(0);
68        Assert.assertEquals("test2", theme2.getName());
69        Assert.assertNotNull(theme2.getResources());
70        Assert.assertEquals(1, theme2.getResources().getScriptList().size());
71        Assert.assertEquals(1, theme2.getResources().getStyleList().size());
72      } else {
73        Assert.fail();
74      }
75  
76      urls = classLoader.getResources("theme-config3.xml");
77  
78      ThemeImpl theme3 = null;
79      if (urls.hasMoreElements()) {
80        final URL themeUrl = urls.nextElement();
81        theme3 = parser.parse(themeUrl).getThemeDefinitions().get(0);
82        Assert.assertEquals("test3", theme3.getName());
83        Assert.assertEquals(0, theme3.getResources().getScriptList().size());
84        Assert.assertEquals(0, theme3.getResources().getStyleList().size());
85      } else {
86        Assert.fail();
87      }
88  
89      urls = classLoader.getResources("theme-config4.xml");
90  
91      ThemeImpl theme4 = null;
92      if (urls.hasMoreElements()) {
93        final URL themeUrl = urls.nextElement();
94        theme4 = parser.parse(themeUrl).getThemeDefinitions().get(0);
95        Assert.assertEquals("test4", theme4.getName());
96        Assert.assertEquals(0, theme4.getResources().getScriptList().size());
97        Assert.assertEquals(0, theme4.getResources().getStyleList().size());
98      } else {
99        Assert.fail();
100     }
101   }
102 }