| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2010, 2012 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Francois Chouinard - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.core.tests.trace; |
| 14 | |
| 15 | import junit.framework.TestCase; |
| 16 | |
| 17 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
| 18 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
| 19 | import org.eclipse.linuxtools.tmf.core.trace.TmfLocation; |
| 20 | |
| 21 | /** |
| 22 | * <b><u>TmfLocationTest</u></b> |
| 23 | * <p> |
| 24 | * Test suite for the TmfLocation class. |
| 25 | */ |
| 26 | @SuppressWarnings("nls") |
| 27 | public class TmfLocationTest extends TestCase { |
| 28 | |
| 29 | // ------------------------------------------------------------------------ |
| 30 | // Variables |
| 31 | // ------------------------------------------------------------------------ |
| 32 | |
| 33 | String aString = "some location"; |
| 34 | Long aLong = 12345L; |
| 35 | TmfTimestamp aTimestamp = new TmfTimestamp(); |
| 36 | |
| 37 | TmfLocation<Boolean> fLocation0; |
| 38 | TmfLocation<String> fLocation1; |
| 39 | TmfLocation<String> fLocation2; |
| 40 | TmfLocation<Long> fLocation3; |
| 41 | TmfLocation<ITmfTimestamp> fLocation4; |
| 42 | |
| 43 | // ------------------------------------------------------------------------ |
| 44 | // Housekeeping |
| 45 | // ------------------------------------------------------------------------ |
| 46 | |
| 47 | /** |
| 48 | * @param name |
| 49 | * the test name |
| 50 | */ |
| 51 | public TmfLocationTest(String name) { |
| 52 | super(name); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | protected void setUp() throws Exception { |
| 57 | super.setUp(); |
| 58 | fLocation0 = TmfLocation.NULL_LOCATION; |
| 59 | fLocation1 = new TmfLocation<String>((String) null); |
| 60 | fLocation2 = new TmfLocation<String>(aString); |
| 61 | fLocation3 = new TmfLocation<Long>(aLong); |
| 62 | fLocation4 = new TmfLocation<ITmfTimestamp>(aTimestamp); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | protected void tearDown() throws Exception { |
| 67 | super.tearDown(); |
| 68 | } |
| 69 | |
| 70 | // ------------------------------------------------------------------------ |
| 71 | // Constructors |
| 72 | // ------------------------------------------------------------------------ |
| 73 | |
| 74 | public void testTmfLocation() { |
| 75 | assertNull("TmfLocation", fLocation0.getLocation()); |
| 76 | assertNull("TmfLocation", fLocation1.getLocation()); |
| 77 | assertEquals("TmfLocation", aString, fLocation2.getLocation()); |
| 78 | assertEquals("TmfLocation", aLong, fLocation3.getLocation()); |
| 79 | assertEquals("TmfLocation", aTimestamp, fLocation4.getLocation()); |
| 80 | } |
| 81 | |
| 82 | public void testTmfLocationCopy() { |
| 83 | TmfLocation<Boolean> location0 = new TmfLocation<Boolean>(fLocation0); |
| 84 | TmfLocation<String> location1 = new TmfLocation<String>(fLocation1); |
| 85 | TmfLocation<String> location2 = new TmfLocation<String>(fLocation2); |
| 86 | TmfLocation<Long> location3 = new TmfLocation<Long>(fLocation3); |
| 87 | TmfLocation<ITmfTimestamp> location4 = new TmfLocation<ITmfTimestamp>(fLocation4); |
| 88 | |
| 89 | assertNull("TmfLocation", location0.getLocation()); |
| 90 | assertNull("TmfLocation", location1.getLocation()); |
| 91 | assertEquals("TmfLocation", aString, location2.getLocation()); |
| 92 | assertEquals("TmfLocation", aLong, location3.getLocation()); |
| 93 | assertEquals("TmfLocation", aTimestamp, location4.getLocation()); |
| 94 | } |
| 95 | |
| 96 | // ------------------------------------------------------------------------ |
| 97 | // clone |
| 98 | // ------------------------------------------------------------------------ |
| 99 | |
| 100 | public void testClone() { |
| 101 | try { |
| 102 | TmfLocation<String> location1 = fLocation1.clone(); |
| 103 | TmfLocation<String> location2 = fLocation2.clone(); |
| 104 | TmfLocation<Long> location3 = fLocation3.clone(); |
| 105 | TmfLocation<ITmfTimestamp> location4 = fLocation4.clone(); |
| 106 | |
| 107 | assertEquals("clone", fLocation1, location1); |
| 108 | assertEquals("clone", fLocation2, location2); |
| 109 | assertEquals("clone", fLocation3, location3); |
| 110 | assertEquals("clone", fLocation4, location4); |
| 111 | |
| 112 | assertEquals("clone", fLocation1.getLocation(), location1.getLocation()); |
| 113 | assertEquals("clone", fLocation2.getLocation(), location2.getLocation()); |
| 114 | assertEquals("clone", fLocation3.getLocation(), location3.getLocation()); |
| 115 | assertEquals("clone", fLocation4.getLocation(), location4.getLocation()); |
| 116 | |
| 117 | assertNull("clone", location1.getLocation()); |
| 118 | assertEquals("clone", aString, location2.getLocation()); |
| 119 | assertEquals("clone", aLong, location3.getLocation()); |
| 120 | assertEquals("clone", aTimestamp, location4.getLocation()); |
| 121 | } catch (InternalError e) { |
| 122 | fail("clone()"); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | public static class MyCloneableClass implements Cloneable, Comparable<MyCloneableClass> { |
| 127 | private String fName; |
| 128 | |
| 129 | public MyCloneableClass(String name) { |
| 130 | fName = name; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public String toString() { |
| 135 | return fName; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public MyCloneableClass clone() { |
| 140 | MyCloneableClass clone = null; |
| 141 | try { |
| 142 | clone = (MyCloneableClass) super.clone(); |
| 143 | clone.fName = fName; |
| 144 | } catch (CloneNotSupportedException e) { |
| 145 | } |
| 146 | return clone; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public boolean equals(Object o) { |
| 151 | return fName.compareTo(((MyCloneableClass) o).fName) == 0; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public int compareTo(MyCloneableClass o) { |
| 156 | return fName.compareTo(o.fName); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | public void testCloneCloneable() { |
| 161 | try { |
| 162 | MyCloneableClass myClass = new MyCloneableClass("myCloneableClass"); |
| 163 | TmfLocation<MyCloneableClass> location = new TmfLocation<MyCloneableClass>(myClass); |
| 164 | TmfLocation<MyCloneableClass> clone = location.clone(); |
| 165 | |
| 166 | assertEquals("clone", location, clone); |
| 167 | assertEquals("clone", location.getLocation(), clone.getLocation()); |
| 168 | assertEquals("clone", myClass, location.getLocation()); |
| 169 | } catch (InternalError e) { |
| 170 | fail("clone a cloneable class"); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | public static class MyUnCloneableClass implements Comparable<MyUnCloneableClass> { |
| 175 | private String fName; |
| 176 | |
| 177 | public MyUnCloneableClass(String name) { |
| 178 | fName = name; |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public String toString() { |
| 183 | return fName; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public Object clone() throws CloneNotSupportedException { |
| 188 | throw new CloneNotSupportedException(); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public boolean equals(Object o) { |
| 193 | return fName.compareTo(((MyUnCloneableClass) o).fName) == 0; |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public int compareTo(MyUnCloneableClass o) { |
| 198 | return fName.compareTo(o.fName); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | public void testCloneUncloneable() { |
| 203 | try { |
| 204 | MyUnCloneableClass myClass = new MyUnCloneableClass("myUncloneableClass"); |
| 205 | TmfLocation<MyUnCloneableClass> myLocation = new TmfLocation<MyUnCloneableClass>(myClass); |
| 206 | myLocation.clone(); |
| 207 | fail("clone an uncloneable class"); |
| 208 | } catch (InternalError e) { |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // ------------------------------------------------------------------------ |
| 213 | // hashCode |
| 214 | // ------------------------------------------------------------------------ |
| 215 | |
| 216 | public void testHashCode() throws Exception { |
| 217 | TmfLocation<String> location1 = new TmfLocation<String>((String) null); |
| 218 | TmfLocation<String> location2 = new TmfLocation<String>(aString); |
| 219 | TmfLocation<Long> location3 = new TmfLocation<Long>(aLong); |
| 220 | |
| 221 | assertTrue("hashCode", fLocation1.hashCode() == location1.hashCode()); |
| 222 | assertTrue("hashCode", fLocation2.hashCode() == location2.hashCode()); |
| 223 | assertTrue("hashCode", fLocation3.hashCode() == location3.hashCode()); |
| 224 | |
| 225 | assertTrue("hashCode", fLocation2.hashCode() != location3.hashCode()); |
| 226 | assertTrue("hashCode", fLocation3.hashCode() != location2.hashCode()); |
| 227 | } |
| 228 | |
| 229 | // ------------------------------------------------------------------------ |
| 230 | // toEquals |
| 231 | // ------------------------------------------------------------------------ |
| 232 | |
| 233 | private static class TmfLocation2 extends TmfLocation<String> { |
| 234 | public TmfLocation2(String location) { |
| 235 | super(location); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | public void testEqualsWrongTypes() throws Exception { |
| 240 | TmfLocation<String> location1 = new TmfLocation<String>(aString); |
| 241 | TmfLocation2 location2 = new TmfLocation2(aString); |
| 242 | |
| 243 | assertFalse("equals", location1.equals(location2)); |
| 244 | assertFalse("equals", location2.equals(location1)); |
| 245 | } |
| 246 | |
| 247 | public void testEqualsWithNulls() throws Exception { |
| 248 | TmfLocation<String> location1 = new TmfLocation<String>(aString); |
| 249 | TmfLocation<String> location2 = new TmfLocation<String>((String) null); |
| 250 | |
| 251 | assertFalse("equals", location1.equals(location2)); |
| 252 | assertFalse("equals", location2.equals(location1)); |
| 253 | } |
| 254 | |
| 255 | public void testEqualsReflexivity() throws Exception { |
| 256 | assertTrue("equals", fLocation2.equals(fLocation2)); |
| 257 | assertTrue("equals", fLocation3.equals(fLocation3)); |
| 258 | |
| 259 | assertTrue("equals", !fLocation2.equals(fLocation3)); |
| 260 | assertTrue("equals", !fLocation3.equals(fLocation2)); |
| 261 | } |
| 262 | |
| 263 | public void testEqualsSymmetry() throws Exception { |
| 264 | TmfLocation<String> location2 = new TmfLocation<String>(aString); |
| 265 | TmfLocation<Long> location3 = new TmfLocation<Long>(aLong); |
| 266 | |
| 267 | assertTrue("equals", location2.equals(fLocation2)); |
| 268 | assertTrue("equals", fLocation2.equals(location2)); |
| 269 | |
| 270 | assertTrue("equals", location3.equals(fLocation3)); |
| 271 | assertTrue("equals", fLocation3.equals(location3)); |
| 272 | } |
| 273 | |
| 274 | public void testEqualsTransivity() throws Exception { |
| 275 | TmfLocation<String> location1 = new TmfLocation<String>(aString); |
| 276 | TmfLocation<String> location2 = new TmfLocation<String>(aString); |
| 277 | TmfLocation<String> location3 = new TmfLocation<String>(aString); |
| 278 | |
| 279 | assertTrue("equals", location1.equals(location2)); |
| 280 | assertTrue("equals", location2.equals(location3)); |
| 281 | assertTrue("equals", location3.equals(location1)); |
| 282 | } |
| 283 | |
| 284 | public void testEqualsNull() throws Exception { |
| 285 | assertTrue("equals", !fLocation2.equals(null)); |
| 286 | assertTrue("equals", !fLocation2.equals(null)); |
| 287 | } |
| 288 | |
| 289 | // ------------------------------------------------------------------------ |
| 290 | // toString |
| 291 | // ------------------------------------------------------------------------ |
| 292 | |
| 293 | public void testToString() { |
| 294 | String aString = "some location"; |
| 295 | Long aLong = 12345L; |
| 296 | TmfTimestamp aTimestamp = new TmfTimestamp(); |
| 297 | |
| 298 | TmfLocation<String> location1 = new TmfLocation<String>(aString); |
| 299 | TmfLocation<Long> location2 = new TmfLocation<Long>(aLong); |
| 300 | TmfLocation<ITmfTimestamp> location3 = new TmfLocation<ITmfTimestamp>(aTimestamp); |
| 301 | |
| 302 | String expected1 = "TmfLocation [fLocation=" + aString + "]"; |
| 303 | String expected2 = "TmfLocation [fLocation=" + aLong + "]"; |
| 304 | String expected3 = "TmfLocation [fLocation=" + aTimestamp + "]"; |
| 305 | |
| 306 | assertEquals("toString", expected1, location1.toString()); |
| 307 | assertEquals("toString", expected2, location2.toString()); |
| 308 | assertEquals("toString", expected3, location3.toString()); |
| 309 | } |
| 310 | |
| 311 | } |