| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2013 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are made |
| 5 | * 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 | * Francois Chouinard - Adjusted for new Event Model |
| 12 | * Alexandre Montplaisir - Port to JUnit4 |
| 13 | *******************************************************************************/ |
| 14 | |
| 15 | package org.eclipse.linuxtools.tmf.core.tests.event; |
| 16 | |
| 17 | import static org.junit.Assert.assertArrayEquals; |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertNull; |
| 21 | import static org.junit.Assert.assertTrue; |
| 22 | import static org.junit.Assert.fail; |
| 23 | |
| 24 | import java.util.Collection; |
| 25 | |
| 26 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventType; |
| 27 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
| 28 | import org.eclipse.linuxtools.tmf.core.event.TmfEventType; |
| 29 | import org.junit.Test; |
| 30 | |
| 31 | /** |
| 32 | * Test suite for the TmfEventType class. |
| 33 | */ |
| 34 | @SuppressWarnings("javadoc") |
| 35 | public class TmfEventTypeTest { |
| 36 | |
| 37 | // ------------------------------------------------------------------------ |
| 38 | // Variables |
| 39 | // ------------------------------------------------------------------------ |
| 40 | |
| 41 | private final String fContext1 = "JUnit context 1"; |
| 42 | private final String fContext2 = "JUnit context 2"; |
| 43 | |
| 44 | private final String fTypeId1 = "Some type"; |
| 45 | private final String fTypeId2 = "Some other type"; |
| 46 | |
| 47 | private final String fLabel0 = "label1"; |
| 48 | private final String fLabel1 = "label2"; |
| 49 | private final String fLabel2 = "label3"; |
| 50 | |
| 51 | private final String[] fLabels0 = new String[] { }; |
| 52 | private final String[] fLabels1 = new String[] { fLabel0, fLabel1 }; |
| 53 | private final String[] fLabels2 = new String[] { fLabel1, fLabel0, fLabel2 }; |
| 54 | |
| 55 | private final ITmfEventType fType0 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels0)); |
| 56 | private final ITmfEventType fType1 = new TmfEventType(fContext1, fTypeId2, TmfEventField.makeRoot(fLabels1)); |
| 57 | private final ITmfEventType fType2 = new TmfEventType(fContext2, fTypeId1, TmfEventField.makeRoot(fLabels2)); |
| 58 | private final ITmfEventType fType3 = new TmfEventType(fContext2, fTypeId2, TmfEventField.makeRoot(fLabels1)); |
| 59 | |
| 60 | // ------------------------------------------------------------------------ |
| 61 | // Constructors |
| 62 | // ------------------------------------------------------------------------ |
| 63 | |
| 64 | @Test |
| 65 | public void testDefaultConstructor() { |
| 66 | final ITmfEventType type = new TmfEventType(); |
| 67 | assertEquals("getContext", ITmfEventType.DEFAULT_CONTEXT_ID, type.getContext()); |
| 68 | assertEquals("getName", ITmfEventType.DEFAULT_TYPE_ID, type.getName()); |
| 69 | assertNull("getRootField", type.getRootField()); |
| 70 | assertEquals("getFieldNames", 0, type.getFieldNames().size()); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testFullConstructor() { |
| 75 | final ITmfEventType type0 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels0)); |
| 76 | assertEquals("getContext", fContext1, type0.getContext()); |
| 77 | assertEquals("getName", fTypeId1, type0.getName()); |
| 78 | assertEquals("getRootField", TmfEventField.makeRoot(fLabels0), type0.getRootField()); |
| 79 | final Collection<String> labels0 = type0.getFieldNames(); |
| 80 | assertEquals("getFieldNames length", fLabels0.length, labels0.size()); |
| 81 | assertArrayEquals(fLabels0, labels0.toArray(new String[labels0.size()])); |
| 82 | |
| 83 | final ITmfEventType type1 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels1)); |
| 84 | assertEquals("getContext", fContext1, type1.getContext()); |
| 85 | assertEquals("getName", fTypeId1, type1.getName()); |
| 86 | assertEquals("getRootField", TmfEventField.makeRoot(fLabels1), type1.getRootField()); |
| 87 | final Collection<String> labels1 = type1.getFieldNames(); |
| 88 | assertEquals("getFieldNames length", fLabels1.length, labels1.size()); |
| 89 | assertArrayEquals(fLabels1, labels1.toArray(new String[labels1.size()])); |
| 90 | |
| 91 | final ITmfEventType type2 = new TmfEventType(fContext2, fTypeId2, TmfEventField.makeRoot(fLabels2)); |
| 92 | assertEquals("getContext", fContext2, type2.getContext()); |
| 93 | assertEquals("getName", fTypeId2, type2.getName()); |
| 94 | assertEquals("getRootField", TmfEventField.makeRoot(fLabels2), type2.getRootField()); |
| 95 | final Collection<String> labels2 = type2.getFieldNames(); |
| 96 | assertEquals("getFieldNames length", fLabels2.length, labels2.size()); |
| 97 | assertArrayEquals(fLabels2, labels2.toArray(new String[labels2.size()])); |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testConstructorCornerCases() { |
| 102 | try { |
| 103 | new TmfEventType(null, fTypeId1, null); |
| 104 | fail("TmfEventType: null context"); |
| 105 | } catch (final IllegalArgumentException e) { |
| 106 | } |
| 107 | |
| 108 | try { |
| 109 | new TmfEventType(fContext1, null, null); |
| 110 | fail("TmfEventType: null type"); |
| 111 | } catch (final IllegalArgumentException e) { |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void testCopyConstructor() { |
| 117 | final TmfEventType original = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels1)); |
| 118 | final TmfEventType copy = new TmfEventType(original); |
| 119 | |
| 120 | assertEquals("getContext", fContext1, copy.getContext()); |
| 121 | assertEquals("getName", fTypeId1, copy.getName()); |
| 122 | assertEquals("getRootField", TmfEventField.makeRoot(fLabels1), copy.getRootField()); |
| 123 | final Collection<String> labels1 = copy.getFieldNames(); |
| 124 | assertEquals("getFieldNames length", fLabels1.length, labels1.size()); |
| 125 | assertArrayEquals(fLabels1, labels1.toArray(new String[labels1.size()])); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void testCopyConstructorCornerCases() { |
| 130 | try { |
| 131 | new TmfEventType(null); |
| 132 | fail("TmfEventType: null argument"); |
| 133 | } catch (final IllegalArgumentException e) { |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // ------------------------------------------------------------------------ |
| 138 | // hashCode |
| 139 | // ------------------------------------------------------------------------ |
| 140 | |
| 141 | @Test |
| 142 | public void testHashCode() { |
| 143 | final TmfEventType copy1 = new TmfEventType(fType0); |
| 144 | |
| 145 | assertTrue("hashCode", fType0.hashCode() == copy1.hashCode()); |
| 146 | assertTrue("hashCode", fType0.hashCode() != fType3.hashCode()); |
| 147 | } |
| 148 | |
| 149 | // ------------------------------------------------------------------------ |
| 150 | // equals |
| 151 | // ------------------------------------------------------------------------ |
| 152 | |
| 153 | @Test |
| 154 | public void testEqualsReflexivity() { |
| 155 | assertTrue("equals", fType0.equals(fType0)); |
| 156 | assertTrue("equals", fType3.equals(fType3)); |
| 157 | |
| 158 | assertFalse("equals", fType0.equals(fType3)); |
| 159 | assertFalse("equals", fType3.equals(fType0)); |
| 160 | } |
| 161 | |
| 162 | @Test |
| 163 | public void testEqualsSymmetry() { |
| 164 | final TmfEventType copy0 = new TmfEventType(fType0); |
| 165 | assertTrue("equals", fType0.equals(copy0)); |
| 166 | assertTrue("equals", copy0.equals(fType0)); |
| 167 | |
| 168 | final TmfEventType copy1 = new TmfEventType(fType1); |
| 169 | assertTrue("equals", fType1.equals(copy1)); |
| 170 | assertTrue("equals", copy1.equals(fType1)); |
| 171 | |
| 172 | final TmfEventType copy2 = new TmfEventType(fType2); |
| 173 | assertTrue("equals", fType2.equals(copy2)); |
| 174 | assertTrue("equals", copy2.equals(fType2)); |
| 175 | } |
| 176 | |
| 177 | @Test |
| 178 | public void testEqualsTransivity() { |
| 179 | TmfEventType copy1 = new TmfEventType(fType1); |
| 180 | TmfEventType copy2 = new TmfEventType(copy1); |
| 181 | assertTrue("equals", fType1.equals(copy1)); |
| 182 | assertTrue("equals", copy1.equals(copy2)); |
| 183 | assertTrue("equals", fType1.equals(copy2)); |
| 184 | |
| 185 | copy1 = new TmfEventType(fType2); |
| 186 | copy2 = new TmfEventType(copy1); |
| 187 | assertTrue("equals", fType2.equals(copy1)); |
| 188 | assertTrue("equals", copy1.equals(copy2)); |
| 189 | assertTrue("equals", fType2.equals(copy2)); |
| 190 | |
| 191 | copy1 = new TmfEventType(fType3); |
| 192 | copy2 = new TmfEventType(copy1); |
| 193 | assertTrue("equals", fType3.equals(copy1)); |
| 194 | assertTrue("equals", copy1.equals(copy2)); |
| 195 | assertTrue("equals", fType3.equals(copy2)); |
| 196 | } |
| 197 | |
| 198 | @Test |
| 199 | public void testEqualsNull() { |
| 200 | assertFalse("equals", fType0.equals(null)); |
| 201 | assertFalse("equals", fType3.equals(null)); |
| 202 | } |
| 203 | |
| 204 | @Test |
| 205 | public void testNonEquals() { |
| 206 | assertFalse("equals", fType0.equals(fType1)); |
| 207 | assertFalse("equals", fType1.equals(fType2)); |
| 208 | assertFalse("equals", fType2.equals(fType3)); |
| 209 | assertFalse("equals", fType3.equals(fType0)); |
| 210 | } |
| 211 | |
| 212 | @Test |
| 213 | public void testNonEqualsClasses() { |
| 214 | assertFalse("equals", fType1.equals(fLabels1)); |
| 215 | } |
| 216 | |
| 217 | // ------------------------------------------------------------------------ |
| 218 | // toString |
| 219 | // ------------------------------------------------------------------------ |
| 220 | |
| 221 | @Test |
| 222 | public void testToString() { |
| 223 | final String expected1 = "TmfEventType [fContext=" + ITmfEventType.DEFAULT_CONTEXT_ID + |
| 224 | ", fTypeId=" + ITmfEventType.DEFAULT_TYPE_ID + "]"; |
| 225 | final TmfEventType type1 = new TmfEventType(); |
| 226 | assertEquals("toString", expected1, type1.toString()); |
| 227 | |
| 228 | final String expected2 = "TmfEventType [fContext=" + fContext1 + ", fTypeId=" + fTypeId1 + "]"; |
| 229 | final TmfEventType type2 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels1)); |
| 230 | assertEquals("toString", expected2, type2.toString()); |
| 231 | } |
| 232 | |
| 233 | } |