From 875829f33244c715cadf99073c6d63e0e779cec2 Mon Sep 17 00:00:00 2001 From: djr4488 Date: Thu, 27 Sep 2018 08:34:18 -0500 Subject: [PATCH] added - a test case around property resolution --- .../java/org/djr/cdi/properties/Config.java | 3 ++- .../djr/cdi/properties/PropertyResolver.java | 1 - .../cdi/properties/PropertyResolverTest.java | 25 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/djr/cdi/properties/Config.java b/src/main/java/org/djr/cdi/properties/Config.java index 69f2bc0..95dc0cd 100644 --- a/src/main/java/org/djr/cdi/properties/Config.java +++ b/src/main/java/org/djr/cdi/properties/Config.java @@ -15,6 +15,7 @@ */ package org.djr.cdi.properties; +import org.djr.cdi.properties.decrypt.Decryptor; import org.djr.cdi.properties.decrypt.DefaultDecryptor; import javax.enterprise.util.Nonbinding; @@ -42,5 +43,5 @@ @Nonbinding boolean isEncrypted() default false; @Nonbinding - Class defaultDecryptor() default DefaultDecryptor.class; + Class defaultDecryptor() default DefaultDecryptor.class; } \ No newline at end of file diff --git a/src/main/java/org/djr/cdi/properties/PropertyResolver.java b/src/main/java/org/djr/cdi/properties/PropertyResolver.java index ea4c071..b1870ad 100644 --- a/src/main/java/org/djr/cdi/properties/PropertyResolver.java +++ b/src/main/java/org/djr/cdi/properties/PropertyResolver.java @@ -30,7 +30,6 @@ import javax.enterprise.inject.spi.InjectionPoint; import javax.inject.Inject; import java.util.Arrays; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; diff --git a/src/test/java/org/djr/cdi/properties/PropertyResolverTest.java b/src/test/java/org/djr/cdi/properties/PropertyResolverTest.java index 790fbbc..470c8e7 100644 --- a/src/test/java/org/djr/cdi/properties/PropertyResolverTest.java +++ b/src/test/java/org/djr/cdi/properties/PropertyResolverTest.java @@ -26,9 +26,12 @@ import org.jboss.weld.junit5.auto.EnableAlternatives; import org.jboss.weld.junit5.auto.EnableAutoWeld; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import org.mockito.Mock; import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.Annotated; +import javax.enterprise.inject.spi.InjectionPoint; import javax.inject.Inject; import java.util.List; @@ -37,7 +40,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @EnableAutoWeld @AddBeanClasses({ PropertyResolver.class, Config.class, FilePropertiesLoader.class, EntityManagerProducer.class, @@ -50,6 +56,9 @@ public class PropertyResolverTest { @DatabasePropertyEM private EntityManagerProducer entityManagerProducer; + @Inject + private PropertyResolver propertyResolver; + @Inject @Config(defaultValue = "testDefaultPropertyValue") private String testProperty; @@ -154,6 +163,9 @@ public class PropertyResolverTest { @Config(propertyName = "PropertyResolverTest_doubleVal", defaultValue = "1.2") private Double doubleTest; + @Config(propertyName = "undefinedProperty") + private String undefinedProperty; + @Test public void testPropertySetWhenInProperties() { assertNotNull(testProperty); @@ -207,4 +219,17 @@ public void testDefaultPropertyWhenNotInPropertyConfigs() { assertEquals(1.1f, floatTest.floatValue()); assertEquals(1.2d, doubleTest.doubleValue()); } + + @Test + public void propertyResolverWhenPropertyNotFound() { + final Executable executable = () -> { + InjectionPoint injectionPoint = mock(InjectionPoint.class); + Config config = this.getClass().getDeclaredField("undefinedProperty").getAnnotation(Config.class); + Annotated annotated = mock(Annotated.class); + when(injectionPoint.getAnnotated()).thenReturn(annotated); + when(annotated.getAnnotation(Config.class)).thenReturn(config); + propertyResolver.getProperty("undefinedProperty", injectionPoint); + }; + assertThrows(PropertyLoadException.class, executable); + } }