KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > forms > layout > FormLayoutGroupsTest


1 /*
2  * Copyright (c) 2003 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.forms.layout;
32
33 import junit.framework.TestCase;
34
35 /**
36  * Tests column and row groups of the FormLayout.
37  *
38  * @author Karsten Lentzsch
39  * @version $Revision: 1.4 $
40  */

41
42 public final class FormLayoutGroupsTest extends TestCase {
43     
44     private FormLayout layout;
45
46     /**
47      * @see TestCase#setUp()
48      */

49     protected void setUp() throws Exception JavaDoc {
50         super.setUp();
51         layout = new FormLayout(
52             "pref, pref, pref, pref",
53             "pref, pref, pref, pref");
54     }
55
56     /**
57      * @see TestCase#tearDown()
58      */

59     protected void tearDown() throws Exception JavaDoc {
60         super.tearDown();
61         layout = null;
62     }
63
64     /**
65      * Checks that column groups use a deep copy mechanism,
66      * not a shallow copy.
67      */

68     public void testDeepCopyColumnGroups() {
69         int[][] columnGroups = createAllGroups();
70         layout.setColumnGroups(columnGroups);
71
72         // Modify the column group set (first level).
73
columnGroups[1] = new int[]{1, 4};
74         if (equals(columnGroups, layout.getColumnGroups()))
75             fail("Column group sets should be immutable.");
76         
77         // Modify a column group (second level)
78
columnGroups[0][0] = 5;
79         if (equals(columnGroups, layout.getColumnGroups()))
80             fail("Column groups should be immutable.");
81     }
82     
83     
84     /**
85      * Checks that row groups use a deep copy mechanism,
86      * not a shallow copy.
87      */

88     public void testDeepCopyRowGroups() {
89         int[][] rowGroups = createAllGroups();
90         layout.setRowGroups(rowGroups);
91
92         // Modify the row group set (first level).
93
rowGroups[1] = new int[]{1, 4};
94         if (equals(rowGroups, layout.getRowGroups()))
95             fail("The row group sets should be immutable.");
96         
97         // Modify a row group (second level)
98
rowGroups[0][0] = 5;
99         if (equals(rowGroups, layout.getRowGroups()))
100             fail("Row groups should be immutable.");
101     }
102     
103     /**
104      * Tests if invalid column indices are rejected.
105      */

106     public void testRejectInvalidColumnIndex() {
107         try {
108             layout.setColumnGroups(new int[][]{{1, 5}});
109             fail("An invalid column index should be rejected.");
110         } catch (IndexOutOfBoundsException JavaDoc e) {
111             // The expected behavior
112
}
113     }
114     
115     /**
116      * Tests if invalid row indices are rejected.
117      */

118     public void testRejectInvalidRowIndex() {
119         try {
120             layout.setRowGroups(new int[][]{{1, 5}});
121             fail("An invalid row index should be rejected.");
122         } catch (IndexOutOfBoundsException JavaDoc e) {
123             // The expected behavior
124
}
125     }
126     
127     /**
128      * Tests if duplicate column indices are rejected.
129      */

130     public void testRejectDuplicateColumnIndex() {
131         try {
132             layout.setColumnGroups(new int[][]{{1, 2}, {2, 3}});
133             fail("A duplicate column index should be rejected.");
134         } catch (IllegalArgumentException JavaDoc e) {
135             // The expected behavior
136
}
137     }
138     
139     /**
140      * Tests if duplicate row indices are rejected.
141      */

142     public void testRejectDuplicateRowIndex() {
143         try {
144             layout.setRowGroups(new int[][]{{1, 2}, {2, 3}});
145             fail("A duplicate row index should be rejected.");
146         } catch (IllegalArgumentException JavaDoc e) {
147             // The expected behavior
148
}
149     }
150     
151
152     // Helper Code *************************************************
153

154     private int[][] createAllGroups() {
155         int[] group1 = new int[]{1, 2};
156         int[] group2 = new int[]{3, 4};
157         return new int[][] {group1, group2};
158     }
159     
160     /**
161      * Checks and answers if the two-dimensional arrays are equal.
162      * @param array1 a two-dimensional array
163      * @param array2 a second two-dimensional array
164      * @return true if both arrays are equal, false otherwise
165      */

166     private boolean equals(int[][] array1, int[][] array2) {
167         if (array1.length != array2.length)
168             return false;
169         for (int i = 0; i < array1.length; i++) {
170             int[] subarray1 = array1[i];
171             int[] subarray2 = array2[i];
172             if (subarray1.length != subarray2.length)
173                 return false;
174             for (int j = 0; j < subarray1.length; j++) {
175                 if (subarray1[j] != subarray2[j])
176                     return false;
177             }
178         }
179         return true;
180     }
181
182 }
Popular Tags