PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Fest/src/org/gjt/sp/jedit/testframework/EBFixture.java

#
Java | 173 lines | 91 code | 25 blank | 57 comment | 8 complexity | df60046c5a691d8fdd0f440553adf2f1 MD5 | raw file
  1/*
  2 * EBFixture.java
  3 * :folding=explicit:collapseFolds=1:
  4 *
  5 * Copyright (C) 2009-2010 Eric Le Lay
  6 *
  7 * The XML plugin is licensed under the GNU General Public License, with
  8 * the following exception:
  9 *
 10 * "Permission is granted to link this code with software released under
 11 * the Apache license version 1.1, for example used by the Xerces XML
 12 * parser package."
 13 */
 14package org.gjt.sp.jedit.testframework;
 15
 16// {{{ jUnit imports 
 17import java.util.concurrent.TimeUnit;
 18
 19import org.junit.*;
 20import static org.junit.Assert.*;
 21
 22import org.fest.swing.fixture.*;
 23import org.fest.swing.core.*;
 24import org.fest.swing.finder.*;
 25import org.fest.swing.edt.*;
 26import org.fest.swing.timing.*;
 27import org.fest.swing.exception.WaitTimedOutError;
 28
 29import static org.fest.assertions.Assertions.*;
 30
 31import org.gjt.sp.jedit.testframework.Log;
 32import org.gjt.sp.jedit.testframework.TestUtils;
 33
 34
 35// }}}
 36
 37import java.io.*;
 38import java.awt.Dialog;
 39
 40import org.gjt.sp.jedit.jEdit;
 41import org.gjt.sp.jedit.PluginJAR;
 42
 43import org.gjt.sp.jedit.EditBus;
 44import org.gjt.sp.jedit.EBComponent;
 45import org.gjt.sp.jedit.EBMessage;
 46import org.gjt.sp.jedit.msg.PluginUpdate;
 47
 48/**
 49 * Try to wrap nicely the EditBus for tests
 50 * $Id: EBFixture.java 21245 2012-03-04 15:15:56Z kerik-sf $
 51 */
 52public class EBFixture{
 53	
 54	/**
 55	 * register for a certain message, run the runnable r, wait for the message and the runnable.
 56	 * @param	r	runnable to run asyncronously
 57	 * @param	condition	message to wait for
 58	 * @param	timeout	how long to wait for the message
 59	 */
 60	public static void doInBetween(Runnable r,EBCondition condition, long timeout){
 61		MessageListener listen =  new MessageListener();
 62		listen.registerForMessage(condition);
 63		Thread t = new Thread(r);
 64		t.start();
 65		listen.waitForMessage(timeout);
 66		try{
 67			t.join(timeout);
 68		}catch(InterruptedException e){
 69			fail("Interrupted !");
 70		}
 71	}
 72	
 73	/**
 74	 * blocks until an EBMessage of class clazz happened, or fail after timeout.
 75	 * @param	clazz	class to wait for
 76	 * @param	timeout	how long to wait for the message
 77	 */
 78	public static void simplyWaitForMessageOfClass(final Class clazz, long timeout){
 79		MessageListener listen = new MessageListener();
 80		listen.registerForMessage(messageOfClassCondition(clazz));
 81		listen.waitForMessage(timeout);
 82	}
 83	
 84	/**
 85	 * @param	clazz	class to wait for
 86	 * @return	an EBCondition returning true when the message instanceof clazz
 87	 */
 88	public static EBCondition messageOfClassCondition(final Class clazz){
 89		return new EBCondition(){
 90				public boolean matches(EBMessage ebm){
 91					return clazz.isInstance(ebm);
 92				}
 93			};
 94	}
 95
 96	/**
 97	 * Matcher for EBMessages
 98	 */
 99	public static interface EBCondition {
100		
101		/**
102		 * @return true if the message is what you were waiting for
103		 */
104		public boolean matches(EBMessage message);
105		
106	}
107	
108	
109	
110	/**
111	 * listener for messages, with a timeout
112	 */
113	public static class MessageListener implements EBComponent{
114		private EBCondition condition;
115		private EBMessage msg;
116		
117		public MessageListener(){}
118		
119		/**
120		 * register and pass messages to matcher.
121		 * You can reuse a MessageListener, but only after waitForMessage has been called
122		 * @param	matcher	what message interests us
123		 */
124		public synchronized void registerForMessage(EBCondition matcher) throws IllegalStateException{
125			if(condition!=null)throw new IllegalStateException("already listening");
126			EditBus.addToBus(this);
127			condition = matcher;
128			msg = null;
129		}
130		
131		/**
132		 * wait for a matching message for the given amount of time.
133		 * fails when we get an interrupt or a timeout
134		 *
135		 * @param	timeout	the amount of milliseconds to wait for a message
136		 * @return	found message
137		 */
138		public EBMessage waitForMessage(long timeout){
139			
140			try{
141				synchronized(this){
142					if(msg == null){
143						this.wait(timeout);
144					}
145					EditBus.removeFromBus(this);
146					condition = null;
147					if(msg == null){
148						fail("Timeout : "+timeout+"ms !");
149						return null;
150					}else{
151						return msg;
152					}
153				}
154			}catch(InterruptedException ie){
155				EditBus.removeFromBus(this);
156				condition = null;
157				fail("Interrupted !");
158				return null;
159			}
160		}
161		
162		public void handleMessage(EBMessage message){
163			// condition == null iff timeout or interrupted
164			// so the message will be discarded anyway
165			if(condition == null || condition.matches(message)){
166				synchronized(this){
167					msg=message;
168					this.notify();
169				}
170			}
171		}
172	}
173}