KickJava   Java API By Example, From Geeks To Geeks.

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


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 ColumnSpec}.
37  *
38  * @author Karsten Lentzsch
39  * @version $Revision: 1.5 $
40  */

41 public final class ColumnSpecTest extends TestCase {
42     
43     /**
44      * Checks that the constructor rejects negative resize weights.
45      */

46     public void testRejectNegativeResizeWeight() {
47         try {
48             new ColumnSpec(ColumnSpec.DEFAULT, Sizes.DEFAULT, -1);
49             fail("The ColumnSpec constructor should reject negative resize weights.");
50         } catch (IllegalArgumentException JavaDoc e) {
51             // The expected behavior
52
} catch (Exception JavaDoc e) {
53             fail("The ColumnSpec constructor has thrown an unexpected exception.");
54         }
55     }
56     
57     /**
58      * Checks that the constructor rejects negative resize weights.
59      */

60     public void testRejectParsedNegativeResizeWeight() {
61         try {
62             new ColumnSpec("right:default:-1");
63             fail("The ColumnSpec parser constructor should reject negative resize weights.");
64         } catch (IllegalArgumentException JavaDoc e) {
65             // The expected behavior
66
} catch (Exception JavaDoc e) {
67             fail("The ColumnSpec constructor has thrown an unexpected exception.");
68         }
69     }
70
71     /**
72      * Tests the ColumnSpec parser on valid encodings.
73      */

74     public void testValidColumnSpecEncodings() {
75         ColumnSpec spec;
76         spec = new ColumnSpec(ColumnSpec.LEFT, Sizes.PREFERRED, FormSpec.NO_GROW);
77         assertEquals(spec, new ColumnSpec("l:p"));
78         assertEquals(spec, new ColumnSpec("left:p"));
79         assertEquals(spec, new ColumnSpec("l:pref"));
80         assertEquals(spec, new ColumnSpec("left:pref"));
81         
82         spec = new ColumnSpec(ColumnSpec.DEFAULT, Sizes.MINIMUM, FormSpec.NO_GROW);
83         assertEquals(spec, new ColumnSpec("min"));
84         assertEquals(spec, new ColumnSpec("f:min"));
85         assertEquals(spec, new ColumnSpec("fill:min"));
86         assertEquals(spec, new ColumnSpec("f:min:nogrow"));
87         assertEquals(spec, new ColumnSpec("fill:min:grow(0)"));
88         
89         spec = new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.NO_GROW);
90         assertEquals(spec, new ColumnSpec("d"));
91         assertEquals(spec, new ColumnSpec("default"));
92         assertEquals(spec, new ColumnSpec("f:default"));
93         assertEquals(spec, new ColumnSpec("fill:default"));
94         assertEquals(spec, new ColumnSpec("f:default:nogrow"));
95         assertEquals(spec, new ColumnSpec("fill:default:grow(0)"));
96         
97         spec = new ColumnSpec(ColumnSpec.RIGHT, Sizes.pixel(10), FormSpec.NO_GROW);
98         assertEquals(spec, new ColumnSpec("r:10px"));
99         assertEquals(spec, new ColumnSpec("right:10px"));
100         assertEquals(spec, new ColumnSpec("right:10px:nogrow"));
101         assertEquals(spec, new ColumnSpec("right:10px:grow(0)"));
102         assertEquals(spec, new ColumnSpec("right:10px:g(0)"));
103         
104         Size size = Sizes.bounded(Sizes.PREFERRED, Sizes.pixel(10), null);
105         spec = new ColumnSpec(ColumnSpec.RIGHT, size, FormSpec.NO_GROW);
106         assertEquals(spec, new ColumnSpec("right:max(10px;pref)"));
107         assertEquals(spec, new ColumnSpec("right:max(pref;10px)"));
108         
109         size = Sizes.bounded(Sizes.PREFERRED, null, Sizes.pixel(10));
110         spec = new ColumnSpec(ColumnSpec.RIGHT, size, FormSpec.NO_GROW);
111         assertEquals(spec, new ColumnSpec("right:min(10px;pref)"));
112         assertEquals(spec, new ColumnSpec("right:min(pref;10px)"));
113
114         size = Sizes.bounded(Sizes.DEFAULT, null, Sizes.pixel(10));
115         spec = new ColumnSpec(ColumnSpec.DEFAULT, size, FormSpec.NO_GROW);
116         assertEquals(spec, new ColumnSpec("min(10px;default)"));
117         assertEquals(spec, new ColumnSpec("min(10px;d)"));
118         assertEquals(spec, new ColumnSpec("min(default;10px)"));
119         assertEquals(spec, new ColumnSpec("min(d;10px)"));
120         
121         spec = new ColumnSpec(ColumnSpec.DEFAULT, Sizes.DEFAULT, FormSpec.DEFAULT_GROW);
122         assertEquals(spec, new ColumnSpec("d:grow"));
123         assertEquals(spec, new ColumnSpec("default:grow(1)"));
124         assertEquals(spec, new ColumnSpec("f:d:g"));
125         assertEquals(spec, new ColumnSpec("f:d:grow(1.0)"));
126         assertEquals(spec, new ColumnSpec("f:d:g(1.0)"));
127         
128         spec = new ColumnSpec(ColumnSpec.DEFAULT, Sizes.DEFAULT, 0.75);
129         assertEquals(spec, new ColumnSpec("d:grow(0.75)"));
130         assertEquals(spec, new ColumnSpec("default:grow(0.75)"));
131         assertEquals(spec, new ColumnSpec("f:d:grow(0.75)"));
132         assertEquals(spec, new ColumnSpec("fill:default:grow(0.75)"));
133     }
134     
135     
136     /**
137      * Tests that the ColumnSpec parser rejects invalid encodings.
138      */

