001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model;
038:
039: import edu.rice.cs.drjava.DrJavaTestCase;
040: import edu.rice.cs.drjava.model.GlobalModelTestCase.TestListener;
041:
042: /**
043: * Tests the functionality of the class that notifies listeners
044: * of a global model.
045: * @version $Id: EventNotifierTest.java 4255 2007-08-28 19:17:37Z mgricken $
046: */
047: public final class EventNotifierTest extends DrJavaTestCase {
048:
049: protected GlobalEventNotifier _notifier;
050:
051: public void setUp() throws Exception {
052: super .setUp();
053: _notifier = new GlobalEventNotifier();
054: }
055:
056: public void tearDown() throws Exception {
057: _notifier = null;
058: super .tearDown();
059: }
060:
061: /**
062: * Checks that the notifier adds and removes listeners correctly,
063: * notifying the correct ones on a particular event.
064: */
065: public void testAddAndRemoveListeners() {
066: TestListener listener1 = new TestListener() {
067: public void junitSuiteStarted(int numTests) {
068: junitSuiteStartedCount++;
069: }
070:
071: public void interpreterExited(int status) {
072: interpreterExitedCount++;
073: }
074: };
075: TestListener listener2 = new TestListener() {
076: public void junitSuiteStarted(int numTests) {
077: junitSuiteStartedCount++;
078: }
079: };
080:
081: _notifier.addListener(listener1);
082: _notifier.addListener(listener2);
083: _notifier.junitSuiteStarted(1);
084:
085: listener1.assertJUnitSuiteStartedCount(1);
086: listener2.assertJUnitSuiteStartedCount(1);
087:
088: //remove one listener and fire another event
089: _notifier.removeListener(listener2);
090: _notifier.interpreterExited(1);
091:
092: listener1.assertInterpreterExitedCount(1);
093: listener2.assertInterpreterExitedCount(0);
094: }
095:
096: /**
097: * Checks that the notifier can poll multiple listeners.
098: */
099: public void testPollListeners() {
100: TestListener trueListener = new TestListener() {
101: public boolean canAbandonFile(OpenDefinitionsDocument doc) {
102: return true;
103: }
104: };
105: TestListener falseListener = new TestListener() {
106: public boolean canAbandonFile(OpenDefinitionsDocument doc) {
107: return false;
108: }
109: };
110:
111: // Make sure trueListener says yes
112: _notifier.addListener(trueListener);
113: boolean result = _notifier.canAbandonFile(null);
114: assertTrue("should be able to abandon file", result);
115:
116: // Make sure falseListener says no
117: _notifier.addListener(falseListener);
118: result = _notifier.canAbandonFile(null);
119: assertTrue("should not be able to abandon file", !result);
120: }
121: }
|