001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.forms.layout;
032:
033: import java.util.Locale;
034:
035: import junit.framework.TestCase;
036:
037: /**
038: * A test case for class {@link CellConstraints}.
039: *
040: * @author Karsten Lentzsch
041: * @version $Revision: 1.11 $
042: */
043: public final class CellConstraintsTest extends TestCase {
044:
045: /**
046: * Checks that the constructor rejects non-positive origin and extent.
047: */
048: public void testRejectNonPositiveOriginAndExtent() {
049: assertRejects(0, 1, 1, 1);
050: assertRejects(-1, 1, 1, 1);
051: assertRejects(1, 0, 1, 1);
052: assertRejects(1, -1, 1, 1);
053: assertRejects(1, 1, 0, 1);
054: assertRejects(1, 1, -1, 1);
055: assertRejects(1, 1, 1, 0);
056: assertRejects(1, 1, 1, -1);
057: }
058:
059: /**
060: * Tests that the CellConstraints parser rejects invalid alignments.
061: */
062: public void testRejectInvalidCellConstraintsAlignments() {
063: try {
064: new CellConstraints(1, 1, CellConstraints.BOTTOM,
065: CellConstraints.CENTER);
066: fail("The CellConstraints constructor should reject invalid orientations.");
067: } catch (IllegalArgumentException e) {
068: // The expected behavior
069: } catch (Exception e) {
070: fail("The constructor has thrown an unexpected exception: "
071: + e);
072: }
073: try {
074: new CellConstraints(1, 1, CellConstraints.CENTER,
075: CellConstraints.RIGHT);
076: fail("The CellConstraints constructor should reject invalid orientations.");
077: } catch (IllegalArgumentException e) {
078: // The expected behavior
079: } catch (Exception e) {
080: fail("The constructor has thrown an unexpected exception: "
081: + e);
082: }
083: CellConstraints cc = new CellConstraints();
084: try {
085: cc.xy(1, 1, CellConstraints.BOTTOM, CellConstraints.CENTER);
086: fail("The CellConstraints setter should reject invalid orientations.");
087: } catch (IllegalArgumentException e) {
088: // The expected behavior
089: } catch (Exception e) {
090: fail("The setter has thrown an unexpected exception: " + e);
091: }
092: try {
093: cc.xy(1, 1, CellConstraints.BOTTOM, CellConstraints.CENTER);
094: fail("The CellConstraints setter should reject invalid orientations.");
095: } catch (IllegalArgumentException e) {
096: // The expected behavior
097: } catch (Exception e) {
098: fail("The setter has thrown an unexpected exception: " + e);
099: }
100: }
101:
102: /**
103: * Tests the CellConstraints parser on valid encodings with different locales.
104: */
105: public void testValidEncodings() {
106: testValidEncodings(Locale.ENGLISH);
107: testValidEncodings(AllFormsTests.TURKISH);
108: }
109:
110: /**
111: * Tests with different locales that the CellConstraints parser
112: * rejects invalid encodings.
113: */
114: public void testRejectInvalidCellConstraintsEncodings() {
115: testRejectInvalidCellConstraintsEncodings(Locale.ENGLISH);
116: testRejectInvalidCellConstraintsEncodings(AllFormsTests.TURKISH);
117: }
118:
119: /**
120: * Tests the CellConstraints parser on valid encodings with a given locale.
121: *
122: * @param locale the Locale used while parsing the strings
123: */
124: private void testValidEncodings(Locale locale) {
125: Locale oldDefault = Locale.getDefault();
126: Locale.setDefault(locale);
127: try {
128: CellConstraints cc;
129: cc = new CellConstraints();
130: assertEquals(cc, new CellConstraints("1, 1"));
131:
132: cc = new CellConstraints(2, 3);
133: assertEquals(cc, new CellConstraints("2, 3"));
134:
135: cc = new CellConstraints(3, 4, 2, 5);
136: assertEquals(cc, new CellConstraints("3, 4, 2, 5"));
137:
138: cc = new CellConstraints(5, 6, CellConstraints.LEFT,
139: CellConstraints.BOTTOM);
140: assertEquals(cc, new CellConstraints("5, 6, l, b"));
141: assertEquals(cc, new CellConstraints("5, 6, L, B"));
142: assertEquals(cc, new CellConstraints("5, 6, left, bottom"));
143: assertEquals(cc, new CellConstraints("5, 6, LEFT, BOTTOM"));
144:
145: cc = new CellConstraints(7, 8, 3, 2, CellConstraints.FILL,
146: CellConstraints.DEFAULT);
147: assertEquals(cc, new CellConstraints("7, 8, 3, 2, f, d"));
148: assertEquals(cc, new CellConstraints("7, 8, 3, 2, F, D"));
149: assertEquals(cc, new CellConstraints(
150: "7, 8, 3, 2, fill, default"));
151: assertEquals(cc, new CellConstraints(
152: "7, 8, 3, 2, FILL, DEFAULT"));
153: } finally {
154: Locale.setDefault(oldDefault);
155: }
156: }
157:
158: /**
159: * Tests that the CellConstraints parser rejects invalid encodings
160: * on a given locale.
161: *
162: * @param locale the Locale used while parsing the strings
163: */
164: private void testRejectInvalidCellConstraintsEncodings(Locale locale) {
165: Locale oldDefault = Locale.getDefault();
166: Locale.setDefault(locale);
167: try {
168: assertRejects("0, 1, 1, 1"); // Illegal bounds
169: assertRejects("0, 1, 1"); // Illegal number of arguments
170: assertRejects("0, 1, 1, 1, 1"); // Illegal number of arguments
171: assertRejects("1"); // Syntax error
172: assertRejects("1, 1, fill"); // Syntax error
173: assertRejects("1, 1, 3, 4, f"); // Syntax error
174: assertRejects("1, 1, top, center"); // Illegal column alignment
175: assertRejects("1, 1, fill, left"); // Illegal row alignment
176: assertRejects("1, 1, F\u0131LL, TOP"); // Illegal Turkish char
177: assertRejects("1, 1, 2, 3, t, c"); // Illegal column alignment
178: assertRejects("1, 1, 2, 3, f, l"); // Illegal row alignment
179: } finally {
180: Locale.setDefault(oldDefault);
181: }
182: }
183:
184: // Helper Code ***********************************************************
185:
186: /**
187: * Checks if the CellConstraints constructor allows to construct
188: * an instance for the specified cell bounds.
189: *
190: * @param invalidEncoding the encoding that should be rejected
191: */
192: private void assertRejects(String invalidEncoding) {
193: try {
194: new CellConstraints(invalidEncoding);
195: fail("The parser should reject the invalid encoding: "
196: + invalidEncoding);
197: } catch (IllegalArgumentException e) {
198: // The expected behavior
199: } catch (IndexOutOfBoundsException e) {
200: // The expected behavior
201: } catch (Exception e) {
202: fail("The parser has thrown an unexpected exception for:"
203: + invalidEncoding + "; exception=" + e);
204: }
205: }
206:
207: /**
208: * Checks if the CellConstraints constructor allows to construct
209: * an instance for the specified cell bounds.
210: *
211: * @param gridX the first column in the grid
212: * @param gridY the first row in the grid
213: * @param gridWidth the column span
214: * @param gridHeight the row span
215: */
216: private void assertRejects(int gridX, int gridY, int gridWidth,
217: int gridHeight) {
218: try {
219: new CellConstraints(gridX, gridY, gridWidth, gridHeight);
220: fail("The CellConstraints constructor should reject non-positive bounds values.");
221: } catch (IndexOutOfBoundsException e) {
222: // The expected behavior
223: } catch (Exception e) {
224: fail("The CellConstraints constructor has thrown an unexpected exception:"
225: + e);
226: }
227: }
228:
229: /**
230: * Checks if the given RowSpec instances are equal and throws a failure
231: * if not.
232: *
233: * @param cc1 the first constraints object to be compared
234: * @param cc2 the second constraints object to be compared
235: */
236: private void assertEquals(CellConstraints cc1, CellConstraints cc2) {
237: if (cc1.gridX != cc2.gridX || cc1.gridY != cc2.gridY
238: || cc1.gridWidth != cc2.gridWidth
239: || cc1.gridHeight != cc2.gridHeight) {
240: fail("Bounds mismatch: cc1=" + cc1 + "; cc2=" + cc2);
241: }
242: if (cc1.hAlign != cc2.hAlign || cc1.vAlign != cc2.vAlign) {
243: fail("Alignment mismatch: cc1=" + cc1 + "; cc2=" + cc2);
244: }
245: if (!cc1.insets.equals(cc2.insets)) {
246: fail("Insets mismatch: cc1=" + cc1 + "; cc2=" + cc2);
247: }
248: }
249:
250: }
|