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.definitions;
016
017import static org.assertj.core.api.Assertions.assertThat;
018import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.numberDefinition;
019import static org.junit.jupiter.api.Assertions.assertEquals;
020import static org.junit.jupiter.api.Assertions.assertFalse;
021import static org.junit.jupiter.api.Assertions.assertThrows;
022
023import org.basepom.mojo.propertyhelper.IgnoreWarnFail;
024import org.basepom.mojo.propertyhelper.IgnoreWarnFailCreate;
025
026import java.util.UUID;
027
028import org.junit.jupiter.api.Test;
029
030
031public class TestNumberDefinition {
032
033    @Test
034    public void testValid() {
035        final NumberDefinition nd = numberDefinition("hello");
036        nd.export = true;
037        nd.initialValue = "1";
038
039        nd.check();
040    }
041
042    @Test
043    public void testValid2() {
044        final NumberDefinition nd = new NumberDefinition();
045        nd.id = "hello";
046        nd.check();
047    }
048
049    @Test
050    public void testDefaults() {
051        final String id = UUID.randomUUID().toString();
052        final NumberDefinition nd = numberDefinition(id);
053
054        nd.check();
055        assertEquals(id, nd.getId());
056        assertEquals("0", nd.getInitialValue().get());
057
058        assertThat(nd.getFieldNumber()).isEmpty();
059
060        assertEquals(1, nd.getIncrement());
061        assertEquals(id, nd.getId());
062        assertFalse(nd.getPropertyFile().isPresent());
063        assertEquals(IgnoreWarnFailCreate.FAIL, nd.getOnMissingFile());
064        assertEquals(IgnoreWarnFailCreate.FAIL, nd.getOnMissingFileProperty());
065        assertEquals(IgnoreWarnFail.FAIL, nd.getOnMissingProperty());
066        assertFalse(nd.isExport());
067    }
068
069    @Test
070    public void testPropNameOverridesId() {
071        final NumberDefinition nd = numberDefinition("hello");
072        nd.propertyNameInFile = "world";
073
074        assertEquals("hello", nd.getId());
075        assertEquals("world", nd.getPropertyNameInFile());
076    }
077
078    @Test
079    public void testIdSuppliesPropName() {
080        final NumberDefinition nd = numberDefinition("hello");
081
082        assertEquals("hello", nd.getId());
083        assertEquals("hello", nd.getPropertyNameInFile());
084    }
085
086    @Test
087    public void testNullInitialValue() {
088        assertThrows(IllegalStateException.class, () -> {
089            final NumberDefinition nd = new NumberDefinition();
090            nd.initialValue = null;
091
092            nd.check();
093        });
094    }
095
096    @Test
097    public void testBlankInitialValue() {
098        assertThrows(IllegalStateException.class, () -> {
099            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}