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.ClassPathLoader;
46  import de.smartics.properties.spi.config.support.ConfigurationPropertiesManagementFactory;
47  import de.smartics.properties.spi.config.support.FactoryCache;
48  import de.smartics.properties.spi.config.support.InMemoryConfigurationRepositoryManagement;
49  import de.smartics.properties.test.domain.ValidConfigurationKey;
50  import de.smartics.testdoc.annotations.Uut;
51  import de.smartics.testdoc.categories.Technical;
52  import de.smartics.util.lang.NullArgumentException;
53  import example.de.smartics.properties.simple.SimpleTestProperties;
54  
55  /**
56   * Tests {@link ClassPathLoader}.
57   */
58  @Uut(type = ClassPathLoader.class)
59  public class ClassPathLoaderTest
60  {
61    // ********************************* Fields *********************************
62  
63    // --- constants ------------------------------------------------------------
64  
65    // --- members --------------------------------------------------------------
66  
67    private ClassPathLoader<PropertiesConfigurationProperties> uut;
68  
69    // ****************************** Inner Classes *****************************
70  
71    // ********************************* Methods ********************************
72  
73    // --- prepare --------------------------------------------------------------
74  
75    // CHECKSTYLE:OFF
76    @Before
77    public void setUp()
78    {
79      final PropertiesConfigurationPropertiesFactory factory =
80          new PropertiesConfigurationPropertiesFactory();
81      final PropertyDescriptorRegistry registry = factory.getRegistry();
82  
83      final ConfigurationPropertiesManagementFactory dummy =
84          new ConfigurationPropertiesManagementFactory()
85          {
86            private static final long serialVersionUID = 1L;
87  
88            @Override
89            public ConfigurationPropertiesManagement create(
90                final ConfigurationKey<?> key)
91            {
92              return a().with(ValidConfigurationKey.KEY).with(registry)
93                  .with(new Base64PropertyValueSecurity()).build();
94            }
95  
96            @Override
97            public FactoryConfiguration getFactoryConfiguration()
98            {
99              return null;
100           }
101 
102           @Override
103           public void addRootLocations(final Collection<URL> urls)
104           {
105           }
106 
107           @Override
108           public void addRootLocations(final URL... urls)
109           {
110           }
111 
112           @Override
113           public void addPropertyProviders(
114               final Collection<PropertyProvider> providers)
115           {
116           }
117 
118           @Override
119           public void addPropertyProviders(final PropertyProvider... providers)
120           {
121           }
122 
123           @Override
124           public ConfigurationPropertiesManagement createManagement(
125               final ConfigurationKey<?> key) throws NullPointerException,
126             ConfigurationException
127           {
128             return null;
129           }
130 
131           @Override
132           public ConfigurationProperties createDefault()
133             throws ConfigurationException
134           {
135             return null;
136           }
137 
138           @Override
139           public ConfigurationPropertiesManagement createDefaultManagement()
140             throws ConfigurationException
141           {
142             return null;
143           }
144 
145           @Override
146           public Collection<ConfigurationKey<?>> getRegisteredConfigurationKeys()
147           {
148             return null;
149           }
150 
151           @Override
152           public PropertyDescriptorRegistry getRegistry()
153           {
154             return null;
155           }
156 
157           @Override
158           public Collection<ConfigurationKey<?>> materialize()
159           {
160             return null;
161           }
162 
163           @Override
164           public void release()
165           {
166           }
167 
168           @Override
169           public ConfigurationPropertiesManagement remove(
170               final ConfigurationKey<?> key) throws NullPointerException
171           {
172             return null;
173           }
174 
175           @Override
176           public String addRootUrls(ArtifactId artifactId)
177             throws NullArgumentException, RepositoryException,
178             CompoundConfigurationException
179           {
180             return null;
181           }
182 
183           @Override
184           public ArtifactRef getArtifactRef(String artifactId)
185             throws NullPointerException
186           {
187             return null;
188           }
189         };
190 
191     final ConfigurationRepositoryManagement cache =
192         new InMemoryConfigurationRepositoryManagement(registry, dummy);
193     final FactoryCache<PropertiesConfigurationProperties> factoryCache =
194         new FactoryCache<PropertiesConfigurationProperties>(cache, factory);
195     uut =
196         new ClassPathLoader<PropertiesConfigurationProperties>(factoryCache,
197             true, false, new Base64PropertyValueSecurity());
198   }
199 
200   // CHECKSTYLE:ON
201 
202   // --- helper ---------------------------------------------------------------
203 
204   // --- tests ----------------------------------------------------------------
205 
206   @Test
207   public void readsPropertyDescriptorsAndDefinitionsFromTheClassPath()
208   {
209     final URL url = SimpleTestProperties.class.getClassLoader().getResource(""); // NOPMD
210     uut.addRootUrl(url);
211 
212     final ConfigurationRepositoryManagement repo = uut.load();
213     final ConfigurationKey<?> key = ValidConfigurationKey.KEY;
214     final ConfigurationPropertiesManagement config =
215         repo.getPropertiesManagementWithDefaults(key);
216     final SimpleTestProperties properties =
217         config.getProperties(SimpleTestProperties.class);
218 
219     final String value = properties.name();
220     assertThat(value, is(equalTo("value")));
221   }
222 
223   @Test
224   @Category(Technical.class)
225   public void allowsToPassInAClassToDeriveTheRootUrl()
226   {
227     uut.addRootUrl(SimpleTestProperties.class);
228 
229     final ConfigurationRepositoryManagement repo = uut.load();
230     final ConfigurationKey<?> key = ValidConfigurationKey.KEY;
231     final ConfigurationPropertiesManagement config =
232         repo.getPropertiesManagementWithDefaults(key);
233 
234     final SimpleTestProperties properties =
235         config.getProperties(SimpleTestProperties.class);
236 
237     final String value = properties.name();
238     assertThat(value, is(equalTo("value")));
239   }
240 }