1 /* =========================================================== 2 * JFreeChart : a free chart library for the Java(tm) platform 3 * =========================================================== 4 * 5 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors. 6 * 7 * Project Info: http://www.jfree.org/jfreechart/index.html 8 * 9 * This library is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU Lesser General Public License as published by 11 * the Free Software Foundation; either version 2.1 of the License, or 12 * (at your option) any later version. 13 * 14 * This library is distributed in the hope that it will be useful, but 15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17 * License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this library; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22 * USA. 23 * 24 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 25 * Other names may be trademarks of their respective owners.] 26 * 27 * ------------------------------- 28 * CategoryLabelPositionsTest.java 29 * ------------------------------- 30 * (C) Copyright 2004-2013, by Object Refinery Limited and Contributors. 31 * 32 * Original Author: David Gilbert (for Object Refinery Limited); 33 * Contributor(s): -; 34 * 35 * Changes 36 * ------- 37 * 17-Feb-2004 : Version 1 (DG); 38 * 07-Jan-2005 : Added test for hashCode() (DG); 39 * 40 */ 41 42 package org.jfree.chart.axis; 43 44 import static org.junit.Assert.assertEquals; 45 import static org.junit.Assert.assertTrue; 46 47 import org.jfree.chart.TestUtilities; 48 49 import org.jfree.text.TextBlockAnchor; 50 import org.jfree.ui.RectangleAnchor; 51 import org.junit.Test; 52 53 /** 54 * Tests for the {@link CategoryLabelPositions} class. 55 */ 56 public class CategoryLabelPositionsTest { 57 58 private static final RectangleAnchor RA_TOP = RectangleAnchor.TOP; 59 private static final RectangleAnchor RA_BOTTOM = RectangleAnchor.BOTTOM; 60 61 /** 62 * Check that the equals method distinguishes all fields. 63 */ 64 @Test testEquals()65 public void testEquals() { 66 CategoryLabelPositions p1 = new CategoryLabelPositions( 67 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 68 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 69 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 70 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)); 71 CategoryLabelPositions p2 = new CategoryLabelPositions( 72 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 73 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 74 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 75 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)); 76 assertEquals(p1, p2); 77 78 p1 = new CategoryLabelPositions( 79 new CategoryLabelPosition(RA_BOTTOM, 80 TextBlockAnchor.TOP_CENTER), 81 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 82 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 83 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)); 84 assertTrue(!p1.equals(p2)); 85 p2 = new CategoryLabelPositions( 86 new CategoryLabelPosition(RA_BOTTOM, 87 TextBlockAnchor.TOP_CENTER), 88 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 89 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 90 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)); 91 assertTrue(p1.equals(p2)); 92 93 p1 = new CategoryLabelPositions( 94 new CategoryLabelPosition(RA_BOTTOM, 95 TextBlockAnchor.TOP_CENTER), 96 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 97 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 98 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)); 99 assertTrue(!p1.equals(p2)); 100 p2 = new CategoryLabelPositions( 101 new CategoryLabelPosition(RA_BOTTOM, 102 TextBlockAnchor.TOP_CENTER), 103 new CategoryLabelPosition(RA_BOTTOM, 104 TextBlockAnchor.TOP_CENTER), 105 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 106 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)); 107 assertTrue(p1.equals(p2)); 108 109 p1 = new CategoryLabelPositions( 110 new CategoryLabelPosition(RA_BOTTOM, 111 TextBlockAnchor.TOP_CENTER), 112 new CategoryLabelPosition(RA_BOTTOM, 113 TextBlockAnchor.TOP_CENTER), 114 new CategoryLabelPosition(RA_BOTTOM, 115 TextBlockAnchor.TOP_CENTER), 116 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)); 117 assertTrue(!p1.equals(p2)); 118 p2 = new CategoryLabelPositions( 119 new CategoryLabelPosition(RA_BOTTOM, 120 TextBlockAnchor.TOP_CENTER), 121 new CategoryLabelPosition(RA_BOTTOM, 122 TextBlockAnchor.TOP_CENTER), 123 new CategoryLabelPosition(RA_BOTTOM, 124 TextBlockAnchor.TOP_CENTER), 125 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)); 126 assertTrue(p1.equals(p2)); 127 128 p1 = new CategoryLabelPositions( 129 new CategoryLabelPosition(RA_BOTTOM, 130 TextBlockAnchor.TOP_CENTER), 131 new CategoryLabelPosition(RA_BOTTOM, 132 TextBlockAnchor.TOP_CENTER), 133 new CategoryLabelPosition(RA_BOTTOM, 134 TextBlockAnchor.TOP_CENTER), 135 new CategoryLabelPosition(RA_BOTTOM, 136 TextBlockAnchor.TOP_CENTER)); 137 assertTrue(!p1.equals(p2)); 138 p2 = new CategoryLabelPositions( 139 new CategoryLabelPosition(RA_BOTTOM, 140 TextBlockAnchor.TOP_CENTER), 141 new CategoryLabelPosition(RA_BOTTOM, 142 TextBlockAnchor.TOP_CENTER), 143 new CategoryLabelPosition(RA_BOTTOM, 144 TextBlockAnchor.TOP_CENTER), 145 new CategoryLabelPosition(RA_BOTTOM, 146 TextBlockAnchor.TOP_CENTER)); 147 assertTrue(p1.equals(p2)); 148 } 149 150 /** 151 * Two objects that are equal are required to return the same hashCode. 152 */ 153 @Test testHashCode()154 public void testHashCode() { 155 CategoryLabelPositions p1 = new CategoryLabelPositions( 156 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 157 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 158 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 159 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)); 160 CategoryLabelPositions p2 = new CategoryLabelPositions( 161 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 162 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 163 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 164 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)); 165 assertTrue(p1.equals(p2)); 166 int h1 = p1.hashCode(); 167 int h2 = p2.hashCode(); 168 assertEquals(h1, h2); 169 } 170 171 /** 172 * Serialize an instance, restore it, and check for equality. 173 */ 174 @Test testSerialization()175 public void testSerialization() { 176 CategoryLabelPositions p1 = CategoryLabelPositions.STANDARD; 177 CategoryLabelPositions p2 = (CategoryLabelPositions) TestUtilities.serialised(p1); 178 assertEquals(p1, p2); 179 } 180 181 } 182