View Javadoc

1   // Copyright (C) 2004 - 2009 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.console.common;
23  
24  import java.lang.reflect.Method;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import junit.framework.TestCase;
29  import net.grinder.testutility.CallData;
30  import net.grinder.testutility.RandomObjectFactory;
31  import net.grinder.testutility.RandomStubFactory;
32  
33  
34  /**
35   *  Unit test case for {@link ErrorQueue}.
36   *
37   * @author Philip Aston
38   */
39  public class TestErrorQueue extends TestCase {
40  
41    public void testErrorQueue() throws Exception {
42  
43      final ErrorQueue errorQueue = new ErrorQueue();
44  
45      final RandomObjectFactory randomObjectFactory = new RandomObjectFactory();
46  
47      final Method[] methods = ErrorQueue.class.getMethods();
48  
49      final List<CallData> callDataList = new ArrayList<CallData>();
50  
51      // Call without delegate.
52      for (int i = 0; i < methods.length; ++i) {
53        final Method method = methods[i];
54  
55        if (method.getName().startsWith("handle")) {
56          final Object[] parameters =
57            randomObjectFactory.generateParameters(method.getParameterTypes());
58  
59          callDataList.add(new CallData(method, (Object)null, parameters));
60  
61          method.invoke(errorQueue, parameters);
62        }
63      }
64  
65      // Set delegate and assert that it gets the pending events.
66      final RandomStubFactory<ErrorHandler> delegateErrorHandlerStubFactory =
67        RandomStubFactory.create(ErrorHandler.class);
68      errorQueue.setErrorHandler(delegateErrorHandlerStubFactory.getStub());
69  
70      for (CallData data : callDataList) {
71        delegateErrorHandlerStubFactory.assertSuccess(data.getMethodName(),
72                                                      data.getParameters());
73      }
74  
75      delegateErrorHandlerStubFactory.assertNoMoreCalls();
76  
77      // Delegate should get all new pending events.
78      for (int i = 0; i < methods.length; ++i) {
79        final String methodName = methods[i].getName();
80  
81        if (methodName.startsWith("handle")) {
82          final Object[] parameters =
83            randomObjectFactory.generateParameters(
84              methods[i].getParameterTypes());
85  
86          methods[i].invoke(errorQueue, parameters);
87  
88          delegateErrorHandlerStubFactory.assertSuccess(methodName, parameters);
89        }
90      }
91  
92      delegateErrorHandlerStubFactory.assertNoMoreCalls();
93  
94      // Set no delegate, assert last delegate gets no new events.
95      errorQueue.setErrorHandler(null);
96  
97      for (int i = 0; i < methods.length; ++i) {
98        final Method method = methods[i];
99  
100       if (method.getName().startsWith("handle")) {
101         final Object[] parameters =
102           randomObjectFactory.generateParameters(method.getParameterTypes());
103 
104         callDataList.add(new CallData(method, null, parameters));
105 
106         method.invoke(errorQueue, parameters);
107       }
108     }
109 
110     delegateErrorHandlerStubFactory.assertNoMoreCalls();
111   }
112 }