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

/projects/netbeans-7.3/csl.api/test/unit/src/org/netbeans/modules/csl/api/EditHistoryTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 458 lines | 293 code | 39 blank | 126 comment | 11 complexity | 73492e38dcf221c5a460d905b10091ce MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2008 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.csl.api;
  43. import org.netbeans.modules.csl.api.EditHistory;
  44. import java.util.Random;
  45. import javax.swing.text.Document;
  46. import javax.swing.text.PlainDocument;
  47. import junit.framework.TestCase;
  48. /**
  49. *
  50. * @author Tor Norbye
  51. */
  52. public class EditHistoryTest extends TestCase {
  53. public EditHistoryTest(String testName) {
  54. super(testName);
  55. }
  56. @Override
  57. protected void setUp() throws Exception {
  58. super.setUp();
  59. }
  60. @Override
  61. protected void tearDown() throws Exception {
  62. super.tearDown();
  63. }
  64. private Document getDocument(String s) throws Exception {
  65. Document doc = new PlainDocument();
  66. doc.insertString(0, s, null);
  67. return doc;
  68. }
  69. private void validateHistory(String original, String modified, EditHistory history) throws Exception {
  70. // Check position mapping
  71. // This won't work in the presence of removes! There will be positions lost that maps back to the
  72. // boundaries of the deleted block rather than the interior
  73. // for (int i = 0; i < original.length(); i++) {
  74. // int newPos = history.convertOldToNew(i);
  75. // int oldPos = history.convertNewToOld(newPos);
  76. // assertEquals("Incorrect position mapping for " + modified.charAt(i), i, oldPos);
  77. // }
  78. // Ensure that the head and tail of the document is identical to the beginning
  79. String head = original.substring(0, history.getStart());
  80. assertEquals("Wrong head; ", head, modified.substring(0, history.getStart()));
  81. String tail = original.substring(history.getStart()+history.getOriginalSize());
  82. assertEquals("Wrong tail; ", tail, modified.substring(history.getStart()+history.getEditedSize()));
  83. }
  84. private void insert(Document document, EditHistory history, int offset, String string) throws Exception {
  85. try {
  86. document.addDocumentListener(history);
  87. document.insertString(offset, string, null);
  88. } finally {
  89. document.removeDocumentListener(history);
  90. }
  91. }
  92. private void remove(Document document, EditHistory history, int offset, int length) throws Exception {
  93. try {
  94. document.addDocumentListener(history);
  95. document.remove(offset, length);
  96. } finally {
  97. document.removeDocumentListener(history);
  98. }
  99. }
  100. public void testInserts1() throws Exception {
  101. EditHistory history = new EditHistory();
  102. String original = " HelloWorld";
  103. Document doc = getDocument(original);
  104. //012345678901234567890
  105. // HelloWorld
  106. // He__lloWorld
  107. insert(doc, history, 5, "__");
  108. String modified = doc.getText(0, doc.getLength());
  109. assertEquals(" He__lloWorld", modified);
  110. validateHistory(original, modified, history);
  111. assertEquals(5, history.getStart());
  112. assertEquals(0, history.getOriginalSize());
  113. assertEquals(2, history.getEditedSize());
  114. }
  115. public void testRemoves1() throws Exception {
  116. EditHistory history = new EditHistory();
  117. String original = " HelloWorld";
  118. Document doc = getDocument(original);
  119. //012345678901234567890
  120. // HelloWorld
  121. // HeoWorld
  122. remove(doc, history, 5, 2);
  123. String modified = doc.getText(0, doc.getLength());
  124. assertEquals(" HeoWorld", modified);
  125. validateHistory(original, modified, history);
  126. assertEquals(5, history.getStart());
  127. assertEquals(2, history.getOriginalSize());
  128. assertEquals(0, history.getEditedSize());
  129. }
  130. public void testMultipleRemoves1() throws Exception {
  131. EditHistory history = new EditHistory();
  132. String original = " HelloWorld";
  133. Document doc = getDocument(original);
  134. //012345678901234567890
  135. // HelloWorld
  136. // HeoWorld
  137. remove(doc, history, 5, 2);
  138. //012345678901234567890
  139. // HelloWorld
  140. // HeoWld
  141. remove(doc, history, 7, 2);
  142. String modified = doc.getText(0, doc.getLength());
  143. assertEquals(" HeoWld", modified);
  144. assertEquals(5, history.getStart());
  145. assertEquals(11, history.getOriginalEnd());
  146. assertEquals(7, history.getEditedEnd());
  147. assertEquals(6, history.getOriginalSize());
  148. assertEquals(2, history.getEditedSize());
  149. assertEquals(-4, history.getSizeDelta());
  150. validateHistory(original, modified, history);
  151. }
  152. public void testMultipleRemoves2() throws Exception {
  153. EditHistory history = new EditHistory();
  154. String original = " HelloWorld";
  155. Document doc = getDocument(original);
  156. //012345678901234567890
  157. // HelloWorld
  158. // HelloWod
  159. remove(doc, history, 10, 2);
  160. //012345678901234567890
  161. // HelloWod
  162. // HeoWod
  163. remove(doc, history, 5, 2);
  164. String modified = doc.getText(0, doc.getLength());
  165. assertEquals(" HeoWod", modified);
  166. assertEquals(5, history.getStart());
  167. assertEquals(12, history.getOriginalEnd());
  168. assertEquals(8, history.getEditedEnd());
  169. assertEquals(7, history.getOriginalSize());
  170. assertEquals(3, history.getEditedSize());
  171. assertEquals(-4, history.getSizeDelta());
  172. validateHistory(original, modified, history);
  173. }
  174. public void testMultipleRemoves3() throws Exception {
  175. EditHistory history = new EditHistory();
  176. String original = " HelloWorld";
  177. Document doc = getDocument(original);
  178. //012345678901234567890
  179. // HelloWorld
  180. // HelloWod
  181. remove(doc, history, 10, 2);
  182. //012345678901234567890
  183. // HelloWod
  184. // HeoWod
  185. remove(doc, history, 5, 2);
  186. //012345678901234567890
  187. // HeoWod
  188. // Heood
  189. remove(doc, history, 6, 1);
  190. String modified = doc.getText(0, doc.getLength());
  191. assertEquals(" Heood", modified);
  192. assertEquals(5, history.getStart());
  193. assertEquals(12, history.getOriginalEnd());
  194. assertEquals(7, history.getEditedEnd());
  195. assertEquals(7, history.getOriginalSize());
  196. assertEquals(2, history.getEditedSize());
  197. assertEquals(-5, history.getSizeDelta());
  198. validateHistory(original, modified, history);
  199. }
  200. public void testMultipleInserts1() throws Exception {
  201. EditHistory history = new EditHistory();
  202. String original = " HelloWorld";
  203. Document doc = getDocument(original);
  204. //012345678901234567890
  205. // HelloWorld
  206. // He__lloWorld
  207. insert(doc, history, 5, "__");
  208. //012345678901234567890
  209. // He__lloWorld
  210. // He__llo__World
  211. insert(doc, history, 10, "__");
  212. String modified = doc.getText(0, doc.getLength());
  213. assertEquals(" He__llo__World", modified);
  214. assertEquals(5, history.getStart());
  215. assertEquals(8, history.getOriginalEnd());
  216. assertEquals(12, history.getEditedEnd());
  217. assertEquals(3, history.getOriginalSize());
  218. assertEquals(7, history.getEditedSize());
  219. assertEquals(4, history.getSizeDelta());
  220. validateHistory(original, modified, history);
  221. }
  222. public void testMultipleInserts2() throws Exception {
  223. EditHistory history = new EditHistory();
  224. String original = " HelloWorld";
  225. Document doc = getDocument(original);
  226. //012345678901234567890
  227. // HelloWorld
  228. // HelloWo__rld
  229. insert(doc, history, 10, "__");
  230. //012345678901234567890
  231. // HelloWo__rld
  232. // He__lloWo__rld
  233. insert(doc, history, 5, "__");
  234. String modified = doc.getText(0, doc.getLength());
  235. assertEquals(" He__lloWo__rld", modified);
  236. assertEquals(5, history.getStart());
  237. assertEquals(10, history.getOriginalEnd());
  238. assertEquals(14, history.getEditedEnd());
  239. assertEquals(5, history.getOriginalSize());
  240. assertEquals(9, history.getEditedSize());
  241. assertEquals(4, history.getSizeDelta());
  242. validateHistory(original, modified, history);
  243. }
  244. public void testMultipleInserts3() throws Exception {
  245. EditHistory history = new EditHistory();
  246. String original = " HelloWorld";
  247. Document doc = getDocument(original);
  248. //012345678901234567890
  249. // HelloWorld
  250. // HelloWo__rld
  251. insert(doc, history, 10, "__");
  252. //012345678901234567890
  253. // HelloWo__rld
  254. // He__lloWo__rld
  255. insert(doc, history, 5, "__");
  256. //012345678901234567890
  257. // He__lloWo__rld
  258. // He__ll__oWo__rld
  259. insert(doc, history, 9, "__");
  260. String modified = doc.getText(0, doc.getLength());
  261. assertEquals(" He__ll__oWo__rld", modified);
  262. assertEquals(5, history.getStart());
  263. assertEquals(10, history.getOriginalEnd());
  264. assertEquals(16, history.getEditedEnd());
  265. assertEquals(5, history.getOriginalSize());
  266. assertEquals(11, history.getEditedSize());
  267. assertEquals(6, history.getSizeDelta());
  268. validateHistory(original, modified, history);
  269. }
  270. public void testMixed2() throws Exception {
  271. EditHistory history = new EditHistory();
  272. String original = " HelloWorld";
  273. Document doc = getDocument(original);
  274. //012345678901234567890
  275. // HelloWorld
  276. // He__lloWorld
  277. insert(doc, history, 5, "__");
  278. //012345678901234567890
  279. // He__lloWorld
  280. // He__llorld
  281. remove(doc, history, 10, 2);
  282. String modified = doc.getText(0, doc.getLength());
  283. assertEquals(" He__llorld", modified);
  284. assertEquals(5, history.getStart());
  285. assertEquals(10, history.getOriginalEnd());
  286. assertEquals(10, history.getEditedEnd());
  287. assertEquals(5, history.getOriginalSize());
  288. assertEquals(5, history.getEditedSize());
  289. assertEquals(0, history.getSizeDelta());
  290. validateHistory(original, modified, history);
  291. }
  292. public void testMixed3() throws Exception {
  293. EditHistory history = new EditHistory();
  294. String original = " HelloWorld";
  295. Document doc = getDocument(original);
  296. //012345678901234567890
  297. // HelloWorld
  298. // He__lloWorld
  299. insert(doc, history, 5, "__");
  300. //012345678901234567890
  301. // He__lloWorld
  302. // He__llo__World
  303. insert(doc, history, 10, "__");
  304. //012345678901234567890
  305. // He__llo__World
  306. // He__l__World
  307. remove(doc, history, 8, 2);
  308. String modified = doc.getText(0, doc.getLength());
  309. assertEquals(" He__l__World", modified);
  310. assertEquals(5, history.getStart());
  311. assertEquals(8, history.getOriginalEnd());
  312. assertEquals(10, history.getEditedEnd());
  313. assertEquals(3, history.getOriginalSize());
  314. assertEquals(5, history.getEditedSize());
  315. assertEquals(2, history.getSizeDelta());
  316. validateHistory(original, modified, history);
  317. }
  318. public void testRandom() throws Exception {
  319. // Try lots of edits and make sure the edit history at the end is valid
  320. String PREFIX = "DONTTOUCHSTART";
  321. String SUFFIX = "DONTTOUCHEND";
  322. String original = PREFIX +
  323. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" +
  324. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" +
  325. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" +
  326. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" +
  327. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" +
  328. SUFFIX;
  329. Document doc = getDocument(original);
  330. EditHistory history = new EditHistory();
  331. Random random = new Random(500);
  332. for (int i = 0; i < 100; i++) {
  333. boolean insert = random.nextBoolean();
  334. int docLength = doc.getLength()-PREFIX.length()-SUFFIX.length()-1;
  335. int offset = (int)(random.nextDouble()*docLength)+PREFIX.length();
  336. if (insert) {
  337. insert(doc, history, offset, "_");
  338. } else {
  339. remove(doc, history, offset, 1);
  340. }
  341. }
  342. String modified = doc.getText(0, doc.getLength());
  343. validateHistory(original, modified, history);
  344. }
  345. public void testCombined1() throws Exception {
  346. EditHistory firstHistory = new EditHistory();
  347. EditHistory history = new EditHistory();
  348. firstHistory.add(history);
  349. String original = " HelloWorld";
  350. Document doc = getDocument(original);
  351. //012345678901234567890
  352. // HelloWorld
  353. // He__lloWorld
  354. insert(doc, history, 5, "__");
  355. String modified = doc.getText(0, doc.getLength());
  356. assertEquals(" He__lloWorld", modified);
  357. validateHistory(original, modified, history);
  358. assertEquals(5, history.getStart());
  359. assertEquals(0, history.getOriginalSize());
  360. assertEquals(2, history.getEditedSize());
  361. // Add some more history
  362. original = modified;
  363. EditHistory oldHistory = history;
  364. history = new EditHistory();
  365. oldHistory.add(history);
  366. insert(doc, history, 10, "__");
  367. modified = doc.getText(0, doc.getLength());
  368. assertEquals(" He__llo__World", modified);
  369. validateHistory(original, modified, history);
  370. assertEquals(10, history.getStart());
  371. assertEquals(0, history.getOriginalSize());
  372. assertEquals(2, history.getEditedSize());
  373. // Now test combined
  374. // Just most recent:
  375. assertEquals(1, history.getVersion());
  376. EditHistory h;
  377. h = EditHistory.getCombinedEdits(oldHistory.getVersion(), history);
  378. assertNotNull(h);
  379. assertEquals(10, h.getStart());
  380. assertEquals(0, h.getOriginalSize());
  381. assertEquals(2, h.getEditedSize());
  382. h = EditHistory.getCombinedEdits(-1, history);
  383. assertNotNull(h);
  384. assertEquals(5, h.getStart());
  385. assertEquals(3, h.getOriginalSize());
  386. assertEquals(7, h.getEditedSize());
  387. // From the beginning:
  388. assertEquals(0, oldHistory.getVersion());
  389. h = EditHistory.getCombinedEdits(history.getVersion(), history);
  390. assertNull(h);
  391. }
  392. public void testChoppedHistory() throws Exception {
  393. EditHistory old = new EditHistory();
  394. for (int i = 0; i < 50; i++) {
  395. EditHistory history = new EditHistory();
  396. old.add(history);
  397. old = history;
  398. }
  399. assertNotNull(EditHistory.getCombinedEdits(48, old));
  400. assertNotNull(EditHistory.getCombinedEdits(47, old));
  401. assertNotNull(EditHistory.getCombinedEdits(46, old));
  402. EditHistory curr = old;
  403. for (int i = 0; i < 5; i++) {
  404. assertTrue(curr.previous != null);
  405. assertTrue(curr.previous != curr);
  406. curr = curr.previous;
  407. }
  408. int i = 0;
  409. for (; i < 49; i++) {
  410. assertTrue(curr.previous != curr);
  411. curr = curr.previous;
  412. if (curr == null) {
  413. break;
  414. }
  415. }
  416. // Make sure we reached the end of the previous pointers well before the 50
  417. assertTrue(i < 40);
  418. }
  419. }