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

1 package jenkins.util;
2
3 import static org.junit.Assert.*;
4
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.Random;
8
9 import org.junit.Test;
10
11 /**
12 * Tests the class {@link TreeStringBuilder}.
13 *
14 * @author Kohsuke Kawaguchi
15 */
16 @SuppressWarnings({"PMD", "all"})
17 //CHECKSTYLE:OFF
18 public class TreeStringBuilderTest {
19 /**
20 * Tests the simple operations inside the builder.
21 */
22 @Test
23 public void test() {
24 TreeStringBuilder b = new TreeStringBuilder();
25 verify("foo", b.intern("foo"));
26 TreeString s = b.intern("foo/bar/zot");
27 verify("foo/bar/zot", s);
28 verify("", b.intern(""));
29 verify("foo/bar/xxx", b.intern("foo/bar/xxx")); // this will create new
30 // middle node
31 verify("foo/bar/zot", s); // make sure existing strings aren't affected
32 }
33
34 /**
35 * Pseudo random (but deterministic) test.
36 */
37 @Test
38 public void testRandom() {
39 String[] dict = new String[]{"aa","b","aba","ba"};
40 TreeStringBuilder x = new TreeStringBuilder();
41 Random r = new Random(0);
42
43 List<String> a = new ArrayList<String>();
44 List<TreeString> o = new ArrayList<TreeString>();
45
46 for (int i = 0; i < 1000; i++) {
47 StringBuilder b = new StringBuilder();
48 for (int j = 0; j < r.nextInt(10) + 3; j++) {
49 b.append(dict[r.nextInt(4)]);
50 }
51 String s = b.toString();
52
53 a.add(s);
54 TreeString p = x.intern(s);
55 verify(s, p);
56 o.add(p);
57 }
58
59 // make sure values are still all intact
60 for (int i = 0; i < a.size(); i++) {
61 verify(a.get(i), o.get(i));
62 }
63
64 x.dedup();
65
66 // verify one more time
67 for (int i = 0; i < a.size(); i++) {
68 verify(a.get(i), o.get(i));
69 }
70 }
71
72 private void verify(final String s, final TreeString t) {
73 assertEquals(s, t.toString());
74 }
75 }
76