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.fields;
16  
17  import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setOnMissingFileProperty;
18  import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setValue;
19  import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.uuidDefinition;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import org.basepom.mojo.propertyhelper.ValueCache;
23  import org.basepom.mojo.propertyhelper.ValueProvider;
24  import org.basepom.mojo.propertyhelper.ValueProvider.PropertyBackedValueAdapter;
25  import org.basepom.mojo.propertyhelper.ValueProvider.SingleValueProvider;
26  import org.basepom.mojo.propertyhelper.definitions.UuidDefinition;
27  
28  import java.util.Properties;
29  import java.util.UUID;
30  
31  import com.google.common.collect.ImmutableMap;
32  import org.junit.jupiter.api.Assertions;
33  import org.junit.jupiter.api.Test;
34  
35  public class TestUuidField {
36  
37      @Test
38      public void testSimple() {
39          final UUID uuid = UUID.randomUUID();
40          final UuidDefinition uuidFieldDefinition = uuidDefinition("hello");
41          setValue(uuidFieldDefinition, uuid.toString());
42  
43          uuidFieldDefinition.check();
44  
45          final UuidField uf1 = UuidField.forTesting(uuidFieldDefinition, ValueProvider.NULL_PROVIDER);
46          Assertions.assertEquals(uuid.toString(), uf1.getValue());
47      }
48  
49      @Test
50      public void testSimpleProperty() {
51          final UUID uuid = UUID.randomUUID();
52          final UuidDefinition uuidDefinition = uuidDefinition("hello");
53  
54          uuidDefinition.check();
55  
56          final Properties props = new Properties();
57          props.setProperty("hello", uuid.toString());
58          final UuidField uf1 = UuidField.forTesting(uuidDefinition, new PropertyBackedValueAdapter(props, uuidDefinition.getId()));
59          Assertions.assertEquals(uuid.toString(), uf1.getValue());
60      }
61  
62      @Test
63      public void testSimplePropertyWithDefault() {
64          final UUID uuid1 = UUID.randomUUID();
65          final UUID uuid2 = UUID.randomUUID();
66  
67          final UuidDefinition uuidDefinition = uuidDefinition("hello");
68          setValue(uuidDefinition, uuid1.toString());
69  
70          uuidDefinition.check();
71  
72          final Properties props = new Properties();
73          props.setProperty("hello", uuid2.toString());
74          final UuidField uf1 = UuidField.forTesting(uuidDefinition, new PropertyBackedValueAdapter(props, uuidDefinition.getId()));
75          Assertions.assertEquals(uuid2.toString(), uf1.getValue());
76      }
77  
78      @Test
79      public void testNoProperty() {
80          final UUID uuid1 = UUID.randomUUID();
81          final UUID uuid2 = UUID.randomUUID();
82  
83          final UuidDefinition uuidDefinition = uuidDefinition("hello");
84          setValue(uuidDefinition, uuid1.toString());
85  
86          uuidDefinition.check();
87  
88          final Properties props = new Properties();
89          props.setProperty("hello2", uuid2.toString());
90          final UuidField uf1 = UuidField.forTesting(uuidDefinition, new PropertyBackedValueAdapter(props, uuidDefinition.getId()));
91          Assertions.assertEquals(uuid1.toString(), uf1.getValue());
92      }
93  
94      @Test
95      public void testNothing() {
96          final UUID uuid = UUID.randomUUID();
97  
98          final UuidDefinition uuidDefinition = uuidDefinition("hello");
99  
100         uuidDefinition.check();
101 
102         final ValueProvider provider = new SingleValueProvider();
103         provider.setValue(uuid.toString());
104         final UuidField uf1 = UuidField.forTesting(uuidDefinition, provider);
105         Assertions.assertEquals(uuid.toString(), uf1.getValue());
106     }
107 
108     @Test
109     public void testMissingProperty() {
110         ValueCache valueCache = new ValueCache();
111 
112         assertThrows(IllegalStateException.class, () -> {
113             final UuidDefinition uuidDefinition = uuidDefinition("hello");
114             setOnMissingFileProperty(uuidDefinition, "fail");
115 
116             uuidDefinition.check();
117 
118             final ValueProvider provider = valueCache.findCurrentValueProvider(ImmutableMap.of(), uuidDefinition);
119 
120             final UuidField uf1 = UuidField.forTesting(uuidDefinition, provider);
121             Assertions.assertEquals("", uf1.getValue());
122         });
123     }
124 }