This project has retired. For details please refer to its Attic page.
DefaultConfigurationTest xref
View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *  http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.any23.configuration;
19  
20  import org.junit.After;
21  import org.junit.Assert;
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  /**
26   * Test case for {@link Configuration} class.
27   *
28   * @author Michele Mostarda (michele.mostarda@gmail.com)
29   */
30  public class DefaultConfigurationTest {
31  
32      private Configuration configuration;
33  
34      @Before
35      public void setUp() {
36          configuration = DefaultConfiguration.singleton();
37      }
38  
39      @After
40      public void tearDown() {
41          configuration = null;
42      }
43  
44      @Test
45      public void testSingletonAccessor() {
46          final Configuration s1 = DefaultConfiguration.singleton();
47          final Configuration s2 = DefaultConfiguration.singleton();
48          Assert.assertTrue("Invalid singleton condition.", s1 == s2);
49      }
50  
51      @Test
52      public void testCopyAccessor() {
53          final ModifiableConfiguration c1 = DefaultConfiguration.copy();
54          final ModifiableConfiguration c2 = DefaultConfiguration.copy();
55          Assert.assertTrue("Invalid copy condition.", c1 != c2);
56      }
57  
58      @Test
59      public void testCopyIsolation() {
60          final String TARGET_PROPERTY = "any23.http.client.max.connections";
61          final ModifiableConfiguration copy = DefaultConfiguration.copy();
62          copy.setProperty(TARGET_PROPERTY, "100");
63          Assert.assertEquals(DefaultConfiguration.singleton().getPropertyIntOrFail(TARGET_PROPERTY), 5);
64          Assert.assertEquals(copy.getPropertyIntOrFail(TARGET_PROPERTY), 100);
65      }
66  
67      @Test
68      public void testModifiableConfigurationSuccess() {
69          final String TARGET_PROPERTY = "any23.extraction.metadata.nesting";
70          final ModifiableConfiguration modifiable = DefaultConfiguration.copy();
71          final String oldValue = modifiable.setProperty(TARGET_PROPERTY, "off");
72          Assert.assertEquals("on", oldValue);
73          Assert.assertEquals("off", modifiable.getPropertyOrFail(TARGET_PROPERTY));
74      }
75  
76      @Test(expected = IllegalArgumentException.class)
77      public void testModifiableConfigurationFail() {
78          final ModifiableConfiguration modifiable = DefaultConfiguration.copy();
79          modifiable.setProperty("fake.property", "fake.value");
80      }
81  
82      @Test
83      public void testGetProperties() {
84          final String[] properties = configuration.getProperties();
85          Assert.assertTrue(properties.length > 6);
86          for (String property : properties) {
87              Assert.assertTrue(property.startsWith("any23."));
88          }
89      }
90  
91      @Test
92      public void testDefineProperty() {
93          Assert.assertTrue(configuration.defineProperty("any23.core.version"));
94          Assert.assertFalse(configuration.defineProperty("any23.fake"));
95      }
96  
97      @Test
98      public void testGetProperty() {
99          final String value1 = configuration.getProperty("any23.rdfa.extractor.xslt", null);
100         Assert.assertEquals("rdfa.xslt", value1);
101         final String value2 = configuration.getProperty("any23.fake", "fake.default");
102         Assert.assertEquals("fake.default", value2);
103     }
104 
105     @Test
106     public void testGetPropertySysOverride() {
107         System.setProperty("any23.rdfa.extractor.xslt", "fake.rdfa.xslt");
108         try {
109             final String value = configuration.getPropertyOrFail("any23.rdfa.extractor.xslt");
110             Assert.assertEquals("fake.rdfa.xslt", value);
111         } finally {
112             System.clearProperty("any23.rdfa.extractor.xslt");
113         }
114     }
115 
116     @Test
117     public void testGetPropertyOrFailOk() {
118         final String value = configuration.getPropertyOrFail("any23.rdfa.extractor.xslt");
119         Assert.assertEquals("rdfa.xslt", value);
120     }
121 
122     @Test(expected = IllegalArgumentException.class)
123     public void testGetPropertyOrFailNok() {
124         configuration.getPropertyOrFail("any23.fake");
125     }
126 
127     @Test
128     public void testGetPropertyIntOrFailOk() {
129         final int value = configuration.getPropertyIntOrFail("any23.http.client.timeout");
130         Assert.assertEquals(10000, value);
131     }
132 
133     @Test(expected = NumberFormatException.class)
134     public void testGetPropertyIntOrFailNok() {
135         configuration.getPropertyIntOrFail("any23.http.user.agent.default");
136     }
137 
138     @Test
139     public void testGetFlagProperty() {
140         Assert.assertTrue(configuration.getFlagProperty("any23.extraction.metadata.nesting"));
141     }
142 
143 }