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.fields; 016 017import static org.assertj.core.api.Assertions.assertThat; 018import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.numberDefinition; 019import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setFieldNumber; 020import static org.junit.jupiter.api.Assertions.assertEquals; 021 022import org.basepom.mojo.propertyhelper.ValueProvider.PropertyBackedValueAdapter; 023import org.basepom.mojo.propertyhelper.definitions.NumberDefinition; 024import org.basepom.mojo.propertyhelper.fields.NumberField; 025 026import java.util.Properties; 027 028import org.junit.jupiter.api.Test; 029 030 031public class TestNumberField { 032 033 @Test 034 public void testSimple() { 035 final NumberDefinition f1 = numberDefinition("hello"); 036 setFieldNumber(f1, 0); 037 f1.check(); 038 039 final Properties props = new Properties(); 040 props.setProperty("hello", "100"); 041 final NumberField nf1 = NumberField.forTesting(f1, new PropertyBackedValueAdapter(props, f1.getId())); 042 assertThat(nf1.getNumberValue()).isPresent().contains(100L); 043 044 assertThat(nf1.getValue()).isEqualTo("100"); 045 assertThat(props).extracting("hello").isEqualTo("100"); 046 } 047 048 @Test 049 public void testThreeElements() { 050 final NumberDefinition f1 = numberDefinition("hello"); 051 setFieldNumber(f1, 0); 052 final NumberDefinition f2 = numberDefinition("hello"); 053 setFieldNumber(f2, 1); 054 final NumberDefinition f3 = numberDefinition("hello"); 055 setFieldNumber(f3, 2); 056 f1.check(); 057 f2.check(); 058 f3.check(); 059 060 final String value = "4.8.15"; 061 062 final Properties props = new Properties(); 063 props.setProperty("hello", value); 064 065 final NumberField nf1 = NumberField.forTesting(f1, new PropertyBackedValueAdapter(props, f1.getId())); 066 final NumberField nf2 = NumberField.forTesting(f2, new PropertyBackedValueAdapter(props, f2.getId())); 067 final NumberField nf3 = NumberField.forTesting(f3, new PropertyBackedValueAdapter(props, f3.getId())); 068 assertThat(nf1.getNumberValue()).isPresent().contains(4L); 069 assertThat(nf2.getNumberValue()).isPresent().contains(8L); 070 assertThat(nf3.getNumberValue()).isPresent().contains(15L); 071 072 assertEquals("4", nf1.getValue()); 073 assertEquals("8", nf2.getValue()); 074 assertEquals("15", nf3.getValue()); 075 assertThat(props).extracting("hello").isEqualTo("4.8.15"); 076 } 077 078 @Test 079 public void testItsComplicated() { 080 final NumberDefinition f1 = numberDefinition("hello"); 081 setFieldNumber(f1, 0); 082 final NumberDefinition f2 = numberDefinition("hello"); 083 setFieldNumber(f2, 1); 084 final NumberDefinition f3 = numberDefinition("hello"); 085 setFieldNumber(f3, 2); 086 final NumberDefinition f4 = numberDefinition("hello"); 087 setFieldNumber(f4, 3); 088 f1.check(); 089 f2.check(); 090 f3.check(); 091 f4.check(); 092 093 final String value = "3.2-alpha-1-test-4"; 094 095 final Properties props = new Properties(); 096 props.setProperty("hello", value); 097 098 final NumberField nf1 = NumberField.forTesting(f1, new PropertyBackedValueAdapter(props, f1.getId())); 099 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