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:
Dave Syer
2014-04-17 20:25:50 -07:00
parent f134e96053
commit 7b07fe8ce0
23 changed files with 239 additions and 72 deletions

View File

@@ -137,6 +137,12 @@ class StartupInfoLogger {
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();
String path = (codeSourceLocation == null ? "" : codeSourceLocation
.getAbsolutePath());
@@ -146,7 +152,10 @@ class StartupInfoLogger {
if (StringUtils.hasLength(startedBy) && StringUtils.hasLength(path)) {
startedBy = " " + startedBy;
}
return " (" + path + startedBy + ")";
if (StringUtils.hasLength(in) && StringUtils.hasLength(startedBy)) {
in = " " + in;
}
return " (" + path + startedBy + in + ")";
}
private File getCodeSourceLocation() {

View File

@@ -22,6 +22,7 @@ import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
/**
* {@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")) {
return getSource().nextInt();
}
if (name.endsWith("long")) {
if (name.startsWith("random.long")) {
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];
getSource().nextBytes(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) {
environment.getPropertySources().addAfter(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,

View File

@@ -143,7 +143,6 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
if (annotation == null) {
// Not running an embedded server, just setting up web context
args.put("server.port", "-1");
args.put("management.port", "-1");
}
else {
args.putAll(extractProperties(annotation.value()));

View File

@@ -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);
}
}