PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. */
  14. package org.gjt.sp.jedit.testframework;
  15. // {{{ jUnit imports
  16. import java.util.concurrent.TimeUnit;
  17. import org.junit.*;
  18. import static org.junit.Assert.*;
  19. import org.fest.swing.fixture.*;
  20. import org.fest.swing.core.*;
  21. import org.fest.swing.finder.*;
  22. import org.fest.swing.edt.*;
  23. import org.fest.swing.timing.*;
  24. import org.fest.swing.exception.WaitTimedOutError;
  25. import static org.fest.assertions.Assertions.*;
  26. import org.gjt.sp.jedit.testframework.Log;
  27. import org.gjt.sp.jedit.testframework.TestUtils;
  28. // }}}
  29. import java.io.*;
  30. import java.awt.Dialog;
  31. import org.gjt.sp.jedit.jEdit;
  32. import org.gjt.sp.jedit.PluginJAR;
  33. import org.gjt.sp.jedit.EditBus;
  34. import org.gjt.sp.jedit.EBComponent;
  35. import org.gjt.sp.jedit.EBMessage;
  36. import org.gjt.sp.jedit.msg.PluginUpdate;
  37. /**
  38. * Try to wrap nicely the EditBus for tests
  39. * $Id: EBFixture.java 21245 2012-03-04 15:15:56Z kerik-sf $
  40. */
  41. public class EBFixture{
  42. /**
  43. * register for a certain message, run the runnable r, wait for the message and the runnable.
  44. * @param r runnable to run asyncronously
  45. * @param condition message to wait for
  46. * @param timeout how long to wait for the message
  47. */
  48. public static void doInBetween(Runnable r,EBCondition condition, long timeout){
  49. MessageListener listen = new MessageListener();
  50. listen.registerForMessage(condition);
  51. Thread t = new Thread(r);
  52. t.start();
  53. listen.waitForMessage(timeout);
  54. try{
  55. t.join(timeout);
  56. }catch(InterruptedException e){
  57. fail("Interrupted !");
  58. }
  59. }
  60. /**
  61. * blocks until an EBMessage of class clazz happened, or fail after timeout.
  62. * @param clazz class to wait for
  63. * @param timeout how long to wait for the message
  64. */
  65. public static void simplyWaitForMessageOfClass(final Class clazz, long timeout){
  66. MessageListener listen = new MessageListener();
  67. listen.registerForMessage(messageOfClassCondition(clazz));
  68. listen.waitForMessage(timeout);
  69. }
  70. /**
  71. * @param clazz class to wait for
  72. * @return an EBCondition returning true when the message instanceof clazz
  73. */
  74. public static EBCondition messageOfClassCondition(final Class clazz){
  75. return new EBCondition(){
  76. public boolean matches(EBMessage ebm){
  77. return clazz.isInstance(ebm);
  78. }
  79. };
  80. }
  81. /**
  82. * Matcher for EBMessages
  83. */
  84. public static interface EBCondition {
  85. /**
  86. * @return true if the message is what you were waiting for
  87. */
  88. public boolean matches(EBMessage message);
  89. }
  90. /**
  91. * listener for messages, with a timeout
  92. */
  93. public static class MessageListener implements EBComponent{
  94. private EBCondition condition;
  95. private EBMessage msg;
  96. public MessageListener(){}
  97. /**
  98. * register and pass messages to matcher.
  99. * You can reuse a MessageListener, but only after waitForMessage has been called
  100. * @param matcher what message interests us
  101. */
  102. public synchronized void registerForMessage(EBCondition matcher) throws IllegalStateException{
  103. if(condition!=null)throw new IllegalStateException("already listening");
  104. EditBus.addToBus(this);
  105. condition = matcher;
  106. msg = null;
  107. }
  108. /**
  109. * wait for a matching message for the given amount of time.
  110. * fails when we get an interrupt or a timeout
  111. *
  112. * @param timeout the amount of milliseconds to wait for a message
  113. * @return found message
  114. */
  115. public EBMessage waitForMessage(long timeout){
  116. try{
  117. synchronized(this){
  118. if(msg == null){
  119. this.wait(timeout);
  120. }
  121. EditBus.removeFromBus(this);
  122. condition = null;
  123. if(msg == null){
  124. fail("Timeout : "+timeout+"ms !");
  125. return null;
  126. }else{
  127. return msg;
  128. }
  129. }
  130. }catch(InterruptedException ie){
  131. EditBus.removeFromBus(this);
  132. condition = null;
  133. fail("Interrupted !");
  134. return null;
  135. }
  136. }
  137. public void handleMessage(EBMessage message){
  138. // condition == null iff timeout or interrupted
  139. // so the message will be discarded anyway
  140. if(condition == null || condition.matches(message)){
  141. synchronized(this){
  142. msg=message;
  143. this.notify();
  144. }
  145. }
  146. }
  147. }
  148. }