spring-boot/spring-boot-samples/spring-boot-sample-data-mon.../src/test/java/sample/data/mongo/SampleMongoApplicationTests...

72 lines
2.0 KiB
Java
Raw Normal View History

/*
* Copyright 2012-2014 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 sample.data.mongo;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.test.OutputCapture;
import org.springframework.core.NestedCheckedException;
import com.mongodb.MongoServerSelectionException;
2014-06-26 08:25:31 +08:00
import com.mongodb.MongoTimeoutException;
2014-02-06 14:37:41 +08:00
import static org.junit.Assert.assertTrue;
/**
* Tests for {@link SampleMongoApplication}.
2014-07-03 05:43:43 +08:00
*
* @author Dave Syer
*/
public class SampleMongoApplicationTests {
@Rule
public OutputCapture outputCapture = new OutputCapture();
2013-09-19 03:17:56 +08:00
@Test
public void testDefaultSettings() throws Exception {
try {
SampleMongoApplication.main(new String[0]);
2013-11-16 15:42:40 +08:00
}
catch (IllegalStateException ex) {
2013-11-16 15:37:38 +08:00
if (serverNotRunning(ex)) {
return;
}
}
String output = this.outputCapture.toString();
2013-11-16 15:42:40 +08:00
assertTrue("Wrong output: " + output,
output.contains("firstName='Alice', lastName='Smith'"));
}
2014-01-22 05:30:23 +08:00
private boolean serverNotRunning(IllegalStateException ex) {
@SuppressWarnings("serial")
2014-01-22 05:30:23 +08:00
NestedCheckedException nested = new NestedCheckedException("failed", ex) {
};
Throwable root = nested.getRootCause();
2014-06-26 08:25:31 +08:00
if (root instanceof MongoServerSelectionException
|| root instanceof MongoTimeoutException) {
if (root.getMessage().contains("Unable to connect to any server")) {
return true;
}
if (root.getMessage().contains("Timed out while waiting for a server")) {
return true;
}
}
return false;
}
}