| 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.TmfTimestamp; |
| 18 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; |
| 19 | import org.eclipse.linuxtools.tmf.core.trace.TmfLongLocation; |
| 20 | import org.eclipse.linuxtools.tmf.core.trace.TmfTimestampLocation; |
| 21 | |
| 22 | /** |
| 23 | * Test suite for the TmfLocation class. |
| 24 | */ |
| 25 | @SuppressWarnings({"nls","javadoc"}) |
| 26 | public class TmfLocationTest extends TestCase { |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | // Variables |
| 30 | // ------------------------------------------------------------------------ |
| 31 | |
| 32 | String aString = "some location"; |
| 33 | Long aLong = 12345L; |
| 34 | TmfTimestamp aTimestamp = new TmfTimestamp(); |
| 35 | |
| 36 | TmfStringLocation fLocation1; |
| 37 | TmfStringLocation fLocation2; |
| 38 | TmfLongLocation fLocation3; |
| 39 | TmfTimestampLocation fLocation4; |
| 40 | |
| 41 | // ------------------------------------------------------------------------ |
| 42 | // Housekeeping |
| 43 | // ------------------------------------------------------------------------ |
| 44 | |
| 45 | /** |
| 46 | * @param name |
| 47 | * the test name |
| 48 | */ |
| 49 | public TmfLocationTest(String name) { |
| 50 | super(name); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected void setUp() throws Exception { |
| 55 | super.setUp(); |
| 56 | fLocation1 = new TmfStringLocation((String) null); |
| 57 | fLocation2 = new TmfStringLocation(aString); |
| 58 | fLocation3 = new TmfLongLocation(aLong); |
| 59 | fLocation4 = new TmfTimestampLocation(aTimestamp); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | protected void tearDown() throws Exception { |
| 64 | super.tearDown(); |
| 65 | } |
| 66 | |
| 67 | // ------------------------------------------------------------------------ |
| 68 | // Constructors |
| 69 | // ------------------------------------------------------------------------ |
| 70 | |
| 71 | public void testTmfLocation() { |
| 72 | assertNull("TmfLocation", fLocation1.getLocationData()); |
| 73 | assertEquals("TmfLocation", aString, fLocation2.getLocationData()); |
| 74 | assertEquals("TmfLocation", aLong, fLocation3.getLocationData()); |
| 75 | assertEquals("TmfLocation", aTimestamp, fLocation4.getLocationData()); |
| 76 | } |
| 77 | |
| 78 | public void testTmfLocationCopy() { |
| 79 | TmfStringLocation location1 = new TmfStringLocation(fLocation1); |
| 80 | TmfStringLocation location2 = new TmfStringLocation(fLocation2); |
| 81 | TmfLongLocation location3 = new TmfLongLocation(fLocation3); |
| 82 | TmfTimestampLocation location4 = new TmfTimestampLocation(fLocation4); |
| 83 | |
| 84 | assertNull("TmfLocation", location1.getLocationData()); |
| 85 | assertEquals("TmfLocation", aString, location2.getLocationData()); |
| 86 | assertEquals("TmfLocation", aLong, location3.getLocationData()); |
| 87 | assertEquals("TmfLocation", aTimestamp, location4.getLocationData()); |
| 88 | } |
| 89 | |
| 90 | // ------------------------------------------------------------------------ |
| 91 | // clone |
| 92 | // ------------------------------------------------------------------------ |
| 93 | |
| 94 | public void testClone() { |
| 95 | try { |
| 96 | TmfStringLocation location1 = fLocation1.clone(); |
| 97 | TmfStringLocation location2 = fLocation2.clone(); |
| 98 | TmfLongLocation location3 = fLocation3.clone(); |
| 99 | TmfTimestampLocation location4 = fLocation4.clone(); |
| 100 | |
| 101 | assertEquals("clone", fLocation1, location1); |
| 102 | assertEquals("clone", fLocation2, location2); |
| 103 | assertEquals("clone", fLocation3, location3); |
| 104 | assertEquals("clone", fLocation4, location4); |
| 105 | |
| 106 | assertEquals("clone", fLocation1.getLocationData(), location1.getLocationData()); |
| 107 | assertEquals("clone", fLocation2.getLocationData(), location2.getLocationData()); |
| 108 | assertEquals("clone", fLocation3.getLocationData(), location3.getLocationData()); |
| 109 | assertEquals("clone", fLocation4.getLocationData(), location4.getLocationData()); |
| 110 | |
| 111 | assertNull("clone", location1.getLocationData()); |
| 112 | assertEquals("clone", aString, location2.getLocationData()); |
| 113 | assertEquals("clone", aLong, location3.getLocationData()); |
| 114 | assertEquals("clone", aTimestamp, location4.getLocationData()); |
| 115 | } catch (InternalError e) { |
| 116 | fail("clone()"); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // public static class MyCloneableClass implements Cloneable, Comparable<MyCloneableClass> { |
| 121 | // private String fName; |
| 122 | // |
| 123 | // public MyCloneableClass(String name) { |
| 124 | // fName = name; |
| 125 | // } |
| 126 | // |
| 127 | // @Override |
| 128 | // public String toString() { |
| 129 | // return fName; |
| 130 | // } |
| 131 | // |
| 132 | // @Override |
| 133 | // public MyCloneableClass clone() { |
| 134 | // MyCloneableClass clone = null; |
| 135 | // try { |
| 136 | // clone = (MyCloneableClass) super.clone(); |
| 137 | // clone.fName = fName; |
| 138 | // } catch (CloneNotSupportedException e) { |
| 139 | // } |
| 140 | // return clone; |
| 141 | // } |
| 142 | // |
| 143 | // @Override |
| 144 | // public int compareTo(MyCloneableClass o) { |
| 145 | // return fName.compareTo(o.fName); |
| 146 | // } |
| 147 | // |
| 148 | // @Override |
| 149 | // public int hashCode() { |
| 150 | // final int prime = 31; |
| 151 | // int result = 1; |
| 152 | // result = prime * result + ((fName == null) ? 0 : fName.hashCode()); |
| 153 | // return result; |
| 154 | // } |
| 155 | // |
| 156 | // @Override |
| 157 | // public boolean equals(Object obj) { |
| 158 | // if (this == obj) { |
| 159 | // return true; |
| 160 | // } |
| 161 | // if (obj == null) { |
| 162 | // return false; |
| 163 | // } |
| 164 | // if (!(obj instanceof MyCloneableClass)) { |
| 165 | // return false; |
| 166 | // } |
| 167 | // MyCloneableClass other = (MyCloneableClass) obj; |
| 168 | // if (fName == null) { |
| 169 | // if (other.fName != null) { |
| 170 | // return false; |
| 171 | // } |
| 172 | // } else if (!fName.equals(other.fName)) { |
| 173 | // return false; |
| 174 | // } |
| 175 | // return true; |
| 176 | // } |
| 177 | // } |
| 178 | // |
| 179 | // public void testCloneCloneable() { |
| 180 | // try { |
| 181 | // MyCloneableClass myClass = new MyCloneableClass("myCloneableClass"); |
| 182 | // TmfLocation<MyCloneableClass> location = new TmfLocation<MyCloneableClass>(myClass); |
| 183 | // TmfLocation<MyCloneableClass> clone = location.clone(); |
| 184 | // |
| 185 | // assertEquals("clone", location, clone); |
| 186 | // assertEquals("clone", location.getLocationData(), clone.getLocationData()); |
| 187 | // assertEquals("clone", myClass, location.getLocationData()); |
| 188 | // } catch (InternalError e) { |
| 189 | // fail("clone a cloneable class"); |
| 190 | // } |
| 191 | // } |
| 192 | // |
| 193 | // private static class MyUnCloneableClass implements Comparable<MyUnCloneableClass> { |
| 194 | // private String fName; |
| 195 | // |
| 196 | // public MyUnCloneableClass(String name) { |
| 197 | // fName = name; |
| 198 | // } |
| 199 | // |
| 200 | // @Override |
| 201 | // public String toString() { |
| 202 | // return fName; |
| 203 | // } |
| 204 | // |
| 205 | // @Override |
| 206 | // public Object clone() throws CloneNotSupportedException { |
| 207 | // throw new CloneNotSupportedException(); |
| 208 | // } |
| 209 | // |
| 210 | // @Override |
| 211 | // public int compareTo(MyUnCloneableClass o) { |
| 212 | // return fName.compareTo(o.fName); |
| 213 | // } |
| 214 | // |
| 215 | // @Override |
| 216 | // public int hashCode() { |
| 217 | // final int prime = 31; |
| 218 | // int result = 1; |
| 219 | // result = prime * result + ((fName == null) ? 0 : fName.hashCode()); |
| 220 | // return result; |
| 221 | // } |
| 222 | // |
| 223 | // @Override |
| 224 | // public boolean equals(Object obj) { |
| 225 | // if (this == obj) { |
| 226 | // return true; |
| 227 | // } |
| 228 | // if (obj == null) { |
| 229 | // return false; |
| 230 | // } |
| 231 | // if (!(obj instanceof MyUnCloneableClass)) { |
| 232 | // return false; |
| 233 | // } |
| 234 | // MyUnCloneableClass other = (MyUnCloneableClass) obj; |
| 235 | // if (fName == null) { |
| 236 | // if (other.fName != null) { |
| 237 | // return false; |
| 238 | // } |
| 239 | // } else if (!fName.equals(other.fName)) { |
| 240 | // return false; |
| 241 | // } |
| 242 | // return true; |
| 243 | // } |
| 244 | // } |
| 245 | // |
| 246 | // public void testCloneUncloneable() { |
| 247 | // try { |
| 248 | // MyUnCloneableClass myClass = new MyUnCloneableClass("myUncloneableClass"); |
| 249 | // TmfLocation<MyUnCloneableClass> myLocation = new TmfLocation<MyUnCloneableClass>(myClass); |
| 250 | // myLocation.clone(); |
| 251 | // fail("clone an uncloneable class"); |
| 252 | // } catch (InternalError e) { |
| 253 | // } |
| 254 | // } |
| 255 | |
| 256 | // ------------------------------------------------------------------------ |
| 257 | // hashCode |
| 258 | // ------------------------------------------------------------------------ |
| 259 | |
| 260 | public void testHashCode() { |
| 261 | TmfStringLocation location1 = new TmfStringLocation((String) null); |
| 262 | TmfStringLocation location2 = new TmfStringLocation(aString); |
| 263 | TmfLongLocation location3 = new TmfLongLocation(aLong); |
| 264 | |
| 265 | assertTrue("hashCode", fLocation1.hashCode() == location1.hashCode()); |
| 266 | assertTrue("hashCode", fLocation2.hashCode() == location2.hashCode()); |
| 267 | assertTrue("hashCode", fLocation3.hashCode() == location3.hashCode()); |
| 268 | |
| 269 | assertTrue("hashCode", fLocation2.hashCode() != location3.hashCode()); |
| 270 | assertTrue("hashCode", fLocation3.hashCode() != location2.hashCode()); |
| 271 | } |
| 272 | |
| 273 | // ------------------------------------------------------------------------ |
| 274 | // toEquals |
| 275 | // ------------------------------------------------------------------------ |
| 276 | |
| 277 | private static class TmfLocation2 extends TmfStringLocation { |
| 278 | public TmfLocation2(String location) { |
| 279 | super(location); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | public void testEqualsWrongTypes() { |
| 284 | ITmfLocation location1 = new TmfStringLocation(aString); |
| 285 | TmfLocation2 location2 = new TmfLocation2(aString); |
| 286 | |
| 287 | assertFalse("equals", location1.equals(location2)); |
| 288 | assertFalse("equals", location2.equals(location1)); |
| 289 | } |
| 290 | |
| 291 | public void testEqualsWithNulls() { |
| 292 | TmfStringLocation location1 = new TmfStringLocation(aString); |
| 293 | TmfStringLocation location2 = new TmfStringLocation((String) null); |
| 294 | |
| 295 | assertFalse("equals", location1.equals(location2)); |
| 296 | assertFalse("equals", location2.equals(location1)); |
| 297 | } |
| 298 | |
| 299 | public void testEqualsReflexivity() { |
| 300 | assertTrue("equals", fLocation2.equals(fLocation2)); |
| 301 | assertTrue("equals", fLocation3.equals(fLocation3)); |
| 302 | |
| 303 | assertTrue("equals", !fLocation2.equals(fLocation3)); |
| 304 | assertTrue("equals", !fLocation3.equals(fLocation2)); |
| 305 | } |
| 306 | |
| 307 | public void testEqualsSymmetry() { |
| 308 | TmfStringLocation location2 = new TmfStringLocation(aString); |
| 309 | TmfLongLocation location3 = new TmfLongLocation(aLong); |
| 310 | |
| 311 | assertTrue("equals", location2.equals(fLocation2)); |
| 312 | assertTrue("equals", fLocation2.equals(location2)); |
| 313 | |
| 314 | assertTrue("equals", location3.equals(fLocation3)); |
| 315 | assertTrue("equals", fLocation3.equals(location3)); |
| 316 | } |
| 317 | |
| 318 | public void testEqualsTransivity() { |
| 319 | TmfStringLocation location1 = new TmfStringLocation(aString); |
| 320 | TmfStringLocation location2 = new TmfStringLocation(aString); |
| 321 | TmfStringLocation location3 = new TmfStringLocation(aString); |
| 322 | |
| 323 | assertTrue("equals", location1.equals(location2)); |
| 324 | assertTrue("equals", location2.equals(location3)); |
| 325 | assertTrue("equals", location3.equals(location1)); |
| 326 | } |
| 327 | |
| 328 | public void testEqualsNull() { |
| 329 | assertTrue("equals", !fLocation2.equals(null)); |
| 330 | assertTrue("equals", !fLocation2.equals(null)); |
| 331 | } |
| 332 | |
| 333 | // ------------------------------------------------------------------------ |
| 334 | // toString |
| 335 | // ------------------------------------------------------------------------ |
| 336 | |
| 337 | @SuppressWarnings("hiding") |
| 338 | public void testToString() { |
| 339 | String aString = "some location"; |
| 340 | Long aLong = 12345L; |
| 341 | TmfTimestamp aTimestamp = new TmfTimestamp(); |
| 342 | |
| 343 | TmfStringLocation location1 = new TmfStringLocation(aString); |
| 344 | TmfLongLocation location2 = new TmfLongLocation(aLong); |
| 345 | TmfTimestampLocation location3 = new TmfTimestampLocation(aTimestamp); |
| 346 | |
| 347 | String expected1 = "TmfLocation [fLocation=" + aString + "]"; |
| 348 | String expected2 = "TmfLocation [fLocation=" + aLong + "]"; |
| 349 | String expected3 = "TmfLocation [fLocation=" + aTimestamp + "]"; |
| 350 | |
| 351 | assertEquals("toString", expected1, location1.toString()); |
| 352 | assertEquals("toString", expected2, location2.toString()); |
| 353 | assertEquals("toString", expected3, location3.toString()); |
| 354 | } |
| 355 | |
| 356 | } |