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. J Exampleoptions; 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 findreplac Edocumentadaptertestjunit4jexample {@ test () public document setup () {document fdocument = new document (); string text; text = "package testpackage; \ n/* \ n * Comment \ n */\ n \ tpublic class {\ n \ t // comment1 \ n \ t \ tpublic void Method1 () {\ n \ t} \ n \ t // comment2 \ n \ t \ tpublic void method2 () {\ n \ t} \ N // gel ST "; fdocument. set (text); Return fdocument ;}@ test () @ given ("# testfindcaretinmiddleofwo Rd; # region; # testillegalstate; # testregexreplace2; # region; # testregexreplace3; # testutf8pattern; # region; # testreplace; # testbug74993; # testfind; # testregexretaincase; # testregexfindlinebreak3 ") public document teardown (document fdocument) {fdocument = NULL; retur N fdocument ;}@ test () @ given ("# setup") public document testfind (document fdocument) {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); assertequals (R, result); r = findreplacedocumentadapter. find (0, "testpackage", true, false); assertequals (R, result);} catch (badlocationexception e) {assert. asserttrue (false);} return fdocument;} @ test () @ given ("# setup") public document testfindcaretinmiddleofw Ord (document fdocument) {findreplacedocumentadapter = new findreplacedocumentadapter (fdocument); try {iregion r = findreplacedocumentadapter. find (12, "testpackage", true, false); assertnull (r); r = findreplacedocumentadapter. find (12, "testpackage", false, false); assertnull (r);} catch (badlocationexception e) {assert. asserttrue (false);} r Eturn fdocument ;}@ test () @ given ("# setup") public document libraries (document fdocument) {findreplacedocumentadapter = new findreplacedocumentadapter (fdocument); try {iregion r = findreplaceadapter document. find (8, "testpackage", true, false); assertequals (new region (8, 11), R);} catch (badlocationexception e) {assert. asserttrue (false );} Return fdocument ;}@ test () @ given ("# setup") public document libraries (document fdocument) {findreplacedocumentadapter = new findreplacedocumentadapter (fdocument); try {iregion r = findreplacedocumentadapter. find (19, "testpackage", 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 = new findreplacedocumentadapter (fdocument ); try {iregion r = findreplacedocumentadapter. find (12, "\ W +", false, true); assertequals (new region (6, 1), R);} catch (badlocationexception E) {assert. asserttrue (false);} return fdocum Ent ;}@ test () @ given ("# setup") public document testutf8pattern (document fdocument) {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, Fa LSE, false); assertequals (result, R); r = findreplacedocumentadapter. find (0, "gel before st", true, false); assertequals (result, R); r = findreplacedocumentadapter. find (0, "gel sponst", true, false); assertequals (result, R);} catch (badlocationexception e) {assert. asserttrue (false) ;}return fdocument ;}@ test () @ given ("# setup") public document testreplace (document fdocument ){ 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); assertnot NULL (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 {\ n \ t // comment1 \ n \ t \ tprivate void Method1 () {\ n \ t} \ n \ t // comment2 \ n \ t \ tprivate void method2 () {\ 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"); findreplaced Ocumentadapter 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 \\, x, X, \ t, \ n, \ r, \ f, \ 007, \ 033, \ 006 "; assertequals (text, fdocument. get () ;}@ test () @ given ("# setup") public document testregexreplace2 (document fdocument) throws exception {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", findreplacedoc Umentadapter); assertequals ("foo10", fdocument. get (); Return fdocument ;}@ test () @ given ("# setup") public document testregexreplace3 (document fdocument) throws exception {findreplacedocumentadapter = new findreplaceadapter (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 = new findreplacedocumentadapter (fdocument ); fdocument. set ("foo"); regexreplace ("foo", "XYZ \ cbar \ CFAR", findreplacedo Cumentadapter); 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", findreplacedocume Ntadapter); assertequals ("oo", fdocument. get (); fdocument. set ("foo"); regexreplace ("foo", "XYZ \ CNA \ u00efve \ xFF \ c \ xFF", findreplacedocumentadapter ); assertequals ("xyzna Mave per \ u0178", fdocument. get (); fdocument. set ("foo"); regexreplace ("foo", "XYZ \ CNA \ u00efve \ xFF", findreplacedocumentadapter); assertequals ("xyzna keep ve \ u0178 ", fdocument. get (); Return fdocument;} private void regexreplace (stri Ng Find, string replace, 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 = 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 findreplacedocu Mentadapter (fdocument); string contents = "UNIX \ n [\ 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 fdocumen) T) throws exception {findreplacedocumentadapter adapter = new findreplacedocumentadapter (fdocument); string contents = "+ [\ 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", 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 = 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 ;}}
Note: This article is reproduced from:
Https://www.iam.unibe.ch/scg/svn_repos/Students/haense/CaseStudies/org.eclipse.text.tests/jexample/org/eclipse/text/tests/FindReplaceDocumentAdapterTestJUnit4JExample.java