001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.junit;
043:
044: import java.util.ArrayList;
045: import java.util.List;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import junit.framework.TestFailure;
049: import junit.framework.TestResult;
050:
051: /** Checks that behaviour of LoggingTestCaseHid is correct.
052: *
053: * @author Jaroslav Tulach
054: */
055: public class FlowControlTest extends NbTestCase {
056:
057: private Logger err;
058: private Logger list;
059:
060: public FlowControlTest(String name) {
061: super (name);
062: }
063:
064: // protected int timeOut() {
065: // return 10000;
066: // }
067:
068: protected Level logLevel() {
069: return Level.FINE;
070: }
071:
072: protected void setUp() throws Exception {
073: err = Logger.getLogger("TEST-" + getName());
074: list = Logger.getLogger("observe");
075: }
076:
077: public void testCorrectThreadSwitching() throws Exception {
078:
079: class Run implements Runnable {
080: public List<Object> events = new ArrayList<Object>();
081:
082: public void run() {
083: events.add("A");
084: err.info("A");
085: events.add("B");
086: err.info("B");
087: events.add("C");
088: err.info("C");
089: }
090:
091: public void directly() {
092: err.info("0");
093: events.add(new Integer(1));
094: err.info("1");
095: events.add(new Integer(2));
096: err.info("2");
097: events.add(new Integer(3));
098: err.info("3");
099: }
100: }
101:
102: Run run = new Run();
103:
104: String order = "THREAD:Para MSG:A" + "THREAD:main MSG:0"
105: + "THREAD:main MSG:1" + "THREAD:Para MSG:B"
106: + "THREAD:main MSG:2" + "THREAD:Para MSG:C"
107: + "THREAD:main MSG:3";
108: Log.controlFlow(err, list, order, 0);
109:
110: FutureTask task = new FutureTask(run);
111: run.directly();
112: if (!task.waitFinished(10000)) {
113: fail("Runnable deadlocked");
114: }
115:
116: String res = run.events.toString();
117:
118: assertEquals(
119: "Really changing the execution according to the provided order: "
120: + res, "[A, 1, B, 2, C, 3]", res);
121: }
122:
123: public void testWorksWithRegularExpressionsAsWell()
124: throws Exception {
125:
126: class Run implements Runnable {
127: public List<Object> events = new ArrayList<Object>();
128:
129: public void run() {
130: events.add("A");
131: err.info("4329043A");
132: events.add("B");
133: err.info("B");
134: events.add("C");
135: err.info("CCCC");
136: }
137:
138: public void directly() {
139: err.info("0");
140: events.add(new Integer(1));
141: err.info("1");
142: events.add(new Integer(2));
143: err.info("2");
144: events.add(new Integer(3));
145: err.info("3");
146: }
147: }
148:
149: Run run = new Run();
150:
151: String order = "THREAD:Para MSG:[0-9]*A" + "THREAD:main MSG:0"
152: + "THREAD:main MSG:^1$" + "THREAD:Para MSG:B"
153: + "THREAD:main MSG:2" + "THREAD:Para MSG:C+"
154: + "THREAD:main MSG:3";
155: Log.controlFlow(err, list, order, 0);
156:
157: FutureTask task = new FutureTask(run);
158: run.directly();
159: if (!task.waitFinished(10000)) {
160: fail("Runnable deadlocked");
161: }
162:
163: String res = run.events.toString();
164:
165: assertEquals(
166: "Really changing the execution according to the provided order: "
167: + res, "[A, 1, B, 2, C, 3]", res);
168: }
169:
170: public void testLogMessagesCanRepeat() throws Exception {
171:
172: class Run implements Runnable {
173: public List<Object> events = new ArrayList<Object>();
174:
175: public void run() {
176: events.add("A");
177: err.info("A");
178: events.add("A");
179: err.info("A");
180: events.add("A");
181: err.info("A");
182: }
183:
184: public void directly() {
185: err.info("0");
186: events.add(new Integer(1));
187: err.info("1");
188: events.add(new Integer(2));
189: err.info("2");
190: events.add(new Integer(3));
191: err.info("3");
192: }
193: }
194:
195: Run run = new Run();
196:
197: String order = "THREAD:Para MSG:A" + "THREAD:main MSG:0"
198: + "THREAD:main MSG:^1$" + "THREAD:Para MSG:A"
199: + "THREAD:main MSG:2" + "THREAD:Para MSG:A"
200: + "THREAD:main MSG:3";
201: Log.controlFlow(err, list, order, 0);
202:
203: FutureTask task = new FutureTask(run);
204: run.directly();
205: if (!task.waitFinished(10000)) {
206: fail("Runnable deadlocked");
207: }
208:
209: String res = run.events.toString();
210:
211: assertEquals(
212: "Really changing the execution according to the provided order: "
213: + res, "[A, 1, A, 2, A, 3]", res);
214: }
215:
216: private Exception throwIt;
217:
218: public void testRuntimeExceptionsAlsoGenerateLog() throws Exception {
219: if (throwIt != null) {
220: Logger.getLogger("").info("Ahoj");
221: throw throwIt;
222: }
223:
224: FlowControlTest l = new FlowControlTest(
225: "testRuntimeExceptionsAlsoGenerateLog");
226: l.throwIt = new NullPointerException();
227: TestResult res = l.run();
228: assertEquals("No failures", 0, res.failureCount());
229: assertEquals("One error", 1, res.errorCount());
230:
231: Object o = res.errors().nextElement();
232: TestFailure f = (TestFailure) o;
233:
234: if (f.exceptionMessage() == null
235: || f.exceptionMessage().indexOf("Ahoj") == -1) {
236: fail("Logged messages shall be in exception message: "
237: + f.exceptionMessage());
238: }
239: }
240:
241: private static final class FutureTask {
242: private Thread thread;
243:
244: public FutureTask(Runnable delegate) {
245: thread = new Thread(delegate, "Para");
246: thread.start();
247: }
248:
249: private boolean waitFinished(int i) throws InterruptedException {
250: thread.join();
251: return !thread.isAlive();
252: }
253: }
254: }
|