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.resolve;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.equalTo;
20  import static org.hamcrest.Matchers.is;
21  import static org.mockito.Mockito.mock;
22  import static org.mockito.Mockito.when;
23  
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import de.smartics.properties.api.config.domain.UnknownPropertyException;
28  import de.smartics.testdoc.annotations.Uut;
29  
30  /**
31   * Tests {@link Resolver}.
32   */
33  public class ResolverTest
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    /**
40     * Test value.
41     */
42    private static final String VALUE_1 = "v1";
43  
44    // --- members --------------------------------------------------------------
45  
46    @Uut
47    private Resolver uut;
48  
49    // ****************************** Inner Classes *****************************
50  
51    // ********************************* Methods ********************************
52  
53    // --- prepare --------------------------------------------------------------
54  
55    @Before
56    public void setUp()
57    {
58      final ResolveContext context = mock(ResolveContext.class);
59      when(context.get("component.name1")).thenReturn(VALUE_1);
60      when(context.get("component.name2")).thenReturn("X${component.name1}");
61      when(context.get("illegal.recursion")).thenReturn("${illegal.recursion}");
62      when(context.get("illegal.recursion1")).thenReturn("${illegal.recursion2}");
63      when(context.get("illegal.recursion2")).thenReturn("${illegal.recursion1}");
64      when(context.get("illegal.unknown")).thenReturn("${unknown}");
65      when(context.get("illegal.null")).thenReturn("${null}");
66      when(context.get("illegal.unknownInDepth"))
67          .thenReturn("${illegal.unknown}");
68      when(context.get("unknown")).thenThrow(
69          new UnknownPropertyException(null, "unknown"));
70      when(context.get("null")).thenReturn(null);
71  
72      uut = new Resolver(context);
73    }
74  
75    // --- helper ---------------------------------------------------------------
76  
77    // --- tests ----------------------------------------------------------------
78  
79    @Test
80    public void resolvesValueWithoutPlaceholders()
81    {
82      final String expression = "test";
83      final String resolved = uut.resolve(expression);
84      assertThat(resolved, is(equalTo(expression)));
85    }
86  
87    @Test(expected = UnknownPropertyException.class)
88    public void resolvesWithUnknownPlaceholder()
89    {
90      final String expression = "${unknown}";
91      final String resolved = uut.resolve(expression);
92      assertThat(resolved, is(equalTo(VALUE_1)));
93    }
94  
95    @Test
96    public void resolvesValueWithOnePlaceholder()
97    {
98      final String expression = "${component.name1}";
99      final String resolved = uut.resolve(expression);
100     assertThat(resolved, is(equalTo(VALUE_1)));
101   }
102 
103   @Test
104   public void resolvesValueWithMultiplePlaceholders()
105   {
106     final String expression = "$${component.name1} ${component.name1}${";
107     final String resolved = uut.resolve(expression);
108     assertThat(resolved, is(equalTo('$' + VALUE_1 + ' ' + VALUE_1 + "${")));
109   }
110 
111   @Test
112   public void resolvesRecursively()
113   {
114     final String expression = "${component.name2}";
115     final String resolved = uut.resolve(expression);
116     assertThat(resolved, is(equalTo('X' + VALUE_1)));
117   }
118 
119   @Test(expected = RecursivePropertyException.class)
120   public void detectsDirectInfiniteRecursions()
121   {
122     final String expression = "${illegal.recursion}";
123     uut.resolve(expression);
124   }
125 
126   @Test(expected = RecursivePropertyException.class)
127   public void detectsInfiniteRecursions()
128   {
129     final String expression = "${illegal.recursion1}";
130     uut.resolve(expression);
131   }
132 
133   @Test(expected = UnknownPropertyException.class)
134   public void detectsReferencesToUnknownPlaceholder()
135   {
136     final String expression = "${illegal.unknown}";
137     uut.resolve(expression);
138   }
139 
140   @Test(expected = UnknownPropertyException.class)
141   public void detectsReferencesToUnknownPlaceholderInDepth()
142   {
143     final String expression = "${illegal.unknownInDepth}";
144     uut.resolve(expression);
145   }
146 
147   @Test(expected = UnresolvablePropertyException.class)
148   public void detectsReferencedNullValue()
149   {
150     final String expression = "${illegal.null}";
151     uut.resolve(expression);
152   }
153 }