KickJava   Java API By Example, From Geeks To Geeks.

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


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  * A test case for class {@link CellConstraints}.
37  *
38  * @author Karsten Lentzsch
39  * @version $Revision: 1.4 $
40  */

41 public final class CellConstraintsTest extends TestCase {
42     
43     /**
44      * Checks that the constructor rejects non-positive origin and extent.
45      */

46     public void testRejectNonPositiveOriginAndExtent() {
47         assertRejects( 0, 1, 1, 1);
48         assertRejects(-1, 1, 1, 1);
49         assertRejects( 1, 0, 1, 1);
50         assertRejects( 1, -1, 1, 1);
51         assertRejects( 1, 1, 0, 1);
52         assertRejects( 1, 1, -1, 1);
53         assertRejects( 1, 1, 1, 0);
54         assertRejects( 1, 1, 1, -1);
55     }
56     
57     /**
58      * Tests the CellConstraints parser on valid encodings.
59      */

60     public void testValidEncodings() {
61         assertEquals(new CellConstraints(),
62                      new CellConstraints("1, 1"));
63
64         assertEquals(new CellConstraints(2, 3),
65                      new CellConstraints("2, 3"));
66
67         assertEquals(new CellConstraints(3, 4, 2, 5),
68                      new CellConstraints("3, 4, 2, 5"));
69         
70         assertEquals(new CellConstraints(5, 6,
71                                          CellConstraints.LEFT,
72                                          CellConstraints.BOTTOM),
73                      new CellConstraints("5, 6, left, bottom"));
74
75         assertEquals(new CellConstraints(7, 8, 3, 2,
76                                          CellConstraints.FILL,
77                                          CellConstraints.DEFAULT),
78                      new CellConstraints("7, 8, 3, 2, f, d"));
79     }
80
81     /**
82      * Tests that the CellConstraints parser rejects invalid encodings.
83      */

84     public void testRejectInvalidCellConstraintsEncodings() {
85         assertRejects("0, 1, 1, 1"); // Illegal bounds
86
assertRejects("0, 1, 1"); // Illegal number of arguments
87
assertRejects("0, 1, 1, 1, 1"); // Illegal number of arguments
88
assertRejects("1"); // Syntax error
89
assertRejects("1, 1, fill"); // Syntax error
90
assertRejects("1, 1, 3, 4, f"); // Syntax error
91
assertRejects("1, 1, top, center"); // Illegal column alignment
92
assertRejects("1, 1, fill, left"); // Illegal row alignment
93
assertRejects("1, 1, 2, 3, t, c"); // Illegal column alignment
94
assertRejects("1, 1, 2, 3, f, l"); // Illegal row alignment
95
}
96
97     /**
98      * Tests that the CellConstraints parser rejects invalid encodings.
99      */

100     public void testRejectInvalidCellConstraintsAlignments() {
101         try {
102             new CellConstraints(1, 1, CellConstraints.BOTTOM, CellConstraints.CENTER);
103             fail("The CellConstraints constructor should reject invalid orientations.");
104         } catch (IllegalArgumentException JavaDoc e) {
105             // The expected behavior
106
} catch (Exception JavaDoc e) {
107             fail("The constructor has thrown an unexpected exception: " + e);
108         }
109         try {
110             new CellConstraints(1, 1, CellConstraints.CENTER, CellConstraints.RIGHT);
111             fail("The CellConstraints constructor should reject invalid orientations.");
112         } catch (IllegalArgumentException JavaDoc e) {
113             // The expected behavior
114
} catch (Exception JavaDoc e) {
115             fail("The constructor has thrown an unexpected exception: " + e);
116         }
117         CellConstraints cc = new CellConstraints();
118         try {
119             cc.xy(1, 1, CellConstraints.BOTTOM, CellConstraints.CENTER);
120             fail("The CellConstraints setter should reject invalid orientations.");
121         } catch (IllegalArgumentException JavaDoc e) {
122             // The expected behavior
123
} catch (Exception JavaDoc e) {
124             fail("The setter has thrown an unexpected exception: " + e);
125         }
126         try {
127             cc.xy(1, 1, CellConstraints.BOTTOM, CellConstraints.CENTER);
128             fail("The CellConstraints setter should reject invalid orientations.");
129         } catch (IllegalArgumentException JavaDoc e) {
130             // The expected behavior
131
} catch (Exception JavaDoc e) {
132             fail("The setter has thrown an unexpected exception: " + e);
133         }
134     }
135
136     // Helper Code ***********************************************************
137

138     /**
139      * Checks if the CellConstraints constructor allows to construct
140      * an instance for the specified cell bounds.
141      */

142     private void assertRejects(String JavaDoc invalidEncoding) {
143         try {
144             new CellConstraints(invalidEncoding);
145             fail("The parser should reject the invalid encoding: " + invalidEncoding);
146         } catch (IllegalArgumentException JavaDoc e) {
147             // The expected behavior
148
} catch (IndexOutOfBoundsException JavaDoc e) {
149             // The expected behavior
150
} catch (Exception JavaDoc e) {
151             fail("The parser has thrown an unexpected exception for:"
152                  + invalidEncoding
153                  + "; exception=" + e);
154         }
155     }
156     
157     /**
158      * Checks if the CellConstraints constructor allows to construct
159      * an instance for the specified cell bounds.
160      */

161     private void assertRejects(int gridX, int gridY,
162                                 int gridWidth, int gridHeight) {
163         try {
164             new CellConstraints(gridX, gridY, gridWidth, gridHeight);
165             fail("The CellConstraints constructor should reject non-positive bounds values.");
166         } catch (IndexOutOfBoundsException JavaDoc e) {
167             // The expected behavior
168
} catch (Exception JavaDoc e) {
169             fail("The CellConstraints constructor has thrown an unexpected exception:" + e);
170         }
171     }
172     
173     /**
174      * Checks if the given RowSpec instances are equal and throws a failure
175      * if not.
176      */

177     private void assertEquals(CellConstraints cc1, CellConstraints cc2) {
178         if ( cc1.gridX != cc2.gridX
179             || cc1.gridY != cc2.gridY
180             || cc1.gridWidth != cc2.gridWidth
181             || cc1.gridHeight != cc2.gridHeight) {
182             fail("Bounds mismatch: cc1=" + cc1 + "; cc2=" + cc2);
183         }
184         if ( cc1.hAlign != cc2.hAlign
185             || cc1.vAlign != cc2.vAlign) {
186             fail("Alignment mismatch: cc1=" + cc1 + "; cc2=" + cc2);
187         }
188         if (!cc1.insets.equals(cc2.insets)) {
189             fail("Insets mismatch: cc1=" + cc1 + "; cc2=" + cc2);
190         }
191     }
192     
193     
194 }
195
196
Popular Tags