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.definition;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.allOf;
20  import static org.hamcrest.Matchers.containsString;
21  
22  import java.io.BufferedInputStream;
23  import java.io.InputStream;
24  
25  import org.apache.commons.io.IOUtils;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import de.smartics.properties.api.core.domain.ConfigException;
30  import de.smartics.properties.impl.config.domain.key.envapp.EnvAppDefinitionConfigParser;
31  import de.smartics.properties.spi.config.definition.DefinitionConfigParser;
32  import de.smartics.properties.spi.config.definition.PropertiesDefinitionContext;
33  import de.smartics.testdoc.annotations.Uut;
34  
35  /**
36   * Tests {@link EnvAppDefinitionConfigParser}.
37   */
38  @SuppressWarnings("unchecked")
39  public class DefinitionConfigParserTest
40  {
41    // ********************************* Fields *********************************
42  
43    // --- constants ------------------------------------------------------------
44    private static final String TLDS_LABEL = "TLDs          :";
45  
46    private static final String ENVIRONMENTS_LABEL = "Environments  :";
47  
48    private static final String NODES_LABEL = "Nodes         :";
49  
50    private static final String GROUPS_LABEL = "Groups        :";
51  
52    private static final String PATHS_LABEL = "Paths:";
53  
54    private static final String DEFAULT_TLDS =
55        TLDS_LABEL + " com edu net org biz gov info\n";
56  
57    private static final String EMPTY_ENVIRONMENTS = ENVIRONMENTS_LABEL + "\n";
58  
59    private static final String EMPTY_NODES = NODES_LABEL + "\n";
60  
61    private static final String EMPTY_GROUPS = GROUPS_LABEL + "\n";
62  
63    private static final String EMPTY_PATHS = PATHS_LABEL;
64  
65    private static final String CONFIG_KEY_1 =
66        "test-environment-1:test-node-1/test-group-1:test-artifact-1:test-version-1";
67  
68    private static final String CONFIG_KEY_2 =
69        "test-environment-2:test-node-2/test-group-2:test-artifact-2:test-version-2";
70  
71    // --- members --------------------------------------------------------------
72  
73    @SuppressWarnings("rawtypes")
74    @Uut
75    private DefinitionConfigParser uut;
76  
77    // ****************************** Inner Classes *****************************
78  
79    // ********************************* Methods ********************************
80  
81    // --- prepare --------------------------------------------------------------
82  
83    @Before
84    public void setUp()
85    {
86      uut = new EnvAppDefinitionConfigParser();
87    }
88  
89    // --- helper ---------------------------------------------------------------
90  
91    private InputStream openStream(final String name)
92    {
93      final InputStream input =
94          DefinitionConfigParserTest.class.getResourceAsStream(name + ".xml");
95      if (input != null)
96      {
97        return new BufferedInputStream(input);
98      }
99  
100     throw new IllegalArgumentException("Cannot open stream '" + name + "'.");
101   }
102 
103   private PropertiesDefinitionContext readContext(final String testId)
104     throws ConfigException
105   {
106     final InputStream input = openStream(testId);
107     try
108     {
109       final PropertiesDefinitionContext context = uut.parse("test", input);
110       return context;
111     }
112     finally
113     {
114       IOUtils.closeQuietly(input);
115     }
116   }
117 
118   // --- tests ----------------------------------------------------------------
119 
120   @Test
121   public void readsEmptyDefinitionFile()
122   {
123     final PropertiesDefinitionContext context = readContext("empty");
124     final String string = context.toString();
125     assertThat(
126         string,
127         allOf(containsString(DEFAULT_TLDS), containsString(EMPTY_ENVIRONMENTS),
128             containsString(EMPTY_NODES), containsString(EMPTY_GROUPS),
129             containsString(EMPTY_PATHS)));
130   }
131 
132   @Test
133   public void readsTldOnlyDefinitionFile()
134   {
135     final PropertiesDefinitionContext context = readContext("tlds-only");
136     final String string = context.toString();
137     assertThat(
138         string,
139         allOf(containsString(TLDS_LABEL), containsString(" test-tld-1"),
140             containsString(" test-tld-2"), containsString(EMPTY_ENVIRONMENTS),
141             containsString(EMPTY_NODES), containsString(EMPTY_GROUPS),
142             containsString(EMPTY_PATHS)));
143   }
144 
145   @Test
146   public void readsEnvironmentsOnlyDefinitionFile()
147   {
148     final PropertiesDefinitionContext context =
149         readContext("environments-only");
150     final String string = context.toString();
151     assertThat(
152         string,
153         allOf(containsString(DEFAULT_TLDS), containsString(ENVIRONMENTS_LABEL),
154             containsString(" test-environment-1"),
155             containsString(" test-environment-2"), containsString(EMPTY_NODES),
156             containsString(EMPTY_GROUPS), containsString(EMPTY_PATHS)));
157   }
158 
159   @Test
160   public void readsNodesOnlyDefinitionFile()
161   {
162     final PropertiesDefinitionContext context = readContext("nodes-only");
163     final String string = context.toString();
164     assertThat(
165         string,
166         allOf(containsString(DEFAULT_TLDS), containsString(EMPTY_ENVIRONMENTS),
167             containsString(NODES_LABEL), containsString(" test-node-1"),
168             containsString(" test-node-2"), containsString(EMPTY_GROUPS),
169             containsString(EMPTY_PATHS)));
170   }
171 
172   @Test
173   public void readsGroupsOnlyDefinitionFile()
174   {
175     final PropertiesDefinitionContext context = readContext("groups-only");
176     final String string = context.toString();
177     assertThat(
178         string,
179         allOf(containsString(DEFAULT_TLDS), containsString(EMPTY_ENVIRONMENTS),
180             containsString(EMPTY_NODES), containsString(GROUPS_LABEL),
181             containsString(" test-group-1"), containsString(" test-group-2"),
182             containsString(EMPTY_PATHS)));
183   }
184 
185   @Test
186   public void readsFilesOnlyDefinitionFile()
187   {
188     final PropertiesDefinitionContext context = readContext("files-only");
189     final String string = context.toString();
190     assertThat(
191         string,
192         allOf(
193             containsString(DEFAULT_TLDS),
194             containsString(EMPTY_ENVIRONMENTS),
195             containsString(EMPTY_NODES),
196             containsString(EMPTY_GROUPS),
197             containsString(PATHS_LABEL),
198             containsString(CONFIG_KEY_1
199                            + ": test-1/file-1.properties test-1/file-2.properties"),
200             containsString(CONFIG_KEY_2
201                            + ": test-2/file-1.properties test-2/file-2.properties")));
202   }
203 
204   @Test
205   public void readsFullDefinitionFile()
206   {
207     final PropertiesDefinitionContext context = readContext("full");
208     final String string = context.toString();
209     assertThat(
210         string,
211         allOf(
212             containsString(TLDS_LABEL),
213             containsString(" test-tld-1"),
214             containsString(" test-tld-2"),
215             containsString(ENVIRONMENTS_LABEL),
216             containsString(" test-environment-1"),
217             containsString(" test-environment-2"),
218             containsString(NODES_LABEL),
219             containsString(" test-node-1"),
220             containsString(" test-node-2"),
221             containsString(GROUPS_LABEL),
222             containsString(" test-group-1"),
223             containsString(" test-group-2"),
224             containsString(PATHS_LABEL),
225             containsString(CONFIG_KEY_1
226                            + ": test-1/file-1.properties test-1/file-2.properties"),
227             containsString(CONFIG_KEY_2
228                            + ": test-2/file-1.properties test-2/file-2.properties")));
229   }
230 }