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