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.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setOnMissingFileProperty;
018import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setValue;
019import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.uuidDefinition;
020import static org.junit.jupiter.api.Assertions.assertThrows;
021
022import org.basepom.mojo.propertyhelper.ValueCache;
023import org.basepom.mojo.propertyhelper.ValueProvider;
024import org.basepom.mojo.propertyhelper.ValueProvider.PropertyBackedValueAdapter;
025import org.basepom.mojo.propertyhelper.ValueProvider.SingleValueProvider;
026import org.basepom.mojo.propertyhelper.definitions.UuidDefinition;
027
028import java.util.Properties;
029import java.util.UUID;
030
031import com.google.common.collect.ImmutableMap;
032import org.junit.jupiter.api.Assertions;
033import org.junit.jupiter.api.Test;
034
035public class TestUuidField {
036
037    @Test
038    public void testSimple() {
039        final UUID uuid = UUID.randomUUID();
040        final UuidDefinition uuidFieldDefinition = uuidDefinition("hello");
041        setValue(uuidFieldDefinition, uuid.toString());
042
043        uuidFieldDefinition.check();
044
045        final UuidField uf1 = UuidField.forTesting(uuidFieldDefinition, ValueProvider.NULL_PROVIDER);
046        Assertions.assertEquals(uuid.toString(), uf1.getValue());
047    }
048
049    @Test
050    public void testSimpleProperty() {
051        final UUID uuid = UUID.randomUUID();
052        final UuidDefinition uuidDefinition = uuidDefinition("hello");
053
054        uuidDefinition.check();
055
056        final Properties props = new Properties();
057        props.setProperty("hello", uuid.toString());
058        final UuidField uf1 = UuidField.forTesting(uuidDefinition, new PropertyBackedValueAdapter(props, uuidDefinition.getId()));
059        Assertions.assertEquals(uuid.toString(), uf1.getValue());
060    }
061
062    @Test
063    public void testSimplePropertyWithDefault() {
064        final UUID uuid1 = UUID.randomUUID();
065        final UUID uuid2 = UUID.randomUUID();
066
067        final UuidDefinition uuidDefinition = uuidDefinition("hello");
068        setValue(uuidDefinition, uuid1.toString());
069
070        uuidDefinition.check();
071
072        final Properties props = new Properties();
073        props.setProperty("hello", uuid2.toString());
074        final UuidField uf1 = UuidField.forTesting(uuidDefinition, new PropertyBackedValueAdapter(props, uuidDefinition.getId()));
075        Assertions.assertEquals(uuid2.toString(), uf1.getValue());
076    }
077
078    @Test
079    public void testNoProperty() {
080        final UUID uuid1 = UUID.randomUUID();
081        final UUID uuid2 = UUID.randomUUID();
082
083        final UuidDefinition uuidDefinition = uuidDefinition("hello");
084        setValue(uuidDefinition, uuid1.toString());
085
086        uuidDefinition.check();
087
088        final Properties props = new Properties();
089        props.setProperty("hello2", uuid2.toString());
090        final UuidField uf1 = UuidField.forTesting(uuidDefinition, new PropertyBackedValueAdapter(props, uuidDefinition.getId()));
091        Assertions.assertEquals(uuid1.toString(), uf1.getValue());
092    }
093
094    @Test
095    public void testNothing() {
096        final UUID uuid = UUID.randomUUID();
097
098        final UuidDefinition uuidDefinition = uuidDefinition("hello");
099
100        uuidDefinition.check();
101
102        final ValueProvider provider = new SingleValueProvider();
103        provider.setValue(uuid.toString());
104        final UuidField uf1 = UuidField.forTesting(uuidDefinition, provider);
105        Assertions.assertEquals(uuid.toString(), uf1.getValue());
106    }
107
108    @Test
109    public void testMissingProperty() {
110        ValueCache valueCache = new ValueCache();
111
112        assertThrows(IllegalStateException.class, () -> {
113            final UuidDefinition uuidDefinition = uuidDefinition("hello");
114            setOnMissingFileProperty(uuidDefinition, "fail");
115
116            uuidDefinition.check();
117
118            final ValueProvider provider = valueCache.findCurrentValueProvider(ImmutableMap.of(), uuidDefinition);
119
120            final UuidField uf1 = UuidField.forTesting(uuidDefinition, provider);
121            Assertions.assertEquals("", uf1.getValue());
122        });
123    }
124}