Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30ce62d

Browse files
committed
Applied patch - 6_03_extract_rules
1 parent 03f26d1 commit 30ce62d

2 files changed

Lines changed: 49 additions & 32 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ru.javawebinar.topjava;
2+
3+
import org.junit.rules.ExternalResource;
4+
import org.junit.rules.Stopwatch;
5+
import org.junit.runner.Description;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import java.util.concurrent.TimeUnit;
10+
11+
public class TimingRules {
12+
private static final Logger log = LoggerFactory.getLogger("result");
13+
14+
private static StringBuilder results = new StringBuilder();
15+
16+
// https://dzone.com/articles/applying-new-jdk-11-string-methods
17+
private static String DELIM = "-".repeat(103);
18+
19+
// http://stackoverflow.com/questions/14892125/what-is-the-best-practice-to-determine-the-execution-time-of-the-bussiness-relev
20+
public static final Stopwatch STOPWATCH = new Stopwatch() {
21+
@Override
22+
protected void finished(long nanos, Description description) {
23+
String result = String.format("%-95s %7d", description.getDisplayName(), TimeUnit.NANOSECONDS.toMillis(nanos));
24+
results.append(result).append('\n');
25+
log.info(result + " ms\n");
26+
}
27+
};
28+
29+
public static final ExternalResource SUMMARY = new ExternalResource() {
30+
@Override
31+
protected void before() throws Throwable {
32+
results.setLength(0);
33+
}
34+
35+
@Override
36+
protected void after() {
37+
log.info("\n" + DELIM +
38+
"\nTest Duration, ms" +
39+
"\n" + DELIM + "\n" + results + DELIM + "\n");
40+
}
41+
};
42+
}
Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
package ru.javawebinar.topjava.service;
22

3-
import org.junit.AfterClass;
3+
import org.junit.ClassRule;
44
import org.junit.Rule;
55
import org.junit.rules.ExpectedException;
6+
import org.junit.rules.ExternalResource;
67
import org.junit.rules.Stopwatch;
7-
import org.junit.runner.Description;
88
import org.junit.runner.RunWith;
9-
import org.slf4j.Logger;
109
import org.springframework.test.context.ActiveProfiles;
1110
import org.springframework.test.context.ContextConfiguration;
1211
import org.springframework.test.context.jdbc.Sql;
1312
import org.springframework.test.context.jdbc.SqlConfig;
1413
import org.springframework.test.context.junit4.SpringRunner;
1514
import ru.javawebinar.topjava.ActiveDbProfileResolver;
16-
17-
import java.util.concurrent.TimeUnit;
18-
19-
import static org.slf4j.LoggerFactory.getLogger;
15+
import ru.javawebinar.topjava.TimingRules;
2016

2117
@ContextConfiguration({
2218
"classpath:spring/spring-app.xml",
@@ -26,33 +22,12 @@
2622
@Sql(scripts = "classpath:db/populateDB.sql", config = @SqlConfig(encoding = "UTF-8"))
2723
@ActiveProfiles(resolver = ActiveDbProfileResolver.class)
2824
abstract public class AbstractServiceTest {
29-
private static final Logger log = getLogger("result");
30-
31-
private static StringBuilder results = new StringBuilder();
32-
33-
// https://dzone.com/articles/applying-new-jdk-11-string-methods
34-
private static String DELIM = "-".repeat(103);
25+
@ClassRule
26+
public static ExternalResource summary = TimingRules.SUMMARY;
3527

3628
@Rule
37-
public ExpectedException thrown = ExpectedException.none();
29+
public Stopwatch stopwatch = TimingRules.STOPWATCH;
3830

3931
@Rule
40-
// http://stackoverflow.com/questions/14892125/what-is-the-best-practice-to-determine-the-execution-time-of-the-bussiness-relev
41-
public Stopwatch stopwatch = new Stopwatch() {
42-
@Override
43-
protected void finished(long nanos, Description description) {
44-
String result = String.format("%-95s %7d", description.getDisplayName(), TimeUnit.NANOSECONDS.toMillis(nanos));
45-
results.append(result).append('\n');
46-
log.info(result + " ms\n");
47-
}
48-
};
49-
50-
51-
@AfterClass
52-
public static void printResult() {
53-
log.info("\n" + DELIM +
54-
"\nTest Duration, ms" +
55-
"\n" + DELIM + "\n" + results + DELIM + "\n");
56-
results.setLength(0);
57-
}
32+
public ExpectedException thrown = ExpectedException.none();
5833
}

0 commit comments

Comments
 (0)