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