    1   /*
    2    * @(#) D2Test.java Aug 26, 2003
    3    *
    4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
    5    * the Netherlands All rights reserved.
    6    *
    7    * This software is proprietary information of Delft University of Technology
    8    * The code is published under the General Public License
    9    */
    10  package nl.tudelft.simulation.dsol.animation.D2;
    11
    12  import java.awt.Dimension;
    13  import java.awt.geom.Point2D;
    14  import java.awt.geom.Rectangle2D;
    15
    16  import junit.framework.Assert;
    17  import junit.framework.TestCase;
    18  import nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface;
    19
    20  /***
    21   * This class defines the JUnit test for the D2Test <br>
    22   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
    23   * University of Technology </a>, the Netherlands. <br>
    24   * See for project information <a
    25   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
    26   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
    27   * License (GPL) </a>, no warranty <br>
    28   *
    29   * @version 2.0 21.09.2003 <br>
    30   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
    31   *         Jacobs </a>, <a
    32   *         href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
    33   *         Verbraeck </a>
    34   */
    35  public class D2Test extends TestCase
    36  {
    37  	/*** TEST_METHOD_NAME refers to the name of the test method */
    38  	public static final String TEST_METHOD_NAME = "test";
    39
    40  	/***
    41  	 * constructs a new D2Test
    42  	 */
    43  	public D2Test()
    44  	{
    45  		super(TEST_METHOD_NAME);
    46  	}
    47
    48  	/***
    49  	 * tests the 2D Animation
    50  	 */
    51  	public void test()
    52  	{
    53  		Rectangle2D extent = new Rectangle2D.Double(0, 0, 100, 100);
    54  		Dimension size = new Dimension(100, 100);
    55  		//Let's focus on the scale..
    56  		Assert
    57  				.assertTrue(Renderable2DInterface.Util.getScale(extent, size) == 1.0);
    58  		size.setSize(200, 200);
    59  		Assert
    60  				.assertTrue(Renderable2DInterface.Util.getScale(extent, size) == 0.5);
    61  		extent.setRect(0, 0, 50, 50);
    62  		Assert
    63  				.assertTrue(Renderable2DInterface.Util.getScale(extent, size) == 0.25);
    64  		//Let's test infinity pointer values...
    65  		size.setSize(0, 0);
    66  		Assert.assertTrue(Double.isInfinite(Renderable2DInterface.Util
    67  				.getScale(extent, size)));
    68  		//Let's test invalid screen size
    69  		size.setSize(-1, -1);
    70  		Assert.assertTrue(Double.isNaN(Renderable2DInterface.Util.getScale(
    71  				extent, size)));
    72  		//Let's test wrong ratio values
    73  		size.setSize(50, 100);
    74  		Assert.assertTrue(Double.isNaN(Renderable2DInterface.Util.getScale(
    75  				extent, size)));
    76  		size.setSize(100, 100);
    77  		extent.setRect(0, 0, 100, 100);
    78  		Point2D point = new Point2D.Double(1, 1);
    79  		Assert.assertTrue(Renderable2DInterface.Util.getScreenCoordinates(
    80  				point, extent, size).distance(1, 99) == 0);
    81  		size.setSize(200, 200);
    82  		extent.setRect(0, 0, 100, 100);
    83  		point = new Point2D.Double(1, 1);
    84  		Assert.assertTrue(Renderable2DInterface.Util.getScreenCoordinates(
    85  				point, extent, size).distance(2, 198) == 0);
    86  		//Invalid screen
    87  		size.setSize(-200, -200);
    88  		extent.setRect(0, 0, 100, 100);
    89  		point = new Point2D.Double(1, 1);
    90  		Assert.assertNull(Renderable2DInterface.Util.getScreenCoordinates(
    91  				point, extent, size));
    92  		//Invalid ratio
    93  		size.setSize(200, 100);
    94  		extent.setRect(0, 0, 100, 100);
    95  		point = new Point2D.Double(1, 1);
    96  		Assert.assertNull(Renderable2DInterface.Util.getScreenCoordinates(
    97  				point, extent, size));
    98  		//Let's test for null values
    99  		size.setSize(100, 100);
    100 		extent.setRect(0, 0, 100, 100);
    101 		point = new Point2D.Double(1, 1);
    102 		Assert.assertNull(Renderable2DInterface.Util.getScreenCoordinates(null,
    103 				extent, size));
    104 		Assert.assertNull(Renderable2DInterface.Util.getScreenCoordinates(
    105 				point, null, size));
    106 		Assert.assertNull(Renderable2DInterface.Util.getScreenCoordinates(
    107 				point, extent, null));
    108 		// point not in extent
    109 		size.setSize(100, 100);
    110 		extent.setRect(0, 0, 100, 100);
    111 		point = new Point2D.Double(-1, 1);
    112 		Assert.assertNull(Renderable2DInterface.Util.getScreenCoordinates(
    113 				point, extent, size));
    114 		// ********************* WORLD COORDINATES ASSERTIONS **************//
    115 		size.setSize(100, 100);
    116 		extent.setRect(0, 0, 100, 100);
    117 		point = new Point2D.Double(1, 1);
    118 		Assert.assertTrue(Renderable2DInterface.Util.getWorldCoordinates(point,
    119 				extent, size).distance(1, 99) == 0);
    120
    121 		size.setSize(200, 200);
    122 		extent.setRect(0, 0, 100, 100);
    123 		point = new Point2D.Double(1, 1);
    124 		Assert.assertTrue(Renderable2DInterface.Util.getWorldCoordinates(point,
    125 				extent, size).distance(0.5, 99.5) == 0);
    126
    127 		//Invalid screen
    128 		size.setSize(-200, -200);
    129 		extent.setRect(0, 0, 100, 100);
    130 		point = new Point2D.Double(1, 1);
    131 		Assert.assertNull(Renderable2DInterface.Util.getWorldCoordinates(point,
    132 				extent, size));
    133
    134 		//Invalid ratio
    135 		size.setSize(200, 100);
    136 		extent.setRect(0, 0, 100, 100);
    137 		point = new Point2D.Double(1, 1);
    138 		Assert.assertNull(Renderable2DInterface.Util.getWorldCoordinates(point,
    139 				extent, size));
    140
    141 		//Let's test for null values
    142 		size.setSize(100, 100);
    143 		extent.setRect(0, 0, 100, 100);
    144 		point = new Point2D.Double(1, 1);
    145 		Assert.assertNull(Renderable2DInterface.Util.getWorldCoordinates(null,
    146 				extent, size));
    147 		Assert.assertNull(Renderable2DInterface.Util.getWorldCoordinates(point,
    148 				null, size));
    149 		Assert.assertNull(Renderable2DInterface.Util.getWorldCoordinates(point,
    150 				extent, null));
    151
    152 		// point not in extent
    153 		size.setSize(100, 100);
    154 		extent.setRect(0, 0, 100, 100);
    155 		point = new Point2D.Double(-1, 1);
    156 		Assert.assertNull(Renderable2DInterface.Util.getWorldCoordinates(point,
    157 				extent, size));
    158
    159 		// ********************* COMPUTE VISIBLE EXTENT **************//
    160 		size.setSize(200, 50);
    161 		extent.setRect(0, 0, 1000, 1000);
    162 		extent = Renderable2DInterface.Util.computeVisibleExtent(extent, size);
    163 		Assert
    164 				.assertTrue(Renderable2DInterface.Util.getScale(extent, size) == 5.0);
    165 		Assert.assertTrue(extent.getMinY() == 375.0);
    166 		Assert.assertTrue(extent.getHeight() == 250.0);
    167 	}
    168 }