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 ColumnSpec}.
039: *
040: * @author Karsten Lentzsch
041: * @version $Revision: 1.13 $
042: */
043: public final class ColumnSpecTest extends TestCase {
044:
045: /**
046: * Checks that the constructor rejects negative resize weights.
047: */
048: public void testRejectNegativeResizeWeight() {
049: try {
050: new ColumnSpec(ColumnSpec.DEFAULT, Sizes.DEFAULT, -1);
051: fail("The ColumnSpec constructor should reject negative resize weights.");
052: } catch (IllegalArgumentException e) {
053: // The expected behavior
054: } catch (Exception e) {
055: fail("The ColumnSpec constructor has thrown an unexpected exception.");
056: }
057: }
058:
059: /**
060: * Checks that the constructor rejects negative resize weights.
061: */
062: public void testRejectParsedNegativeResizeWeight() {
063: try {
064: new ColumnSpec("right:default:-1");
065: fail("The ColumnSpec parser constructor should reject negative resize weights.");
066: } catch (IllegalArgumentException e) {
067: // The expected behavior
068: } catch (Exception e) {
069: fail("The ColumnSpec constructor has thrown an unexpected exception.");
070: }
071: }
072:
073: /**
074: * Tests the ColumnSpec parser on valid encodings with different Locales.
075: */
076: public void testValidColumnSpecEncodings() {
077: testValidColumnSpecEncodings(Locale.ENGLISH);
078: testValidColumnSpecEncodings(AllFormsTests.TURKISH);
079: }
080:
081: /**
082: * Tests with different Locales that the ColumnSpec parser
083: * rejects invalid encodings.
084: */
085: public void testRejectInvalidColumnSpecEncodings() {
086: testRejectInvalidColumnSpecEncodings(Locale.ENGLISH);
087: testRejectInvalidColumnSpecEncodings(AllFormsTests.TURKISH);
088: }
089:
090: /**
091: * Tests the ColumnSpec parser on valid encodings for a given Locale.
092: *
093: * @param locale the Locale used while parsing the strings
094: */
095: private void testValidColumnSpecEncodings(Locale locale) {
096: Locale oldDefault = Locale.getDefault();
097: Locale.setDefault(locale);
098: try {
099: ColumnSpec spec;
100: spec = new ColumnSpec(ColumnSpec.LEFT, Sizes.PREFERRED,
101: FormSpec.NO_GROW);
102: assertEquals(spec, new ColumnSpec("l:p"));
103: assertEquals(spec, new ColumnSpec("L:P"));
104: assertEquals(spec, new ColumnSpec("left:p"));
105: assertEquals(spec, new ColumnSpec("LEFT:P"));
106: assertEquals(spec, new ColumnSpec("l:pref"));
107: assertEquals(spec, new ColumnSpec("L:PREF"));
108: assertEquals(spec, new ColumnSpec("left:pref"));
109: assertEquals(spec, new ColumnSpec("LEFT:PREF"));
110:
111: spec = new ColumnSpec(ColumnSpec.DEFAULT, Sizes.MINIMUM,
112: FormSpec.NO_GROW);
113: assertEquals(spec, new ColumnSpec("min"));
114: assertEquals(spec, new ColumnSpec("MIN"));
115: assertEquals(spec, new ColumnSpec("f:min"));
116: assertEquals(spec, new ColumnSpec("fill:min"));
117: assertEquals(spec, new ColumnSpec("FILL:MIN"));
118: assertEquals(spec, new ColumnSpec("f:min:nogrow"));
119: assertEquals(spec, new ColumnSpec("F:MIN:NOGROW"));
120: assertEquals(spec, new ColumnSpec("fill:min:grow(0)"));
121:
122: spec = new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT,
123: FormSpec.NO_GROW);
124: assertEquals(spec, new ColumnSpec("d"));
125: assertEquals(spec, new ColumnSpec("default"));
126: assertEquals(spec, new ColumnSpec("DEFAULT"));
127: assertEquals(spec, new ColumnSpec("f:default"));
128: assertEquals(spec, new ColumnSpec("fill:default"));
129: assertEquals(spec, new ColumnSpec("f:default:nogrow"));
130: assertEquals(spec, new ColumnSpec("fill:default:grow(0)"));
131: assertEquals(spec, new ColumnSpec("FILL:DEFAULT:GROW(0)"));
132:
133: spec = new ColumnSpec(ColumnSpec.RIGHT, Sizes.pixel(10),
134: FormSpec.NO_GROW);
135: assertEquals(spec, new ColumnSpec("r:10px"));
136: assertEquals(spec, new ColumnSpec("right:10px"));
137: assertEquals(spec, new ColumnSpec("right:10px:nogrow"));
138: assertEquals(spec, new ColumnSpec("RIGHT:10PX:NOGROW"));
139: assertEquals(spec, new ColumnSpec("right:10px:grow(0)"));
140: assertEquals(spec, new ColumnSpec("right:10px:g(0)"));
141:
142: Size size = Sizes.bounded(Sizes.PREFERRED, Sizes.pixel(10),
143: null);
144: spec = new ColumnSpec(ColumnSpec.RIGHT, size,
145: FormSpec.NO_GROW);
146: assertEquals(spec, new ColumnSpec("right:max(10px;pref)"));
147: assertEquals(spec, new ColumnSpec("right:max(pref;10px)"));
148:
149: size = Sizes
150: .bounded(Sizes.PREFERRED, null, Sizes.pixel(10));
151: spec = new ColumnSpec(ColumnSpec.RIGHT, size,
152: FormSpec.NO_GROW);
153: assertEquals(spec, new ColumnSpec("right:min(10px;pref)"));
154: assertEquals(spec, new ColumnSpec("right:min(pref;10px)"));
155:
156: size = Sizes.bounded(Sizes.DEFAULT, null, Sizes.pixel(10));
157: spec = new ColumnSpec(ColumnSpec.DEFAULT, size,
158: FormSpec.NO_GROW);
159: assertEquals(spec, new ColumnSpec("min(10px;default)"));
160: assertEquals(spec, new ColumnSpec("MIN(10PX;DEFAULT)"));
161: assertEquals(spec, new ColumnSpec("min(10px;d)"));
162: assertEquals(spec, new ColumnSpec("min(default;10px)"));
163: assertEquals(spec, new ColumnSpec("min(d;10px)"));
164:
165: spec = new ColumnSpec(ColumnSpec.DEFAULT, Sizes.DEFAULT,
166: FormSpec.DEFAULT_GROW);
167: assertEquals(spec, new ColumnSpec("d:grow"));
168: assertEquals(spec, new ColumnSpec("default:grow(1)"));
169: assertEquals(spec, new ColumnSpec("f:d:g"));
170: assertEquals(spec, new ColumnSpec("f:d:grow(1.0)"));
171: assertEquals(spec, new ColumnSpec("f:d:g(1.0)"));
172:
173: spec = new ColumnSpec(ColumnSpec.DEFAULT, Sizes.DEFAULT,
174: 0.75);
175: assertEquals(spec, new ColumnSpec("d:grow(0.75)"));
176: assertEquals(spec, new ColumnSpec("default:grow(0.75)"));
177: assertEquals(spec, new ColumnSpec("f:d:grow(0.75)"));
178: assertEquals(spec,
179: new ColumnSpec("fill:default:grow(0.75)"));
180: assertEquals(spec,
181: new ColumnSpec("FILL:DEFAULT:GROW(0.75)"));
182:
183: spec = new ColumnSpec("fill:10in");
184: assertEquals(spec, new ColumnSpec("FILL:10IN"));
185: } finally {
186: Locale.setDefault(oldDefault);
187: }
188: }
189:
190: /**
191: * Tests that the ColumnSpec parser rejects invalid encodings for a given Locale.
192: *
193: * @param locale the Locale used while parsing the strings
194: */
195: private void testRejectInvalidColumnSpecEncodings(Locale locale) {
196: Locale oldDefault = Locale.getDefault();
197: Locale.setDefault(locale);
198: try {
199: assertRejects("karsten");
200: assertRejects("d:a:b:");
201: assertRejects("top:default:grow");
202: assertRejects("bottom:10px");
203: } finally {
204: Locale.setDefault(oldDefault);
205: }
206: }
207:
208: // Helper Code ***********************************************************
209:
210: /**
211: * Checks if the given ColumnSpec instances are equal and throws a failure
212: * if not.
213: *
214: * @param spec1 the first spec object to be compared
215: * @param spec2 the second spec object to be compared
216: */
217: private void assertEquals(ColumnSpec spec1, ColumnSpec spec2) {
218: if (!spec1.getDefaultAlignment().equals(
219: spec2.getDefaultAlignment())) {
220: fail("Alignment mismatch: spec1=" + spec1 + "; spec2="
221: + spec2);
222: }
223: if (!spec1.getSize().equals(spec2.getSize())) {
224: fail("Size mismatch: spec1=" + spec1 + "; spec2=" + spec2);
225: }
226: if (!(spec1.getResizeWeight() == spec2.getResizeWeight())) {
227: fail("Resize weight mismatch: spec1=" + spec1 + "; spec2="
228: + spec2);
229: }
230: }
231:
232: /**
233: * Asserts that the specified column spec encoding is rejected.
234: *
235: * @param invalidEncoding the invalid encoded column spec
236: */
237: private void assertRejects(String invalidEncoding) {
238: try {
239: new ColumnSpec(invalidEncoding);
240: fail("The parser should reject the invalid encoding:"
241: + invalidEncoding);
242: } catch (Exception e) {
243: // The expected behavior
244: }
245: }
246:
247: }
|