1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.swing;
17  
18  import java.awt.Dimension;
19  import java.awt.Point;
20  
21  import junit.framework.Test;
22  import junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  import junit.textui.TestRunner;
25  
26  import org.apache.commons.beanutils.ConvertUtils;
27  
28  import org.apache.commons.jelly.tags.swing.SwingTagLibrary;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /***
34   * Tests the Swing converters
35   *
36   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
37   * @version $Revision: 1.8 $
38   */
39  public class TestConverters extends TestCase {
40  
41      /*** The Log to which logging calls will be made. */
42      private static final Log log = LogFactory.getLog(TestConverters.class);
43  
44      /*** Delta used to compare doubles */
45      double delta = 0.0000001;
46  
47      // force the Swing converters to be loaded
48      SwingTagLibrary dummy = new SwingTagLibrary();
49  
50      public static void main(String[] args) {
51          TestRunner.run(suite());
52      }
53  
54      public static Test suite() {
55          return new TestSuite(TestConverters.class);
56      }
57  
58      public TestConverters(String testName) {
59          super(testName);
60      }
61  
62  
63      public void testDimensions() throws Exception {
64          assertDimension("100, 200", new Dimension(100, 200));
65          assertDimension("100", new Dimension(100, 0));
66          assertDimension(" 100  ,  200  ", new Dimension(100, 200));
67          assertDimension(" 0  ,  200  ", new Dimension(0, 200));
68      }
69  
70      public void testPoints() throws Exception {
71          assertPoint("100, 200", new Point(100, 200));
72          assertPoint("100", new Point(100, 0));
73          assertPoint(" 100  ,  200  ", new Point(100, 200));
74          assertPoint(" 0  ,  200  ", new Point(0, 200));
75      }
76  
77      // Implementation methods
78      //-------------------------------------------------------------------------
79  
80      protected void assertPoint(String expression, Point expected) throws Exception {
81          Object answer = ConvertUtils.convert(expression, Point.class );
82  
83          assertTrue( "Returned type: "+  answer.getClass() + " is-a Point", answer instanceof Point );
84  
85          Point value = (Point) answer;
86  
87          assertEquals( "x", expected.getX(), value.getX(), delta );
88          assertEquals( "y", expected.getY(), value.getY(), delta );
89  
90          assertEquals( expected, value );
91      }
92  
93      protected void assertDimension(String expression, Dimension expected) throws Exception {
94          Object answer = ConvertUtils.convert(expression, Dimension.class );
95  
96          assertTrue( "Returned type: "+  answer.getClass() + " is-a Dimension", answer instanceof Dimension );
97  
98          Dimension value = (Dimension) answer;
99  
100         assertEquals( "width", expected.getWidth(), value.getWidth(), delta );
101         assertEquals( "height", expected.getHeight(), value.getHeight(), delta );
102 
103         assertEquals( expected, value );
104     }
105 }