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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.core;
041:
042: import java.awt.AWTEvent;
043: import java.awt.EventQueue;
044: import java.util.Collections;
045: import java.util.LinkedList;
046: import java.util.List;
047: import java.util.logging.Handler;
048: import java.util.logging.Level;
049: import java.util.logging.LogRecord;
050: import java.util.logging.Logger;
051: import junit.framework.TestCase;
052: import org.netbeans.junit.NbTestCase;
053: import org.openide.util.Exceptions;
054:
055: /**
056: *
057: * @author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
058: */
059: public class TimeableEventQueueTest extends NbTestCase {
060: static {
061: System.setProperty(
062: "org.netbeans.core.TimeableEventQueue.quantum", "300");
063: System.setProperty(
064: "org.netbeans.core.TimeableEventQueue.pause", "3000");
065: TimeableEventQueue.initialize();
066: }
067: private static CountingHandler handler = new CountingHandler();
068:
069: public TimeableEventQueueTest(String testName) {
070: super (testName);
071: }
072:
073: @Override
074: protected Level logLevel() {
075: return Level.FINEST;
076: }
077:
078: @Override
079: protected void setUp() throws Exception {
080: Logger l = Logger.getLogger("org.netbeans.core");
081: l.setUseParentHandlers(false);
082: l.removeHandler(handler);
083: l.addHandler(handler);
084: handler.records.clear();
085: }
086:
087: @Override
088: protected void tearDown() throws Exception {
089: }
090:
091: public void testDispatchEvent() throws Exception {
092: class Slow implements Runnable {
093: private int ok;
094:
095: public void run() {
096: try {
097: Thread.sleep(1000);
098: } catch (InterruptedException ex) {
099: Exceptions.printStackTrace(ex);
100: }
101: ok++;
102: }
103: }
104: Slow slow = new Slow();
105:
106: EventQueue.invokeAndWait(slow);
107: EventQueue.invokeAndWait(slow);
108:
109: assertEquals("called", 2, slow.ok);
110:
111: assertEquals("Only One report:" + handler.records, 1,
112: handler.records.size());
113: LogRecord r = handler.records.get(0);
114:
115: assertNotNull("Exception present", r.getThrown());
116: if (r.getThrown().getMessage().indexOf("Slow") == -1) {
117: fail("There should be stacktrace from slow:\n"
118: + r.getThrown().getMessage());
119: }
120: boolean found = false;
121: for (StackTraceElement stackTraceElement : r.getThrown()
122: .getStackTrace()) {
123: found = found
124: || stackTraceElement.getClassName().indexOf("Slow") >= 0;
125: }
126: assertTrue("Slow is in the stack trace", found);
127: }
128:
129: private static final class CountingHandler extends Handler {
130: List<LogRecord> records = Collections
131: .synchronizedList(new LinkedList<LogRecord>());
132:
133: @Override
134: public void publish(LogRecord record) {
135: if (record.getLevel().intValue() >= Level.INFO.intValue()) {
136: records.add(record);
137: }
138: }
139:
140: @Override
141: public void flush() {
142: }
143:
144: @Override
145: public void close() throws SecurityException {
146: }
147: }
148: }
|