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 de.smartics.properties.spi.config.support;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.is;
20  
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import de.smartics.properties.api.config.domain.key.ApplicationId;
28  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
29  import de.smartics.properties.api.config.domain.key.EnvironmentId;
30  import de.smartics.testdoc.annotations.Uut;
31  
32  /**
33   * Tests {@link KeySetBuilder}.
34   */
35  public class KeySetBuilderTest
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    private static final String ENV_NAME = "test";
42  
43    private static final String NODEN_AME = "test-node";
44  
45    private static final String GROUP_ID = "test-group";
46  
47    private static final String ARTIFACT_ID = "test-artifact";
48  
49    private static final String VERSION = "1.0";
50  
51    // ... key parts ............................................................
52  
53    private static final EnvironmentId ENV_NAME_ONLY =
54        new EnvironmentId(ENV_NAME);
55  
56    private static final EnvironmentId ENV_NAME_AND_NODE = new EnvironmentId(
57        ENV_NAME, NODEN_AME);
58  
59    private static final ApplicationId APP_GROUP_ONLY = new ApplicationId(
60        GROUP_ID, null, null);
61  
62    private static final ApplicationId APP_NO_VERSION = new ApplicationId(
63        GROUP_ID, ARTIFACT_ID, null);
64  
65    private static final ApplicationId APP_ALL = new ApplicationId(GROUP_ID,
66        ARTIFACT_ID, VERSION);
67  
68    // ... keys .................................................................
69  
70    private static final ConfigurationKey ANY = new ConfigurationKey(
71        EnvironmentId.ANY_ENV, ApplicationId.ANY_APP);
72  
73    private static final ConfigurationKey E = new ConfigurationKey(ENV_NAME_ONLY,
74        ApplicationId.ANY_APP);
75  
76    private static final ConfigurationKey EN = new ConfigurationKey(
77        ENV_NAME_AND_NODE, ApplicationId.ANY_APP);
78  
79    private static final ConfigurationKey E_G = new ConfigurationKey(
80        ENV_NAME_ONLY, APP_GROUP_ONLY);
81  
82    private static final ConfigurationKey EN_G = new ConfigurationKey(
83        ENV_NAME_AND_NODE, APP_GROUP_ONLY);
84  
85    private static final ConfigurationKey E_GA = new ConfigurationKey(
86        ENV_NAME_ONLY, APP_NO_VERSION);
87  
88    private static final ConfigurationKey EN_GA = new ConfigurationKey(
89        ENV_NAME_AND_NODE, APP_NO_VERSION);
90  
91    private static final ConfigurationKey E_GAV = new ConfigurationKey(
92        ENV_NAME_ONLY, APP_ALL);
93  
94    private static final ConfigurationKey EN_GAV = new ConfigurationKey(
95        ENV_NAME_AND_NODE, APP_ALL);
96  
97    private static final ConfigurationKey ANY_G = new ConfigurationKey(
98        EnvironmentId.ANY_ENV, APP_GROUP_ONLY);
99  
100   private static final ConfigurationKey ANY_GA = new ConfigurationKey(
101       EnvironmentId.ANY_ENV, APP_NO_VERSION);
102 
103   private static final ConfigurationKey ANY_GAV = new ConfigurationKey(
104       EnvironmentId.ANY_ENV, APP_ALL);
105 
106   // --- members --------------------------------------------------------------
107 
108   @Uut
109   private KeySetBuilder uut;
110 
111   // ****************************** Inner Classes *****************************
112 
113   // ********************************* Methods ********************************
114 
115   // --- prepare --------------------------------------------------------------
116 
117   @Before
118   public void setUp()
119   {
120     uut = new KeySetBuilder();
121   }
122 
123   // --- helper ---------------------------------------------------------------
124 
125   // --- tests ----------------------------------------------------------------
126 
127   @Test
128   public void derivesFromKeyWithOnlyAnEnvironmentName()
129   {
130     final List<ConfigurationKey> keySet = uut.createKeySet(E);
131 
132     assertThat(keySet, is(Arrays.asList(E, ANY)));
133   }
134 
135   @Test
136   public void derivesFromKeyWithEnvironmentNameAndNode()
137   {
138     final List<ConfigurationKey> keySet = uut.createKeySet(EN);
139 
140     assertThat(keySet, is(Arrays.asList(EN, E, ANY)));
141   }
142 
143   @Test
144   public void derivesFromKeyWithFullEnvironmentNameOnlyAndGroup()
145   {
146     final List<ConfigurationKey> keySet = uut.createKeySet(E_G);
147 
148     assertThat(keySet, is(Arrays.asList(E_G, E, ANY_G, ANY)));
149   }
150 
151   @Test
152   public void derivesFromKeyWithFullEnvironmentAndGroup()
153   {
154     final List<ConfigurationKey> keySet = uut.createKeySet(EN_G);
155 
156     assertThat(keySet, is(Arrays.asList(EN_G, EN, E, ANY_G, ANY)));
157   }
158 
159   @Test
160   public void derivesFromKeyWithFullEnvironmentNameOnlyAndGA()
161   {
162     final List<ConfigurationKey> keySet = uut.createKeySet(E_GA);
163 
164     assertThat(keySet, is(Arrays.asList(E_GA, E_G, E, ANY_GA, ANY_G, ANY)));
165   }
166 
167   @Test
168   public void derivesFromKeyWithFullEnvironmentAndGA()
169   {
170     final List<ConfigurationKey> keySet = uut.createKeySet(EN_GA);
171 
172     assertThat(keySet, is(Arrays.asList(EN_GA, EN_G, EN, E_G, E, ANY_GA, ANY_G, ANY)));
173   }
174 
175   @Test
176   public void derivesFromKeyWithFullEnvironmentNameOnlyAndGAV()
177   {
178     final List<ConfigurationKey> keySet = uut.createKeySet(E_GAV);
179 
180     assertThat(keySet,
181         is(Arrays.asList(E_GAV, E_GA, E_G, E, ANY_GAV, ANY_GA, ANY_G, ANY)));
182   }
183 
184   @Test
185   public void derivesFromKeyWithFullEnvironmentAndGAV()
186   {
187     final List<ConfigurationKey> keySet = uut.createKeySet(EN_GAV);
188 
189     assertThat(keySet, is(Arrays.asList(EN_GAV, EN_GA, EN_G, EN, E_GAV, E_GA,
190         E_G, E, ANY_GAV, ANY_GA, ANY_G, ANY)));
191   }
192 }