package org.eclipse.text.tests; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.Arrays; import java.util.regex.PatternSyntaxException; import ch.unibe.jexample.Given; import ch.unibe.jexample.JExample; import ch.unibe.jexample.JExampleOptions; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.FindReplaceDocumentAdapter; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.Region; @RunWith(JExample.class) @JExampleOptions(cloneTestCase = true) public class FindReplaceDocumentAdapterTestJUnit4JExample { @Test() public Document setUp() { Document fDocument = new Document(); String text; text = "package TestPackage;\n/*\n* comment\n*/\n\tpublic class Class {\n\t\t// comment1\n\t\tpublic void method1() {\n\t\t}\n\t\t// comment2\n\t\tpublic void method2() {\n\t\t}\n\t}\n// Gel枚st"; fDocument.set(text); return fDocument; } @Test() @Given("#testFindCaretInMiddleOfWord;#testRegexFindLinebreak2_fail;#testIllegalState;#testRegexReplace2;#testFindCaretAtEndStart;#testRegexReplace3;#testUTF8Pattern;#testFindCaretAtWordStart;#testRegexFindLinebreakIllegal;#testRegexFindStackOverflow_fail;#testRegexFindLinebreak;#testReplace;#testBug74993;#testFind;#testRegexRetainCase;#testRegexFindLinebreak3") public Document tearDown(Document fDocument) { fDocument = null; return fDocument; } @Test() @Given("#setUp") public Document testFind(Document fDocument) { FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument); try { IRegion result = new Region(8, 11); IRegion r = findReplaceDocumentAdapter.find(0, "TestPackage", true, true, false, false); assertEquals(result, r); r = findReplaceDocumentAdapter.find(0, "testpackage", true, true, false, false); assertNull(r); r = findReplaceDocumentAdapter.find(0, "TestPackage", true, false, false, false); assertEquals(r, result); r = findReplaceDocumentAdapter.find(0, "testpackage", true, false, false, false); assertEquals(r, result); } catch (BadLocationException e) { Assert.assertTrue(false); } return fDocument; } @Test() @Given("#setUp") public Document testFindCaretInMiddleOfWord(Document fDocument) { FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument); try { IRegion r = findReplaceDocumentAdapter.find(12, "TestPackage", true, false, false, false); assertNull(r); r = findReplaceDocumentAdapter.find(12, "TestPackage", false, false, false, false); assertNull(r); } catch (BadLocationException e) { Assert.assertTrue(false); } return fDocument; } @Test() @Given("#setUp") public Document testFindCaretAtWordStart(Document fDocument) { FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument); try { IRegion r = findReplaceDocumentAdapter.find(8, "TestPackage", true, false, false, false); assertEquals(new Region(8, 11), r); } catch (BadLocationException e) { Assert.assertTrue(false); } return fDocument; } @Test() @Given("#setUp") public Document testFindCaretAtEndStart(Document fDocument) { FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument); try { IRegion r = findReplaceDocumentAdapter.find(19, "TestPackage", false, false, false, false); assertEquals(new Region(8, 11), r); } catch (BadLocationException e) { Assert.assertTrue(false); } return fDocument; } @Test() @Given("#setUp") public Document testBug74993(Document fDocument) { FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument); try { IRegion r = findReplaceDocumentAdapter.find(12, "\\w+", false, false, false, true); assertEquals(new Region(6, 1), r); } catch (BadLocationException e) { Assert.assertTrue(false); } return fDocument; } @Test() @Given("#setUp") public Document testUTF8Pattern(Document fDocument) { FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument); try { IRegion result = new Region(153, 6); IRegion r = findReplaceDocumentAdapter.find(0, "Gel枚st", true, true, false, false); assertEquals(result, r); r = findReplaceDocumentAdapter.find(0, "Gel枚st", true, true, false, false); assertEquals(result, r); r = findReplaceDocumentAdapter.find(0, "Gel脰st", true, false, false, false); assertEquals(result, r); r = findReplaceDocumentAdapter.find(0, "Gel脰st", true, false, false, false); assertEquals(result, r); } catch (BadLocationException e) { Assert.assertTrue(false); } return fDocument; } @Test() @Given("#setUp") public Document testReplace(Document fDocument) { FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument); try { findReplaceDocumentAdapter.find(0, "public", true, true, false, false); IRegion r = findReplaceDocumentAdapter.replace("private", false); assertNotNull(r); findReplaceDocumentAdapter.find(0, "public", true, true, false, false); r = findReplaceDocumentAdapter.replace("private", false); assertNotNull(r); findReplaceDocumentAdapter.find(0, "public", true, true, false, false); r = findReplaceDocumentAdapter.replace("private", false); assertNotNull(r); findReplaceDocumentAdapter.find(0, "public", true, true, false, false); try { findReplaceDocumentAdapter.replace("private", false); } catch (IllegalStateException e) { assertTrue(true); } String text = "package TestPackage;\n/*\n* comment\n*/\n\tprivate class Class {\n\t\t// comment1\n\t\tprivate void method1() {\n\t\t}\n\t\t// comment2\n\t\tprivate void method2() {\n\t\t}\n\t}\n// Gel枚st"; assertEquals(text, fDocument.get()); } catch (BadLocationException e) { Assert.assertTrue(false); } return fDocument; } public void _testRegexReplace(Document fDocument) throws Exception { fDocument.set("UnixWindowsMacInferred\nChars"); FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument); regexReplace("Unix", "$0\\n", findReplaceDocumentAdapter); regexReplace("(Windows)", "$1\\r\\n", findReplaceDocumentAdapter); regexReplace("(M)ac", "\\0\\r", findReplaceDocumentAdapter); regexReplace("(Inferred)", "\\1\\R", findReplaceDocumentAdapter); regexReplace("Chars", "\\\\, \\xF6, \\u00F6, \\t, \\n, \\r, \\f, \\a, \\e, \\cF", findReplaceDocumentAdapter); String text = "Unix\nWindows\r\nMac\rInferred\n\n\\, 枚, 枚, \t, \n, \r, \f, \007, \033, \006"; assertEquals(text, fDocument.get()); } @Test() @Given("#setUp") public Document testRegexReplace2(Document fDocument) throws Exception { FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument); fDocument.set("foo"); regexReplace("foo", "\\00", findReplaceDocumentAdapter); assertEquals("foo0", fDocument.get()); fDocument.set("foo"); regexReplace("foo", "\\010", findReplaceDocumentAdapter); assertEquals("foo10", fDocument.get()); fDocument.set("foo"); regexReplace("foo", "$00", findReplaceDocumentAdapter); assertEquals("foo0", fDocument.get()); fDocument.set("foo"); regexReplace("foo", "$010", findReplaceDocumentAdapter); assertEquals("foo10", fDocument.get()); return fDocument; } @Test() @Given("#setUp") public Document testRegexReplace3(Document fDocument) throws Exception { FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument); fDocument.set("foo"); regexReplace("(f)oo", "\\10", findReplaceDocumentAdapter); assertEquals("f0", fDocument.get()); fDocument.set("foo"); regexReplace("(f)oo", "$10", findReplaceDocumentAdapter); assertEquals("f0", fDocument.get()); return fDocument; } @Test() @Given("#setUp") public Document testRegexRetainCase(Document fDocument) throws Exception { FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument); fDocument.set("foo"); regexReplace("foo", "xyz\\Cbar\\Cfar", findReplaceDocumentAdapter); assertEquals("xyzbarfar", fDocument.get()); fDocument.set("FOO"); regexReplace("FOO", "xyz\\Cbar\\Cfar", findReplaceDocumentAdapter); assertEquals("xyzBARFAR", fDocument.get()); fDocument.set("Foo"); regexReplace("Foo", "xyz\\Cbar\\Cfar", findReplaceDocumentAdapter); assertEquals("xyzBarFar", fDocument.get()); fDocument.set("Foox"); regexReplace("F(oo)x", "\\C$1", findReplaceDocumentAdapter); assertEquals("oo", fDocument.get()); fDocument.set("Foo"); regexReplace("Foo", "xyz\\Cna\\u00EFve\\xFF\\C\\xFF", findReplaceDocumentAdapter); assertEquals("xyzNa茂ve每\u0178", fDocument.get()); fDocument.set("FOO"); regexReplace("FOO", "xyz\\Cna\\u00EFve\\xFF", findReplaceDocumentAdapter); assertEquals("xyzNA脧VE\u0178", fDocument.get()); return fDocument; } private void regexReplace(String find, String replace, FindReplaceDocumentAdapter findReplaceDocumentAdapter) throws BadLocationException { findReplaceDocumentAdapter.find(0, find, true, true, false, true); IRegion r = findReplaceDocumentAdapter.replace(replace, true); assertNotNull(r); } @Test() @Given("#setUp") public Document testRegexFindLinebreak(Document fDocument) throws Exception { FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(fDocument); String contents = "Unix\nWindows\r\nMac\rEnd"; fDocument.set(contents); int n = contents.indexOf("\n"); int rn = contents.indexOf("\r\n"); int r = contents.indexOf("\rEnd"); IRegion region = adapter.find(0, "\\R", true, false, false, true); assertEquals(new Region(n, 1), region); region = adapter.find(n + 1, "\\R", true, false, false, true); assertEquals(new Region(rn, 2), region); region = adapter.find(rn + 2, "\\R", true, false, false, true); assertEquals(new Region(r, 1), region); region = adapter.find(r + 1, "\\R", true, false, false, true); assertNull(region); return fDocument; } @Test() @Given("#setUp") public Document testRegexFindLinebreak2_fail(Document fDocument) throws Exception { FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(fDocument); String contents = "Unix\n[\\R]\\R\r\n"; fDocument.set(contents); int n = contents.indexOf("\n"); int rn = contents.indexOf("\r\n"); IRegion region = adapter.find(0, "[a-zA-Z\\t{\\\\R}]*\\{?\\R", true, false, false, true); assertEquals(new Region(0, n + 1), region); region = adapter.find(n + 1, "\\Q[\\R]\\R\\E{0,1}(\\R)", true, false, false, true); assertEquals(new Region(n + 1, rn + 2 - (n + 1)), region); try { adapter.replace("Win\\1$1", true); } catch (PatternSyntaxException ex) { return fDocument; } fail(); return fDocument; } public void _testRegexFindLinebreak2(Document fDocument) throws Exception { FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(fDocument); String contents = "+[\\R]\\R\r\n"; fDocument.set(contents); int n = contents.indexOf("["); int rn = contents.indexOf("\r\n"); IRegion region = adapter.find(0, "[a-zA-Z\\t{\\\\R}]*\\{?\\R", true, false, false, true); assertEquals(new Region(0, n - 1), region); region = adapter.find(n, "\\Q[\\R]\\R\\E{0,1}(\\R)", true, false, false, true); assertEquals(new Region(n, rn + 2 - n), region); adapter.replace("Win\\1$1", true); assertEquals("+Win\r\n\r\n", fDocument.get()); } @Test() @Given("#setUp") public Document testRegexFindLinebreak3(Document fDocument) throws Exception { FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(fDocument); String contents = "One\r\nTwo\r\n\r\nEnd"; fDocument.set(contents); int two = contents.indexOf("Two"); int end = contents.indexOf("End"); IRegion region = adapter.find(0, "[a-zA-Z]+\\R", true, false, false, true); assertEquals(new Region(0, two), region); region = adapter.find(two, "[a-zA-Z]+\\R", true, false, false, true); assertEquals(new Region(two, 3 + 2), region); region = adapter.find(0, "[a-zA-Z]+\\R{2}", true, false, false, true); assertEquals(new Region(two, end - two), region); return fDocument; } @Test() @Given("#setUp") public Document testRegexFindLinebreakIllegal(Document fDocument) throws Exception { FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(fDocument); fDocument.set("\n"); IRegion region = null; try { region = adapter.find(0, "[\\R]", true, false, false, true); } catch (PatternSyntaxException e) { } assertNull(region); try { region = adapter.find(0, "[\\s&&[^\\R]]", true, false, false, true); } catch (PatternSyntaxException e) { } assertNull(region); try { region = adapter.find(0, "\\p{\\R}", true, false, false, true); } catch (PatternSyntaxException e) { } assertNull(region); return fDocument; } @Test() @Given("#setUp") public Document testIllegalState(Document fDocument) { FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument); try { findReplaceDocumentAdapter.replace("TestPackage", false); } catch (IllegalStateException e) { Assert.assertTrue(true); } catch (BadLocationException e) { Assert.assertTrue(false); } findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument); try { findReplaceDocumentAdapter.replace("TestPackage", true); } catch (IllegalStateException e) { Assert.assertTrue(true); } catch (BadLocationException e) { Assert.assertTrue(false); } return fDocument; } @Test() @Given("#setUp") public Document testRegexFindStackOverflow_fail(Document fDocument) throws Exception { FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(fDocument); int len = 100000; char[] chars = new char[len]; Arrays.fill(chars, '\n'); chars[0] = '{'; chars[len - 1] = '}'; fDocument.set(new String(chars)); try { adapter.find(0, "\\{(.|[\\r\\n])*\\}", true, false, false, true); } catch (PatternSyntaxException ex) { return fDocument; } fail(); return fDocument; } }
注:本文转载自:
https://www.iam.unibe.ch/scg/svn_repos/Students/haense/CaseStudies/org.eclipse.text.tests/jexample/org/eclipse/text/tests/FindReplaceDocumentAdapterTestJUnit4JExample.java