package sample;

import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.Map;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.config.server.test.ConfigServerTestUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@IntegrationTest({"server.port:0", "spring.application.name:bad"})
@WebAppConfiguration
public class ServerNativeApplicationTests {

	private static int configPort = 0;
	
	@Autowired
	private ConfigurableEnvironment environment;

	@Value("${local.server.port}")
	private int port;

	private static ConfigurableApplicationContext server;

	@BeforeClass
	public static void startConfigServer() throws IOException {
		String repo = ConfigServerTestUtils.prepareLocalRepo();
		server = SpringApplication.run(
				org.springframework.cloud.config.server.ConfigServerApplication.class,
				"--server.port=" + configPort, "--spring.config.name=server",
				"--spring.cloud.config.server.git.uri=" + repo, "--spring.profiles.active=native");
		configPort = ((EmbeddedWebApplicationContext) server)
				.getEmbeddedServletContainer().getPort();
		System.setProperty("config.port", "" + configPort);
	}
	
	@AfterClass
	public static void close() {
		System.clearProperty("config.port");		
		if (server!=null) {
			server.close();
		}
	}

	@SuppressWarnings("rawtypes")
	@Test
	public void contextLoads() {
		// The remote config was bad so there is no bootstrap
		assertTrue(((Map)environment.getPropertySources().get("bootstrap").getSource()).isEmpty());
	}

	public static void main(String[] args) throws IOException {
		configPort = 8888;
		startConfigServer();
		SpringApplication.run(Application.class, args);
	}

}

@@ -47,19 +47,20 @@
 t.assert
-Tru
+Fals
 e;%0A%0Aimpo
@@ -86,30 +86,8 @@
 ion;
-%0Aimport java.util.Map;
 %0A%0Aim
@@ -1186,18 +1186,17 @@
 rt = 0;%0A
-%09
 %0A
+
 %09@Autowi
@@ -1909,18 +1909,17 @@
 rt);%0A%09%7D%0A
-%09
 %0A
+
 %09@AfterC
@@ -1991,18 +1991,16 @@
 .port%22);
-%09%09
 %0A%09%09if (s
@@ -2045,39 +2045,8 @@
 %09%7D%0A%0A
-%09@SuppressWarnings(%22rawtypes%22)%0A
 %09@Te
@@ -2146,19 +2146,19 @@
 sert
-True(((Map)
+False(this.
 envi
@@ -2186,19 +2186,24 @@
 urces().
-get
+contains
 (%22bootst
@@ -2211,31 +2211,8 @@
 ap%22)
-.getSource()).isEmpty()
 );%0A%09

Fix broken test (changes in commons)