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