X-Git-Url: http://drtracing.org/?a=blobdiff_plain;f=org.eclipse.linuxtools.tmf.core.tests%2Fsrc%2Forg%2Feclipse%2Flinuxtools%2Ftmf%2Fcore%2Ftests%2Ftrace%2FTmfLocationTest.java;h=0f4ce3b21fe8309200a2c6c340c0bb7fe525e1e1;hb=fcccd90096c4a9d4b5ca41f02cd71390cf1a93d3;hp=4b1aac4f1bb3da02242be83dd5f2ba313372abca;hpb=00e738f70189f53e10a1111b499c82b0e4199505;p=deliverable%2Ftracecompass.git diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfLocationTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfLocationTest.java index 4b1aac4f1b..0f4ce3b21f 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfLocationTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfLocationTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2010 Ericsson + * Copyright (c) 2009, 2010, 2012 Ericsson * * All rights reserved. This program and the accompanying materials are * made available under the terms of the Eclipse Public License v1.0 which @@ -14,6 +14,7 @@ package org.eclipse.linuxtools.tmf.core.tests.trace; import junit.framework.TestCase; +import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; import org.eclipse.linuxtools.tmf.core.trace.TmfLocation; @@ -25,247 +26,286 @@ import org.eclipse.linuxtools.tmf.core.trace.TmfLocation; @SuppressWarnings("nls") public class TmfLocationTest extends TestCase { - // ------------------------------------------------------------------------ - // Variables - // ------------------------------------------------------------------------ + // ------------------------------------------------------------------------ + // Variables + // ------------------------------------------------------------------------ - String aString = "some location"; - Long aLong = 12345L; - TmfTimestamp aTimestamp = new TmfTimestamp(); + String aString = "some location"; + Long aLong = 12345L; + TmfTimestamp aTimestamp = new TmfTimestamp(); - TmfLocation fLocation1; - TmfLocation fLocation2; - TmfLocation fLocation3; + TmfLocation fLocation0; + TmfLocation fLocation1; + TmfLocation fLocation2; + TmfLocation fLocation3; + TmfLocation fLocation4; // ------------------------------------------------------------------------ // Housekeeping // ------------------------------------------------------------------------ - /** - * @param name the test name - */ - public TmfLocationTest(String name) { - super(name); - } - - @Override - protected void setUp() throws Exception { - super.setUp(); - fLocation1 = new TmfLocation(aString); - fLocation2 = new TmfLocation(aLong); - fLocation3 = new TmfLocation(aTimestamp); - } - - @Override - protected void tearDown() throws Exception { - super.tearDown(); - } + /** + * @param name + * the test name + */ + public TmfLocationTest(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + fLocation0 = TmfLocation.NULL_LOCATION; + fLocation1 = new TmfLocation((String) null); + fLocation2 = new TmfLocation(aString); + fLocation3 = new TmfLocation(aLong); + fLocation4 = new TmfLocation(aTimestamp); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } // ------------------------------------------------------------------------ // Constructors // ------------------------------------------------------------------------ - public void testTmfLocation() { - assertEquals("TmfLocation", aString, fLocation1.getLocation()); - assertEquals("TmfLocation", aLong, fLocation2.getLocation()); - assertEquals("TmfLocation", aTimestamp, fLocation3.getLocation()); - } - - public void testTmfLocationCopy() { - TmfLocation location1 = new TmfLocation(fLocation1); - TmfLocation location2 = new TmfLocation(fLocation2); - TmfLocation location3 = new TmfLocation(fLocation3); - - assertEquals("TmfLocation", aString, location1.getLocation()); - assertEquals("TmfLocation", aLong, location2.getLocation()); - assertEquals("TmfLocation", aTimestamp, location3.getLocation()); - } - - public void testTmfLocationCopy2() throws Exception { - try { - new TmfLocation((TmfLocation) null); - fail("null copy"); - } - catch (IllegalArgumentException e) { - // Success - } - } + public void testTmfLocation() { + assertNull("TmfLocation", fLocation0.getLocation()); + assertNull("TmfLocation", fLocation1.getLocation()); + assertEquals("TmfLocation", aString, fLocation2.getLocation()); + assertEquals("TmfLocation", aLong, fLocation3.getLocation()); + assertEquals("TmfLocation", aTimestamp, fLocation4.getLocation()); + } + + public void testTmfLocationCopy() { + TmfLocation location0 = new TmfLocation(fLocation0); + TmfLocation location1 = new TmfLocation(fLocation1); + TmfLocation location2 = new TmfLocation(fLocation2); + TmfLocation location3 = new TmfLocation(fLocation3); + TmfLocation location4 = new TmfLocation(fLocation4); + + assertNull("TmfLocation", location0.getLocation()); + assertNull("TmfLocation", location1.getLocation()); + assertEquals("TmfLocation", aString, location2.getLocation()); + assertEquals("TmfLocation", aLong, location3.getLocation()); + assertEquals("TmfLocation", aTimestamp, location4.getLocation()); + } + + // ------------------------------------------------------------------------ + // clone + // ------------------------------------------------------------------------ + + public void testClone() { + try { + TmfLocation location1 = fLocation1.clone(); + TmfLocation location2 = fLocation2.clone(); + TmfLocation location3 = fLocation3.clone(); + TmfLocation location4 = fLocation4.clone(); + + assertEquals("clone", fLocation1, location1); + assertEquals("clone", fLocation2, location2); + assertEquals("clone", fLocation3, location3); + assertEquals("clone", fLocation4, location4); + + assertEquals("clone", fLocation1.getLocation(), location1.getLocation()); + assertEquals("clone", fLocation2.getLocation(), location2.getLocation()); + assertEquals("clone", fLocation3.getLocation(), location3.getLocation()); + assertEquals("clone", fLocation4.getLocation(), location4.getLocation()); + + assertNull("clone", location1.getLocation()); + assertEquals("clone", aString, location2.getLocation()); + assertEquals("clone", aLong, location3.getLocation()); + assertEquals("clone", aTimestamp, location4.getLocation()); + } catch (InternalError e) { + fail("clone()"); + } + } + + public static class MyCloneableClass implements Cloneable, Comparable { + private String fName; + + public MyCloneableClass(String name) { + fName = name; + } + + @Override + public String toString() { + return fName; + } + + @Override + public MyCloneableClass clone() { + MyCloneableClass clone = null; + try { + clone = (MyCloneableClass) super.clone(); + clone.fName = fName; + } catch (CloneNotSupportedException e) { + } + return clone; + } + + @Override + public boolean equals(Object o) { + return fName.compareTo(((MyCloneableClass) o).fName) == 0; + } + + @Override + public int compareTo(MyCloneableClass o) { + return fName.compareTo(o.fName); + } + } + + public void testCloneCloneable() { + try { + MyCloneableClass myClass = new MyCloneableClass("myCloneableClass"); + TmfLocation location = new TmfLocation(myClass); + TmfLocation clone = location.clone(); + + assertEquals("clone", location, clone); + assertEquals("clone", location.getLocation(), clone.getLocation()); + assertEquals("clone", myClass, location.getLocation()); + } catch (InternalError e) { + fail("clone a cloneable class"); + } + } + + public static class MyUnCloneableClass implements Comparable { + private String fName; + + public MyUnCloneableClass(String name) { + fName = name; + } + + @Override + public String toString() { + return fName; + } + + @Override + public Object clone() throws CloneNotSupportedException { + throw new CloneNotSupportedException(); + } + + @Override + public boolean equals(Object o) { + return fName.compareTo(((MyUnCloneableClass) o).fName) == 0; + } + + @Override + public int compareTo(MyUnCloneableClass o) { + return fName.compareTo(o.fName); + } + } + + public void testCloneUncloneable() { + try { + MyUnCloneableClass myClass = new MyUnCloneableClass("myUncloneableClass"); + TmfLocation myLocation = new TmfLocation(myClass); + myLocation.clone(); + fail("clone an uncloneable class"); + } catch (InternalError e) { + } + } // ------------------------------------------------------------------------ - // setLocation + // hashCode // ------------------------------------------------------------------------ - public void testSetLocation() { - String aString2 = "some other location"; - Long aLong2 = 1234567L; - TmfTimestamp aTimestamp2 = (TmfTimestamp) TmfTimestamp.BIG_BANG; + public void testHashCode() throws Exception { + TmfLocation location1 = new TmfLocation((String) null); + TmfLocation location2 = new TmfLocation(aString); + TmfLocation location3 = new TmfLocation(aLong); - fLocation1.setLocation(aString2); - fLocation2.setLocation(aLong2); - fLocation3.setLocation(aTimestamp2); - - assertEquals("TmfLocation", aString2, fLocation1.getLocation()); - assertEquals("TmfLocation", aLong2, fLocation2.getLocation()); - assertEquals("TmfLocation", aTimestamp2, fLocation3.getLocation()); - } + assertTrue("hashCode", fLocation1.hashCode() == location1.hashCode()); + assertTrue("hashCode", fLocation2.hashCode() == location2.hashCode()); + assertTrue("hashCode", fLocation3.hashCode() == location3.hashCode()); + + assertTrue("hashCode", fLocation2.hashCode() != location3.hashCode()); + assertTrue("hashCode", fLocation3.hashCode() != location2.hashCode()); + } // ------------------------------------------------------------------------ // toEquals // ------------------------------------------------------------------------ - public void testEqualsReflexivity() throws Exception { - assertTrue("equals", fLocation1.equals(fLocation1)); - assertTrue("equals", fLocation2.equals(fLocation2)); - - assertTrue("equals", !fLocation1.equals(fLocation2)); - assertTrue("equals", !fLocation2.equals(fLocation1)); - } - - public void testEqualsSymmetry() throws Exception { - TmfLocation location1 = new TmfLocation(aString); - TmfLocation location2 = new TmfLocation(aLong); - - assertTrue("equals", location1.equals(fLocation1)); - assertTrue("equals", fLocation1.equals(location1)); - - assertTrue("equals", location2.equals(fLocation2)); - assertTrue("equals", fLocation2.equals(location2)); - } - - public void testEqualsTransivity() throws Exception { - TmfLocation location1 = new TmfLocation(aString); - TmfLocation location2 = new TmfLocation(aString); - TmfLocation location3 = new TmfLocation(aString); - - assertTrue("equals", location1.equals(location2)); - assertTrue("equals", location2.equals(location3)); - assertTrue("equals", location1.equals(location3)); - } - - public void testEqualsNull() throws Exception { - assertTrue("equals", !fLocation1.equals(null)); - assertTrue("equals", !fLocation1.equals(null)); - } - - // ------------------------------------------------------------------------ - // hashCode - // ------------------------------------------------------------------------ - - public void testHashCode() throws Exception { - TmfLocation location1 = new TmfLocation(aString); - TmfLocation location2 = new TmfLocation(aLong); - - assertTrue("hashCode", fLocation1.hashCode() == location1.hashCode()); - assertTrue("hashCode", fLocation2.hashCode() == location2.hashCode()); - - assertTrue("hashCode", fLocation1.hashCode() != location2.hashCode()); - assertTrue("hashCode", fLocation2.hashCode() != location1.hashCode()); - } - + private static class TmfLocation2 extends TmfLocation { + public TmfLocation2(String location) { + super(location); + } + } + + public void testEqualsWrongTypes() throws Exception { + TmfLocation location1 = new TmfLocation(aString); + TmfLocation2 location2 = new TmfLocation2(aString); + + assertFalse("equals", location1.equals(location2)); + assertFalse("equals", location2.equals(location1)); + } + + public void testEqualsWithNulls() throws Exception { + TmfLocation location1 = new TmfLocation(aString); + TmfLocation location2 = new TmfLocation((String) null); + + assertFalse("equals", location1.equals(location2)); + assertFalse("equals", location2.equals(location1)); + } + + public void testEqualsReflexivity() throws Exception { + assertTrue("equals", fLocation2.equals(fLocation2)); + assertTrue("equals", fLocation3.equals(fLocation3)); + + assertTrue("equals", !fLocation2.equals(fLocation3)); + assertTrue("equals", !fLocation3.equals(fLocation2)); + } + + public void testEqualsSymmetry() throws Exception { + TmfLocation location2 = new TmfLocation(aString); + TmfLocation location3 = new TmfLocation(aLong); + + assertTrue("equals", location2.equals(fLocation2)); + assertTrue("equals", fLocation2.equals(location2)); + + assertTrue("equals", location3.equals(fLocation3)); + assertTrue("equals", fLocation3.equals(location3)); + } + + public void testEqualsTransivity() throws Exception { + TmfLocation location1 = new TmfLocation(aString); + TmfLocation location2 = new TmfLocation(aString); + TmfLocation location3 = new TmfLocation(aString); + + assertTrue("equals", location1.equals(location2)); + assertTrue("equals", location2.equals(location3)); + assertTrue("equals", location3.equals(location1)); + } + + public void testEqualsNull() throws Exception { + assertTrue("equals", !fLocation2.equals(null)); + assertTrue("equals", !fLocation2.equals(null)); + } + // ------------------------------------------------------------------------ // toString // ------------------------------------------------------------------------ - public void testToString() { - String aString = "some location"; - Long aLong = 12345L; - TmfTimestamp aTimestamp = new TmfTimestamp(); - - TmfLocation location1 = new TmfLocation(aString); - TmfLocation location2 = new TmfLocation(aLong); - TmfLocation location3 = new TmfLocation(aTimestamp); + public void testToString() { + String aString = "some location"; + Long aLong = 12345L; + TmfTimestamp aTimestamp = new TmfTimestamp(); - assertEquals("TmfLocation", aString.toString(), location1.toString()); - assertEquals("TmfLocation", aLong.toString(), location2.toString()); - assertEquals("TmfLocation", aTimestamp.toString(), location3.toString()); - } + TmfLocation location1 = new TmfLocation(aString); + TmfLocation location2 = new TmfLocation(aLong); + TmfLocation location3 = new TmfLocation(aTimestamp); - // ------------------------------------------------------------------------ - // clone - // ------------------------------------------------------------------------ + String expected1 = "TmfLocation [fLocation=" + aString + "]"; + String expected2 = "TmfLocation [fLocation=" + aLong + "]"; + String expected3 = "TmfLocation [fLocation=" + aTimestamp + "]"; - public void testClone() { - try { - TmfLocation location1 = fLocation1.clone(); - TmfLocation location2 = fLocation2.clone(); - TmfLocation location3 = fLocation3.clone(); - - assertEquals("TmfLocation", aString.toString(), location1.toString()); - assertEquals("TmfLocation", aLong.toString(), location2.toString()); - assertEquals("TmfLocation", aTimestamp.toString(), location3.toString()); - } - catch (InternalError e) { - fail("clone()"); - } - } - - public static class MyCloneableClass implements Cloneable, Comparable { - private String fName; - public MyCloneableClass(String name) { - fName = name; - } - @Override - public String toString() { - return fName; - } - @Override - public MyCloneableClass clone() { - MyCloneableClass clone = null; - try { - clone = (MyCloneableClass) super.clone(); - clone.fName = fName; - } catch (CloneNotSupportedException e) { - } - return clone; - } - @Override - public int compareTo(MyCloneableClass o) { - return 0; - } - } - - public void testCloneCloneable() { - try { - MyCloneableClass myClass = new MyCloneableClass("myClass"); - TmfLocation myLocation = new TmfLocation(myClass); - TmfLocation location4 = myLocation.clone(); - - assertEquals("TmfLocation", myClass.toString(), location4.toString()); - } - catch (InternalError e) { - fail("clone()"); - } - } - - public static class MyUnCloneableClass implements Comparable { - private String fName; - public MyUnCloneableClass(String name) { - fName = name; - } - @Override - public String toString() { - return fName; - } - @Override - public Object clone() throws CloneNotSupportedException { - throw new CloneNotSupportedException(); - } - @Override - public int compareTo(MyUnCloneableClass o) { - return 0; - } - } - - public void testCloneUnCloneable() { - try { - MyUnCloneableClass myClass = new MyUnCloneableClass("myClass"); - TmfLocation myLocation = new TmfLocation(myClass); - myLocation.clone(); - fail("clone()"); - } - catch (InternalError e) { - // Success - } - } + assertEquals("toString", expected1, location1.toString()); + assertEquals("toString", expected2, location2.toString()); + assertEquals("toString", expected3, location3.toString()); + } }