PageRenderTime 46ms CodeModel.GetById 10ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 1ms

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

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