From 0721a349c6176e98f99584fc4952617cf61bd34d Mon Sep 17 00:00:00 2001 From: John McClean Date: Fri, 26 Jun 2015 16:18:30 +0100 Subject: [PATCH 1/2] fix for https://github.com/aol/micro-server/issues/28 --- gradle.properties | 2 +- micro-core/application.properties | 1 + .../server/module/ConfigurableModule.java | 8 +++++++ .../spring/properties/PropertyFileConfig.java | 3 ++- .../async/com/aol/micro/server/Simple.java | 12 ++++++++-- .../server/module/ConfigurableModuleTest.java | 22 ++++++++++++++++++- 6 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 micro-core/application.properties diff --git a/gradle.properties b/gradle.properties index 291bf3594..b20f82638 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version=0.58 +version=0.59 diff --git a/micro-core/application.properties b/micro-core/application.properties new file mode 100644 index 000000000..f7184f073 --- /dev/null +++ b/micro-core/application.properties @@ -0,0 +1 @@ +test=hello world \ No newline at end of file diff --git a/micro-core/src/main/java/com/aol/micro/server/module/ConfigurableModule.java b/micro-core/src/main/java/com/aol/micro/server/module/ConfigurableModule.java index f39a58282..3ab5e3b74 100644 --- a/micro-core/src/main/java/com/aol/micro/server/module/ConfigurableModule.java +++ b/micro-core/src/main/java/com/aol/micro/server/module/ConfigurableModule.java @@ -32,6 +32,7 @@ public class ConfigurableModule implements Module{ private final List restResourceClasses; + private final List restAnnotationClasses; private final List defaultResources; private final List listeners; private final List requestListeners; @@ -78,6 +79,13 @@ public List getRestResourceClasses() { return Module.super.getRestResourceClasses(); } + @Override + public List getRestAnnotationClasses() { + if(restAnnotationClasses!=null) + return ImmutableList.copyOf(Iterables.concat(restAnnotationClasses, extract(() -> Module.super.getRestAnnotationClasses()))); + + return Module.super.getRestAnnotationClasses(); + } @Override public List getDefaultResources() { diff --git a/micro-core/src/main/java/com/aol/micro/server/spring/properties/PropertyFileConfig.java b/micro-core/src/main/java/com/aol/micro/server/spring/properties/PropertyFileConfig.java index 6f17ef6a5..4e5adb4b6 100644 --- a/micro-core/src/main/java/com/aol/micro/server/spring/properties/PropertyFileConfig.java +++ b/micro-core/src/main/java/com/aol/micro/server/spring/properties/PropertyFileConfig.java @@ -17,6 +17,7 @@ import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; +import com.aol.micro.server.config.Config; import com.aol.micro.server.config.ConfigAccessor; import com.google.common.collect.Lists; @@ -53,7 +54,7 @@ public Properties propertyFactory() throws IOException { private List loadPropertyResource() { List resources = Lists.newArrayList(); loadProperties().ifPresent(it -> resources.add(it)); - + new Config().set(); //make sure config instance is set String instancePropertyFileName = new ConfigAccessor().get().getInstancePropertiesName(); URL instanceResource = getClass().getClassLoader().getResource(instancePropertyFileName); diff --git a/micro-core/src/test/java/app/async/com/aol/micro/server/Simple.java b/micro-core/src/test/java/app/async/com/aol/micro/server/Simple.java index 377921fc1..137f92da9 100644 --- a/micro-core/src/test/java/app/async/com/aol/micro/server/Simple.java +++ b/micro-core/src/test/java/app/async/com/aol/micro/server/Simple.java @@ -1,10 +1,18 @@ package app.async.com.aol.micro.server; +import java.io.IOException; +import java.util.Properties; + import com.aol.micro.server.MicroserverApp; +import com.aol.micro.server.config.Config; +import com.aol.micro.server.spring.properties.PropertyFileConfig; public class Simple { - public static void main(String[] args){ - new MicroserverApp(()->"test-app").run(); + public static void main(String[] args) throws IOException{ + new Config().set() ; + Properties props = new PropertyFileConfig().propertyFactory() ; + System.out.println(props); + // new MicroserverApp(()->"test-app").run(); } } diff --git a/micro-core/src/test/java/com/aol/micro/server/module/ConfigurableModuleTest.java b/micro-core/src/test/java/com/aol/micro/server/module/ConfigurableModuleTest.java index 0dc658ac9..aff088cf8 100644 --- a/micro-core/src/test/java/com/aol/micro/server/module/ConfigurableModuleTest.java +++ b/micro-core/src/test/java/com/aol/micro/server/module/ConfigurableModuleTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.*; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -20,6 +21,7 @@ import org.junit.Before; import org.junit.Test; +import com.aol.micro.server.auto.discovery.Rest; import com.aol.micro.server.auto.discovery.RestResource; import com.aol.micro.server.servers.model.ServerData; import com.aol.micro.server.web.filter.QueryIPRetriever; @@ -39,6 +41,7 @@ public class ConfigurableModuleTest { private List requestListeners; private String providers; private List resourceClasses; + private List resourceAnnotationClasses; private Map servlets; private Set springConfigurationClasses; private List defaultJaxRsPackages; @@ -58,6 +61,7 @@ public void setup(){ providers = "providers2"; Module m = () -> "hello"; resourceClasses =new ArrayList<>(); + resourceAnnotationClasses = Arrays.asList(Rest.class); servlets = new HashMap<>(); springConfigurationClasses = ImmutableSet.of(this.getClass()); @@ -107,6 +111,19 @@ public void testGetRestResourceClasses() { public void testGetRestResourceClassesUnchanged() { assertThat(unchanged.getRestResourceClasses(),is(m.getRestResourceClasses())); } + @Test + public void testGetRestAnnotationClassesResetAll() { + assertThat(module.withResetAll(true).getRestAnnotationClasses(),is(resourceAnnotationClasses)); + } + @Test + public void testGetRestAnnotationClasses() { + assertThat(module.getRestAnnotationClasses(),hasItem(Rest.class)); + } + + @Test + public void testGetRestAnnotationClassesUnchanged() { + assertThat(unchanged.getRestAnnotationClasses(),is(m.getRestAnnotationClasses())); + } @Test public void testGetDefaultResourcesReset() { @@ -225,7 +242,10 @@ public void testGetSpringConfigurationClassesUnchanged() { - + @Test + public void testWithResourceAnnotationClasses() { + assertThat(unchanged.withRestAnnotationClasses(this.resourceClasses).getRestAnnotationClasses(),is(module.getRestAnnotationClasses())); + } @Test public void testWithResourceClasses() { From af4eafd515b4b3fbd7002585a0d3f8116fc1b70f Mon Sep 17 00:00:00 2001 From: John McClean Date: Fri, 26 Jun 2015 16:29:39 +0100 Subject: [PATCH 2/2] fix failing tests --- .../server/spring/properties/PropertyFileConfig.java | 10 ++++++++-- .../async/com/aol/micro/server/AsyncAppRunner.java | 11 +++++++++++ .../java/app/async/com/aol/micro/server/Simple.java | 6 ++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/micro-core/src/main/java/com/aol/micro/server/spring/properties/PropertyFileConfig.java b/micro-core/src/main/java/com/aol/micro/server/spring/properties/PropertyFileConfig.java index 4e5adb4b6..87ac3022f 100644 --- a/micro-core/src/main/java/com/aol/micro/server/spring/properties/PropertyFileConfig.java +++ b/micro-core/src/main/java/com/aol/micro/server/spring/properties/PropertyFileConfig.java @@ -25,7 +25,13 @@ public class PropertyFileConfig { private final Logger logger = LoggerFactory.getLogger(getClass()); - + public PropertyFileConfig(){ + + } + public PropertyFileConfig(boolean set){ + if(set) + new Config().set(); //make sure config instance is set + } @Bean public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws IOException { @@ -54,7 +60,7 @@ public Properties propertyFactory() throws IOException { private List loadPropertyResource() { List resources = Lists.newArrayList(); loadProperties().ifPresent(it -> resources.add(it)); - new Config().set(); //make sure config instance is set + String instancePropertyFileName = new ConfigAccessor().get().getInstancePropertiesName(); URL instanceResource = getClass().getClassLoader().getResource(instancePropertyFileName); diff --git a/micro-core/src/test/java/app/async/com/aol/micro/server/AsyncAppRunner.java b/micro-core/src/test/java/app/async/com/aol/micro/server/AsyncAppRunner.java index bca77babd..c9123a181 100644 --- a/micro-core/src/test/java/app/async/com/aol/micro/server/AsyncAppRunner.java +++ b/micro-core/src/test/java/app/async/com/aol/micro/server/AsyncAppRunner.java @@ -3,6 +3,8 @@ import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import java.io.IOException; +import java.util.Properties; import java.util.concurrent.ExecutionException; import org.junit.After; @@ -10,7 +12,9 @@ import org.junit.Test; import com.aol.micro.server.MicroserverApp; +import com.aol.micro.server.config.Config; import com.aol.micro.server.config.Microserver; +import com.aol.micro.server.spring.properties.PropertyFileConfig; import com.aol.micro.server.testing.RestAgent; @Microserver @@ -42,6 +46,13 @@ public void runAppAndBasicTest() throws InterruptedException, ExecutionException } + @Test + public void loadProperties() throws IOException{ + + Properties props = new PropertyFileConfig(true).propertyFactory() ; + assertThat(props.getProperty("test"),is("hello world")); + } + } \ No newline at end of file diff --git a/micro-core/src/test/java/app/async/com/aol/micro/server/Simple.java b/micro-core/src/test/java/app/async/com/aol/micro/server/Simple.java index 137f92da9..edefa80ef 100644 --- a/micro-core/src/test/java/app/async/com/aol/micro/server/Simple.java +++ b/micro-core/src/test/java/app/async/com/aol/micro/server/Simple.java @@ -10,9 +10,7 @@ public class Simple { public static void main(String[] args) throws IOException{ - new Config().set() ; - Properties props = new PropertyFileConfig().propertyFactory() ; - System.out.println(props); - // new MicroserverApp(()->"test-app").run(); + + new MicroserverApp(()->"test-app").run(); } }