1515 */
1616package io .fabric8 .maven .core .util ;
1717
18- import java .util .*;
19-
2018import io .fabric8 .utils .PropertiesHelper ;
21-
19+ import org .apache .maven .model .Build ;
20+ import org .apache .maven .project .MavenProject ;
21+ import org .codehaus .plexus .util .FileUtils ;
2222import org .junit .Test ;
23-
24- import static org .junit .Assert .assertEquals ;
25- import static org .junit .Assert .assertNotEquals ;
26- import static org .junit .Assert .assertNotNull ;
23+ import org .springframework .boot .Banner ;
24+ import org .springframework .boot .SpringApplication ;
25+ import org .springframework .boot .builder .SpringApplicationBuilder ;
26+ import org .springframework .context .ConfigurableApplicationContext ;
27+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
28+ import org .springframework .core .env .MapPropertySource ;
29+ import org .springframework .core .env .PropertySource ;
30+ import org .springframework .util .ResourceUtils ;
31+
32+ import java .io .File ;
33+ import java .io .IOException ;
34+ import java .net .URL ;
35+ import java .nio .file .Files ;
36+ import java .util .Collections ;
37+ import java .util .Properties ;
38+ import java .util .UUID ;
39+
40+ import static org .junit .Assert .*;
2741
2842/**
2943 * Checking the behaviour of utility methods.
3044 */
3145public class SpringBootUtilTest {
3246
33-
3447 @ Test
35- public void testYamlToPropertiesParsing () {
48+ public void testYamlToPropertiesParsing () throws Exception {
49+
50+ MavenProject project = new MavenProject ();
51+ Build build = new Build ();
52+
53+ setMavenProject (project , build );
54+
55+ URL testAppPropertyResource = SpringBootUtilTest .class .getResource ("/util/test-application.yml" );
56+
57+ FileUtils .copyFile (ResourceUtils .getFile (testAppPropertyResource ), new File ("target/test-classes" ,
58+ "application.yml" ));
59+
60+ Properties props = SpringBootUtil .getApplicationProperties (project , Collections .<String >emptyList ());
3661
37- Properties props = SpringBootUtil .getPropertiesFromYamlResource (
38- SpringBootUtilTest .class .getResource ("/util/test-application.yml" ), Collections .<String >emptyList ());
3962 assertNotEquals (0 , props .size ());
4063
4164 assertEquals (new Integer (8081 ), PropertiesHelper .getInteger (props , "management.port" ));
@@ -48,55 +71,154 @@ public void testYamlToPropertiesParsing() {
4871 }
4972
5073 @ Test
51- public void testYamlToPropertiesParsingWithActiveProfiles () {
74+ public void testYamlToPropertiesMerge () throws Exception {
75+
76+ MavenProject project = new MavenProject ();
77+ Build build = new Build ();
78+
79+ setMavenProject (project , build );
80+
81+ URL testAppPropertyResource = SpringBootUtilTest .class .getResource ("/util/test-application-merge-multi.yml" );
5282
53- List < String > activeProfiles = new ArrayList < String >() {{
54- add ( "dev" );
55- add ( "qa" );
56- }} ;
83+ FileUtils . copyFile ( ResourceUtils . getFile ( testAppPropertyResource ), new File ( "target/test-classes" ,
84+ "application.yml" ), "UTF-8" , null , true );
85+
86+ Properties props = SpringBootUtil . getApplicationProperties ( project , Collections .< String > emptyList ()) ;
5787
58- Properties props = SpringBootUtil .getPropertiesFromYamlResource (
59- SpringBootUtilTest .class .getResource ("/util/test-application-multi.yml" ), activeProfiles );
6088 assertNotEquals (0 , props .size ());
6189
6290 assertEquals (new Integer (9090 ), PropertiesHelper .getInteger (props , "server.port" ));
6391 assertEquals ("Hello" , props .getProperty ("my.name" ));
64- assertEquals ("Hola! " , props .getProperty ("their.name" ));
92+ assertEquals ("Foo " , props .getProperty ("their.name" ));
6593 }
6694
6795 @ Test
68- public void testYamlToPropertiesParsingWithActiveProfiles2 () {
96+ public void testWithDifferentConfigName () throws Exception {
97+
98+ System .setProperty ("spring.config.name" , "foo" );
99+
100+ MavenProject project = new MavenProject ();
101+ Build build = new Build ();
69102
70- List <String > activeProfiles = new ArrayList <String >() {{
71- add ("qa" );
72- add ("dev" );
73- }};
103+ setMavenProject (project , build );
104+
105+ URL testAppPropertyResource = SpringBootUtilTest .class .getResource ("/util/test-application-named.yml" );
106+
107+ FileUtils .copyFile (ResourceUtils .getFile (testAppPropertyResource ), new File ("target/test-classes" ,
108+ "foo.yml" ), "UTF-8" , null , true );
109+
110+ Properties props = SpringBootUtil .getApplicationProperties (project , Collections .<String >emptyList ());
74111
75- Properties props = SpringBootUtil .getPropertiesFromYamlResource (
76- SpringBootUtilTest .class .getResource ("/util/test-application-multi.yml" ), activeProfiles );
77112 assertNotEquals (0 , props .size ());
78113
114+ assertEquals (new Integer (9090 ), PropertiesHelper .getInteger (props , "server.port" ));
115+ assertEquals ("Foo" , props .getProperty ("their.name" ));
116+
117+ System .getProperties ().remove ("spring.config.name" );
118+ }
119+
120+ @ Test
121+ public void testPropertiesInclude () throws Exception {
122+
123+ MavenProject project = new MavenProject ();
124+ Build build = new Build ();
125+
126+ setMavenProject (project , build );
127+
128+ URL testAppPropertyResource = SpringBootUtilTest .class .getResource ("/util/test-application-include.yml" );
129+
130+ FileUtils .copyFile (ResourceUtils .getFile (testAppPropertyResource ), new File ("target/test-classes" ,
131+ "application.yml" ), "UTF-8" , null , true );
132+
133+ Properties props = SpringBootUtil .getApplicationProperties (project ,Collections .<String >emptyList ());
134+
135+ assertNotEquals (0 , props .size ());
136+
137+ assertEquals (new Integer (2020 ), PropertiesHelper .getInteger (props , "my.port" ));
138+ assertEquals ("bar" , props .getProperty ("my.name" ));
139+ assertEquals ("foo" , props .getProperty ("name" ));
140+ }
141+
142+
143+ @ Test
144+ public void testProfilePropertiesForDev () throws Exception {
145+
146+ MavenProject project = new MavenProject ();
147+ Build build = new Build ();
148+
149+ setMavenProject (project , build );
150+
151+ URL testAppPropertyResource = SpringBootUtilTest .class .getResource ("/util/test-application-multi.yml" );
152+
153+ FileUtils .copyFile (ResourceUtils .getFile (testAppPropertyResource ), new File ("target/test-classes" ,
154+ "application.yml" ), "UTF-8" , null , true );
155+
156+ Properties props = SpringBootUtil .getApplicationProperties (project ,"dev" );
157+
79158 assertEquals (new Integer (8080 ), PropertiesHelper .getInteger (props , "server.port" ));
80159 assertEquals ("Hello" , props .getProperty ("my.name" ));
81- assertEquals ("Hola!" , props .getProperty ("their.name" ));
82160 }
83161
84162 @ Test
85- public void testNonExistentYamlToPropertiesParsing () {
163+ public void testProfilePropertiesForQa () throws Exception {
164+
165+ MavenProject project = new MavenProject ();
166+ Build build = new Build ();
167+
168+ setMavenProject (project , build );
169+
170+ URL testAppPropertyResource = SpringBootUtilTest .class .getResource ("/util/test-application-multi.yml" );
86171
87- Properties props = SpringBootUtil .getPropertiesFromYamlResource (
88- SpringBootUtilTest .class .getResource ("/this-file-does-not-exist" )
89- , Collections .<String >emptyList ());
90- assertNotNull (props );
91- assertEquals (0 , props .size ());
172+ FileUtils .copyFile (ResourceUtils .getFile (testAppPropertyResource ), new File ("target/test-classes" ,
173+ "application.yml" ), "UTF-8" , null , true );
92174
175+ Properties props = SpringBootUtil .getApplicationProperties (project ,"qa" );
176+
177+ assertNotEquals (0 , props .size ());
178+
179+ assertEquals (new Integer (9090 ), PropertiesHelper .getInteger (props , "server.port" ));
180+ assertEquals ("Hola!" , props .getProperty ("their.name" ));
93181 }
94182
183+ // // @Test TODO SK Remove this after Roland Review - this will not happen at all
184+ // public void testNonExistentYamlToPropertiesParsing() throws Exception {
185+ //
186+ // Properties props = SpringBootUtil.getPropertiesFromYamlResource(
187+ // SpringBootUtilTest.class.getResource("/this-file-does-not-exist")
188+ // , null);
189+ //
190+ // MavenProject project = new MavenProject();
191+ // Build build = new Build();
192+ //
193+ // setMavenProject(project, build);
194+ //
195+ // URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/this-file-does-not-exist");
196+ //
197+ // FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
198+ // "application.yml"), "UTF-8", null, true);
199+ //
200+ // Properties props = SpringBootUtil.getApplicationProperties(project,"qa");
201+ // assertNotNull(props);
202+ // assertEquals(0, props.size());
203+ //
204+ // }
205+
95206 @ Test
96- public void testPropertiesParsing () {
207+ public void testPropertiesParsing () throws Exception {
208+
209+ MavenProject project = new MavenProject ();
210+ Build build = new Build ();
211+
212+ setMavenProject (project , build );
213+
214+ URL testAppPropertyResource = SpringBootUtilTest .class .getResource ("/util/test-application.properties" );
215+
216+ FileUtils .copyFile (ResourceUtils .getFile (testAppPropertyResource ), new File ("target/test-classes" ,
217+ "application.properties" ), "UTF-8" , null , true );
218+
219+ Properties props = SpringBootUtil .getApplicationProperties (project ,Collections .<String >emptyList ());
220+
97221
98- Properties props = SpringBootUtil .getPropertiesResource (
99- SpringBootUtilTest .class .getResource ("/util/test-application.properties" ));
100222 assertNotEquals (0 , props .size ());
101223
102224 assertEquals (new Integer (8081 ), PropertiesHelper .getInteger (props , "management.port" ));
@@ -106,13 +228,21 @@ public void testPropertiesParsing() {
106228
107229 }
108230
109- @ Test
110- public void testNonExistentPropertiesParsing () {
111-
112- Properties props = SpringBootUtil .getPropertiesResource (SpringBootUtilTest .class .getResource ("/this-file-does-not-exist" ));
113- assertNotNull (props );
114- assertEquals (0 , props .size ());
115-
231+ // @Test TODO SK Remove this after Roland Review
232+ // public void testNonExistentPropertiesParsing() throws IOException {
233+ //
234+ // Properties props = SpringBootUtil.getPropertiesResource(SpringBootUtilTest.class.getResource(
235+ // "/this-file-does-not-exist"), null);
236+ // assertNotNull(props);
237+ // assertEquals(0, props.size());
238+ // }
239+
240+ public void setMavenProject (final MavenProject project , final Build build ) throws IOException {
241+ //Set Build Dir
242+ final String outputTempDir = Files .createTempDirectory (UUID .randomUUID ().toString ()).toFile ().getAbsolutePath ();
243+ new File (outputTempDir ).mkdirs ();
244+ build .setOutputDirectory (outputTempDir );
245+ project .setBuild (build );
116246 }
117247
118248}
0 commit comments