View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package test.de.smartics.properties.spi.config.properties.support;
17  
18  import static help.de.smartics.properties.api.config.domain.ConfigurationPropertiesManagementBuilder.a;
19  import static org.hamcrest.MatcherAssert.assertThat;
20  import static org.hamcrest.Matchers.equalTo;
21  import static org.hamcrest.Matchers.is;
22  
23  import java.net.URL;
24  import java.util.Collection;
25  
26  import org.junit.Before;
27  import org.junit.Test;
28  import org.junit.experimental.categories.Category;
29  
30  import de.smartics.properties.api.config.app.FactoryConfiguration;
31  import de.smartics.properties.api.config.domain.CompoundConfigurationException;
32  import de.smartics.properties.api.config.domain.ConfigurationException;
33  import de.smartics.properties.api.config.domain.ConfigurationProperties;
34  import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
35  import de.smartics.properties.api.config.domain.ConfigurationRepositoryManagement;
36  import de.smartics.properties.api.config.domain.PropertyProvider;
37  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
38  import de.smartics.properties.api.core.domain.PropertyDescriptorRegistry;
39  import de.smartics.properties.api.core.security.Base64PropertyValueSecurity;
40  import de.smartics.properties.impl.config.properties.PropertiesConfigurationProperties;
41  import de.smartics.properties.impl.config.properties.PropertiesConfigurationPropertiesFactory;
42  import de.smartics.properties.resource.domain.ArtifactId;
43  import de.smartics.properties.resource.domain.ArtifactRef;
44  import de.smartics.properties.resource.repository.RepositoryException;
45  import de.smartics.properties.spi.config.support.AbstractConfigurationPropertiesFactory;
46  import de.smartics.properties.spi.config.support.ClassPathLoader;
47  import de.smartics.properties.spi.config.support.ConfigurationPropertiesManagementFactory;
48  import de.smartics.properties.spi.config.support.FactoryCache;
49  import de.smartics.properties.spi.config.support.InMemoryConfigurationRepositoryManagement;
50  import de.smartics.properties.test.domain.ValidConfigurationKey;
51  import de.smartics.testdoc.annotations.Uut;
52  import de.smartics.testdoc.categories.Technical;
53  import de.smartics.util.lang.NullArgumentException;
54  import example.de.smartics.properties.simple.SimpleTestProperties;
55  
56  /**
57   * Tests {@link ClassPathLoader}.
58   */
59  @Uut(type = ClassPathLoader.class)
60  public class ClassPathLoaderTest
61  {
62    // ********************************* Fields *********************************
63  
64    // --- constants ------------------------------------------------------------
65  
66    // --- members --------------------------------------------------------------
67  
68    private ClassPathLoader<PropertiesConfigurationProperties> uut;
69  
70    // ****************************** Inner Classes *****************************
71  
72    // ********************************* Methods ********************************
73  
74    // --- prepare --------------------------------------------------------------
75  
76    // CHECKSTYLE:OFF
77    @Before
78    public void setUp()
79    {
80      final PropertiesConfigurationPropertiesFactory factory =
81          new PropertiesConfigurationPropertiesFactory();
82      final PropertyDescriptorRegistry registry = factory.getRegistry();
83  
84      final ConfigurationPropertiesManagementFactory dummy =
85          new ConfigurationPropertiesManagementFactory()
86          {
87            private static final long serialVersionUID = 1L;
88  
89            @Override
90            public String getId()
91            {
92              return null;
93            }
94  
95            @Override
96            public ConfigurationPropertiesManagement create(
97                final ConfigurationKey<?> key)
98            {
99              return a().with(ValidConfigurationKey.KEY).with(registry)
100                 .with(new Base64PropertyValueSecurity()).build();
101           }
102 
103           @Override
104           public FactoryConfiguration getFactoryConfiguration()
105           {
106             return null;
107           }
108 
109           @Override
110           public void addRootLocations(final Collection<URL> urls)
111           {
112           }
113 
114           @Override
115           public void addRootLocations(final URL... urls)
116           {
117           }
118 
119           @Override
120           public void addPropertyProviders(
121               final Collection<PropertyProvider> providers)
122           {
123           }
124 
125           @Override
126           public void addPropertyProviders(final PropertyProvider... providers)
127           {
128           }
129 
130           @Override
131           public ConfigurationPropertiesManagement createManagement(
132               final ConfigurationKey<?> key) throws NullPointerException,
133             ConfigurationException
134           {
135             return null;
136           }
137 
138           @Override
139           public ConfigurationProperties createDefault()
140             throws ConfigurationException
141           {
142             return null;
143           }
144 
145           @Override
146           public ConfigurationPropertiesManagement createDefaultManagement()
147             throws ConfigurationException
148           {
149             return null;
150           }
151 
152           @Override
153           public Collection<ConfigurationKey<?>> getRegisteredConfigurationKeys()
154           {
155             return null;
156           }
157 
158           @Override
159           public PropertyDescriptorRegistry getRegistry()
160           {
161             return null;
162           }
163 
164           @Override
165           public Collection<ConfigurationKey<?>> materialize()
166           {
167             return null;
168           }
169 
170           @Override
171           public void release()
172           {
173           }
174 
175           @Override
176           public ConfigurationPropertiesManagement remove(
177               final ConfigurationKey<?> key) throws NullPointerException
178           {
179             return null;
180           }
181 
182           @Override
183           public String addRootUrls(ArtifactId artifactId)
184             throws NullArgumentException, RepositoryException,
185             CompoundConfigurationException
186           {
187             return null;
188           }
189 
190           @Override
191           public ArtifactRef getArtifactRef(String artifactId)
192             throws NullPointerException
193           {
194             return null;
195           }
196         };
197 
198     final ConfigurationRepositoryManagement cache =
199         new InMemoryConfigurationRepositoryManagement(registry, dummy);
200     final FactoryCache<PropertiesConfigurationProperties> factoryCache =
201         new FactoryCache<PropertiesConfigurationProperties>(cache, factory);
202     uut =
203         new ClassPathLoader<PropertiesConfigurationProperties>(factoryCache,
204             true, false, new Base64PropertyValueSecurity());
205   }
206 
207   // CHECKSTYLE:ON
208 
209   // --- helper ---------------------------------------------------------------
210 
211   // --- tests ----------------------------------------------------------------
212 
213   @Test
214   public void readsPropertyDescriptorsAndDefinitionsFromTheClassPath()
215   {
216     final URL url = SimpleTestProperties.class.getClassLoader().getResource(""); // NOPMD
217     uut.addRootUrl(url);
218 
219     final ConfigurationRepositoryManagement repo = uut.load();
220     final ConfigurationKey<?> key = ValidConfigurationKey.KEY;
221     final ConfigurationPropertiesManagement config =
222         repo.getPropertiesManagementWithDefaults(key);
223     final SimpleTestProperties properties =
224         config.getProperties(SimpleTestProperties.class);
225 
226     final String value = properties.name();
227     assertThat(value, is(equalTo("value")));
228   }
229 
230   @Test
231   @Category(Technical.class)
232   public void allowsToPassInAClassToDeriveTheRootUrl()
233   {
234     uut.addRootUrl(SimpleTestProperties.class);
235 
236     final ConfigurationRepositoryManagement repo = uut.load();
237     final ConfigurationKey<?> key = ValidConfigurationKey.KEY;
238     final ConfigurationPropertiesManagement config =
239         repo.getPropertiesManagementWithDefaults(key);
240 
241     final SimpleTestProperties properties =
242         config.getProperties(SimpleTestProperties.class);
243 
244     final String value = properties.name();
245     assertThat(value, is(equalTo("value")));
246   }
247 }