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.Random;
045: import java.util.logging.Level;
046: import java.util.logging.Logger;
047: import junit.framework.TestCase;
048: import junit.framework.*;
049:
050: /**
051: *
052: * @author Jaroslav Tulach
053: */
054: public class FlowCountingTest extends NbTestCase {
055: Logger LOG;
056: CharSequence MSG;
057:
058: public FlowCountingTest(String testName) {
059: super (testName);
060: }
061:
062: protected void setUp() throws Exception {
063: LOG = Logger.getLogger(getName());
064: LOG.setLevel(Level.FINE);
065: MSG = Log.enable("", Level.WARNING);
066: }
067:
068: protected void tearDown() throws Exception {
069: }
070:
071: protected Level logLevel() {
072: return Level.FINEST;
073: }
074:
075: public void testFirstPrints5ThenSecond2AndThenFirst6()
076: throws Exception {
077: org.netbeans.junit.Log.controlFlow(LOG, Logger.global,
078: "THREAD: 1st MSG: cnt: 5" + "THREAD: 2nd MSG: cnt: 2"
079: + "THREAD: 2nd MSG: cnt: 3"
080: + "THREAD: 1st MSG: cnt: 6", 5000);
081: Parael.doCount(LOG);
082:
083: String msg = MSG.toString();
084: // the reason why we check for cnt: 4 here is because of order of Handlers
085: // the thread that does the logging of cnt: 5 is blocked sooner than
086: // its messages gets into the MSG char sequence...
087: int index1 = msg.indexOf("THREAD: 1st MSG: cnt: 4");
088: int index2 = msg.indexOf("THREAD: 2nd MSG: cnt: 2");
089: int index3 = msg.indexOf("THREAD: 1st MSG: cnt: 6");
090:
091: if (index1 == -1)
092: fail("index1 is -1 in: " + msg);
093: if (index2 == -1)
094: fail("index2 is -1 in: " + msg);
095: if (index3 == -1)
096: fail("index3 is -1 in: " + msg);
097:
098: if (index2 < index1)
099: fail("index2[" + index2 + "] < index1[" + index1 + "]: "
100: + msg);
101: if (index3 < index2)
102: fail("index3[" + index3 + "] < index2[" + index2 + "]: "
103: + msg);
104: }
105:
106: private static class Parael implements Runnable {
107: private Logger log;
108:
109: public Parael(Logger log) {
110: this .log = log;
111: }
112:
113: public void run() {
114: Random r = new Random();
115: for (int i = 0; i < 10; i++) {
116: try {
117: Thread.sleep(r.nextInt(100));
118: } catch (InterruptedException ex) {
119: }
120: log.log(Level.WARNING, "cnt: {0}", new Integer(i));
121: }
122: }
123:
124: public static void doCount(Logger log)
125: throws InterruptedException {
126: Parael p = new Parael(log);
127: Thread t1 = new Thread(p, "1st");
128: Thread t2 = new Thread(p, "2nd");
129: t1.start();
130: t2.start();
131: t1.join();
132: t2.join();
133: }
134: }
135:
136: }
|