Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
4038b394
Commit
4038b394
authored
Aug 05, 2013
by
Phillip Webb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add OutputCapture test class
parent
17442788
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
186 additions
and
198 deletions
+186
-198
pom.xml
spring-boot-cli/pom.xml
+8
-0
SampleIntegrationTests.java
.../org/springframework/boot/cli/SampleIntegrationTests.java
+15
-38
SampleBatchApplicationTests.java
...mework/boot/sample/batch/SampleBatchApplicationTests.java
+5
-29
SampleProfileApplicationTests.java
...rk/boot/sample/profile/SampleProfileApplicationTests.java
+8
-27
SampleSimpleApplicationTests.java
...work/boot/sample/simple/SampleSimpleApplicationTests.java
+6
-17
SampleSpringXmlApplicationTests.java
...work/boot/sample/xml/SampleSpringXmlApplicationTests.java
+5
-25
OutputCapture.java
...src/test/java/org/springframework/boot/OutputCapture.java
+120
-0
SimpleMainTests.java
...c/test/java/org/springframework/boot/SimpleMainTests.java
+5
-20
LoggingApplicationContextInitializerTests.java
...nitializer/LoggingApplicationContextInitializerTests.java
+8
-20
LogbackLoggingSystemTests.java
...ework/boot/logging/logback/LogbackLoggingSystemTests.java
+6
-22
No files found.
spring-boot-cli/pom.xml
View file @
4038b394
...
@@ -34,6 +34,14 @@
...
@@ -34,6 +34,14 @@
<artifactId>
groovy-templates
</artifactId>
<artifactId>
groovy-templates
</artifactId>
<optional>
true
</optional>
<optional>
true
</optional>
</dependency>
</dependency>
<!-- Test -->
<dependency>
<groupId>
${project.groupId}
</groupId>
<artifactId>
spring-boot
</artifactId>
<version>
${project.version}
</version>
<classifier>
tests
</classifier>
<scope>
test
</scope>
</dependency>
<dependency>
<dependency>
<groupId>
org.javassist
</groupId>
<groupId>
org.javassist
</groupId>
<artifactId>
javassist
</artifactId>
<artifactId>
javassist
</artifactId>
...
...
spring-boot-cli/src/test/java/org/springframework/boot/cli/SampleIntegrationTests.java
View file @
4038b394
...
@@ -16,8 +16,6 @@
...
@@ -16,8 +16,6 @@
package
org
.
springframework
.
boot
.
cli
;
package
org
.
springframework
.
boot
.
cli
;
import
java.io.ByteArrayOutputStream
;
import
java.io.PrintStream
;
import
java.net.URL
;
import
java.net.URL
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Executors
;
...
@@ -26,9 +24,10 @@ import java.util.concurrent.TimeUnit;
...
@@ -26,9 +24,10 @@ import java.util.concurrent.TimeUnit;
import
org.apache.ivy.util.FileUtil
;
import
org.apache.ivy.util.FileUtil
;
import
org.junit.After
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.BeforeClass
;
import
org.junit.BeforeClass
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.OutputCapture
;
import
org.springframework.boot.cli.command.RunCommand
;
import
org.springframework.boot.cli.command.RunCommand
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
...
@@ -43,37 +42,14 @@ public class SampleIntegrationTests {
...
@@ -43,37 +42,14 @@ public class SampleIntegrationTests {
@BeforeClass
@BeforeClass
public
static
void
cleanGrapes
()
throws
Exception
{
public
static
void
cleanGrapes
()
throws
Exception
{
GrapesCleaner
.
cleanIfNecessary
();
//
GrapesCleaner.cleanIfNecessary();
// System.setProperty("ivy.message.logger.level", "3");
// System.setProperty("ivy.message.logger.level", "3");
}
}
private
RunCommand
command
;
@Rule
public
OutputCapture
outputCapture
=
new
OutputCapture
();
private
PrintStream
savedOutput
;
private
ByteArrayOutputStream
output
;
private
PrintStream
savedErr
;
@Before
public
void
init
()
{
this
.
savedOutput
=
System
.
out
;
this
.
savedErr
=
System
.
err
;
this
.
output
=
new
ByteArrayOutputStream
();
System
.
setOut
(
new
PrintStream
(
this
.
output
));
System
.
setErr
(
new
PrintStream
(
this
.
output
));
}
@After
private
RunCommand
command
;
public
void
clear
()
{
System
.
setOut
(
this
.
savedOutput
);
System
.
setErr
(
this
.
savedErr
);
System
.
out
.
println
(
getOutput
());
}
private
String
getOutput
()
{
return
this
.
output
.
toString
();
}
private
void
start
(
final
String
...
sample
)
throws
Exception
{
private
void
start
(
final
String
...
sample
)
throws
Exception
{
Future
<
RunCommand
>
future
=
Executors
.
newSingleThreadExecutor
().
submit
(
Future
<
RunCommand
>
future
=
Executors
.
newSingleThreadExecutor
().
submit
(
...
@@ -98,21 +74,22 @@ public class SampleIntegrationTests {
...
@@ -98,21 +74,22 @@ public class SampleIntegrationTests {
@Test
@Test
public
void
appSample
()
throws
Exception
{
public
void
appSample
()
throws
Exception
{
start
(
"samples/app.groovy"
);
start
(
"samples/app.groovy"
);
String
output
=
getOutput
();
String
output
=
this
.
outputCapture
.
getOutputAndRelease
();
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello World"
));
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello World"
));
}
}
@Test
@Test
public
void
templateSample
()
throws
Exception
{
public
void
templateSample
()
throws
Exception
{
start
(
"samples/template.groovy"
);
start
(
"samples/template.groovy"
);
String
output
=
getOutput
();
String
output
=
this
.
outputCapture
.
getOutputAndRelease
();
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello World!"
));
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello World!"
));
}
}
@Test
@Test
public
void
jobSample
()
throws
Exception
{
public
void
jobSample
()
throws
Exception
{
start
(
"samples/job.groovy"
,
"foo=bar"
);
start
(
"samples/job.groovy"
,
"foo=bar"
);
String
output
=
getOutput
();
String
output
=
this
.
outputCapture
.
getOutputAndRelease
();
System
.
out
.
println
(
output
);
assertTrue
(
"Wrong output: "
+
output
,
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"completed with the following parameters"
));
output
.
contains
(
"completed with the following parameters"
));
}
}
...
@@ -120,11 +97,11 @@ public class SampleIntegrationTests {
...
@@ -120,11 +97,11 @@ public class SampleIntegrationTests {
@Test
@Test
public
void
reactorSample
()
throws
Exception
{
public
void
reactorSample
()
throws
Exception
{
start
(
"samples/reactor.groovy"
,
"Phil"
);
start
(
"samples/reactor.groovy"
,
"Phil"
);
String
output
=
getOutput
();
String
output
=
this
.
outputCapture
.
getOutputAndRelease
();
int
count
=
0
;
int
count
=
0
;
while
(!
output
.
contains
(
"Hello Phil"
)
&&
count
++
<
5
)
{
while
(!
output
.
contains
(
"Hello Phil"
)
&&
count
++
<
5
)
{
Thread
.
sleep
(
200
);
Thread
.
sleep
(
200
);
output
=
getOutput
();
output
=
this
.
outputCapture
.
getOutputAndRelease
();
}
}
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello Phil"
));
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello Phil"
));
}
}
...
@@ -132,7 +109,7 @@ public class SampleIntegrationTests {
...
@@ -132,7 +109,7 @@ public class SampleIntegrationTests {
@Test
@Test
public
void
jobWebSample
()
throws
Exception
{
public
void
jobWebSample
()
throws
Exception
{
start
(
"samples/job.groovy"
,
"samples/web.groovy"
,
"foo=bar"
);
start
(
"samples/job.groovy"
,
"samples/web.groovy"
,
"foo=bar"
);
String
output
=
getOutput
();
String
output
=
this
.
outputCapture
.
getOutputAndRelease
();
assertTrue
(
"Wrong output: "
+
output
,
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"completed with the following parameters"
));
output
.
contains
(
"completed with the following parameters"
));
String
result
=
FileUtil
.
readEntirely
(
new
URL
(
"http://localhost:8080"
)
String
result
=
FileUtil
.
readEntirely
(
new
URL
(
"http://localhost:8080"
)
...
@@ -170,14 +147,14 @@ public class SampleIntegrationTests {
...
@@ -170,14 +147,14 @@ public class SampleIntegrationTests {
@Test
@Test
public
void
integrationSample
()
throws
Exception
{
public
void
integrationSample
()
throws
Exception
{
start
(
"samples/integration.groovy"
);
start
(
"samples/integration.groovy"
);
String
output
=
getOutput
();
String
output
=
this
.
outputCapture
.
getOutputAndRelease
();
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello, World"
));
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello, World"
));
}
}
@Test
@Test
public
void
xmlSample
()
throws
Exception
{
public
void
xmlSample
()
throws
Exception
{
start
(
"samples/app.xml"
,
"samples/runner.groovy"
);
start
(
"samples/app.xml"
,
"samples/runner.groovy"
);
String
output
=
getOutput
();
String
output
=
this
.
outputCapture
.
getOutputAndRelease
();
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello World"
));
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello World"
));
}
}
...
...
spring-boot-samples/spring-boot-sample-batch/src/test/java/org/springframework/boot/sample/batch/SampleBatchApplicationTests.java
View file @
4038b394
...
@@ -16,48 +16,24 @@
...
@@ -16,48 +16,24 @@
package
org
.
springframework
.
boot
.
sample
.
batch
;
package
org
.
springframework
.
boot
.
sample
.
batch
;
import
java.io.ByteArrayOutputStream
;
import
org.junit.Rule
;
import
java.io.PrintStream
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.OutputCapture
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.sample.batch.SampleBatchApplication
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
public
class
SampleBatchApplicationTests
{
public
class
SampleBatchApplicationTests
{
private
PrintStream
savedOutput
;
@Rule
private
PrintStream
savedErr
;
public
OutputCapture
outputCapture
=
new
OutputCapture
();
private
ByteArrayOutputStream
output
;
@Before
public
void
init
()
{
this
.
savedOutput
=
System
.
out
;
this
.
savedErr
=
System
.
err
;
this
.
output
=
new
ByteArrayOutputStream
();
System
.
setOut
(
new
PrintStream
(
this
.
output
));
System
.
setErr
(
new
PrintStream
(
this
.
output
));
}
@After
public
void
after
()
{
System
.
setOut
(
this
.
savedOutput
);
System
.
setErr
(
this
.
savedErr
);
}
private
String
getOutput
()
{
return
this
.
output
.
toString
();
}
@Test
@Test
public
void
testDefaultSettings
()
throws
Exception
{
public
void
testDefaultSettings
()
throws
Exception
{
assertEquals
(
0
,
SpringApplication
.
exit
(
SpringApplication
assertEquals
(
0
,
SpringApplication
.
exit
(
SpringApplication
.
run
(
SampleBatchApplication
.
class
)));
.
run
(
SampleBatchApplication
.
class
)));
String
output
=
getOutput
();
String
output
=
this
.
outputCapture
.
toString
();
assertTrue
(
"Wrong output: "
+
output
,
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"completed with the following parameters"
));
output
.
contains
(
"completed with the following parameters"
));
}
}
...
...
spring-boot-samples/spring-boot-sample-profile/src/test/java/org/springframework/boot/sample/profile/SampleProfileApplicationTests.java
View file @
4038b394
...
@@ -16,30 +16,20 @@
...
@@ -16,30 +16,20 @@
package
org
.
springframework
.
boot
.
sample
.
profile
;
package
org
.
springframework
.
boot
.
sample
.
profile
;
import
java.io.ByteArrayOutputStream
;
import
java.io.PrintStream
;
import
org.junit.After
;
import
org.junit.After
;
import
org.junit.AfterClass
;
import
org.junit.Before
;
import
org.junit.Before
;
import
org.junit.
BeforeClass
;
import
org.junit.
Rule
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.
sample.profile.SampleProfileApplication
;
import
org.springframework.boot.
OutputCapture
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
public
class
SampleProfileApplicationTests
{
public
class
SampleProfileApplicationTests
{
private
static
PrintStream
savedOutput
;
@Rule
private
static
ByteArrayOutputStream
output
;
public
OutputCapture
outputCapture
=
new
OutputCapture
();
private
String
profiles
;
@BeforeClass
private
String
profiles
;
public
static
void
init
()
{
savedOutput
=
System
.
out
;
output
=
new
ByteArrayOutputStream
();
System
.
setOut
(
new
PrintStream
(
output
));
}
@Before
@Before
public
void
before
()
{
public
void
before
()
{
...
@@ -56,19 +46,10 @@ public class SampleProfileApplicationTests {
...
@@ -56,19 +46,10 @@ public class SampleProfileApplicationTests {
}
}
}
}
@AfterClass
public
static
void
clear
()
{
System
.
setOut
(
savedOutput
);
}
private
static
String
getOutput
()
{
return
output
.
toString
();
}
@Test
@Test
public
void
testDefaultProfile
()
throws
Exception
{
public
void
testDefaultProfile
()
throws
Exception
{
SampleProfileApplication
.
main
(
new
String
[
0
]);
SampleProfileApplication
.
main
(
new
String
[
0
]);
String
output
=
getOutput
();
String
output
=
this
.
outputCapture
.
toString
();
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello Phil"
));
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello Phil"
));
}
}
...
@@ -76,7 +57,7 @@ public class SampleProfileApplicationTests {
...
@@ -76,7 +57,7 @@ public class SampleProfileApplicationTests {
public
void
testGoodbyeProfile
()
throws
Exception
{
public
void
testGoodbyeProfile
()
throws
Exception
{
System
.
setProperty
(
"spring.profiles.active"
,
"goodbye"
);
System
.
setProperty
(
"spring.profiles.active"
,
"goodbye"
);
SampleProfileApplication
.
main
(
new
String
[
0
]);
SampleProfileApplication
.
main
(
new
String
[
0
]);
String
output
=
getOutput
();
String
output
=
this
.
outputCapture
.
toString
();
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Goodbye Everyone"
));
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Goodbye Everyone"
));
}
}
...
@@ -84,7 +65,7 @@ public class SampleProfileApplicationTests {
...
@@ -84,7 +65,7 @@ public class SampleProfileApplicationTests {
public
void
testGoodbyeProfileFromCommandline
()
throws
Exception
{
public
void
testGoodbyeProfileFromCommandline
()
throws
Exception
{
SampleProfileApplication
SampleProfileApplication
.
main
(
new
String
[]
{
"--spring.profiles.active=goodbye"
});
.
main
(
new
String
[]
{
"--spring.profiles.active=goodbye"
});
String
output
=
getOutput
();
String
output
=
this
.
outputCapture
.
toString
();
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Goodbye Everyone"
));
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Goodbye Everyone"
));
}
}
...
...
spring-boot-samples/spring-boot-sample-simple/src/test/java/org/springframework/boot/sample/simple/SampleSimpleApplicationTests.java
View file @
4038b394
...
@@ -16,13 +16,11 @@
...
@@ -16,13 +16,11 @@
package
org
.
springframework
.
boot
.
sample
.
simple
;
package
org
.
springframework
.
boot
.
sample
.
simple
;
import
java.io.ByteArrayOutputStream
;
import
java.io.PrintStream
;
import
org.junit.After
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.
sample.simple.SampleSimpleApplication
;
import
org.springframework.boot.
OutputCapture
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
...
@@ -34,17 +32,13 @@ import static org.junit.Assert.assertTrue;
...
@@ -34,17 +32,13 @@ import static org.junit.Assert.assertTrue;
*/
*/
public
class
SampleSimpleApplicationTests
{
public
class
SampleSimpleApplicationTests
{
private
PrintStream
savedOutput
;
@Rule
public
OutputCapture
outputCapture
=
new
OutputCapture
();
private
ByteArrayOutputStream
output
;
private
String
profiles
;
private
String
profiles
;
@Before
@Before
public
void
init
()
{
public
void
init
()
{
this
.
savedOutput
=
System
.
out
;
this
.
output
=
new
ByteArrayOutputStream
();
System
.
setOut
(
new
PrintStream
(
this
.
output
));
this
.
profiles
=
System
.
getProperty
(
"spring.profiles.active"
);
this
.
profiles
=
System
.
getProperty
(
"spring.profiles.active"
);
}
}
...
@@ -56,24 +50,19 @@ public class SampleSimpleApplicationTests {
...
@@ -56,24 +50,19 @@ public class SampleSimpleApplicationTests {
else
{
else
{
System
.
clearProperty
(
"spring.profiles.active"
);
System
.
clearProperty
(
"spring.profiles.active"
);
}
}
System
.
setOut
(
this
.
savedOutput
);
}
private
String
getOutput
()
{
return
this
.
output
.
toString
();
}
}
@Test
@Test
public
void
testDefaultSettings
()
throws
Exception
{
public
void
testDefaultSettings
()
throws
Exception
{
SampleSimpleApplication
.
main
(
new
String
[
0
]);
SampleSimpleApplication
.
main
(
new
String
[
0
]);
String
output
=
getOutput
();
String
output
=
this
.
outputCapture
.
toString
();
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello Phil"
));
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello Phil"
));
}
}
@Test
@Test
public
void
testCommandLineOverrides
()
throws
Exception
{
public
void
testCommandLineOverrides
()
throws
Exception
{
SampleSimpleApplication
.
main
(
new
String
[]
{
"--name=Gordon"
});
SampleSimpleApplication
.
main
(
new
String
[]
{
"--name=Gordon"
});
String
output
=
getOutput
();
String
output
=
this
.
outputCapture
.
toString
();
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello Gordon"
));
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello Gordon"
));
}
}
...
...
spring-boot-samples/spring-boot-sample-xml/src/test/java/org/springframework/boot/sample/xml/SampleSpringXmlApplicationTests.java
View file @
4038b394
...
@@ -16,41 +16,21 @@
...
@@ -16,41 +16,21 @@
package
org
.
springframework
.
boot
.
sample
.
xml
;
package
org
.
springframework
.
boot
.
sample
.
xml
;
import
java.io.ByteArrayOutputStream
;
import
org.junit.Rule
;
import
java.io.PrintStream
;
import
org.junit.AfterClass
;
import
org.junit.BeforeClass
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.
sample.xml.SampleSpringXmlApplication
;
import
org.springframework.boot.
OutputCapture
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
public
class
SampleSpringXmlApplicationTests
{
public
class
SampleSpringXmlApplicationTests
{
private
static
PrintStream
savedOutput
;
@Rule
private
static
ByteArrayOutputStream
output
;
public
OutputCapture
outputCapture
=
new
OutputCapture
();
@BeforeClass
public
static
void
init
()
{
savedOutput
=
System
.
out
;
output
=
new
ByteArrayOutputStream
();
System
.
setOut
(
new
PrintStream
(
output
));
}
@AfterClass
public
static
void
clear
()
{
System
.
setOut
(
savedOutput
);
}
private
static
String
getOutput
()
{
return
output
.
toString
();
}
@Test
@Test
public
void
testDefaultSettings
()
throws
Exception
{
public
void
testDefaultSettings
()
throws
Exception
{
SampleSpringXmlApplication
.
main
(
new
String
[
0
]);
SampleSpringXmlApplication
.
main
(
new
String
[
0
]);
String
output
=
getOutput
();
String
output
=
this
.
outputCapture
.
toString
();
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello World"
));
assertTrue
(
"Wrong output: "
+
output
,
output
.
contains
(
"Hello World"
));
}
}
...
...
spring-boot/src/test/java/org/springframework/boot/OutputCapture.java
0 → 100644
View file @
4038b394
/*
* 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
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.io.PrintStream
;
import
org.junit.rules.TestRule
;
import
org.junit.runner.Description
;
import
org.junit.runners.model.Statement
;
/**
* Capture output from System.out and System.err.
*
* @author Phillip Webb
*/
public
class
OutputCapture
implements
TestRule
{
private
CaptureOutputStream
captureOut
;
private
CaptureOutputStream
captureErr
;
private
ByteArrayOutputStream
copy
;
@Override
public
Statement
apply
(
final
Statement
base
,
Description
description
)
{
return
new
Statement
()
{
@Override
public
void
evaluate
()
throws
Throwable
{
captureOutput
();
try
{
base
.
evaluate
();
}
finally
{
releaseOutput
();
}
}
};
}
protected
void
captureOutput
()
{
this
.
copy
=
new
ByteArrayOutputStream
();
this
.
captureOut
=
new
CaptureOutputStream
(
System
.
out
,
this
.
copy
);
this
.
captureErr
=
new
CaptureOutputStream
(
System
.
err
,
this
.
copy
);
System
.
setOut
(
new
PrintStream
(
this
.
captureOut
));
System
.
setErr
(
new
PrintStream
(
this
.
captureErr
));
}
protected
void
releaseOutput
()
{
System
.
setOut
(
this
.
captureOut
.
getOriginal
());
System
.
setErr
(
this
.
captureErr
.
getOriginal
());
this
.
copy
=
null
;
}
public
String
getOutputAndRelease
()
{
try
{
return
toString
();
}
finally
{
releaseOutput
();
}
}
@Override
public
String
toString
()
{
return
this
.
copy
.
toString
();
}
private
static
class
CaptureOutputStream
extends
OutputStream
{
private
final
PrintStream
original
;
private
final
OutputStream
copy
;
public
CaptureOutputStream
(
PrintStream
original
,
OutputStream
copy
)
{
this
.
original
=
original
;
this
.
copy
=
copy
;
}
@Override
public
void
write
(
int
b
)
throws
IOException
{
this
.
copy
.
write
(
b
);
this
.
original
.
write
(
b
);
this
.
original
.
flush
();
}
@Override
public
void
write
(
byte
[]
b
)
throws
IOException
{
write
(
b
,
0
,
b
.
length
);
}
@Override
public
void
write
(
byte
[]
b
,
int
off
,
int
len
)
throws
IOException
{
this
.
copy
.
write
(
b
,
off
,
len
);
this
.
original
.
write
(
b
,
off
,
len
);
this
.
original
.
flush
();
}
public
PrintStream
getOriginal
()
{
return
this
.
original
;
}
}
}
spring-boot/src/test/java/org/springframework/boot/SimpleMainTests.java
View file @
4038b394
...
@@ -16,14 +16,11 @@
...
@@ -16,14 +16,11 @@
package
org
.
springframework
.
boot
;
package
org
.
springframework
.
boot
;
import
java.io.ByteArrayOutputStream
;
import
java.io.PrintStream
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.List
;
import
org.junit.After
;
import
org.junit.Rule
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.ClassUtils
;
...
@@ -39,22 +36,10 @@ import static org.junit.Assert.assertTrue;
...
@@ -39,22 +36,10 @@ import static org.junit.Assert.assertTrue;
@Configuration
@Configuration
public
class
SimpleMainTests
{
public
class
SimpleMainTests
{
private
static
final
String
SPRING_STARTUP
=
"root of context hierarchy"
;
@Rule
private
PrintStream
savedOutput
;
public
OutputCapture
outputCapture
=
new
OutputCapture
();
private
ByteArrayOutputStream
output
;
@Before
public
void
open
()
{
this
.
savedOutput
=
System
.
out
;
this
.
output
=
new
ByteArrayOutputStream
();
System
.
setOut
(
new
PrintStream
(
this
.
output
));
}
@After
private
static
final
String
SPRING_STARTUP
=
"root of context hierarchy"
;
public
void
after
()
{
System
.
setOut
(
this
.
savedOutput
);
System
.
out
.
println
(
getOutput
());
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
emptyApplicationContext
()
throws
Exception
{
public
void
emptyApplicationContext
()
throws
Exception
{
...
@@ -99,7 +84,7 @@ public class SimpleMainTests {
...
@@ -99,7 +84,7 @@ public class SimpleMainTests {
}
}
private
String
getOutput
()
{
private
String
getOutput
()
{
return
this
.
output
.
toString
();
return
this
.
output
Capture
.
toString
();
}
}
}
}
spring-boot/src/test/java/org/springframework/boot/context/initializer/LoggingApplicationContextInitializerTests.java
View file @
4038b394
...
@@ -16,9 +16,7 @@
...
@@ -16,9 +16,7 @@
package
org
.
springframework
.
boot
.
context
.
initializer
;
package
org
.
springframework
.
boot
.
context
.
initializer
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.PrintStream
;
import
java.util.logging.LogManager
;
import
java.util.logging.LogManager
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.Log
;
...
@@ -29,8 +27,8 @@ import org.junit.Before;
...
@@ -29,8 +27,8 @@ import org.junit.Before;
import
org.junit.Rule
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.rules.TemporaryFolder
;
import
org.junit.rules.TemporaryFolder
;
import
org.springframework.boot.OutputCapture
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.context.initializer.LoggingApplicationContextInitializer
;
import
org.springframework.boot.logging.java.JavaLoggingSystem
;
import
org.springframework.boot.logging.java.JavaLoggingSystem
;
import
org.springframework.context.support.GenericApplicationContext
;
import
org.springframework.context.support.GenericApplicationContext
;
import
org.springframework.core.env.PropertySource
;
import
org.springframework.core.env.PropertySource
;
...
@@ -48,19 +46,15 @@ public class LoggingApplicationContextInitializerTests {
...
@@ -48,19 +46,15 @@ public class LoggingApplicationContextInitializerTests {
@Rule
@Rule
public
TemporaryFolder
temporaryFolder
=
new
TemporaryFolder
();
public
TemporaryFolder
temporaryFolder
=
new
TemporaryFolder
();
@Rule
public
OutputCapture
outputCapture
=
new
OutputCapture
();
private
LoggingApplicationContextInitializer
initializer
=
new
LoggingApplicationContextInitializer
();
private
LoggingApplicationContextInitializer
initializer
=
new
LoggingApplicationContextInitializer
();
private
Log
logger
=
new
SLF4JLogFactory
().
getInstance
(
getClass
());
private
Log
logger
=
new
SLF4JLogFactory
().
getInstance
(
getClass
());
private
PrintStream
savedOutput
;
private
ByteArrayOutputStream
output
;
@Before
@Before
public
void
init
()
throws
SecurityException
,
IOException
{
public
void
init
()
throws
SecurityException
,
IOException
{
this
.
savedOutput
=
System
.
err
;
this
.
output
=
new
ByteArrayOutputStream
();
System
.
setOut
(
new
PrintStream
(
this
.
output
));
LogManager
.
getLogManager
().
readConfiguration
(
LogManager
.
getLogManager
().
readConfiguration
(
JavaLoggingSystem
.
class
.
getResourceAsStream
(
"logging.properties"
));
JavaLoggingSystem
.
class
.
getResourceAsStream
(
"logging.properties"
));
this
.
initializer
.
initialize
(
new
SpringApplication
());
this
.
initializer
.
initialize
(
new
SpringApplication
());
...
@@ -71,12 +65,6 @@ public class LoggingApplicationContextInitializerTests {
...
@@ -71,12 +65,6 @@ public class LoggingApplicationContextInitializerTests {
System
.
clearProperty
(
"LOG_FILE"
);
System
.
clearProperty
(
"LOG_FILE"
);
System
.
clearProperty
(
"LOG_PATH"
);
System
.
clearProperty
(
"LOG_PATH"
);
System
.
clearProperty
(
"PID"
);
System
.
clearProperty
(
"PID"
);
System
.
setOut
(
this
.
savedOutput
);
System
.
out
.
println
(
getOutput
());
}
private
String
getOutput
()
{
return
this
.
output
.
toString
();
}
}
@Test
@Test
...
@@ -84,7 +72,7 @@ public class LoggingApplicationContextInitializerTests {
...
@@ -84,7 +72,7 @@ public class LoggingApplicationContextInitializerTests {
GenericApplicationContext
context
=
new
GenericApplicationContext
();
GenericApplicationContext
context
=
new
GenericApplicationContext
();
this
.
initializer
.
initialize
(
context
);
this
.
initializer
.
initialize
(
context
);
this
.
logger
.
info
(
"Hello world"
);
this
.
logger
.
info
(
"Hello world"
);
String
output
=
getOutput
().
trim
();
String
output
=
this
.
outputCapture
.
toString
().
trim
();
assertTrue
(
"Wrong output:\n"
+
output
,
output
.
contains
(
"Hello world"
));
assertTrue
(
"Wrong output:\n"
+
output
,
output
.
contains
(
"Hello world"
));
assertFalse
(
"Wrong output:\n"
+
output
,
output
.
contains
(
"???"
));
assertFalse
(
"Wrong output:\n"
+
output
,
output
.
contains
(
"???"
));
}
}
...
@@ -104,7 +92,7 @@ public class LoggingApplicationContextInitializerTests {
...
@@ -104,7 +92,7 @@ public class LoggingApplicationContextInitializerTests {
});
});
this
.
initializer
.
initialize
(
context
);
this
.
initializer
.
initialize
(
context
);
this
.
logger
.
info
(
"Hello world"
);
this
.
logger
.
info
(
"Hello world"
);
String
output
=
getOutput
().
trim
();
String
output
=
this
.
outputCapture
.
toString
().
trim
();
assertTrue
(
"Wrong output:\n"
+
output
,
output
.
contains
(
"Hello world"
));
assertTrue
(
"Wrong output:\n"
+
output
,
output
.
contains
(
"Hello world"
));
assertFalse
(
"Wrong output:\n"
+
output
,
output
.
contains
(
"???"
));
assertFalse
(
"Wrong output:\n"
+
output
,
output
.
contains
(
"???"
));
assertTrue
(
"Wrong output:\n"
+
output
,
output
.
startsWith
(
"/tmp/spring.log"
));
assertTrue
(
"Wrong output:\n"
+
output
,
output
.
startsWith
(
"/tmp/spring.log"
));
...
@@ -146,7 +134,7 @@ public class LoggingApplicationContextInitializerTests {
...
@@ -146,7 +134,7 @@ public class LoggingApplicationContextInitializerTests {
this
.
initializer
.
initialize
(
context
);
this
.
initializer
.
initialize
(
context
);
Log
logger
=
LogFactory
.
getLog
(
LoggingApplicationContextInitializerTests
.
class
);
Log
logger
=
LogFactory
.
getLog
(
LoggingApplicationContextInitializerTests
.
class
);
logger
.
info
(
"Hello world"
);
logger
.
info
(
"Hello world"
);
String
output
=
getOutput
().
trim
();
String
output
=
this
.
outputCapture
.
toString
().
trim
();
assertTrue
(
"Wrong output:\n"
+
output
,
output
.
startsWith
(
"foo.log"
));
assertTrue
(
"Wrong output:\n"
+
output
,
output
.
startsWith
(
"foo.log"
));
}
}
...
@@ -169,7 +157,7 @@ public class LoggingApplicationContextInitializerTests {
...
@@ -169,7 +157,7 @@ public class LoggingApplicationContextInitializerTests {
this
.
initializer
.
initialize
(
context
);
this
.
initializer
.
initialize
(
context
);
Log
logger
=
LogFactory
.
getLog
(
LoggingApplicationContextInitializerTests
.
class
);
Log
logger
=
LogFactory
.
getLog
(
LoggingApplicationContextInitializerTests
.
class
);
logger
.
info
(
"Hello world"
);
logger
.
info
(
"Hello world"
);
String
output
=
getOutput
().
trim
();
String
output
=
this
.
outputCapture
.
toString
().
trim
();
assertTrue
(
"Wrong output:\n"
+
output
,
output
.
startsWith
(
"foo/spring.log"
));
assertTrue
(
"Wrong output:\n"
+
output
,
output
.
startsWith
(
"foo/spring.log"
));
}
}
...
...
spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java
View file @
4038b394
...
@@ -16,15 +16,12 @@
...
@@ -16,15 +16,12 @@
package
org
.
springframework
.
boot
.
logging
.
logback
;
package
org
.
springframework
.
boot
.
logging
.
logback
;
import
java.io.ByteArrayOutputStream
;
import
java.io.PrintStream
;
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.After
;
import
org.junit.After
;
import
org.junit.
Befor
e
;
import
org.junit.
Rul
e
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.
logging.logback.LogbackLoggingSystem
;
import
org.springframework.boot.
OutputCapture
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
...
@@ -35,30 +32,17 @@ import static org.junit.Assert.assertTrue;
...
@@ -35,30 +32,17 @@ import static org.junit.Assert.assertTrue;
*/
*/
public
class
LogbackLoggingSystemTests
{
public
class
LogbackLoggingSystemTests
{
@Rule
public
OutputCapture
outputCapture
=
new
OutputCapture
();
private
LogbackLoggingSystem
loggingSystem
=
new
LogbackLoggingSystem
(
getClass
()
private
LogbackLoggingSystem
loggingSystem
=
new
LogbackLoggingSystem
(
getClass
()
.
getClassLoader
());
.
getClassLoader
());
private
PrintStream
savedOutput
;
private
ByteArrayOutputStream
output
;
@Before
public
void
init
()
{
this
.
savedOutput
=
System
.
out
;
this
.
output
=
new
ByteArrayOutputStream
();
System
.
setOut
(
new
PrintStream
(
this
.
output
));
}
@After
@After
public
void
clear
()
{
public
void
clear
()
{
System
.
clearProperty
(
"LOG_FILE"
);
System
.
clearProperty
(
"LOG_FILE"
);
System
.
clearProperty
(
"LOG_PATH"
);
System
.
clearProperty
(
"LOG_PATH"
);
System
.
clearProperty
(
"PID"
);
System
.
clearProperty
(
"PID"
);
System
.
setOut
(
this
.
savedOutput
);
}
private
String
getOutput
()
{
return
this
.
output
.
toString
();
}
}
@Test
@Test
...
@@ -66,7 +50,7 @@ public class LogbackLoggingSystemTests {
...
@@ -66,7 +50,7 @@ public class LogbackLoggingSystemTests {
this
.
loggingSystem
.
initialize
(
"classpath:logback-nondefault.xml"
);
this
.
loggingSystem
.
initialize
(
"classpath:logback-nondefault.xml"
);
Log
logger
=
LogFactory
.
getLog
(
LogbackLoggingSystemTests
.
class
);
Log
logger
=
LogFactory
.
getLog
(
LogbackLoggingSystemTests
.
class
);
logger
.
info
(
"Hello world"
);
logger
.
info
(
"Hello world"
);
String
output
=
getOutput
().
trim
();
String
output
=
this
.
outputCapture
.
toString
().
trim
();
assertTrue
(
"Wrong output:\n"
+
output
,
output
.
contains
(
"Hello world"
));
assertTrue
(
"Wrong output:\n"
+
output
,
output
.
contains
(
"Hello world"
));
assertTrue
(
"Wrong output:\n"
+
output
,
output
.
startsWith
(
"/tmp/spring.log"
));
assertTrue
(
"Wrong output:\n"
+
output
,
output
.
startsWith
(
"/tmp/spring.log"
));
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment