PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/XML/test/xml/XMLTestUtils.java

#
Java | 241 lines | 156 code | 36 blank | 49 comment | 12 complexity | ea4dfc443cb6673f8fe8251e2017fe82 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. * XMLTestUtils.java
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * Copyright (C) 2009 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 xml;
  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 javax.swing.JWindow;
  32. import java.awt.Component;
  33. import org.gjt.sp.jedit.jEdit;
  34. import org.gjt.sp.jedit.PluginJAR;
  35. import org.gjt.sp.jedit.Buffer;
  36. import org.gjt.sp.jedit.EditBus;
  37. import org.gjt.sp.jedit.EBComponent;
  38. import org.gjt.sp.jedit.EBMessage;
  39. import org.gjt.sp.jedit.msg.PluginUpdate;
  40. import sidekick.SideKickParsedData;
  41. import static org.gjt.sp.jedit.testframework.EBFixture.*;
  42. import static org.gjt.sp.jedit.testframework.TestUtils.*;
  43. /**
  44. * a handful of utility methods.
  45. * Some of them may be moved to the test-framework as they prove useful
  46. * $Id: XMLTestUtils.java 21248 2012-03-05 19:37:01Z kerik-sf $
  47. */
  48. public class XMLTestUtils{
  49. /**
  50. * test each byte from the streams for equality.
  51. * Only stops when one of the streams is exhausted, so don't reuse them.
  52. *
  53. * @param expectedIn the reference stream
  54. * @param actualIn the tested stream
  55. */
  56. public static void assertInputStreamEquals(InputStream expectedIn, InputStream actualIn)
  57. throws IOException
  58. {
  59. try{
  60. for(int i=0,e=0,a=0;a!=-1 && e!=-1;i++){
  61. a=actualIn.read();
  62. e=expectedIn.read();
  63. assertEquals("at byte "+i, a,e);
  64. }
  65. }finally{
  66. expectedIn.close();
  67. actualIn.close();
  68. }
  69. }
  70. /**
  71. * test each byte from the readers for equality.
  72. * Only stops when one of the readers is exhausted, so don't reuse them.
  73. *
  74. * @param expectedIn the reference reader
  75. * @param actualIn the tested reader
  76. */
  77. public static void assertReaderEquals(Reader expectedIn, Reader actualIn)
  78. throws IOException
  79. {
  80. try{
  81. for(int i=0,e=0,a=0;a!=-1 && e!=-1;i++){
  82. a=actualIn.read();
  83. e=expectedIn.read();
  84. assertEquals("at char "+i, a,e);
  85. }
  86. }finally{
  87. expectedIn.close();
  88. actualIn.close();
  89. }
  90. }
  91. public static void parseAndWait(){
  92. doInBetween(
  93. new Runnable(){
  94. public void run(){
  95. action("sidekick-parse");
  96. }
  97. },
  98. messageOfClassCondition(sidekick.SideKickUpdate.class)
  99. ,10000);
  100. }
  101. /** open, parse and wait for SideKickUpdate in one go, to be sure not to miss the message */
  102. public static Buffer openParseAndWait(final String path){
  103. final Buffer[] ret = new Buffer[1];
  104. doInBetween(
  105. new Runnable(){
  106. public void run(){
  107. ret[0] = openFile(path);
  108. action("sidekick-parse");
  109. }
  110. },
  111. messageOfClassCondition(sidekick.SideKickUpdate.class)
  112. ,10000);
  113. return ret[0];
  114. }
  115. public static void gotoPositionAndWait(int pos){
  116. gotoPosition(pos);
  117. Pause.pause(500);
  118. }
  119. /**
  120. * activates, deactivates and activates again the given plugin.
  121. * this is useful to test that whatever settings it kept are well
  122. * persisted.
  123. *
  124. * @param editPluginClass the main class of the plugin to deactivate and reactivate
  125. */
  126. public static final void reactivatePlugin(Class editPluginClass){
  127. final PluginJAR jar = jEdit.getPlugin(editPluginClass.getName()).getPluginJAR();
  128. assertNotNull(jar);
  129. // XMLPlugin is not activated, even if we tested the resolver, since
  130. // only Resolver.instance() is called
  131. // this ensures that its stop() method will be called !
  132. jar.activatePlugin();
  133. MessageListener listen = new MessageListener();
  134. listen.registerForMessage(new EBCondition(){
  135. public boolean matches(EBMessage msg){
  136. if(msg instanceof PluginUpdate){
  137. PluginUpdate upd = (PluginUpdate)msg;
  138. return upd.getWhat() == PluginUpdate.DEACTIVATED
  139. && upd.getPluginJAR() == jar;
  140. }else return false;
  141. }
  142. });
  143. GuiActionRunner.execute(new GuiTask(){
  144. protected void executeInEDT(){
  145. jar.deactivatePlugin(false);
  146. }
  147. });
  148. listen.waitForMessage(10000);
  149. listen.registerForMessage(new EBCondition(){
  150. public boolean matches(EBMessage msg){
  151. if(msg instanceof PluginUpdate){
  152. PluginUpdate upd = (PluginUpdate)msg;
  153. return upd.getWhat() == PluginUpdate.ACTIVATED
  154. && upd.getPluginJAR() == jar;
  155. }else return false;
  156. }
  157. });
  158. jar.activatePlugin();
  159. listen.waitForMessage(10000);
  160. }
  161. public static class JWindowFixture extends ContainerFixture<JWindow>{
  162. public JWindowFixture(Robot robot, JWindow target){
  163. super(robot,target);
  164. }
  165. public JWindowFixture requireNotVisible(){
  166. assertFalse(target.isVisible());
  167. return this;
  168. }
  169. public JWindowFixture requireVisible(){
  170. assertTrue(target.isVisible());
  171. return this;
  172. }
  173. }
  174. public static JWindowFixture completionPopup(){
  175. Component completionC = TestUtils.robot().finder().find(
  176. new ComponentMatcher(){
  177. public boolean matches(Component c){
  178. return c instanceof org.gjt.sp.jedit.gui.CompletionPopup;
  179. }
  180. });
  181. return new JWindowFixture(TestUtils.robot(),(JWindow)completionC);
  182. }
  183. /**
  184. * utility method to return the XmlParsedData of current view/buffer
  185. * fails if data is not instance of XmlParsedData
  186. */
  187. public static XmlParsedData getXmlParsedData(){
  188. Pause.pause(500);
  189. SideKickParsedData _data = SideKickParsedData.getParsedData(view());
  190. System.err.println("XMLParsedData:"+_data.getClass()+":"+_data);
  191. assertTrue("no XMLParsedData in current view/buffer",
  192. _data instanceof XmlParsedData);
  193. return (XmlParsedData)_data;
  194. }
  195. public static Buffer bufferWithText(final String mode, final String text){
  196. return GuiActionRunner.execute(new GuiQuery<Buffer>(){
  197. public Buffer executeInEDT(){
  198. Buffer b = jEdit.newFile(view());
  199. b.setMode(mode);
  200. b.insert(0, text);
  201. return b;
  202. }
  203. });
  204. }
  205. }