1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.basepom.mojo.propertyhelper.fields;
16
17 import static org.assertj.core.api.Assertions.assertThat;
18 import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setOnMissingFileProperty;
19 import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setValue;
20 import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.uuidDefinition;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22
23 import org.basepom.mojo.propertyhelper.FieldContext;
24 import org.basepom.mojo.propertyhelper.RandomUtil;
25 import org.basepom.mojo.propertyhelper.ValueCache;
26 import org.basepom.mojo.propertyhelper.ValueProvider;
27 import org.basepom.mojo.propertyhelper.ValueProvider.PropertyBackedValueAdapter;
28 import org.basepom.mojo.propertyhelper.ValueProvider.SingleValueProvider;
29 import org.basepom.mojo.propertyhelper.definitions.UuidDefinition;
30
31 import java.security.SecureRandom;
32 import java.util.Properties;
33 import java.util.UUID;
34
35 import com.google.common.collect.ImmutableMap;
36 import org.junit.jupiter.api.Assertions;
37 import org.junit.jupiter.api.Test;
38
39 public class TestUuidField {
40
41 @Test
42 public void testSimple() {
43 final UUID uuid = UUID.randomUUID();
44 final UuidDefinition uuidFieldDefinition = uuidDefinition("hello");
45 setValue(uuidFieldDefinition, uuid.toString());
46
47 uuidFieldDefinition.check();
48
49 final UuidField uf1 = UuidField.forTesting(uuidFieldDefinition, ValueProvider.NULL_PROVIDER);
50 Assertions.assertEquals(uuid.toString(), uf1.getValue());
51 }
52
53 @Test
54 public void testSimpleProperty() {
55 final UUID uuid = UUID.randomUUID();
56 final UuidDefinition uuidDefinition = uuidDefinition("hello");
57
58 uuidDefinition.check();
59
60 final Properties props = new Properties();
61 props.setProperty("hello", uuid.toString());
62 final UuidField uf1 = UuidField.forTesting(uuidDefinition, new PropertyBackedValueAdapter(props, uuidDefinition.getId()));
63 Assertions.assertEquals(uuid.toString(), uf1.getValue());
64 }
65
66 @Test
67 public void testSimplePropertyWithDefault() {
68 final UUID uuid1 = UUID.randomUUID();
69 final UUID uuid2 = UUID.randomUUID();
70
71 final UuidDefinition uuidDefinition = uuidDefinition("hello");
72 setValue(uuidDefinition, uuid1.toString());
73
74 uuidDefinition.check();
75
76 final Properties props = new Properties();
77 props.setProperty("hello", uuid2.toString());
78 final UuidField uf1 = UuidField.forTesting(uuidDefinition, new PropertyBackedValueAdapter(props, uuidDefinition.getId()));
79 Assertions.assertEquals(uuid2.toString(), uf1.getValue());
80 }
81
82 @Test
83 public void testNoProperty() {
84 final UUID uuid1 = UUID.randomUUID();
85 final UUID uuid2 = UUID.randomUUID();
86
87 final UuidDefinition uuidDefinition = uuidDefinition("hello");
88 setValue(uuidDefinition, uuid1.toString());
89
90 uuidDefinition.check();
91
92 final Properties props = new Properties();
93 props.setProperty("hello2", uuid2.toString());
94 final UuidField uf1 = UuidField.forTesting(uuidDefinition, new PropertyBackedValueAdapter(props, uuidDefinition.getId()));
95 Assertions.assertEquals(uuid1.toString(), uf1.getValue());
96 }
97
98 @Test
99 public void testNothing() {
100 final UUID uuid = UUID.randomUUID();
101
102 final UuidDefinition uuidDefinition = uuidDefinition("hello");
103
104 uuidDefinition.check();
105
106 final ValueProvider provider = new SingleValueProvider();
107 provider.setValue(uuid.toString());
108 final UuidField uf1 = UuidField.forTesting(uuidDefinition, provider);
109 Assertions.assertEquals(uuid.toString(), uf1.getValue());
110 }
111
112 @Test
113 public void testMissingProperty() {
114 ValueCache valueCache = new ValueCache();
115
116 assertThrows(IllegalStateException.class, () -> {
117 final UuidDefinition uuidDefinition = uuidDefinition("hello");
118 setOnMissingFileProperty(uuidDefinition, "fail");
119
120 uuidDefinition.check();
121
122 final ValueProvider provider = valueCache.findCurrentValueProvider(ImmutableMap.of(), uuidDefinition);
123
124 final UuidField uf1 = UuidField.forTesting(uuidDefinition, provider);
125 Assertions.assertEquals("", uf1.getValue());
126 });
127 }
128
129 @Test
130 public void testReproducibleUuid() {
131
132 String seedValue = UUID.randomUUID().toString();
133
134 var random1 = RandomUtil.createRandomFromSeed(seedValue);
135 FieldContext context1 = FieldContext.forTesting(random1);
136
137 UuidDefinition uuidDefinition = uuidDefinition("hello");
138 UuidField field1 = new UuidField(uuidDefinition, ValueProvider.NULL_PROVIDER, context1);
139 String value1 = field1.getValue();
140
141 var random2 = RandomUtil.createRandomFromSeed(seedValue);
142 FieldContext context2 = FieldContext.forTesting(random2);
143
144 UuidField field2 = new UuidField(uuidDefinition, ValueProvider.NULL_PROVIDER, context2);
145 String value2 = field2.getValue();
146
147 assertThat(value1).isEqualTo(value2);
148 }
149 }