summaryrefslogtreecommitdiffstats
path: root/testing-client/testsrc/org/aspectj/testing/TesterTest.java
blob: c2fceb81af99051742b8261f0256c9721dbd9c8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* *******************************************************************
 * Copyright (c) 1999-2001 Xerox Corporation, 
 *               2002 Palo Alto Research Center, Incorporated (PARC).
 * All rights reserved. 
 * This program and the accompanying materials are made available 
 * under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 * Contributors: 
 *     Xerox/PARC     initial implementation 
 * ******************************************************************/


package org.aspectj.testing;

import org.aspectj.bridge.IMessage;
import org.aspectj.bridge.IMessageHandler;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import junit.framework.TestCase;
import junit.textui.TestRunner;

/**
 * Test the Tester client API's.
 * See also tests/harness/*.java and tests/ajcHarnessTests.xml for harness-
 * driven Tester tests.
 * @author isberg
 */
public class TesterTest extends TestCase {


   private static final String ME 
        = "org.aspectj.testing.TesterTest";

    /** @param args ignored */
    public static void main(String[] args) {
        TestRunner.main(new String[] {ME});
    }

    /**
     * Constructor for TesterTest.
     * @param arg0
     */
    public TesterTest(String arg0) {
        super(arg0);
    }
    
    /**
     * Test the usage pattern
     * <pre>Tester.event("foo");
     * Tester.checkEvents(new String[] { "foo" }); </pre>
     */
    public void testEventArrayPattern() {
        MyTestReporter reporter = new MyTestReporter();
        Tester.setMessageHandler(reporter);
        
        //--------- positive test - got expected events
        reporter.clear();
        Tester.clear();        
        Tester.event("one");
        Tester.event("two");
        Tester.checkEvents(new String[] { "one", "two"});
        reporter.assertSize(0);

        //--------- failed to get expected events
        reporter.clear();
        Tester.clear();
        Tester.checkEvents(new String[] { "one"});
        assertTrue(reporter.gotFail("one"));
        reporter.assertSize(1);

        //--------- got and didn't get expected events
        reporter.clear();
        Tester.clear();        
        Tester.event("one");
        Tester.event("two");
        Tester.checkEvents(new String[] { "one", "two", "three"});
        reporter.assertSize(1);
        assertTrue(reporter.gotFail("three"));
    }
        
    /**
     * Test the usage pattern
     * <pre>Tester.event("foo");
     * Tester.expectEvent("foo");
     * Tester.checkAllEvents();</pre>
     */
    public void testEventStringPattern() {
        MyTestReporter reporter = new MyTestReporter();
        Tester.setMessageHandler(reporter);
        
        //--------- positive test - got expected events
        reporter.clear();
        Tester.clear();        
        Tester.event("one");
        Tester.event("two");
        Tester.expectEvent("one");
        Tester.expectEvent("two");
        Tester.checkAllEvents();
        reporter.assertSize(0);

        //--------- failed to get expected events
        reporter.clear();
        Tester.clear();
        Tester.expectEvent("one");
        Tester.checkAllEvents();
        assertTrue(reporter.gotFail("one"));
        reporter.assertSize(1);

        //--------- got and didn't get expected events
        reporter.clear();
        Tester.clear();        
        Tester.expectEvent("one");
        Tester.expectEvent("two");
        Tester.expectEvent("three");
        Tester.event("one");
        Tester.event("two");
        Tester.checkAllEvents();
        assertTrue(reporter.gotFail("three"));
        reporter.assertSize(1);
    }
    
    /**
     * Test the usage pattern
     * <pre>Tester.note("foo");
     * Tester.check("foo");</pre>
     */
    public void testNotePattern() {
        MyTestReporter reporter = new MyTestReporter();
        Tester.setMessageHandler(reporter);
        
        //--------- positive test - got expected events
        reporter.clear();
        Tester.clear();        
        Tester.note("one");
        Tester.note("two");
        Tester.check("one");
        Tester.check("two");
        reporter.assertSize(0);

        //--------- failed to get expected events
        reporter.clear();
        Tester.clear();
        Tester.check("one");
        Tester.checkAllEvents();
        assertTrue(reporter.gotFail("one"));
        reporter.assertSize(1);

        //--------- got and didn't get expected events
        reporter.clear();
        Tester.clear();        
        Tester.note("one");
        Tester.check("one");
        Tester.note("two");
        Tester.check("two");
        Tester.check("three");
        assertTrue(reporter.gotFail("three"));
        reporter.assertSize(1);
    }
  
    /**
     * Stub to record failures emitted by Tester.
     * @author isberg
     */
    public static class MyTestReporter implements IMessageHandler {
        public ArrayList failures = new ArrayList();
        public ArrayList passes = new ArrayList();

        public void clear() {
            failures.clear();
            passes.clear();
        }

        void assertSize(int size) {
            assertTrue(-1 < size);
            assertTrue("failures: " + failures, size == failures.size());
        }
        
        boolean gotPass(String substring) {
            return gotItem(passes, substring);
        }
        
        boolean gotFail(String substring) {
            return gotItem(failures, substring);
        }
        
        boolean gotItem(List list, String substring) {
            for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
				IMessage element = (IMessage) iterator.next();
                String s = element.getMessage();
				if ((null != s) && (-1 != s.indexOf(substring))) {
                    return true;
                }
			}
            return false;
        }
        
        public boolean isIgnoring(IMessage.Kind kind) {
            return false;
        }

        public void dontIgnore(IMessage.Kind kind) {
            ;
        }

        public void ignore(IMessage.Kind kind) {
		}

        public boolean handleMessage(IMessage message) {
            (message.isFailed() ? failures : passes).add(message);
            return true;
        }
    }
}                
//        /**
//         * @see ReporterI#abortWithFailure(String, Throwable)
//         */
//        public void abortWithFailure(String message, Throwable exception) {
//            if (null == exception) {
//                check(message, true); 
//            } else {
//                String s = message + Util.unqualifiedClassName(exception)
//                    + ": " + exception.getMessage();
//                check(s, false);
//            }
//        }
//
//        /**
//         * @see ReporterI#check(String, boolean)
//         */
//        public boolean check(String message, boolean passed) {
//            (!passed ? failures : passes).add(message);
//            return passed;
//        }
//