PageRenderTime 184ms CodeModel.GetById 170ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/XML/test/xml/cache/CacheTest.java

#
Java | 246 lines | 166 code | 48 blank | 32 comment | 0 complexity | 5c526afce6b4e07a87d18354ed00fd58 MD5 | raw file
  1/*
  2 * CacheTest.java
  3 * :folding=explicit:collapseFolds=1:
  4 *
  5 * Copyright (C) 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 xml.cache;
 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.*;
 27
 28import static org.fest.assertions.Assertions.*;
 29
 30import org.gjt.sp.jedit.testframework.Log;
 31
 32import static xml.XMLTestUtils.*;
 33import static org.gjt.sp.jedit.testframework.EBFixture.*;
 34import org.gjt.sp.jedit.testframework.TestUtils;
 35import static org.gjt.sp.jedit.testframework.TestUtils.*;
 36
 37// }}}
 38
 39import org.gjt.sp.jedit.jEdit;
 40import org.gjt.sp.jedit.EBMessage;
 41import org.gjt.sp.jedit.textarea.JEditTextArea;
 42import org.gjt.sp.jedit.Buffer;
 43import org.gjt.sp.jedit.Registers;
 44
 45import java.io.*;
 46import javax.xml.XMLConstants;
 47
 48import java.awt.event.KeyEvent;
 49import java.awt.event.InputEvent;
 50import org.gjt.sp.jedit.gui.CompletionPopup;
 51
 52import xml.PathUtilities;
 53
 54/**
 55 * unit tests for the Cache mechanism
 56 * $Id: CacheTest.java 18330 2010-08-11 19:53:08Z kerik-sf $
 57 */
 58public class CacheTest{
 59	private static File testData;
 60	private Cache cache;
 61	
 62    @BeforeClass
 63    public static void setUpjEdit() throws IOException{
 64        TestUtils.beforeClass();
 65        testData = new File(System.getProperty("test_data")).getCanonicalFile();
 66        assertTrue(testData.exists());
 67    }
 68    
 69    @AfterClass
 70    public static void tearDownjEdit() {
 71        TestUtils.afterClass();
 72    }
 73    
 74    @Before
 75    public void setup(){
 76    	cache = Cache.instance();
 77		cache.clear();
 78    }
 79    
 80    @After
 81    public void tearDown(){
 82		cache.clear();
 83    }
 84    
 85	@Test
 86	public void testPutGetClear(){
 87		Object value = new Object();
 88		CacheEntry en = cache.put("path","key",value);
 89		assertEquals("path",en.getPath());
 90		assertEquals("key",en.getKey());
 91		assertEquals(value,en.getCachedItem());
 92		CacheEntry en2 = new CacheEntry("path","key","another value");
 93		assertEquals(en,en2);
 94		
 95		CacheEntry v = cache.get("path","key");
 96		assertEquals(value,v.getCachedItem());
 97		assertEquals(en,v);
 98		
 99		// no overwrite
100		CacheEntry en3 = cache.put("path","key","another value");
101		v = cache.get("path","key");
102		assertEquals(value,v.getCachedItem());
103		assertEquals(en,v);
104		
105		// another entry
106		cache.put("someOtherPath","key", "some other value");
107		assertEquals("some other value",cache.get("someOtherPath","key").getCachedItem());
108		cache.put("path","someOtherkey", "yet another value");
109		assertEquals("yet another value",cache.get("path","someOtherkey").getCachedItem());
110		
111		assertEquals(null,cache.get("not there", "key"));
112		
113		// clear cache
114		cache.clear();
115		assertNull(cache.get("path","key"));
116	}
117	
118	@Test
119	public void testMonitor(){
120		File test = new File(testData,"with_prefix/test.xml");
121		Buffer b = openFile(test.getPath());
122		Pause.pause(2000);
123		close(view(),b);
124		// clears on open
125		cache.put(test.getPath(),"key", "value");
126		b = openFile(test.getPath());
127		Pause.pause(2000);
128		assertNull(cache.get(test.getPath(),"key"));
129		
130		// close requesting buffer
131		CacheEntry en = cache.put(test.getPath(),"key","value");
132		en.getRequestingBuffers().add(b);
133		CacheEntry dep = cache.put("path","key","related");
134		en.getRelated().add(dep);
135		
136		close(view(),b);
137		Pause.pause(1000);
138		assertNull(cache.get(test.getPath(),"key"));
139		assertNull(cache.get("path","key"));
140
141		// modify the buffer
142		final Buffer b2 = openFile(test.getPath());
143		Pause.pause(1000);
144		en = cache.put(test.getPath(),"key","value");
145		dep = cache.put("path","key","value");
146		en.getRelated().add(dep);
147		
148		GuiActionRunner.execute(new GuiTask(){
149				protected void executeInEDT(){
150					b2.insert(1, "HELLO");
151				}
152		});
153		Pause.pause(1000);
154		assertNull(cache.get(test.getPath(),"key"));
155		assertNull(cache.get("path","key"));
156		close(view(),b2);
157	}
158	
159	@Test
160	public void testXSD(){
161		File test = new File(testData,"import_schema/instance.xml");
162		File importxsd = new File(testData,"import_schema/import.xsd");
163		File sourcexsd = new File(testData,"import_schema/source.xsd");
164
165		Buffer b = openFile(test.getPath());
166		
167		parseAndWait();
168		
169		assertNotNull(cache.get(importxsd.getPath(),XMLConstants.W3C_XML_SCHEMA_NS_URI));
170		assertNotNull(cache.get(sourcexsd.getPath(),XMLConstants.W3C_XML_SCHEMA_NS_URI));
171		assertNotNull(cache.get(importxsd.getPath(),"CompletionInfo"));
172		// both URL and file work
173		assertNotNull(cache.get(sourcexsd.getPath(),"CompletionInfo"));
174		assertNotNull(cache.get(PathUtilities.pathToURL(sourcexsd.getPath()),"CompletionInfo"));
175		
176		Pause.pause(5000);
177		// open source.xsd => invalidate everything
178		final Buffer b2 = openFile(sourcexsd.getPath());
179		Pause.pause(5000);
180		assertNull(cache.get(importxsd.getPath(),XMLConstants.W3C_XML_SCHEMA_NS_URI));
181		assertNull(cache.get(sourcexsd.getPath(),XMLConstants.W3C_XML_SCHEMA_NS_URI));
182		assertNull(cache.get(importxsd.getPath(),"CompletionInfo"));
183		assertNull(cache.get(PathUtilities.pathToURL(sourcexsd.getPath()),"CompletionInfo"));
184	}
185	
186	@Test
187	public void testRNG(){
188		File test = new File(testData,"parentRef/instance.xml");
189		File actual = new File(testData,"parentRef/actual_table.rng");
190		File table = new File(testData,"parentRef/table.rng");
191
192		Buffer b = openFile(test.getPath());
193		
194		parseAndWait();
195		
196		assertNotNull(cache.get(actual.getPath(),"Schema"));
197		assertNotNull(cache.get(actual.getPath(),"CompletionInfo"));
198		
199		Pause.pause(5000);
200
201		// open table.rng => invalidate CompletionInfo and schema component
202		Buffer b2 = openFile(table.getPath());
203		Pause.pause(5000);
204		assertNull(cache.get(actual.getPath(),"CompletionInfo"));
205		close(view(),b2);
206		
207		// FIXME: should work with table.rng
208		// open actual_table.rng => invalidate everything
209		b2 = openFile(actual.getPath());
210		Pause.pause(5000);
211		assertNull(cache.get(actual.getPath(),"Schema"));
212		close(view(),b2);
213		
214		// reparse instance
215		parseAndWait();
216
217		assertNotNull(cache.get(actual.getPath(),"Schema"));
218		assertNotNull(cache.get(actual.getPath(),"CompletionInfo"));
219		
220		Pause.pause(5000);
221		close(view(),b);
222		Pause.pause(5000);
223		
224		assertNull(cache.get(actual.getPath(),"Schema"));
225		assertNull(cache.get(actual.getPath(),"CompletionInfo"));
226	}
227
228	@Test
229	public void testDTD() throws IOException{
230		File test = new File(testData,"dtd/actions.xml");
231		String actionsdtd = xml.Resolver.instance().resolveEntityToPath(null,null,null,"actions.dtd");
232
233		Buffer b = openFile(test.getPath());
234		
235		parseAndWait();
236		
237		assertNotNull(cache.get(actionsdtd,XMLConstants.XML_DTD_NS_URI));
238		assertNotNull(cache.get(actionsdtd,"CompletionInfo"));
239		
240		// open actions.dtd => invalidate everything
241		final Buffer b2 = openFile(actionsdtd);
242		Pause.pause(5000);
243		assertNull(cache.get(actionsdtd,XMLConstants.XML_DTD_NS_URI));
244		assertNull(cache.get(actionsdtd,"CompletionInfo"));
245	}
246}