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.jupiter.api.Assertions;
26  import org.junit.jupiter.api.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      if (urls.hasMoreElements()) {
44        final URL themeUrl = urls.nextElement();
45        final ThemeImpl theme = parser.parse(themeUrl).getThemeDefinitions().get(0);
46        Assertions.assertEquals("test", theme.getName());
47        Assertions.assertNotNull(theme.getDevelopmentResources());
48        Assertions.assertNotNull(theme.getProductionResources());
49        final ThemeResources resources = theme.getDevelopmentResources();
50        final ThemeResources productionResources = theme.getProductionResources();
51  
52        Assertions.assertEquals(1, resources.getScriptList().size());
53        Assertions.assertEquals("script/tobago.js", resources.getScriptList().get(0).getName());
54  //      Assertions.assertEquals("script/tobago-console.js", resources.getScriptList().get(1).getName());
55  
56        Assertions.assertEquals(1, productionResources.getScriptList().size());
57      } else {
58        Assertions.fail();
59      }
60  
61      urls = classLoader.getResources("theme-config2.xml");
62  
63      if (urls.hasMoreElements()) {
64        final URL themeUrl = urls.nextElement();
65        final ThemeImpl theme2 = parser.parse(themeUrl).getThemeDefinitions().get(0);
66        Assertions.assertEquals("test2", theme2.getName());
67        Assertions.assertNotNull(theme2.getDevelopmentResources());
68        Assertions.assertEquals(1, theme2.getDevelopmentResources().getScriptList().size());
69        Assertions.assertEquals(1, theme2.getDevelopmentResources().getStyleList().size());
70      } else {
71        Assertions.fail();
72      }
73  
74      urls = classLoader.getResources("theme-config3.xml");
75  
76      if (urls.hasMoreElements()) {
77        final URL themeUrl = urls.nextElement();
78        final ThemeImpl theme3 = parser.parse(themeUrl).getThemeDefinitions().get(0);
79        Assertions.assertEquals("test3", theme3.getName());
80        Assertions.assertEquals(0, theme3.getDevelopmentResources().getScriptList().size());
81        Assertions.assertEquals(0, theme3.getDevelopmentResources().getStyleList().size());
82      } else {
83        Assertions.fail();
84      }
85  
86      urls = classLoader.getResources("theme-config4.xml");
87  
88      if (urls.hasMoreElements()) {
89        final URL themeUrl = urls.nextElement();
90        final ThemeImpl theme4 = parser.parse(themeUrl).getThemeDefinitions().get(0);
91        Assertions.assertEquals("test4", theme4.getName());
92        Assertions.assertEquals(0, theme4.getDevelopmentResources().getScriptList().size());
93        Assertions.assertEquals(0, theme4.getDevelopmentResources().getStyleList().size());
94      } else {
95        Assertions.fail();
96      }
97    }
98  }