Convert remaining samples to use random port
Partial fix for gh-337. See also gh-607 which complements this, but might conflict on a merge.
This commit is contained in:
parent
f134e96053
commit
7b07fe8ce0
|
|
@ -21,6 +21,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.security.SecurityProperties;
|
import org.springframework.boot.autoconfigure.security.SecurityProperties;
|
||||||
|
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||||
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
|
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
@ -51,4 +52,11 @@ public class ManagementServerPropertiesAutoConfiguration {
|
||||||
return new SecurityProperties();
|
return new SecurityProperties();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// In case server auto configuration hasn't been included
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public ServerProperties serverProperties() {
|
||||||
|
return new ServerProperties();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -364,30 +364,23 @@ that and be sure that it has initialized is to add a `@Bean` of type
|
||||||
`ApplicationListener<EmbeddedServletContainerInitializedEvent>` and pull the container
|
`ApplicationListener<EmbeddedServletContainerInitializedEvent>` and pull the container
|
||||||
out of the event when it is published.
|
out of the event when it is published.
|
||||||
|
|
||||||
A really useful thing to do in is to autowire the `EmbeddedWebApplicationContext` into a
|
A really useful thing to do in is to use `@IntegrationTest` to set `server.port=0`
|
||||||
test case and use it to discover the port that the app is running on. In that way you can
|
and then inject the actual ("local") port as a `@Value`. Example:
|
||||||
use a test profile that chooses a random port (`server.port=0`) and make your test suite
|
|
||||||
independent of its environment. Example:
|
|
||||||
|
|
||||||
[source,java,indent=0,subs="verbatim,quotes,attributes"]
|
[source,java,indent=0,subs="verbatim,quotes,attributes"]
|
||||||
----
|
----
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = SampleDataJpaApplication.class)
|
@SpringApplicationConfiguration(classes = SampleDataJpaApplication.class)
|
||||||
@WebApplication
|
@WebApplication
|
||||||
@IntegrationTest
|
@IntegrationTest("server.port:0")
|
||||||
@ActiveProfiles("test")
|
|
||||||
public class CityRepositoryIntegrationTests {
|
public class CityRepositoryIntegrationTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
EmbeddedWebApplicationContext server;
|
EmbeddedWebApplicationContext server;
|
||||||
|
|
||||||
|
@Value("${local.server.port}")
|
||||||
int port;
|
int port;
|
||||||
|
|
||||||
@Before
|
|
||||||
public void init() {
|
|
||||||
port = server.getEmbeddedServletContainer().getPort();
|
|
||||||
}
|
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -199,6 +199,7 @@ sensible overriding of values, properties are considered in the the following or
|
||||||
. Command line arguments.
|
. Command line arguments.
|
||||||
. Java System properties (`System.getProperties()`).
|
. Java System properties (`System.getProperties()`).
|
||||||
. OS environment variables.
|
. OS environment variables.
|
||||||
|
. A `RandomValueProperySource` that only has properties in `random.*`.
|
||||||
. `@PropertySource` annotations on your `@Configuration` classes.
|
. `@PropertySource` annotations on your `@Configuration` classes.
|
||||||
. Application properties outside of your packaged jar (`application.properties`
|
. Application properties outside of your packaged jar (`application.properties`
|
||||||
including YAML and profile variants).
|
including YAML and profile variants).
|
||||||
|
|
@ -230,8 +231,25 @@ default `name`. When running in production, an `application.properties` can be p
|
||||||
outside of your jar that overrides `name`; and for one-off testing, you can launch with
|
outside of your jar that overrides `name`; and for one-off testing, you can launch with
|
||||||
a specific command line switch (e.g. `java -jar app.jar --name="Spring"`).
|
a specific command line switch (e.g. `java -jar app.jar --name="Spring"`).
|
||||||
|
|
||||||
|
The `RandomValueProperySource` is useful for injecting random values
|
||||||
|
(e.g. into secrets or test cases). It can produce integers, longs or
|
||||||
|
strings, e.g.
|
||||||
|
|
||||||
|
|
||||||
|
[source,properties,indent=0]
|
||||||
|
----
|
||||||
|
my.secret=${random.value}
|
||||||
|
my.number=${random.int}
|
||||||
|
my.bignumber=${random.long}
|
||||||
|
my.number.less.than.ten=${random.int(10)}
|
||||||
|
my.number.in.range=${random.int[1024,65536]}
|
||||||
|
----
|
||||||
|
|
||||||
|
The `random.int*` syntax is `OPEN value (,max) CLOSE` where the
|
||||||
|
`OPEN,CLOSE` are any character and `value,max` are integers. If
|
||||||
|
`max` is provided then `value` is the minimum value and `max` is the
|
||||||
|
maximum (exclusive).
|
||||||
|
|
||||||
[[boot-features-external-config-command-line-args]]
|
[[boot-features-external-config-command-line-args]]
|
||||||
=== Accessing command line properties
|
=== Accessing command line properties
|
||||||
By default `SpringApplication` will convert any command line option arguments (starting
|
By default `SpringApplication` will convert any command line option arguments (starting
|
||||||
|
|
@ -1538,6 +1556,10 @@ interaction. For Example:
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
To change the port you can add environment properties to
|
||||||
|
`@IntegrationTest` as colon- or equals-separated name-value pairs,
|
||||||
|
e.g. `@IntegrationTest("server.port:9000")`.
|
||||||
|
|
||||||
[[boot-features-test-utilities]]
|
[[boot-features-test-utilities]]
|
||||||
=== Test utilities
|
=== Test utilities
|
||||||
A few test utility classes are packaged as part of `spring-boot` that are generally
|
A few test utility classes are packaged as part of `spring-boot` that are generally
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.test.IntegrationTest;
|
import org.springframework.boot.test.IntegrationTest;
|
||||||
import org.springframework.boot.test.TestRestTemplate;
|
import org.springframework.boot.test.TestRestTemplate;
|
||||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
|
|
@ -39,15 +40,18 @@ import static org.junit.Assert.assertEquals;
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
|
@SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@IntegrationTest
|
@IntegrationTest("server.port=0")
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class SampleActuatorApplicationTests {
|
public class SampleActuatorApplicationTests {
|
||||||
|
|
||||||
|
@Value("${local.server.port}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHome() throws Exception {
|
public void testHome() throws Exception {
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
|
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
|
||||||
"http://localhost:8080", Map.class);
|
"http://localhost:" + port, Map.class);
|
||||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Map<String, Object> body = entity.getBody();
|
Map<String, Object> body = entity.getBody();
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
package sample.actuator.ui;
|
package sample.actuator.ui;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
@ -24,17 +26,14 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.autoconfigure.security.SecurityProperties;
|
import org.springframework.boot.autoconfigure.security.SecurityProperties;
|
||||||
import org.springframework.boot.test.IntegrationTest;
|
import org.springframework.boot.test.IntegrationTest;
|
||||||
import org.springframework.boot.test.TestRestTemplate;
|
|
||||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
|
import org.springframework.boot.test.TestRestTemplate;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Integration tests for separate management and main service ports.
|
* Integration tests for separate management and main service ports.
|
||||||
*
|
*
|
||||||
|
|
@ -43,18 +42,17 @@ import static org.junit.Assert.assertEquals;
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = SampleActuatorUiApplication.class)
|
@SpringApplicationConfiguration(classes = SampleActuatorUiApplication.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@IntegrationTest
|
@IntegrationTest({"server.port=0", "management.port:0"})
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
@ActiveProfiles("management-port")
|
|
||||||
public class SampleActuatorUiApplicationPortTests {
|
public class SampleActuatorUiApplicationPortTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private SecurityProperties security;
|
private SecurityProperties security;
|
||||||
|
|
||||||
@Value("${server.port}")
|
@Value("${local.server.port}")
|
||||||
private int port = 9010;
|
private int port = 9010;
|
||||||
|
|
||||||
@Value("${management.port}")
|
@Value("${local.management.port}")
|
||||||
private int managementPort = 9011;
|
private int managementPort = 9011;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.test.IntegrationTest;
|
import org.springframework.boot.test.IntegrationTest;
|
||||||
import org.springframework.boot.test.TestRestTemplate;
|
import org.springframework.boot.test.TestRestTemplate;
|
||||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
|
|
@ -44,17 +45,20 @@ import static org.junit.Assert.assertTrue;
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = SampleActuatorUiApplication.class)
|
@SpringApplicationConfiguration(classes = SampleActuatorUiApplication.class)
|
||||||
|
@IntegrationTest("server.port=0")
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@IntegrationTest
|
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class SampleActuatorUiApplicationTests {
|
public class SampleActuatorUiApplicationTests {
|
||||||
|
|
||||||
|
@Value("${local.server.port}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHome() throws Exception {
|
public void testHome() throws Exception {
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
|
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().exchange(
|
ResponseEntity<String> entity = new TestRestTemplate().exchange(
|
||||||
"http://localhost:8080", HttpMethod.GET, new HttpEntity<Void>(headers),
|
"http://localhost:" + port, HttpMethod.GET, new HttpEntity<Void>(headers),
|
||||||
String.class);
|
String.class);
|
||||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity
|
assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity
|
||||||
|
|
@ -64,7 +68,7 @@ public class SampleActuatorUiApplicationTests {
|
||||||
@Test
|
@Test
|
||||||
public void testCss() throws Exception {
|
public void testCss() throws Exception {
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
||||||
"http://localhost:8080/css/bootstrap.min.css", String.class);
|
"http://localhost:" + port + "/css/bootstrap.min.css", String.class);
|
||||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body"));
|
assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body"));
|
||||||
}
|
}
|
||||||
|
|
@ -73,7 +77,7 @@ public class SampleActuatorUiApplicationTests {
|
||||||
public void testMetrics() throws Exception {
|
public void testMetrics() throws Exception {
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
|
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
|
||||||
"http://localhost:8080/metrics", Map.class);
|
"http://localhost:" + port + "/metrics", Map.class);
|
||||||
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
|
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -82,7 +86,7 @@ public class SampleActuatorUiApplicationTests {
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
|
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().exchange(
|
ResponseEntity<String> entity = new TestRestTemplate().exchange(
|
||||||
"http://localhost:8080/error", HttpMethod.GET, new HttpEntity<Void>(
|
"http://localhost:" + port + "/error", HttpMethod.GET, new HttpEntity<Void>(
|
||||||
headers), String.class);
|
headers), String.class);
|
||||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody()
|
assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody()
|
||||||
|
|
|
||||||
|
|
@ -1,2 +0,0 @@
|
||||||
server.port: 9010
|
|
||||||
management.port: 9011
|
|
||||||
|
|
@ -48,6 +48,7 @@ public class UnsecureSampleActuatorApplicationTests {
|
||||||
|
|
||||||
@Value("${local.server.port}")
|
@Value("${local.server.port}")
|
||||||
private int port;
|
private int port;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHome() throws Exception {
|
public void testHome() throws Exception {
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ package sample.jetty;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.test.IntegrationTest;
|
import org.springframework.boot.test.IntegrationTest;
|
||||||
import org.springframework.boot.test.TestRestTemplate;
|
import org.springframework.boot.test.TestRestTemplate;
|
||||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
|
|
@ -37,14 +38,17 @@ import static org.junit.Assert.assertEquals;
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = SampleJettyApplication.class)
|
@SpringApplicationConfiguration(classes = SampleJettyApplication.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@IntegrationTest
|
@IntegrationTest("server.port:0")
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class SampleJettyApplicationTests {
|
public class SampleJettyApplicationTests {
|
||||||
|
|
||||||
|
@Value("${local.server.port}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHome() throws Exception {
|
public void testHome() throws Exception {
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
||||||
"http://localhost:8080", String.class);
|
"http://localhost:" + port, String.class);
|
||||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
assertEquals("Hello World", entity.getBody());
|
assertEquals("Hello World", entity.getBody());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,21 +16,22 @@
|
||||||
|
|
||||||
package sample.servlet;
|
package sample.servlet;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.autoconfigure.security.SecurityProperties;
|
import org.springframework.boot.autoconfigure.security.SecurityProperties;
|
||||||
import org.springframework.boot.test.IntegrationTest;
|
import org.springframework.boot.test.IntegrationTest;
|
||||||
import org.springframework.boot.test.TestRestTemplate;
|
|
||||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
|
import org.springframework.boot.test.TestRestTemplate;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Basic integration tests for demo application.
|
* Basic integration tests for demo application.
|
||||||
*
|
*
|
||||||
|
|
@ -39,24 +40,27 @@ import static org.junit.Assert.assertEquals;
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = SampleServletApplication.class)
|
@SpringApplicationConfiguration(classes = SampleServletApplication.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@IntegrationTest
|
@IntegrationTest("server.port:0")
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class SampleServletApplicationTests {
|
public class SampleServletApplicationTests {
|
||||||
|
|
||||||
|
@Value("${local.server.port}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private SecurityProperties security;
|
private SecurityProperties security;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHomeIsSecure() throws Exception {
|
public void testHomeIsSecure() throws Exception {
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
||||||
"http://localhost:8080", String.class);
|
"http://localhost:" + port, String.class);
|
||||||
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
|
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHome() throws Exception {
|
public void testHome() throws Exception {
|
||||||
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
|
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword())
|
||||||
.getForEntity("http://localhost:8080", String.class);
|
.getForEntity("http://localhost:" + port, String.class);
|
||||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
assertEquals("Hello World", entity.getBody());
|
assertEquals("Hello World", entity.getBody());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ package sample.tomcat;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
|
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
|
||||||
|
|
@ -40,7 +41,6 @@ import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
import sample.tomcat.NonAutoConfigurationSampleTomcatApplicationTests.NonAutoConfigurationSampleTomcatApplication;
|
import sample.tomcat.NonAutoConfigurationSampleTomcatApplicationTests.NonAutoConfigurationSampleTomcatApplication;
|
||||||
import sample.tomcat.service.HelloWorldService;
|
import sample.tomcat.service.HelloWorldService;
|
||||||
import sample.tomcat.web.SampleController;
|
import sample.tomcat.web.SampleController;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -51,10 +51,13 @@ import static org.junit.Assert.assertEquals;
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = NonAutoConfigurationSampleTomcatApplication.class)
|
@SpringApplicationConfiguration(classes = NonAutoConfigurationSampleTomcatApplication.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@IntegrationTest
|
@IntegrationTest("server.port=0")
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class NonAutoConfigurationSampleTomcatApplicationTests {
|
public class NonAutoConfigurationSampleTomcatApplicationTests {
|
||||||
|
|
||||||
|
@Value("${local.server.port}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@Import({ EmbeddedServletContainerAutoConfiguration.class,
|
@Import({ EmbeddedServletContainerAutoConfiguration.class,
|
||||||
DispatcherServletAutoConfiguration.class,
|
DispatcherServletAutoConfiguration.class,
|
||||||
|
|
@ -73,7 +76,7 @@ public class NonAutoConfigurationSampleTomcatApplicationTests {
|
||||||
@Test
|
@Test
|
||||||
public void testHome() throws Exception {
|
public void testHome() throws Exception {
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
||||||
"http://localhost:8080", String.class);
|
"http://localhost:" + port, String.class);
|
||||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
assertEquals("Hello World", entity.getBody());
|
assertEquals("Hello World", entity.getBody());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ package sample.tomcat;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.test.IntegrationTest;
|
import org.springframework.boot.test.IntegrationTest;
|
||||||
import org.springframework.boot.test.TestRestTemplate;
|
import org.springframework.boot.test.TestRestTemplate;
|
||||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
|
|
@ -37,14 +38,17 @@ import static org.junit.Assert.assertEquals;
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = SampleTomcatApplication.class)
|
@SpringApplicationConfiguration(classes = SampleTomcatApplication.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@IntegrationTest
|
@IntegrationTest("server.port:0")
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class SampleTomcatApplicationTests {
|
public class SampleTomcatApplicationTests {
|
||||||
|
|
||||||
|
@Value("${local.server.port}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHome() throws Exception {
|
public void testHome() throws Exception {
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
||||||
"http://localhost:8080", String.class);
|
"http://localhost:" + port, String.class);
|
||||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
assertEquals("Hello World", entity.getBody());
|
assertEquals("Hello World", entity.getBody());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ package sample.traditional;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.test.IntegrationTest;
|
import org.springframework.boot.test.IntegrationTest;
|
||||||
import org.springframework.boot.test.TestRestTemplate;
|
import org.springframework.boot.test.TestRestTemplate;
|
||||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
|
|
@ -38,14 +39,17 @@ import static org.junit.Assert.assertTrue;
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = SampleTraditionalApplication.class)
|
@SpringApplicationConfiguration(classes = SampleTraditionalApplication.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@IntegrationTest
|
@IntegrationTest("server.port:0")
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class SampleTraditionalApplicationTests {
|
public class SampleTraditionalApplicationTests {
|
||||||
|
|
||||||
|
@Value("${local.server.port}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHomeJsp() throws Exception {
|
public void testHomeJsp() throws Exception {
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
||||||
"http://localhost:8080", String.class);
|
"http://localhost:" + port, String.class);
|
||||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
String body = entity.getBody();
|
String body = entity.getBody();
|
||||||
assertTrue("Wrong body:\n" + body, body.contains("<html>"));
|
assertTrue("Wrong body:\n" + body, body.contains("<html>"));
|
||||||
|
|
@ -55,7 +59,7 @@ public class SampleTraditionalApplicationTests {
|
||||||
@Test
|
@Test
|
||||||
public void testStaticPage() throws Exception {
|
public void testStaticPage() throws Exception {
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
||||||
"http://localhost:8080/index.html", String.class);
|
"http://localhost:" + port + "/index.html", String.class);
|
||||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
String body = entity.getBody();
|
String body = entity.getBody();
|
||||||
assertTrue("Wrong body:\n" + body, body.contains("<html>"));
|
assertTrue("Wrong body:\n" + body, body.contains("<html>"));
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ package sample.jsp;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.test.IntegrationTest;
|
import org.springframework.boot.test.IntegrationTest;
|
||||||
import org.springframework.boot.test.TestRestTemplate;
|
import org.springframework.boot.test.TestRestTemplate;
|
||||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
|
|
@ -38,14 +39,17 @@ import static org.junit.Assert.assertTrue;
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = SampleWebJspApplication.class)
|
@SpringApplicationConfiguration(classes = SampleWebJspApplication.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@IntegrationTest
|
@IntegrationTest("server.port:0")
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class SampleWebJspApplicationTests {
|
public class SampleWebJspApplicationTests {
|
||||||
|
|
||||||
|
@Value("${local.server.port}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testJspWithEl() throws Exception {
|
public void testJspWithEl() throws Exception {
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
||||||
"http://localhost:8080", String.class);
|
"http://localhost:" + port, String.class);
|
||||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
assertTrue("Wrong body:\n" + entity.getBody(),
|
assertTrue("Wrong body:\n" + entity.getBody(),
|
||||||
entity.getBody().contains("/resources/text.txt"));
|
entity.getBody().contains("/resources/text.txt"));
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.test.IntegrationTest;
|
import org.springframework.boot.test.IntegrationTest;
|
||||||
import org.springframework.boot.test.TestRestTemplate;
|
import org.springframework.boot.test.TestRestTemplate;
|
||||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
|
|
@ -48,16 +49,19 @@ import static org.junit.Assert.assertTrue;
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = SampleMethodSecurityApplication.class)
|
@SpringApplicationConfiguration(classes = SampleMethodSecurityApplication.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@IntegrationTest
|
@IntegrationTest("server.port:0")
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class SampleMethodSecurityApplicationTests {
|
public class SampleMethodSecurityApplicationTests {
|
||||||
|
|
||||||
|
@Value("${local.server.port}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHome() throws Exception {
|
public void testHome() throws Exception {
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
|
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().exchange(
|
ResponseEntity<String> entity = new TestRestTemplate().exchange(
|
||||||
"http://localhost:8080", HttpMethod.GET, new HttpEntity<Void>(headers),
|
"http://localhost:" + port, HttpMethod.GET, new HttpEntity<Void>(headers),
|
||||||
String.class);
|
String.class);
|
||||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity
|
assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity
|
||||||
|
|
@ -73,11 +77,11 @@ public class SampleMethodSecurityApplicationTests {
|
||||||
form.set("password", "admin");
|
form.set("password", "admin");
|
||||||
getCsrf(form, headers);
|
getCsrf(form, headers);
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().exchange(
|
ResponseEntity<String> entity = new TestRestTemplate().exchange(
|
||||||
"http://localhost:8080/login", HttpMethod.POST,
|
"http://localhost:" + port + "/login", HttpMethod.POST,
|
||||||
new HttpEntity<MultiValueMap<String, String>>(form, headers),
|
new HttpEntity<MultiValueMap<String, String>>(form, headers),
|
||||||
String.class);
|
String.class);
|
||||||
assertEquals(HttpStatus.FOUND, entity.getStatusCode());
|
assertEquals(HttpStatus.FOUND, entity.getStatusCode());
|
||||||
assertEquals("http://localhost:8080/", entity.getHeaders().getLocation()
|
assertEquals("http://localhost:" + port + "/", entity.getHeaders().getLocation()
|
||||||
.toString());
|
.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,7 +94,7 @@ public class SampleMethodSecurityApplicationTests {
|
||||||
form.set("password", "user");
|
form.set("password", "user");
|
||||||
getCsrf(form, headers);
|
getCsrf(form, headers);
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().exchange(
|
ResponseEntity<String> entity = new TestRestTemplate().exchange(
|
||||||
"http://localhost:8080/login", HttpMethod.POST,
|
"http://localhost:" + port + "/login", HttpMethod.POST,
|
||||||
new HttpEntity<MultiValueMap<String, String>>(form, headers),
|
new HttpEntity<MultiValueMap<String, String>>(form, headers),
|
||||||
String.class);
|
String.class);
|
||||||
assertEquals(HttpStatus.FOUND, entity.getStatusCode());
|
assertEquals(HttpStatus.FOUND, entity.getStatusCode());
|
||||||
|
|
@ -106,7 +110,7 @@ public class SampleMethodSecurityApplicationTests {
|
||||||
|
|
||||||
private void getCsrf(MultiValueMap<String, String> form, HttpHeaders headers) {
|
private void getCsrf(MultiValueMap<String, String> form, HttpHeaders headers) {
|
||||||
ResponseEntity<String> page = new TestRestTemplate().getForEntity(
|
ResponseEntity<String> page = new TestRestTemplate().getForEntity(
|
||||||
"http://localhost:8080/login", String.class);
|
"http://localhost:" + port + "/login", String.class);
|
||||||
String cookie = page.getHeaders().getFirst("Set-Cookie");
|
String cookie = page.getHeaders().getFirst("Set-Cookie");
|
||||||
headers.set("Cookie", cookie);
|
headers.set("Cookie", cookie);
|
||||||
String body = page.getBody();
|
String body = page.getBody();
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import java.util.Arrays;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.test.IntegrationTest;
|
import org.springframework.boot.test.IntegrationTest;
|
||||||
import org.springframework.boot.test.TestRestTemplate;
|
import org.springframework.boot.test.TestRestTemplate;
|
||||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
|
|
@ -44,16 +45,19 @@ import static org.junit.Assert.assertTrue;
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = SampleWebSecureApplication.class)
|
@SpringApplicationConfiguration(classes = SampleWebSecureApplication.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@IntegrationTest
|
@IntegrationTest("server.port:0")
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class SampleSecureApplicationTests {
|
public class SampleSecureApplicationTests {
|
||||||
|
|
||||||
|
@Value("${local.server.port}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHome() throws Exception {
|
public void testHome() throws Exception {
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
|
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().exchange(
|
ResponseEntity<String> entity = new TestRestTemplate().exchange(
|
||||||
"http://localhost:8080", HttpMethod.GET, new HttpEntity<Void>(headers),
|
"http://localhost:" + port, HttpMethod.GET, new HttpEntity<Void>(headers),
|
||||||
String.class);
|
String.class);
|
||||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity
|
assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity
|
||||||
|
|
@ -63,7 +67,7 @@ public class SampleSecureApplicationTests {
|
||||||
@Test
|
@Test
|
||||||
public void testCss() throws Exception {
|
public void testCss() throws Exception {
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
||||||
"http://localhost:8080/css/bootstrap.min.css", String.class);
|
"http://localhost:" + port + "/css/bootstrap.min.css", String.class);
|
||||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body"));
|
assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body"));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import java.net.URI;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.test.IntegrationTest;
|
import org.springframework.boot.test.IntegrationTest;
|
||||||
import org.springframework.boot.test.TestRestTemplate;
|
import org.springframework.boot.test.TestRestTemplate;
|
||||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
|
|
@ -43,14 +44,17 @@ import static org.junit.Assert.assertTrue;
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = SampleWebUiApplication.class)
|
@SpringApplicationConfiguration(classes = SampleWebUiApplication.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@IntegrationTest
|
@IntegrationTest("server.port:0")
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class SampleWebUiApplicationTests {
|
public class SampleWebUiApplicationTests {
|
||||||
|
|
||||||
|
@Value("${local.server.port}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHome() throws Exception {
|
public void testHome() throws Exception {
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
||||||
"http://localhost:8080", String.class);
|
"http://localhost:" + port, String.class);
|
||||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity
|
assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity
|
||||||
.getBody().contains("<title>Messages"));
|
.getBody().contains("<title>Messages"));
|
||||||
|
|
@ -63,16 +67,16 @@ public class SampleWebUiApplicationTests {
|
||||||
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
|
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
|
||||||
map.set("text", "FOO text");
|
map.set("text", "FOO text");
|
||||||
map.set("summary", "FOO");
|
map.set("summary", "FOO");
|
||||||
URI location = new TestRestTemplate().postForLocation("http://localhost:8080",
|
URI location = new TestRestTemplate().postForLocation("http://localhost:" + port,
|
||||||
map);
|
map);
|
||||||
assertTrue("Wrong location:\n" + location,
|
assertTrue("Wrong location:\n" + location,
|
||||||
location.toString().contains("localhost:8080"));
|
location.toString().contains("localhost:" + port));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCss() throws Exception {
|
public void testCss() throws Exception {
|
||||||
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
||||||
"http://localhost:8080/css/bootstrap.min.css", String.class);
|
"http://localhost:" + port + "/css/bootstrap.min.css", String.class);
|
||||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||||
assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body"));
|
assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body"));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
package samples.websocket.echo;
|
package samples.websocket.echo;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
|
@ -35,6 +37,7 @@ import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
|
import org.springframework.util.SocketUtils;
|
||||||
import org.springframework.web.socket.client.WebSocketConnectionManager;
|
import org.springframework.web.socket.client.WebSocketConnectionManager;
|
||||||
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
|
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
|
||||||
|
|
||||||
|
|
@ -44,8 +47,6 @@ import samples.websocket.client.SimpleGreetingService;
|
||||||
import samples.websocket.config.SampleWebSocketsApplication;
|
import samples.websocket.config.SampleWebSocketsApplication;
|
||||||
import samples.websocket.echo.CustomContainerWebSocketsApplicationTests.CustomContainerConfiguration;
|
import samples.websocket.echo.CustomContainerWebSocketsApplicationTests.CustomContainerConfiguration;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = { SampleWebSocketsApplication.class,
|
@SpringApplicationConfiguration(classes = { SampleWebSocketsApplication.class,
|
||||||
CustomContainerConfiguration.class })
|
CustomContainerConfiguration.class })
|
||||||
|
|
@ -57,13 +58,15 @@ public class CustomContainerWebSocketsApplicationTests {
|
||||||
private static Log logger = LogFactory
|
private static Log logger = LogFactory
|
||||||
.getLog(CustomContainerWebSocketsApplicationTests.class);
|
.getLog(CustomContainerWebSocketsApplicationTests.class);
|
||||||
|
|
||||||
private static final String WS_URI = "ws://localhost:9010/ws/echo/websocket";
|
private static int PORT = SocketUtils.findAvailableTcpPort();
|
||||||
|
|
||||||
|
private static final String WS_URI = "ws://localhost:" + PORT + "/ws/echo/websocket";
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
protected static class CustomContainerConfiguration {
|
protected static class CustomContainerConfiguration {
|
||||||
@Bean
|
@Bean
|
||||||
public EmbeddedServletContainerFactory embeddedServletContainerFactory() {
|
public EmbeddedServletContainerFactory embeddedServletContainerFactory() {
|
||||||
return new TomcatEmbeddedServletContainerFactory("/ws", 9010);
|
return new TomcatEmbeddedServletContainerFactory("/ws", PORT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,13 +16,17 @@
|
||||||
|
|
||||||
package samples.websocket.echo;
|
package samples.websocket.echo;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.CommandLineRunner;
|
import org.springframework.boot.CommandLineRunner;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.test.IntegrationTest;
|
import org.springframework.boot.test.IntegrationTest;
|
||||||
|
|
@ -41,18 +45,24 @@ import samples.websocket.client.SimpleClientWebSocketHandler;
|
||||||
import samples.websocket.client.SimpleGreetingService;
|
import samples.websocket.client.SimpleGreetingService;
|
||||||
import samples.websocket.config.SampleWebSocketsApplication;
|
import samples.websocket.config.SampleWebSocketsApplication;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = SampleWebSocketsApplication.class)
|
@SpringApplicationConfiguration(classes = SampleWebSocketsApplication.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@IntegrationTest
|
@IntegrationTest("server.port:0")
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class SampleWebSocketsApplicationTests {
|
public class SampleWebSocketsApplicationTests {
|
||||||
|
|
||||||
private static Log logger = LogFactory.getLog(SampleWebSocketsApplicationTests.class);
|
private static Log logger = LogFactory.getLog(SampleWebSocketsApplicationTests.class);
|
||||||
|
|
||||||
private static final String WS_URI = "ws://localhost:8080/echo/websocket";
|
private static String WS_URI;
|
||||||
|
|
||||||
|
@Value("${local.server.port}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
WS_URI = "ws://localhost:" + port + "/echo/websocket";
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void runAndWait() throws Exception {
|
public void runAndWait() throws Exception {
|
||||||
|
|
|
||||||
|
|
@ -137,6 +137,12 @@ class StartupInfoLogger {
|
||||||
return System.getProperty("user.name");
|
return System.getProperty("user.name");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
String in = getValue("in ", new Callable<Object>() {
|
||||||
|
@Override
|
||||||
|
public Object call() throws Exception {
|
||||||
|
return System.getProperty("user.dir");
|
||||||
|
}
|
||||||
|
});
|
||||||
File codeSourceLocation = getCodeSourceLocation();
|
File codeSourceLocation = getCodeSourceLocation();
|
||||||
String path = (codeSourceLocation == null ? "" : codeSourceLocation
|
String path = (codeSourceLocation == null ? "" : codeSourceLocation
|
||||||
.getAbsolutePath());
|
.getAbsolutePath());
|
||||||
|
|
@ -146,7 +152,10 @@ class StartupInfoLogger {
|
||||||
if (StringUtils.hasLength(startedBy) && StringUtils.hasLength(path)) {
|
if (StringUtils.hasLength(startedBy) && StringUtils.hasLength(path)) {
|
||||||
startedBy = " " + startedBy;
|
startedBy = " " + startedBy;
|
||||||
}
|
}
|
||||||
return " (" + path + startedBy + ")";
|
if (StringUtils.hasLength(in) && StringUtils.hasLength(startedBy)) {
|
||||||
|
in = " " + in;
|
||||||
|
}
|
||||||
|
return " (" + path + startedBy + in + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
private File getCodeSourceLocation() {
|
private File getCodeSourceLocation() {
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
import org.springframework.core.env.PropertySource;
|
import org.springframework.core.env.PropertySource;
|
||||||
import org.springframework.core.env.StandardEnvironment;
|
import org.springframework.core.env.StandardEnvironment;
|
||||||
import org.springframework.util.DigestUtils;
|
import org.springframework.util.DigestUtils;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link PropertySource} that returns a random value for any property that starts with
|
* {@link PropertySource} that returns a random value for any property that starts with
|
||||||
|
|
@ -44,14 +45,28 @@ public class RandomValuePropertySource extends PropertySource<Random> {
|
||||||
if (name.endsWith("int")) {
|
if (name.endsWith("int")) {
|
||||||
return getSource().nextInt();
|
return getSource().nextInt();
|
||||||
}
|
}
|
||||||
if (name.endsWith("long")) {
|
if (name.startsWith("random.long")) {
|
||||||
return getSource().nextLong();
|
return getSource().nextLong();
|
||||||
}
|
}
|
||||||
|
if (name.startsWith("random.int") && name.length() > "random.int".length() + 1) {
|
||||||
|
String range = name.substring("random.int".length() + 1);
|
||||||
|
range = range.substring(0, range.length() - 1);
|
||||||
|
return getNextInRange(range);
|
||||||
|
}
|
||||||
byte[] bytes = new byte[32];
|
byte[] bytes = new byte[32];
|
||||||
getSource().nextBytes(bytes);
|
getSource().nextBytes(bytes);
|
||||||
return DigestUtils.md5DigestAsHex(bytes);
|
return DigestUtils.md5DigestAsHex(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int getNextInRange(String range) {
|
||||||
|
String[] tokens = StringUtils.commaDelimitedListToStringArray(range);
|
||||||
|
Integer start = Integer.valueOf(tokens[0]);
|
||||||
|
if (tokens.length == 1) {
|
||||||
|
return getSource().nextInt(start);
|
||||||
|
}
|
||||||
|
return start + getSource().nextInt(Integer.valueOf(tokens[1]) - start);
|
||||||
|
}
|
||||||
|
|
||||||
public static void addToEnvironment(ConfigurableEnvironment environment) {
|
public static void addToEnvironment(ConfigurableEnvironment environment) {
|
||||||
environment.getPropertySources().addAfter(
|
environment.getPropertySources().addAfter(
|
||||||
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
|
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
|
||||||
|
|
|
||||||
|
|
@ -143,7 +143,6 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
|
||||||
if (annotation == null) {
|
if (annotation == null) {
|
||||||
// Not running an embedded server, just setting up web context
|
// Not running an embedded server, just setting up web context
|
||||||
args.put("server.port", "-1");
|
args.put("server.port", "-1");
|
||||||
args.put("management.port", "-1");
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
args.putAll(extractProperties(annotation.value()));
|
args.putAll(extractProperties(annotation.value()));
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,68 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2013 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.context.config;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Dave Syer
|
||||||
|
*/
|
||||||
|
public class RandomValuePropertySourceTests {
|
||||||
|
|
||||||
|
private RandomValuePropertySource source = new RandomValuePropertySource("random");
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void notRandom() {
|
||||||
|
assertNull(this.source.getProperty("foo"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void string() {
|
||||||
|
assertNotNull(this.source.getProperty("random.string"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void intValue() {
|
||||||
|
Integer value = (Integer) this.source.getProperty("random.int");
|
||||||
|
assertNotNull(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void intRange() {
|
||||||
|
Integer value = (Integer) this.source.getProperty("random.int[4,10]");
|
||||||
|
assertNotNull(value);
|
||||||
|
assertTrue(value >= 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void intMax() {
|
||||||
|
Integer value = (Integer) this.source.getProperty("random.int(10)");
|
||||||
|
assertNotNull(value);
|
||||||
|
assertTrue(value < 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void longValue() {
|
||||||
|
Long value = (Long) this.source.getProperty("random.long");
|
||||||
|
assertNotNull(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue