3737import java .util .List ;
3838import java .util .concurrent .TimeUnit ;
3939
40+ import static org .junit .Assert .assertEquals ;
41+ import static org .junit .Assert .assertNull ;
42+ import static org .junit .Assert .assertTrue ;
43+
4044public class TestCommandListener implements CommandListener {
4145 private final List <CommandEvent > events = new ArrayList <CommandEvent >();
42- private int firstRequestId = RequestMessage .getCurrentGlobalId ();
4346 private static final CodecRegistry CODEC_REGISTRY_HACK ;
4447
4548 static {
@@ -59,7 +62,6 @@ public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) {
5962
6063 public void reset () {
6164 events .clear ();
62- firstRequestId = RequestMessage .getCurrentGlobalId ();
6365 }
6466
6567 public List <CommandEvent > getEvents () {
@@ -92,79 +94,54 @@ public void commandFailed(final CommandFailedEvent event) {
9294 events .add (event );
9395 }
9496
95- public boolean eventsWereDelivered (final List <CommandEvent > expectedEvents ) {
96- if (expectedEvents .size () != events .size ()) {
97- return false ;
98- }
99- int currentlyExpectedRequestId = firstRequestId ;
97+ public void eventsWereDelivered (final List <CommandEvent > expectedEvents ) {
98+ assertEquals (expectedEvents .size (), events .size ());
99+
100+ int currentlyExpectedRequestId = 0 ;
100101 for (int i = 0 ; i < events .size (); i ++) {
101102 CommandEvent actual = events .get (i );
102103 CommandEvent expected = expectedEvents .get (i );
103- if (!actual .getClass ().equals (expected .getClass ())) {
104- return false ;
105- }
106104
107- if (actual .getRequestId () != currentlyExpectedRequestId ) {
108- return false ;
109- }
105+ assertEquals (expected .getClass (), actual .getClass ());
110106
111- if (!(actual instanceof CommandStartedEvent )) {
112- currentlyExpectedRequestId ++;
107+ if (actual instanceof CommandStartedEvent ) {
108+ currentlyExpectedRequestId = actual .getRequestId ();
109+ } else {
110+ assertEquals (currentlyExpectedRequestId , actual .getRequestId ());
113111 }
114112
115- if (!actual .getConnectionDescription ().equals (expected .getConnectionDescription ())) {
116- return false ;
117- }
113+ assertEquals (expected .getConnectionDescription (), actual .getConnectionDescription ());
118114
119- if (!actual .getCommandName ().equals (expected .getCommandName ())) {
120- return false ;
121- }
115+ assertEquals (expected .getCommandName (), actual .getCommandName ());
122116
123117 if (actual .getClass ().equals (CommandStartedEvent .class )) {
124- if (!isEquivalent ((CommandStartedEvent ) actual , (CommandStartedEvent ) expected )) {
125- return false ;
126- }
118+ assertEquivalence ((CommandStartedEvent ) actual , (CommandStartedEvent ) expected );
127119 } else if (actual .getClass ().equals (CommandSucceededEvent .class )) {
128- if (!isEquivalent ((CommandSucceededEvent ) actual , (CommandSucceededEvent ) expected )) {
129- return false ;
130- }
120+ assertEquivalence ((CommandSucceededEvent ) actual , (CommandSucceededEvent ) expected );
131121 } else if (actual .getClass ().equals (CommandFailedEvent .class )) {
132- if (!isEquivalent ((CommandFailedEvent ) actual , (CommandFailedEvent ) expected )) {
133- return false ;
134- }
122+ assertEquivalence ((CommandFailedEvent ) actual , (CommandFailedEvent ) expected );
135123 } else {
136124 throw new UnsupportedOperationException ("Unsupported event type: " + actual .getClass ());
137125 }
138126 }
139-
140- return true ;
141127 }
142128
143- private boolean isEquivalent (final CommandFailedEvent actual , final CommandFailedEvent expected ) {
144- if (!actual .getThrowable ().equals (expected .getThrowable ())) {
145- return false ;
146- }
147- return true ;
129+ private void assertEquivalence (final CommandFailedEvent actual , final CommandFailedEvent expected ) {
130+ assertEquals (expected .getThrowable (), actual .getThrowable ());
148131 }
149132
150- private boolean isEquivalent (final CommandSucceededEvent actual , final CommandSucceededEvent expected ) {
133+ private void assertEquivalence (final CommandSucceededEvent actual , final CommandSucceededEvent expected ) {
151134 if (actual .getResponse () == null ) {
152- return expected .getResponse () == null ;
135+ assertNull (expected .getResponse ());
136+ } else {
137+ // ignore extra elements in the actual response
138+ assertTrue ("Expected response contains elements not in the actual response" ,
139+ actual .getResponse ().entrySet ().containsAll (expected .getResponse ().entrySet ()));
153140 }
154- // ignore extra elements in the actual response
155- if (!actual .getResponse ().entrySet ().containsAll (expected .getResponse ().entrySet ())) {
156- return false ;
157- }
158- return true ;
159141 }
160142
161- private boolean isEquivalent (final CommandStartedEvent actual , final CommandStartedEvent expected ) {
162- if (!actual .getDatabaseName ().equals (expected .getDatabaseName ())) {
163- return false ;
164- }
165- if (!actual .getCommand ().equals (expected .getCommand ())) {
166- return false ;
167- }
168- return true ;
143+ private void assertEquivalence (final CommandStartedEvent actual , final CommandStartedEvent expected ) {
144+ assertEquals (expected .getDatabaseName (), actual .getDatabaseName ());
145+ assertEquals (expected .getCommand (), actual .getCommand ());
169146 }
170147}
0 commit comments