KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

46     public void testRejectNegativeResizeWeight() {
47         try {
48             new RowSpec(RowSpec.DEFAULT, Sizes.DEFAULT, -1);
49             fail("The RowSpec constructor should reject negative resize weights.");
50         } catch (IllegalArgumentException JavaDoc e) {
51             // The expected behavior
52
} catch (Exception JavaDoc e) {
53             fail("The RowSpec 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 RowSpec("right:default:-1");
63             fail("The RowSpec parser constructor should reject negative resize weights.");
64         } catch (IllegalArgumentException JavaDoc e) {
65             // The expected behavior
66
} catch (Exception JavaDoc e) {
67             fail("The RowSpec constructor has thrown an unexpected exception.");
68         }
69     }
70
71     /**
72      * Tests the RowSpec parser on valid encodings.
73      */

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

137     public void testRejectInvalidRowSpecEncodings() {
138         assertRejects("karsten");
139         assertRejects("d:a:b:");
140         assertRejects("right:default:grow");
141         assertRejects("left:20dlu");
142     }
143
144
145     /**
146      * Checks that an unmodifyable RowSpec rejects all modifications.
147      */

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

173     /**
174      * Checks if the given RowSpec instances are equal and throws a failure
175      * if not.
176      */

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

194     private void assertRejects(String JavaDoc encodedRowSpec) {
195         try {
196             new RowSpec(encodedRowSpec);
197             fail("The parser should reject encoding:" + encodedRowSpec);
198         } catch (Exception JavaDoc e) {
199             // The expected behavior
200
}
201     }
202
203 }
204
Popular Tags