1+ package org .javaee7 .ejb .timer ;
2+
3+ import org .hamcrest .BaseMatcher ;
4+ import org .hamcrest .Description ;
5+ import org .hamcrest .Matcher ;
6+ import org .jboss .arquillian .container .test .api .Deployment ;
7+ import org .jboss .arquillian .junit .Arquillian ;
8+ import org .jboss .shrinkwrap .api .ShrinkWrap ;
9+ import org .jboss .shrinkwrap .api .spec .WebArchive ;
10+ import org .jboss .shrinkwrap .resolver .api .maven .Maven ;
11+ import org .junit .Test ;
12+ import org .junit .runner .RunWith ;
13+
14+ import javax .inject .Inject ;
15+ import java .io .File ;
16+
17+ import static com .jayway .awaitility .Awaitility .await ;
18+ import static com .jayway .awaitility .Awaitility .to ;
19+ import static org .hamcrest .MatcherAssert .assertThat ;
20+ import static org .hamcrest .Matchers .equalTo ;
21+ import static org .hamcrest .Matchers .is ;
22+
23+ /**
24+ * author: Jacek Jackowiak
25+ */
26+ @ RunWith (Arquillian .class )
27+ public class ProgrammaticTimerBeanTest {
28+
29+ final static long TIMEOUT = 5000l ;
30+ final static long TOLERANCE = 1000l ;
31+
32+ @ Inject
33+ PingsListener pings ;
34+
35+ @ Deployment
36+ public static WebArchive deploy () {
37+ File [] jars = Maven .resolver ().loadPomFromFile ("pom.xml" )
38+ .resolve ("com.jayway.awaitility:awaitility" )
39+ .withTransitivity ().asFile ();
40+
41+ return ShrinkWrap .create (WebArchive .class )
42+ .addAsLibraries (jars )
43+ .addClasses (Ping .class , PingsListener .class , ProgrammaticTimerBean .class );
44+ }
45+
46+ @ Test
47+ public void should_receive_two_pings () {
48+ await ().untilCall (to (pings .getPings ()).size (), equalTo (2 ));
49+
50+ Ping firstPing = pings .getPings ().get (0 );
51+ Ping secondPing = pings .getPings ().get (1 );
52+
53+ long delay = secondPing .getTime () - firstPing .getTime ();
54+ System .out .println ("Actual timeout = " + delay );
55+ assertThat (delay , is (withinWindow (TIMEOUT , TOLERANCE )));
56+ }
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+
73+ }
0 commit comments