139     public void testRejectInvalidColumnSpecEncodings() {
140         assertRejects("karsten");
141         assertRejects("d:a:b:");
142         assertRejects("top:default:grow");
143         assertRejects("bottom:10px");
144     }
145
146
147     /**
148      * Checks that an unmodifyable ColumnSpec rejects all modifications.
149      */

150     public void testUnmodifyable() {
151         ColumnSpec unmodifyableSpec = new ColumnSpec("p").asUnmodifyable();
152         try {
153             unmodifyableSpec.setDefaultAlignment(ColumnSpec.CENTER);
154             fail("An unmodifyable ColumnSpec should reject alignment changes.");
155         } catch (UnsupportedOperationException JavaDoc e) {
156             // The expected behavior
157
}
158         try {
159             unmodifyableSpec.setSize(Sizes.MINIMUM);
160             fail("An unmodifyable ColumnSpec should reject size changes.");
161         } catch (UnsupportedOperationException JavaDoc e) {
162             // The expected behavior
163
}
164         try {
165             unmodifyableSpec.setResizeWeight(5.5);
166             fail("An unmodifyable ColumnSpec should reject resize weight changes.");
167         } catch (UnsupportedOperationException JavaDoc e) {
168             // The expected behavior
169
}
170     }
171
172     
173     // Helper Code ***********************************************************
174

175     /**
176      * Checks if the given ColumnSpec instances are equal and throws a failure
177      * if not.
178      */

179     private void assertEquals(ColumnSpec spec1, ColumnSpec spec2) {
180         if (!spec1.getDefaultAlignment().equals(spec2.getDefaultAlignment())) {
181             fail("Alignment mismatch: spec1=" + spec1 + "; spec2=" + spec2);
182         }
183         if (!spec1.getSize().equals(spec2.getSize())) {
184             fail("Size mismatch: spec1=" + spec1 + "; spec2=" + spec2);
185         }
186         if (!(spec1.getResizeWeight() == spec2.getResizeWeight())) {
187             fail("Resize weight mismatch: spec1=" + spec1 + "; spec2=" + spec2);
188         }
189     }
190     
191     /**
192      * Asserts that the specified column spec encoding is rejected.
193      *
194      * @param encodedColumnSpec an encoded column spec
195      */

196     private void assertRejects(String JavaDoc invalidEncoding) {
197         try {
198             new ColumnSpec(invalidEncoding);
199             fail("The parser should reject the invalid encoding:" + invalidEncoding);
200         } catch (Exception JavaDoc e) {
201             // The expected behavior
202
}
203     }
204
205 }
206
Popular Tags