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