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