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.definitions;
16  
17  import static org.assertj.core.api.Assertions.assertThat;
18  import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.numberDefinition;
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import org.basepom.mojo.propertyhelper.IgnoreWarnFail;
24  import org.basepom.mojo.propertyhelper.IgnoreWarnFailCreate;
25  
26  import java.util.UUID;
27  
28  import org.junit.jupiter.api.Test;
29  
30  
31  public class TestNumberDefinition {
32  
33      @Test
34      public void testValid() {
35          final NumberDefinition nd = numberDefinition("hello");
36          nd.export = true;
37          nd.initialValue = "1";
38  
39          nd.check();
40      }
41  
42      @Test
43      public void testValid2() {
44          final NumberDefinition nd = new NumberDefinition();
45          nd.id = "hello";
46          nd.check();
47      }
48  
49      @Test
50      public void testDefaults() {
51          final String id = UUID.randomUUID().toString();
52          final NumberDefinition nd = numberDefinition(id);
53  
54          nd.check();
55          assertEquals(id, nd.getId());
56          assertEquals("0", nd.getInitialValue().get());
57  
58          assertThat(nd.getFieldNumber()).isEmpty();
59  
60          assertEquals(1, nd.getIncrement());
61          assertEquals(id, nd.getId());
62          assertFalse(nd.getPropertyFile().isPresent());
63          assertEquals(IgnoreWarnFailCreate.FAIL, nd.getOnMissingFile());
64          assertEquals(IgnoreWarnFailCreate.FAIL, nd.getOnMissingFileProperty());
65          assertEquals(IgnoreWarnFail.FAIL, nd.getOnMissingProperty());
66          assertFalse(nd.isExport());
67      }
68  
69      @Test
70      public void testPropNameOverridesId() {
71          final NumberDefinition nd = numberDefinition("hello");
72          nd.propertyNameInFile = "world";
73  
74          assertEquals("hello", nd.getId());
75          assertEquals("world", nd.getPropertyNameInFile());
76      }
77  
78      @Test
79      public void testIdSuppliesPropName() {
80          final NumberDefinition nd = numberDefinition("hello");
81  
82          assertEquals("hello", nd.getId());
83          assertEquals("hello", nd.getPropertyNameInFile());
84      }
85  
86      @Test
87      public void testNullInitialValue() {
88          assertThrows(IllegalStateException.class, () -> {
89              final NumberDefinition nd = new NumberDefinition();
90              nd.initialValue = null;
91  
92              nd.check();
93          });
94      }
95  
96      @Test
97      public void testBlankInitialValue() {
98          assertThrows(IllegalStateException.class, () -> {
99              final NumberDefinition nd = new NumberDefinition();
100             nd.initialValue = "";
101 
102             nd.check();
103         });
104     }
105 
106     @Test
107     public void testBadFieldNumber() {
108         assertThrows(IllegalStateException.class, () -> {
109             final NumberDefinition nd = new NumberDefinition();
110             nd.fieldNumber = -1;
111 
112             nd.check();
113         });
114     }
115 }