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