001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014 015package org.basepom.mojo.propertyhelper; 016 017import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.numberDefinition; 018import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setOnMissingFile; 019import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setOnMissingFileProperty; 020import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setPropertyFile; 021import static org.junit.jupiter.api.Assertions.assertEquals; 022import static org.junit.jupiter.api.Assertions.assertFalse; 023import static org.junit.jupiter.api.Assertions.assertNotNull; 024import static org.junit.jupiter.api.Assertions.assertNull; 025import static org.junit.jupiter.api.Assertions.assertThrows; 026 027import org.basepom.mojo.propertyhelper.definitions.NumberDefinition; 028 029import java.io.File; 030import java.io.FileWriter; 031import java.io.IOException; 032import java.util.Properties; 033 034import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 035import org.junit.jupiter.api.AfterEach; 036import org.junit.jupiter.api.BeforeEach; 037import org.junit.jupiter.api.Test; 038 039public class TestPropertyCache { 040 041 private final Properties props = new Properties(); 042 private ValueCache pc = null; 043 private File propFile = null; 044 private FileWriter writer = null; 045 046 @BeforeEach 047 public void setUp() 048 throws IOException { 049 assertNull(pc); 050 pc = new ValueCache(); 051 052 assertNull(propFile); 053 propFile = File.createTempFile("test", null); 054 propFile.deleteOnExit(); 055 056 assertNull(writer); 057 writer = new FileWriter(propFile); 058 } 059 060 @AfterEach 061 public void tearDown() 062 throws IOException { 063 assertNotNull(pc); 064 assertNotNull(propFile); 065 assertNotNull(writer); 066 writer.close(); 067 } 068 069 @Test 070 public void testEphemeralDefault() 071 throws IOException { 072 final NumberDefinition ephemeral = numberDefinition("hello"); 073 ephemeral.check(); 074 final ValueProvider valueProvider = pc.getValueProvider(ephemeral); 075 assertEquals(ephemeral.getInitialValue(), valueProvider.getValue()); 076 } 077 078 @Test 079 @SuppressFBWarnings(value = "DMI_HARDCODED_ABSOLUTE_FILENAME", justification = "unit test") 080 public void testMissingPropertyFileFail() { 081 assertThrows(IllegalStateException.class, () -> { 082 final NumberDefinition fileBacked = numberDefinition("hello"); 083 setOnMissingFile(fileBacked, "FAIL"); 084 setOnMissingFileProperty(fileBacked, "IGNORE"); 085 setPropertyFile(fileBacked, new File("/does/not/exist")); 086 087 fileBacked.check(); 088 pc.getValueProvider(fileBacked); 089 }); 090 } 091 092 @Test 093 public void testEmptyPropertyFileCreate() 094 throws IOException { 095 props.store(writer, null); 096 writer.flush(); 097 writer.close(); 098 099 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}