1   /*
2    * Copyright (c) 1998, 2005 Gargoyle Software Inc. 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    * 1. Redistributions of source code must retain the above copyright notice,
8    *    this list of conditions and the following disclaimer.
9    * 2. Redistributions in binary form must reproduce the above copyright notice,
10   *    this list of conditions and the following disclaimer in the documentation
11   *    and/or other materials provided with the distribution.
12   * 3. The end-user documentation included with the redistribution, if any, must
13   *    include the following acknowledgment:
14   *
15   *       "This product includes software developed by Gargoyle Software Inc.
16   *        (http://www.GargoyleSoftware.com/)."
17   *
18   *    Alternately, this acknowledgment may appear in the software itself, if
19   *    and wherever such third-party acknowledgments normally appear.
20   * 4. The name "Gargoyle Software" must not be used to endorse or promote
21   *    products derived from this software without prior written permission.
22   *    For written permission, please contact info@GargoyleSoftware.com.
23   * 5. Products derived from this software may not be called "GSBase", nor may
24   *    "GSBase" appear in their name, without prior written permission of
25   *    Gargoyle Software Inc.
26   *
27   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30   * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   */
38  package com.gargoylesoftware.base.gui;
39  
40  import com.gargoylesoftware.base.util.DetailedIllegalArgumentException;
41  import com.gargoylesoftware.base.util.DetailedNullPointerException;
42  import junit.framework.TestCase;
43  
44  /***
45   * Tests for TableLayoutConstraints
46   *
47   * @version  $Revision: 1.6 $
48   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
49   */
50  public class TableLayoutConstraintsTest extends TestCase {
51  
52      /***
53       * Create a new test case.
54       * @param name The name of the test.
55       */
56      public TableLayoutConstraintsTest( final String name ) {
57          super(name);
58      }
59  
60      /***
61       * Test the constuctor with legal values.
62       */
63      public void testConstructor2Parms() {
64          final TableLayoutConstraints constraints
65          = new TableLayoutConstraints(1,2);
66  
67          assertEquals("row", constraints.getRow(), 1 );
68          assertEquals("column", constraints.getColumn(),2);
69          assertEquals("rowSpan", constraints.getRowSpan(), 1);
70          assertEquals("columnSpan", constraints.getColumnSpan(), 1 );
71  
72          assertEquals("verticalAlignment",
73                       constraints.getVerticalAlignment(),
74                       TableLayout.CENTER );
75          assertTrue("verticalStretch",
76                 constraints.getVerticalStretch() == false );
77  
78          assertEquals("horizontalAlignment",
79                       constraints.getHorizontalAlignment(),
80                       TableLayout.LEFT );
81          assertTrue("horizontalStretch",
82                 constraints.getHorizontalStretch() == false );
83      }
84  
85      /***
86       * Test the constructor will negative parameters.
87       */
88      public void testConstructor2Parms_Negative() {
89          try {
90              new TableLayoutConstraints(-1,2);
91              fail("Expected exception for negative row");
92          }
93          catch( final IllegalArgumentException e ) {
94              // Expected path
95          }
96  
97          try {
98              new TableLayoutConstraints(1,-2);
99              fail("Expected exception for negative column");
100         }
101         catch( final IllegalArgumentException e ) {
102             // Expected path
103         }
104     }
105 
106     /***
107      * Test a negative row.
108      */
109     public void testNegativeRow() {
110         try {
111             new TableLayoutConstraints(-1,2);
112             fail("Expected IllegalArgumentException");
113         }
114         catch( final IllegalArgumentException e ) {
115             // Expected path
116         }
117     }
118 
119     /***
120      * Test a negative row span
121      */
122     public void testNegativeRowSpan() {
123         final TableLayoutConstraints constraints = new TableLayoutConstraints(1,2);
124         try {
125             constraints.setRowSpan(-1);
126             fail("Expected IllegalArgumentException");
127         }
128         catch( final IllegalArgumentException e ) {
129             // Expected path
130         }
131     }
132 
133     /***
134      * Test a zero row span.
135      */
136     public void testZeroRowSpan() {
137         final TableLayoutConstraints constraints = new TableLayoutConstraints(1,2);
138         try {
139             constraints.setRowSpan(0);
140             fail("Expected IllegalArgumentException");
141         }
142         catch( final IllegalArgumentException e ) {
143             // Expected path
144         }
145     }
146 
147     /***
148      * Test a negative column.
149      */
150     public void testNegativeColumn() {
151         try {
152             new TableLayoutConstraints(1,-2);
153             fail("Expected IllegalArgumentException");
154         }
155         catch( final IllegalArgumentException e ) {
156             // Expected path
157         }
158     }
159 
160     /***
161      * Test a negative column span.
162      */
163     public void testNegativeColumnSpan() {
164         final TableLayoutConstraints constraints = new TableLayoutConstraints(1,2);
165         try {
166             constraints.setColumnSpan(-4);
167             fail("Expected IllegalArgumentException");
168         }
169         catch( final IllegalArgumentException e ) {
170             // Expected path
171         }
172     }
173 
174     /***
175      * Test a zero column span
176      */
177     public void testZeroColumnSpan() {
178         final TableLayoutConstraints constraints = new TableLayoutConstraints(1,2);
179         try {
180             constraints.setColumnSpan(0);
181             fail("Expected IllegalArgumentException");
182         }
183         catch( final IllegalArgumentException e ) {
184             // Expected path
185         }
186     }
187 
188     /***
189      * Test an illegal vertical alignment.
190      */
191     public void testIllegalVerticalAlignment() {
192         final TableLayoutConstraints constraints = new TableLayoutConstraints(1,2);
193         try {
194             constraints.setVerticalAlignment(TableLayout.RIGHT);
195             fail("Expected IllegalArgumentException");
196         }
197         catch( final IllegalArgumentException e ) {
198             // Expected path
199         }
200     }
201 
202     /***
203      * Test an illegal horizontal alignment.
204      */
205     public void testIllegalHorizontalAlignment() {
206         final TableLayoutConstraints constraints = new TableLayoutConstraints(1,2);
207         try {
208             constraints.setHorizontalAlignment(TableLayout.TOP);
209             fail("Expected IllegalArgumentException");
210         }
211         catch( final IllegalArgumentException e ) {
212             // Expected path
213         }
214     }
215 
216 
217     /***
218      * Test makeConstraints() with bad input.
219      */
220     public void testMakeConstraints_BadData() {
221         final String data[] = {
222             "", ","
223         };
224 
225         int i;
226         for( i=0; i<data.length; i++ ) {
227             try {
228                 TableLayoutConstraints.makeConstraints(data[i]);
229                 fail("Expected exception for ["+data[i]+"]");
230             }
231             catch( final DetailedIllegalArgumentException e ) {
232                 assertEquals(e.getMessage(), "constraintString", e.getArgumentName());
233             }
234         }
235     }
236 
237 
238     /***
239      * Test makeConstraints() with bad input.
240      */
241     public void testMakeConstraints_Null() {
242 
243         try {
244             TableLayoutConstraints.makeConstraints(null);
245             fail("Expected exception for null");
246         }
247         catch( final DetailedNullPointerException e ) {
248             assertEquals("constraintString", e.getArgumentName());
249         }
250     }
251 
252 
253     /***
254      * Test makeConstraints with legal input.
255      */
256     public void testMakeConstraints() {
257         final TableLayoutConstraints constraints
258             = TableLayoutConstraints.makeConstraints("5,17");
259         assertEquals("row", 5, constraints.getRow() );
260         assertEquals("column", 17, constraints.getColumn() );
261     }
262 
263     /***
264      * Test make constraints with spanning.
265      */
266     public void testMakeConstraints_spanning() {
267         final TableLayoutConstraints constraints
268             = TableLayoutConstraints.makeConstraints("5+2,7+3");
269         assertEquals("row", 5, constraints.getRow() );
270         assertEquals("column", 7, constraints.getColumn() );
271         assertEquals("rowSpan", 2, constraints.getRowSpan() );
272         assertEquals("columnSpan", 3, constraints.getColumnSpan() );
273 
274         if( constraints.getVerticalStretch() == true ) {
275             fail("Not expecting vertical stretch");
276         }
277         if( constraints.getHorizontalStretch() == true ) {
278             fail("Not expecting horizontal stretch");
279         }
280     }
281 
282     /***
283      * Test makeConstraints() with stretching.
284      */
285     public void testMakeConstraints_stretching() {
286         final TableLayoutConstraints constraints
287             = TableLayoutConstraints.makeConstraints("5s,7+3s");
288         assertEquals("row", 5, constraints.getRow() );
289         assertEquals("column", 7, constraints.getColumn() );
290         assertEquals("columnSpan", 3, constraints.getColumnSpan() );
291 
292         if( constraints.getVerticalStretch() == false ) {
293             fail("Expected vertical stretch");
294         }
295         if( constraints.getHorizontalStretch() == false ) {
296             fail("Expected horizontal stretch");
297         }
298     }
299 
300 }