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