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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, 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 hudson.model.Saveable;
27 import junit.framework.TestCase;
28
29 import java.io.IOException;
30
31 /**
32 * Tests {@link BulkChange}.
33 *
34 * @author Kohsuke Kawaguchi
35 */
36 public class BulkChangeTest extends TestCase {
37
38 private class Point implements Saveable {
39 /**
40 * Don't actually do any save, but just remember how many the actual I/O would have happened.
41 */
42 int saveCount = 0;
43
44 @SuppressWarnings("unused")
45 int x,y;
46
47 public void setX(int x) throws IOException {
48 this.x = x;
49 save();
50 }
51
52 public void setY(int y) throws IOException {
53 this.y = y;
54 save();
55 }
56
57 public void set(int x, int y) throws IOException {
58 setX(x);
59 setY(y);
60 }
61
62 public void save() throws IOException {
63 if(BulkChange.contains(this)) return;
64 saveCount++;
65 }
66 }
67
68 /**
69 * If there is no BulkChange, we should see two saves.
70 */
71 public void testNoBulkChange() throws Exception {
72 Point pt = new Point();
73 pt.set(0,0);
74 assertEquals(2,pt.saveCount);
75 }
76
77 /**
78 * With a {@link BulkChange}, this will become just one save.
79 */
80 public void testBulkChange() throws Exception {
81 Point pt = new Point();
82 BulkChange bc = new BulkChange(pt);
83 try {
84 pt.set(0,0);
85 } finally {
86 bc.commit();
87 }
88 assertEquals(1,pt.saveCount);
89 }
90
91 /**
92 * {@link BulkChange}s can be nested.
93 */
94 public void testNestedBulkChange() throws Exception {
95 Point pt = new Point();
96 Point _ = new Point();
97 BulkChange bc1 = new BulkChange(pt);
98 try {
99 BulkChange bc2 = new BulkChange(_);
100 try {
101 BulkChange bc3 = new BulkChange(pt);
102 try {
103 pt.set(0,0);
104 } finally {
105 bc3.commit();
106 }
107 } finally {
108 bc2.commit();
109 }
110 pt.set(0,0);
111 } finally {
112 bc1.commit();
113 }
114 assertEquals(1,pt.saveCount);
115 }
116 }
117