File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\test\java\hudson\MarkupTextTest.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson;
25
26 import junit.framework.TestCase;
27 import hudson.MarkupText.SubText;
28
29 import java.util.List;
30 import java.util.regex.Pattern;
31
32 /**
33 * @author Kohsuke Kawaguchi
34 */
35 public class MarkupTextTest extends TestCase {
36 public void test1() {
37 MarkupText t = new MarkupText("I fixed issue #155. The rest is trick text: xissue #155 issue #123x");
38 for (SubText st : t.findTokens(pattern)) {
39 assertEquals(1, st.groupCount());
40 st.surroundWith("<$1>","<$1>");
41 }
42
43 assertEquals("I fixed <155>issue #155<155>. The rest is trick text: xissue #155 issue #123x", t.toString(false));
44 }
45
46 public void testBoundary() {
47 MarkupText t = new MarkupText("issue #155---issue #123");
48 for (SubText st : t.findTokens(pattern))
49 st.surroundWith("<$1>","<$1>");
50
51 assertEquals("<155>issue #155<155>---<123>issue #123<123>", t.toString(false));
52 }
53
54 public void testFindTokensOnSubText() {
55 MarkupText t = new MarkupText("Fixed 2 issues in this commit, fixing issue 155, 145");
56 List<SubText> tokens = t.findTokens(Pattern.compile("issue .*"));
57 assertEquals("Expected one token", 1, tokens.size());
58 assertEquals("Expected single token was incorrect", "issue 155, 145", tokens.get(0).group(0));
59 for (SubText st : tokens.get(0).findTokens(Pattern.compile("([0-9]+)")))
60 st.surroundWith("<$1>","<$1>");
61
62 assertEquals("Fixed 2 issues in this commit, fixing issue <155>155<155>, <145>145<145>", t.toString(false));
63 }
64
65 public void testLiteralTextSurround() {
66 MarkupText text = new MarkupText("AAA test AAA");
67 for(SubText token : text.findTokens(Pattern.compile("AAA"))) {
68 token.surroundWithLiteral("$9","$9");
69 }
70 assertEquals("$9AAA$9 test $9AAA$9",text.toString(false));
71 }
72
73 /**
74 * Start/end tag nesting should be correct regardless of the order tags are added.
75 */
76 public void testAdjacent() {
77 MarkupText text = new MarkupText("abcdef");
78 text.addMarkup(0,3,"$","$");
79 text.addMarkup(3,6,"#","#");
80 assertEquals("$abc$#def#",text.toString(false));
81
82 text = new MarkupText("abcdef");
83 text.addMarkup(3,6,"#","#");
84 text.addMarkup(0,3,"$","$");
85 assertEquals("$abc$#def#",text.toString(false));
86 }
87
88 public void testEscape() {
89 MarkupText text = new MarkupText("&&&");
90 assertEquals("&amp;&amp;&amp;",text.toString(false));
91
92 text.addMarkup(1,"<foo>");
93 text.addMarkup(2,"&nbsp;");
94 assertEquals("&amp;<foo>&amp;&nbsp;&amp;",text.toString(false));
95 }
96
97 public void testPreEscape() {
98 MarkupText text = new MarkupText("Line\n2 & 3\n<End>\n");
99 assertEquals("Line\n2 &amp; 3\n&lt;End>\n", text.toString(true));
100 text.addMarkup(4, "<hr/>");
101 assertEquals("Line<hr/>\n2 &amp; 3\n&lt;End>\n", text.toString(true));
102 }
103
104 /* @Bug(6252) */
105 public void testSubTextSubText() {
106 MarkupText text = new MarkupText("abcdefgh");
107 SubText sub = text.subText(2, 7);
108 assertEquals("cdefg", sub.getText());
109 sub = sub.subText(1, 4);
110 assertEquals("def", sub.getText());
111
112 // test negative end
113 sub = text.subText(2, -3);
114 assertEquals("cdef", sub.getText());
115 sub = sub.subText(1, -2);
116 assertEquals("de", sub.getText());
117 }
118
119 private static final Pattern pattern = Pattern.compile("issue #([0-9]+)");
120 }
121