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.numberDefinition;
19 import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setFieldNumber;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21
22 import org.basepom.mojo.propertyhelper.ValueProvider.PropertyBackedValueAdapter;
23 import org.basepom.mojo.propertyhelper.definitions.NumberDefinition;
24 import org.basepom.mojo.propertyhelper.fields.NumberField;
25
26 import java.util.Properties;
27
28 import org.junit.jupiter.api.Test;
29
30
31 public class TestNumberField {
32
33 @Test
34 public void testSimple() {
35 final NumberDefinition f1 = numberDefinition("hello");
36 setFieldNumber(f1, 0);
37 f1.check();
38
39 final Properties props = new Properties();
40 props.setProperty("hello", "100");
41 final NumberField nf1 = NumberField.forTesting(f1, new PropertyBackedValueAdapter(props, f1.getId()));
42 assertThat(nf1.getNumberValue()).isPresent().contains(100L);
43
44 assertThat(nf1.getValue()).isEqualTo("100");
45 assertThat(props).extracting("hello").isEqualTo("100");
46 }
47
48 @Test
49 public void testThreeElements() {
50 final NumberDefinition f1 = numberDefinition("hello");
51 setFieldNumber(f1, 0);
52 final NumberDefinition f2 = numberDefinition("hello");
53 setFieldNumber(f2, 1);
54 final NumberDefinition f3 = numberDefinition("hello");
55 setFieldNumber(f3, 2);
56 f1.check();
57 f2.check();
58 f3.check();
59
60 final String value = "4.8.15";
61
62 final Properties props = new Properties();
63 props.setProperty("hello", value);
64
65 final NumberField nf1 = NumberField.forTesting(f1, new PropertyBackedValueAdapter(props, f1.getId()));
66 final NumberField nf2 = NumberField.forTesting(f2, new PropertyBackedValueAdapter(props, f2.getId()));
67 final NumberField nf3 = NumberField.forTesting(f3, new PropertyBackedValueAdapter(props, f3.getId()));
68 assertThat(nf1.getNumberValue()).isPresent().contains(4L);
69 assertThat(nf2.getNumberValue()).isPresent().contains(8L);
70 assertThat(nf3.getNumberValue()).isPresent().contains(15L);
71
72 assertEquals("4", nf1.getValue());
73 assertEquals("8", nf2.getValue());
74 assertEquals("15", nf3.getValue());
75 assertThat(props).extracting("hello").isEqualTo("4.8.15");
76 }
77
78 @Test
79 public void testItsComplicated() {
80 final NumberDefinition f1 = numberDefinition("hello");
81 setFieldNumber(f1, 0);
82 final NumberDefinition f2 = numberDefinition("hello");
83 setFieldNumber(f2, 1);
84 final NumberDefinition f3 = numberDefinition("hello");
85 setFieldNumber(f3, 2);
86 final NumberDefinition f4 = numberDefinition("hello");
87 setFieldNumber(f4, 3);
88 f1.check();
89 f2.check();
90 f3.check();
91 f4.check();
92
93 final String value = "3.2-alpha-1-test-4";
94
95 final Properties props = new Properties();
96 props.setProperty("hello", value);
97
98 final NumberField nf1 = NumberField.forTesting(f1, new PropertyBackedValueAdapter(props, f1.getId()));
99 final NumberField nf2 = NumberField.forTesting(f2, new PropertyBackedValueAdapter(props, f2.getId()));
100 final NumberField nf3 = NumberField.forTesting(f3, new PropertyBackedValueAdapter(props, f3.getId()));
101 final NumberField nf4 = NumberField.forTesting(f4, new PropertyBackedValueAdapter(props, f4.getId()));
102 assertThat(nf1.getNumberValue()).isPresent().contains(3L);
103 assertThat(nf2.getNumberValue()).isPresent().contains(2L);
104 assertThat(nf3.getNumberValue()).isPresent().contains(1L);
105 assertThat(nf4.getNumberValue()).isPresent().contains(4L);
106
107 assertEquals("3", nf1.getValue());
108 assertEquals("2", nf2.getValue());
109 assertEquals("1", nf3.getValue());
110 assertEquals("4", nf4.getValue());
111
112 assertThat(props).extracting("hello").isEqualTo(value);
113 }
114
115 @Test
116 public void testIncrementSingle() {
117 final NumberDefinition f1 = numberDefinition("hello");
118 setFieldNumber(f1, 0);
119
120 final String value = "foobar-1.2-barfoo-3";
121
122 final Properties props = new Properties();
123 props.setProperty("hello", value);
124
125 final NumberField nf1 = NumberField.forTesting(f1, new PropertyBackedValueAdapter(props, f1.getId()));
126
127 assertThat(nf1.getNumberValue()).isPresent().contains(1L);
128 assertEquals("1", nf1.getValue());
129
130 assertThat(props).extracting("hello").isEqualTo(value);
131
132 nf1.increment();
133
134 assertThat(nf1.getNumberValue()).isPresent().contains(2L);
135 assertEquals("2", nf1.getValue());
136
137 assertThat(props).extracting("hello").isEqualTo("foobar-2.2-barfoo-3");
138 }
139
140 @Test
141 public void testIncrement2() {
142 final NumberDefinition f1 = numberDefinition("hello");
143 setFieldNumber(f1, 0);
144 final NumberDefinition f2 = numberDefinition("hello");
145 setFieldNumber(f2, 1);
146 f1.check();
147 f2.check();
148
149 final String value = "4.8";
150
151 final Properties props = new Properties();
152 props.setProperty("hello", value);
153
154 final NumberField nf1 = NumberField.forTesting(f1, new PropertyBackedValueAdapter(props, f1.getId()));
155 final NumberField nf2 = NumberField.forTesting(f2, new PropertyBackedValueAdapter(props, f2.getId()));
156
157 assertThat(nf1.getNumberValue()).isPresent().contains(4L);
158 assertThat(nf2.getNumberValue()).isPresent().contains(8L);
159
160 assertEquals("4", nf1.getValue());
161 assertEquals("8", nf2.getValue());
162
163 assertThat(props).extracting("hello").isEqualTo(value);
164
165 nf1.increment();
166
167 assertThat(nf1.getNumberValue()).isPresent().contains(5L);
168 assertThat(nf2.getNumberValue()).isPresent().contains(8L);
169 assertEquals("5", nf1.getValue());
170 assertEquals("8", nf2.getValue());
171
172 assertThat(props).extracting("hello").isEqualTo("5.8");
173
174 nf2.increment();
175
176 assertThat(nf1.getNumberValue()).isPresent().contains(5L);
177 assertThat(nf2.getNumberValue()).isPresent().contains(9L);
178 assertEquals("5", nf1.getValue());
179 assertEquals("9", nf2.getValue());
180
181 assertThat(props).extracting("hello").isEqualTo("5.9");
182 }
183
184 @Test
185 public void testDoubleIncrement() {
186 final NumberDefinition f1 = numberDefinition("hello");
187 setFieldNumber(f1, 0);
188 final NumberDefinition f2 = numberDefinition("hello");
189 setFieldNumber(f2, 0);
190 f1.check();
191 f2.check();
192
193 final String value = "4.8";
194
195 final Properties props = new Properties();
196 props.setProperty("hello", value);
197
198 final NumberField nf1 = NumberField.forTesting(f1, new PropertyBackedValueAdapter(props, f1.getId()));
199 final NumberField nf2 = NumberField.forTesting(f2, new PropertyBackedValueAdapter(props, f2.getId()));
200 assertThat(nf1.getNumberValue()).isPresent().contains(4L);
201 assertThat(nf2.getNumberValue()).isPresent().contains(4L);
202
203 assertEquals("4", nf1.getValue());
204 assertEquals("4", nf2.getValue());
205
206 assertThat(props).extracting("hello").isEqualTo(value);
207
208 nf1.increment();
209
210 assertThat(nf1.getNumberValue()).isPresent().contains(5L);
211 assertThat(nf2.getNumberValue()).isPresent().contains(5L);
212 assertEquals("5", nf1.getValue());
213 assertEquals("5", nf2.getValue());
214
215 assertThat(props).extracting("hello").isEqualTo("5.8");
216
217 nf2.increment();
218
219 assertThat(nf1.getNumberValue()).isPresent().contains(6L);
220 assertThat(nf2.getNumberValue()).isPresent().contains(6L);
221
222 assertEquals("6", nf1.getValue());
223 assertEquals("6", nf2.getValue());
224
225 assertThat(props).extracting("hello").isEqualTo("6.8");
226 }
227 }
228