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