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.config;
21  
22  import org.apache.myfaces.tobago.context.ThemeImpl;
23  import org.junit.jupiter.api.Assertions;
24  import org.junit.jupiter.api.Test;
25  import org.xml.sax.SAXException;
26  
27  import javax.xml.parsers.ParserConfigurationException;
28  import java.io.IOException;
29  import java.lang.reflect.Field;
30  import java.net.URISyntaxException;
31  import java.net.URL;
32  import java.util.ArrayList;
33  import java.util.HashSet;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.Set;
37  
38  public class TobagoConfigParserUnitTest {
39  
40    @Test
41    public void testParser() throws Exception {
42      generalTest("tobago-config-2.0.xml");
43      generalTest("tobago-config-4.0.xml");
44    }
45  
46    @Test
47    public void testParserUntidy() throws Exception {
48      generalTest("tobago-config-untidy-2.0.xml");
49    }
50  
51    private void generalTest(final String name)
52        throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
53      final URL url = getClass().getClassLoader().getResource(name);
54      final TobagoConfigParser parser = new TobagoConfigParser();
55  
56      final TobagoConfigFragment fragment = parser.parse(url);
57  
58      Assertions.assertEquals("my-name", fragment.getName());
59  
60      Assertions.assertEquals(1, fragment.getAfter().size());
61      Assertions.assertEquals("my-after", fragment.getAfter().get(0));
62  
63      Assertions.assertEquals(2, fragment.getBefore().size());
64      Assertions.assertEquals("my-before-1", fragment.getBefore().get(0));
65      Assertions.assertEquals("my-before-2", fragment.getBefore().get(1));
66  
67      Assertions.assertFalse(fragment.getCreateSessionSecret());
68      Assertions.assertFalse(fragment.getCheckSessionSecret());
69      Assertions.assertFalse(fragment.getPreventFrameAttacks());
70  
71      final Map<String, String> directiveMap = fragment.getContentSecurityPolicy().getDirectiveMap();
72      final Set<Map.Entry<String, String>> entries = directiveMap.entrySet();
73      Assertions.assertEquals(2, entries.size());
74      Assertions.assertEquals("'self'", directiveMap.get("default-src"));
75      Assertions.assertEquals("http://apache.org", directiveMap.get("child-src"));
76  
77      Assertions.assertEquals(2, fragment.getThemeDefinitions().size());
78      final ThemeImpl theme1 = fragment.getThemeDefinitions().get(0);
79      Assertions.assertEquals("my-theme-1", theme1.getName());
80      Assertions.assertEquals("My Theme 1", theme1.getDisplayName());
81      Assertions.assertEquals("script.js", theme1.getProductionResources().getScriptList().get(0).getName());
82      Assertions.assertEquals("style.css", theme1.getProductionResources().getStyleList().get(0).getName());
83  
84      final ThemeImpl theme2 = fragment.getThemeDefinitions().get(1);
85      Assertions.assertEquals("my-theme-2", theme2.getName());
86      Assertions.assertEquals("my-theme-1", theme2.getFallbackName());
87      Assertions.assertEquals(0, theme2.getDevelopmentResources().getScriptList().size());
88      Assertions.assertEquals(0, theme2.getDevelopmentResources().getStyleList().size());
89      Assertions.assertEquals(0, theme2.getProductionResources().getScriptList().size());
90      Assertions.assertEquals(0, theme2.getProductionResources().getStyleList().size());
91  
92      Assertions.assertFalse(fragment.getSetNosniffHeader(), "set-nosniff-header");
93    }
94  
95    @Test
96    public void testParserFor10() throws Exception {
97      final URL url = getClass().getClassLoader().getResource("tobago-config-1.0.30.xml");
98      final TobagoConfigParser parser = new TobagoConfigParser();
99      final TobagoConfigFragment fragment = parser.parse(url);
100     Assertions.assertEquals("speyside", fragment.getDefaultThemeName());
101   }
102 
103   @Test
104   public void testFailParserFor10() throws Exception {
105     final URL url = getClass().getClassLoader().getResource("tobago-config-fail-1.0.30.xml");
106     final TobagoConfigParser parser = new TobagoConfigParser();
107     try {
108       parser.parse(url);
109       Assertions.fail("No SAXParseException thrown!");
110     } catch (final SAXException e) {
111       // okay
112     }
113   }
114 
115   @Test
116   public void testParserFor15() throws Exception {
117     final URL url = getClass().getClassLoader().getResource("tobago-config-1.5.xml");
118     final TobagoConfigParser parser = new TobagoConfigParser();
119     final TobagoConfigFragment fragment = parser.parse(url);
120     Assertions.assertEquals("speyside", fragment.getDefaultThemeName());
121   }
122 
123   @Test
124   public void testFailParserFor15() throws Exception {
125     final URL url = getClass().getClassLoader().getResource("tobago-config-fail-1.5.xml");
126     final TobagoConfigParser parser = new TobagoConfigParser();
127     try {
128       parser.parse(url);
129       Assertions.fail("No SAXParseException thrown!");
130     } catch (final SAXException e) {
131       // okay
132     }
133   }
134 
135   @Test
136   public void testFailParserFor20() throws Exception {
137     final URL url = getClass().getClassLoader().getResource("tobago-config-fail-2.0.xml");
138     final TobagoConfigParser parser = new TobagoConfigParser();
139     try {
140       parser.parse(url);
141       Assertions.fail("No SAXParseException thrown!");
142     } catch (final SAXException e) {
143       // okay
144     }
145   }
146 
147   @Test
148   public void testFailParserForUnknownVersion() throws Exception {
149     final URL url = getClass().getClassLoader().getResource("tobago-config-fail-unknown-version.xml");
150     final TobagoConfigParser parser = new TobagoConfigParser();
151     try {
152       parser.parse(url);
153       Assertions.fail("No SAXParseException thrown!");
154     } catch (final SAXException e) {
155       // okay
156     }
157   }
158 
159   @Test
160   public void testUniqueness() throws IllegalAccessException {
161     final Field[] all = TobagoConfigParser.class.getFields();
162     final List<Field> fields = new ArrayList<>();
163     for (final Field field : all) {
164       if (field.getType().equals(Integer.TYPE)) {
165         fields.add(field);
166       }
167     }
168     // uniqueness
169     final TobagoConfigParser dummy = new TobagoConfigParser();
170     final Set<Integer> hashCodes = new HashSet<>();
171     for (final Field field : fields) {
172       hashCodes.add(field.getInt(dummy));
173     }
174     Assertions.assertEquals(fields.size(), hashCodes.size(), "All used hash codes must be unique");
175 
176     // check hash code values
177     for (final Field field : fields) {
178       final int hash = field.getInt(dummy);
179       final String name = field.getName().toLowerCase().replace('_', '-');
180       Assertions.assertEquals(name.hashCode(), hash, "Are the constants correct?");
181     }
182   }
183 
184 }