View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package org.basepom.mojo.propertyhelper;
16  
17  import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.numberDefinition;
18  import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setOnMissingFile;
19  import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setOnMissingFileProperty;
20  import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setPropertyFile;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertNull;
25  import static org.junit.jupiter.api.Assertions.assertThrows;
26  
27  import org.basepom.mojo.propertyhelper.definitions.NumberDefinition;
28  
29  import java.io.File;
30  import java.io.FileWriter;
31  import java.io.IOException;
32  import java.util.Properties;
33  
34  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35  import org.junit.jupiter.api.AfterEach;
36  import org.junit.jupiter.api.BeforeEach;
37  import org.junit.jupiter.api.Test;
38  
39  public class TestPropertyCache {
40  
41      private final Properties props = new Properties();
42      private ValueCache pc = null;
43      private File propFile = null;
44      private FileWriter writer = null;
45  
46      @BeforeEach
47      public void setUp()
48          throws IOException {
49          assertNull(pc);
50          pc = new ValueCache();
51  
52          assertNull(propFile);
53          propFile = File.createTempFile("test", null);
54          propFile.deleteOnExit();
55  
56          assertNull(writer);
57          writer = new FileWriter(propFile);
58      }
59  
60      @AfterEach
61      public void tearDown()
62          throws IOException {
63          assertNotNull(pc);
64          assertNotNull(propFile);
65          assertNotNull(writer);
66          writer.close();
67      }
68  
69      @Test
70      public void testEphemeralDefault()
71          throws IOException {
72          final NumberDefinition ephemeral = numberDefinition("hello");
73          ephemeral.check();
74          final ValueProvider valueProvider = pc.getValueProvider(ephemeral);
75          assertEquals(ephemeral.getInitialValue(), valueProvider.getValue());
76      }
77  
78      @Test
79      @SuppressFBWarnings(value = "DMI_HARDCODED_ABSOLUTE_FILENAME", justification = "unit test")
80      public void testMissingPropertyFileFail() {
81          assertThrows(IllegalStateException.class, () -> {
82              final NumberDefinition fileBacked = numberDefinition("hello");
83              setOnMissingFile(fileBacked, "FAIL");
84              setOnMissingFileProperty(fileBacked, "IGNORE");
85              setPropertyFile(fileBacked, new File("/does/not/exist"));
86  
87              fileBacked.check();
88              pc.getValueProvider(fileBacked);
89          });
90      }
91  
92      @Test
93      public void testEmptyPropertyFileCreate()
94          throws IOException {
95          props.store(writer, null);
96          writer.flush();
97          writer.close();
98  
99          final NumberDefinition fileBacked = numberDefinition("hello");
100         setOnMissingFile(fileBacked, "FAIL");
101         setOnMissingFileProperty(fileBacked, "CREATE");
102         setPropertyFile(fileBacked, propFile);
103         fileBacked.check();
104         final ValueProvider valueProvider = pc.getValueProvider(fileBacked);
105         assertEquals(fileBacked.getInitialValue(), valueProvider.getValue());
106     }
107 
108     @Test
109     public void testEmptyPropertyFileIgnore()
110         throws IOException {
111         props.store(writer, null);
112         writer.flush();
113         writer.close();
114 
115         final NumberDefinition fileBacked = numberDefinition("hello");
116         setOnMissingFile(fileBacked, "FAIL");
117         setOnMissingFileProperty(fileBacked, "IGNORE");
118         setPropertyFile(fileBacked, propFile);
119 
120         fileBacked.check();
121         final ValueProvider valueProvider = pc.getValueProvider(fileBacked);
122         assertFalse(valueProvider.getValue().isPresent());
123     }
124 
125     @Test
126     public void testEmptyPropertyFileFail() {
127 
128         assertThrows(IllegalStateException.class, () -> {
129             final Properties props = new Properties();
130             final FileWriter writer = new FileWriter(propFile);
131             props.store(writer, null);
132             writer.flush();
133             writer.close();
134 
135             final NumberDefinition fileBacked = numberDefinition("hello");
136             setOnMissingFile(fileBacked, "FAIL");
137             setOnMissingFileProperty(fileBacked, "FAIL");
138             setPropertyFile(fileBacked, propFile);
139 
140             fileBacked.check();
141             pc.getValueProvider(fileBacked);
142         });
143     }
144 
145     @Test
146     public void testLoadProperty()
147         throws IOException {
148         final Properties props = new Properties();
149         final FileWriter writer = new FileWriter(propFile);
150         final String propValue = "12345";
151 
152         props.setProperty("hello", propValue);
153         props.store(writer, null);
154         writer.flush();
155         writer.close();
156 
157         final NumberDefinition fileBacked = numberDefinition("hello");
158         setOnMissingFile(fileBacked, "FAIL");
159         setOnMissingFileProperty(fileBacked, "FAIL");
160         setPropertyFile(fileBacked, propFile);
161 
162         fileBacked.check();
163         final ValueProvider valueProvider = pc.getValueProvider(fileBacked);
164         assertEquals(propValue, valueProvider.getValue().get());
165     }
166 
167     @Test
168     public void testIgnoreCreate() throws IOException {
169         final Properties props = new Properties();
170         final FileWriter writer = new FileWriter(propFile);
171         final String propValue = "12345";
172 
173         props.setProperty("hello", propValue);
174         props.store(writer, null);
175         writer.flush();
176         writer.close();
177 
178         final NumberDefinition fileBacked = numberDefinition("hello");
179         setOnMissingFile(fileBacked, "FAIL");
180         setOnMissingFileProperty(fileBacked, "CREATE");
181         setPropertyFile(fileBacked, propFile);
182         fileBacked.check();
183         final ValueProvider valueProvider = pc.getValueProvider(fileBacked);
184         assertEquals(propValue, valueProvider.getValue().get());
185     }
186 
187     @Test
188     public void samePropertyObject()
189         throws IOException {
190         final Properties props = new Properties();
191         final FileWriter writer = new FileWriter(propFile);
192 
193         props.setProperty("hello", "hello");
194         props.setProperty("world", "world");
195         props.store(writer, null);
196         writer.flush();
197         writer.close();
198 
199         final NumberDefinition n1 = numberDefinition("hello");
200         setOnMissingFile(n1, "FAIL");
201         setOnMissingFileProperty(n1, "FAIL");
202         setPropertyFile(n1, propFile);
203 
204         final NumberDefinition n2 = numberDefinition("world");
205         setOnMissingFile(n2, "FAIL");
206         setOnMissingFileProperty(n2, "FAIL");
207         setPropertyFile(n2, propFile);
208 
209         n1.check();
210         n2.check();
211         assertEquals("hello", pc.getValueProvider(n1).getValue().get());
212         assertEquals("world", pc.getValueProvider(n2).getValue().get());
213 
214         assertEquals(pc.getValues(n1).get(), pc.getValues(n2).get());
215     }
216 }