Skip to content

Commit e24189d

Browse files
committed
Removing code duplications
1 parent e32c331 commit e24189d

File tree

5 files changed

+38
-70
lines changed

5 files changed

+38
-70
lines changed

ejb/timer/src/test/java/org/javaee7/ejb/timer/AutomaticTimerBeanTest.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static com.jayway.awaitility.Awaitility.*;
1818
import static org.hamcrest.MatcherAssert.*;
1919
import static org.hamcrest.Matchers.*;
20+
import static org.javaee7.ejb.timer.WithinWindowMatcher.withinWindow;
2021

2122
/**
2223
* author: Jakub Marchwicki
@@ -38,7 +39,7 @@ public static WebArchive deploy() {
3839

3940
return ShrinkWrap.create(WebArchive.class)
4041
.addAsLibraries(jars)
41-
.addClasses(Ping.class, PingsListener.class, AutomaticTimerBean.class);
42+
.addClasses(WithinWindowMatcher.class, Ping.class, PingsListener.class, AutomaticTimerBean.class);
4243
}
4344

4445
@Test
@@ -52,20 +53,4 @@ public void should_receive_two_pings() {
5253
System.out.println("Actual timeout = " + delay);
5354
assertThat(delay, is(withinWindow(TIMEOUT, TOLERANCE)));
5455
}
55-
56-
private Matcher<Long> withinWindow(final long timeout, final long tolerance) {
57-
return new BaseMatcher<Long>() {
58-
@Override
59-
public boolean matches(Object item) {
60-
final Long actual = (Long) item;
61-
return Math.abs(actual - timeout) < tolerance;
62-
}
63-
64-
@Override
65-
public void describeTo(Description description) {
66-
//To change body of implemented methods use File | Settings | File Templates.
67-
}
68-
};
69-
}
70-
7156
}

ejb/timer/src/test/java/org/javaee7/ejb/timer/MultipleScheduleTimerBeanTest.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.hamcrest.MatcherAssert.assertThat;
2020
import static org.hamcrest.Matchers.equalTo;
2121
import static org.hamcrest.Matchers.is;
22+
import static org.javaee7.ejb.timer.WithinWindowMatcher.withinWindow;
2223

2324
/**
2425
* author: Jacek Jackowiak
@@ -40,7 +41,7 @@ public static WebArchive deploy() {
4041

4142
return ShrinkWrap.create(WebArchive.class)
4243
.addAsLibraries(jars)
43-
.addClasses(Ping.class, PingsListener.class, MultipleScheduleTimerBean.class);
44+
.addClasses(WithinWindowMatcher.class, Ping.class, PingsListener.class, MultipleScheduleTimerBean.class);
4445
}
4546

4647
@Test
@@ -58,20 +59,4 @@ public void should_receive_three_pings() {
5859
long smallerDelay = Math.min(delay, delay2);
5960
assertThat(smallerDelay, is(withinWindow(TIMEOUT, TOLERANCE)));
6061
}
61-
62-
private Matcher<Long> withinWindow(final long timeout, final long tolerance) {
63-
return new BaseMatcher<Long>() {
64-
@Override
65-
public boolean matches(Object item) {
66-
final Long actual = (Long) item;
67-
return Math.abs(actual - timeout) < tolerance;
68-
}
69-
70-
@Override
71-
public void describeTo(Description description) {
72-
//To change body of implemented methods use File | Settings | File Templates.
73-
}
74-
};
75-
}
76-
7762
}

ejb/timer/src/test/java/org/javaee7/ejb/timer/ProgrammaticTimerBeanTest.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.hamcrest.MatcherAssert.assertThat;
2020
import static org.hamcrest.Matchers.equalTo;
2121
import static org.hamcrest.Matchers.is;
22+
import static org.javaee7.ejb.timer.WithinWindowMatcher.withinWindow;
2223

2324
/**
2425
* author: Jacek Jackowiak
@@ -40,7 +41,7 @@ public static WebArchive deploy() {
4041

4142
return ShrinkWrap.create(WebArchive.class)
4243
.addAsLibraries(jars)
43-
.addClasses(Ping.class, PingsListener.class, ProgrammaticTimerBean.class);
44+
.addClasses(WithinWindowMatcher.class, Ping.class, PingsListener.class, ProgrammaticTimerBean.class);
4445
}
4546

4647
@Test
@@ -54,20 +55,4 @@ public void should_receive_two_pings() {
5455
System.out.println("Actual timeout = " + delay);
5556
assertThat(delay, is(withinWindow(TIMEOUT, TOLERANCE)));
5657
}
57-
58-
private Matcher<Long> withinWindow(final long timeout, final long tolerance) {
59-
return new BaseMatcher<Long>() {
60-
@Override
61-
public boolean matches(Object item) {
62-
final Long actual = (Long) item;
63-
return Math.abs(actual - timeout) < tolerance;
64-
}
65-
66-
@Override
67-
public void describeTo(Description description) {
68-
//To change body of implemented methods use File | Settings | File Templates.
69-
}
70-
};
71-
}
72-
7358
}

ejb/timer/src/test/java/org/javaee7/ejb/timer/SchedulesTimerBeanTest.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.javaee7.ejb.timer;
22

3-
import org.hamcrest.BaseMatcher;
4-
import org.hamcrest.Description;
53
import org.hamcrest.Matcher;
64
import org.jboss.arquillian.container.test.api.Deployment;
75
import org.jboss.arquillian.junit.Arquillian;
@@ -19,6 +17,7 @@
1917
import static org.hamcrest.MatcherAssert.assertThat;
2018
import static org.hamcrest.Matchers.equalTo;
2119
import static org.hamcrest.Matchers.is;
20+
import static org.javaee7.ejb.timer.WithinWindowMatcher.withinWindow;
2221

2322
/**
2423
* author: Jacek Jackowiak
@@ -40,7 +39,7 @@ public static WebArchive deploy() {
4039

4140
return ShrinkWrap.create(WebArchive.class)
4241
.addAsLibraries(jars)
43-
.addClasses(Ping.class, PingsListener.class, SchedulesTimerBean.class);
42+
.addClasses(WithinWindowMatcher.class, Ping.class, PingsListener.class, SchedulesTimerBean.class);
4443
}
4544

4645
@Test
@@ -58,20 +57,4 @@ public void should_receive_three_pings() {
5857
long smallerDelay = Math.min(delay, delay2);
5958
assertThat(smallerDelay, is(withinWindow(TIMEOUT, TOLERANCE)));
6059
}
61-
62-
private Matcher<Long> withinWindow(final long timeout, final long tolerance) {
63-
return new BaseMatcher<Long>() {
64-
@Override
65-
public boolean matches(Object item) {
66-
final Long actual = (Long) item;
67-
return Math.abs(actual - timeout) < tolerance;
68-
}
69-
70-
@Override
71-
public void describeTo(Description description) {
72-
//To change body of implemented methods use File | Settings | File Templates.
73-
}
74-
};
75-
}
76-
7760
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.javaee7.ejb.timer;
2+
3+
import org.hamcrest.BaseMatcher;
4+
import org.hamcrest.Description;
5+
import org.hamcrest.Matcher;
6+
7+
class WithinWindowMatcher extends BaseMatcher<Long> {
8+
private final long timeout;
9+
private final long tolerance;
10+
11+
public WithinWindowMatcher(long timeout, long tolerance) {
12+
this.timeout = timeout;
13+
this.tolerance = tolerance;
14+
}
15+
16+
@Override
17+
public boolean matches(Object item) {
18+
final Long actual = (Long) item;
19+
return Math.abs(actual - timeout) < tolerance;
20+
}
21+
22+
@Override
23+
public void describeTo(Description description) {
24+
//To change body of implemented methods use File | Settings | File Templates.
25+
}
26+
27+
public static Matcher<Long> withinWindow(final long timeout, final long tolerance) {
28+
return new WithinWindowMatcher(timeout, tolerance);
29+
}
30+
}

0 commit comments

Comments
 (0)