| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2012, 2013 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 | * Bernd Hufmann - Initial API and implementation |
| 11 | * Alexandre Montplaisir - Port to JUnit4 |
| 12 | **********************************************************************/ |
| 13 | |
| 14 | package org.eclipse.linuxtools.lttng2.control.core.tests.model.impl; |
| 15 | |
| 16 | import static org.junit.Assert.assertEquals; |
| 17 | import static org.junit.Assert.assertNotNull; |
| 18 | import static org.junit.Assert.assertTrue; |
| 19 | import static org.junit.Assert.fail; |
| 20 | |
| 21 | import org.eclipse.linuxtools.internal.lttng2.control.core.model.IEventInfo; |
| 22 | import org.eclipse.linuxtools.internal.lttng2.control.core.model.LogLevelType; |
| 23 | import org.eclipse.linuxtools.internal.lttng2.control.core.model.TraceEnablement; |
| 24 | import org.eclipse.linuxtools.internal.lttng2.control.core.model.TraceEventType; |
| 25 | import org.eclipse.linuxtools.internal.lttng2.control.core.model.impl.EventInfo; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | |
| 29 | /** |
| 30 | * The class <code>EventInfoTest</code> contains test for the class |
| 31 | * <code>{@link EventInfo}</code>. |
| 32 | */ |
| 33 | public class EventInfoTest { |
| 34 | |
| 35 | // ------------------------------------------------------------------------ |
| 36 | // Test data |
| 37 | // ------------------------------------------------------------------------ |
| 38 | |
| 39 | private IEventInfo fEventInfo1 = null; |
| 40 | private IEventInfo fEventInfo2 = null; |
| 41 | |
| 42 | // ------------------------------------------------------------------------ |
| 43 | // Housekeeping |
| 44 | // ------------------------------------------------------------------------ |
| 45 | |
| 46 | /** |
| 47 | * Perform pre-test initialization. |
| 48 | */ |
| 49 | @Before |
| 50 | public void setUp() { |
| 51 | ModelImplFactory factory = new ModelImplFactory(); |
| 52 | fEventInfo1 = factory.getEventInfo1(); |
| 53 | fEventInfo2 = factory.getEventInfo2(); |
| 54 | } |
| 55 | |
| 56 | // ------------------------------------------------------------------------ |
| 57 | // Tests |
| 58 | // ------------------------------------------------------------------------ |
| 59 | |
| 60 | /** |
| 61 | * Run the EventInfo() constructor test. |
| 62 | */ |
| 63 | @Test |
| 64 | public void testBaseEventInfo() { |
| 65 | EventInfo fixture = new EventInfo("event"); |
| 66 | assertNotNull(fixture); |
| 67 | |
| 68 | TraceEventType result = fixture.getEventType(); |
| 69 | |
| 70 | assertEquals("event", fixture.getName()); |
| 71 | assertEquals("unknown", result.getInName()); |
| 72 | assertEquals("UNKNOWN", result.name()); |
| 73 | assertEquals("UNKNOWN", result.toString()); |
| 74 | assertEquals(4, result.ordinal()); |
| 75 | |
| 76 | TraceEnablement state = fixture.getState(); |
| 77 | assertEquals("disabled", state.getInName()); |
| 78 | assertEquals("DISABLED", state.name()); |
| 79 | assertEquals("DISABLED", state.toString()); |
| 80 | assertEquals(0, state.ordinal()); |
| 81 | |
| 82 | LogLevelType logType = fixture.getLogLevelType(); |
| 83 | assertEquals("", logType.getShortName()); |
| 84 | assertEquals("LOGLEVEL_NONE", logType.name()); |
| 85 | assertEquals("LOGLEVEL_NONE", logType.toString()); |
| 86 | assertEquals(0, state.ordinal()); |
| 87 | |
| 88 | |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Test Copy Constructor |
| 93 | */ |
| 94 | @Test |
| 95 | public void testEventInfoCopy() { |
| 96 | EventInfo info = new EventInfo((EventInfo)fEventInfo1); |
| 97 | |
| 98 | assertEquals(fEventInfo1.getName(), info.getName()); |
| 99 | assertEquals(fEventInfo1.getEventType(), info.getEventType()); |
| 100 | assertEquals(fEventInfo1.getState(), info.getState()); |
| 101 | assertEquals(fEventInfo1.getLogLevelType(), info.getLogLevelType()); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Test Copy Constructor |
| 106 | */ |
| 107 | @Test |
| 108 | public void testEventCopy2() { |
| 109 | try { |
| 110 | EventInfo info = null; |
| 111 | new EventInfo(info); |
| 112 | fail("null copy"); |
| 113 | } |
| 114 | catch (IllegalArgumentException e) { |
| 115 | // Success |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Getter/Setter tests |
| 121 | */ |
| 122 | @Test |
| 123 | public void testGetAndSetter() { |
| 124 | EventInfo fixture = new EventInfo("event"); |
| 125 | |
| 126 | fixture.setEventType(TraceEventType.TRACEPOINT); |
| 127 | TraceEventType result = fixture.getEventType(); |
| 128 | |
| 129 | // setEventType(TraceEventType type) |
| 130 | assertNotNull(result); |
| 131 | assertEquals("tracepoint", result.getInName()); |
| 132 | assertEquals("TRACEPOINT", result.name()); |
| 133 | assertEquals("TRACEPOINT", result.toString()); |
| 134 | assertEquals(0, result.ordinal()); |
| 135 | |
| 136 | fixture.setEventType(TraceEventType.UNKNOWN); |
| 137 | result = fixture.getEventType(); |
| 138 | assertEquals("unknown", result.getInName()); |
| 139 | assertEquals("UNKNOWN", result.name()); |
| 140 | assertEquals("UNKNOWN", result.toString()); |
| 141 | assertEquals(4, result.ordinal()); |
| 142 | |
| 143 | // setEventType(String typeName) |
| 144 | String typeName = ""; |
| 145 | fixture.setEventType(typeName); |
| 146 | result = fixture.getEventType(); |
| 147 | |
| 148 | assertEquals("unknown", result.getInName()); |
| 149 | assertEquals("UNKNOWN", result.name()); |
| 150 | assertEquals("UNKNOWN", result.toString()); |
| 151 | assertEquals(4, result.ordinal()); |
| 152 | |
| 153 | typeName = "unknown"; |
| 154 | |
| 155 | fixture.setEventType(typeName); |
| 156 | result = fixture.getEventType(); |
| 157 | |
| 158 | assertEquals("unknown", result.getInName()); |
| 159 | assertEquals("UNKNOWN", result.name()); |
| 160 | assertEquals("UNKNOWN", result.toString()); |
| 161 | assertEquals(4, result.ordinal()); |
| 162 | |
| 163 | // setState(String stateName) |
| 164 | fixture.setState("disabled"); |
| 165 | TraceEnablement state = fixture.getState(); |
| 166 | assertEquals("disabled", state.getInName()); |
| 167 | assertEquals("DISABLED", state.name()); |
| 168 | assertEquals("DISABLED", state.toString()); |
| 169 | assertEquals(0, state.ordinal()); |
| 170 | |
| 171 | fixture.setState("bla"); |
| 172 | state = fixture.getState(); |
| 173 | assertEquals("disabled", state.getInName()); |
| 174 | assertEquals("DISABLED", state.name()); |
| 175 | assertEquals("DISABLED", state.toString()); |
| 176 | assertEquals(0, state.ordinal()); |
| 177 | |
| 178 | fixture.setState("enabled"); |
| 179 | state = fixture.getState(); |
| 180 | assertEquals("enabled", state.getInName()); |
| 181 | assertEquals("ENABLED", state.name()); |
| 182 | assertEquals("ENABLED", state.toString()); |
| 183 | assertEquals(1, state.ordinal()); |
| 184 | |
| 185 | // setState(TraceEnablement state) |
| 186 | fixture.setState(TraceEnablement.DISABLED); |
| 187 | state = fixture.getState(); |
| 188 | assertEquals("disabled", state.getInName()); |
| 189 | assertEquals("DISABLED", state.name()); |
| 190 | assertEquals("DISABLED", state.toString()); |
| 191 | assertEquals(0, state.ordinal()); |
| 192 | |
| 193 | fixture.setState(TraceEnablement.ENABLED); |
| 194 | state = fixture.getState(); |
| 195 | assertEquals("enabled", state.getInName()); |
| 196 | assertEquals("ENABLED", state.name()); |
| 197 | assertEquals("ENABLED", state.toString()); |
| 198 | assertEquals(1, state.ordinal()); |
| 199 | |
| 200 | // setLogLevelType(String name) |
| 201 | fixture.setLogLevelType("=="); |
| 202 | assertEquals("LOGLEVEL_ONLY", fixture.getLogLevelType().name()); |
| 203 | assertEquals("==", fixture.getLogLevelType().getShortName()); |
| 204 | |
| 205 | fixture.setLogLevelType("<="); |
| 206 | assertEquals("LOGLEVEL", fixture.getLogLevelType().name()); |
| 207 | assertEquals("<=", fixture.getLogLevelType().getShortName()); |
| 208 | |
| 209 | fixture.setLogLevelType(""); |
| 210 | assertEquals("LOGLEVEL_ALL", fixture.getLogLevelType().name()); |
| 211 | assertEquals("", fixture.getLogLevelType().getShortName()); |
| 212 | |
| 213 | fixture.setLogLevelType(LogLevelType.LOGLEVEL_ONLY); |
| 214 | assertEquals("LOGLEVEL_ONLY", fixture.getLogLevelType().name()); |
| 215 | assertEquals("==", fixture.getLogLevelType().getShortName()); |
| 216 | |
| 217 | fixture.setLogLevelType(LogLevelType.LOGLEVEL); |
| 218 | assertEquals("LOGLEVEL", fixture.getLogLevelType().name()); |
| 219 | assertEquals("<=", fixture.getLogLevelType().getShortName()); |
| 220 | |
| 221 | fixture.setLogLevelType(LogLevelType.LOGLEVEL_ALL); |
| 222 | assertEquals("LOGLEVEL_ALL", fixture.getLogLevelType().name()); |
| 223 | assertEquals("", fixture.getLogLevelType().getShortName()); |
| 224 | |
| 225 | fixture.setLogLevelType(LogLevelType.LOGLEVEL_NONE); |
| 226 | assertEquals("LOGLEVEL_NONE", fixture.getLogLevelType().name()); |
| 227 | assertEquals("", fixture.getLogLevelType().getShortName()); |
| 228 | |
| 229 | // setLogLevelType(String name) |
| 230 | // machine interface |
| 231 | fixture.setLogLevelType("SINGLE"); |
| 232 | assertEquals("LOGLEVEL_ONLY", fixture.getLogLevelType().name()); |
| 233 | assertEquals("SINGLE", fixture.getLogLevelType().getMiName()); |
| 234 | |
| 235 | fixture.setLogLevelType("RANGE"); |
| 236 | assertEquals("LOGLEVEL", fixture.getLogLevelType().name()); |
| 237 | assertEquals("RANGE", fixture.getLogLevelType().getMiName()); |
| 238 | |
| 239 | fixture.setLogLevelType("ALL"); |
| 240 | assertEquals("LOGLEVEL_ALL", fixture.getLogLevelType().name()); |
| 241 | assertEquals("ALL", fixture.getLogLevelType().getMiName()); |
| 242 | |
| 243 | fixture.setLogLevelType("UNKNOWN"); |
| 244 | assertEquals("LOGLEVEL_NONE", fixture.getLogLevelType().name()); |
| 245 | assertEquals("UNKNOWN", fixture.getLogLevelType().getMiName()); |
| 246 | |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Run the String toString() method test. |
| 251 | */ |
| 252 | @Test |
| 253 | public void testToString_1() { |
| 254 | EventInfo fixture = new EventInfo("event"); |
| 255 | fixture.setName("testName"); |
| 256 | fixture.setEventType(TraceEventType.TRACEPOINT); |
| 257 | |
| 258 | String result = fixture.toString(); |
| 259 | |
| 260 | // add additional test code here |
| 261 | assertEquals("[EventInfo([BaseEventInfo([TraceInfo(Name=testName)],type=TRACEPOINT,level=TRACE_DEBUG)],State=DISABLED,levelType=LOGLEVEL_NONE)]", result); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Run the String toString() method test. |
| 266 | */ |
| 267 | @Test |
| 268 | public void testToString_2() { |
| 269 | EventInfo fixture = new EventInfo("event"); |
| 270 | fixture.setName("testName"); |
| 271 | fixture.setEventType(TraceEventType.TRACEPOINT); |
| 272 | fixture.setLogLevelType(LogLevelType.LOGLEVEL_ONLY); |
| 273 | |
| 274 | String result = fixture.toString(); |
| 275 | |
| 276 | // add additional test code here |
| 277 | assertEquals("[EventInfo([BaseEventInfo([TraceInfo(Name=testName)],type=TRACEPOINT,level=TRACE_DEBUG)],State=DISABLED,levelType=LOGLEVEL_ONLY)]", result); |
| 278 | } |
| 279 | |
| 280 | // ------------------------------------------------------------------------ |
| 281 | // equals |
| 282 | // ------------------------------------------------------------------------ |
| 283 | |
| 284 | /** |
| 285 | * Run the equals() method test. |
| 286 | */ |
| 287 | @Test |
| 288 | public void testEqualsReflexivity() { |
| 289 | assertTrue("equals", fEventInfo1.equals(fEventInfo1)); |
| 290 | assertTrue("equals", fEventInfo2.equals(fEventInfo2)); |
| 291 | |
| 292 | assertTrue("equals", !fEventInfo1.equals(fEventInfo2)); |
| 293 | assertTrue("equals", !fEventInfo2.equals(fEventInfo1)); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Run the equals() method test. |
| 298 | */ |
| 299 | @Test |
| 300 | public void testEqualsSymmetry() { |
| 301 | EventInfo info1 = new EventInfo((EventInfo)fEventInfo1); |
| 302 | EventInfo info2 = new EventInfo((EventInfo)fEventInfo2); |
| 303 | |
| 304 | assertTrue("equals", info1.equals(fEventInfo1)); |
| 305 | assertTrue("equals", fEventInfo1.equals(info1)); |
| 306 | |
| 307 | assertTrue("equals", info2.equals(fEventInfo2)); |
| 308 | assertTrue("equals", fEventInfo2.equals(info2)); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Run the equals() method test. |
| 313 | */ |
| 314 | @Test |
| 315 | public void testEqualsTransivity() { |
| 316 | EventInfo info1 = new EventInfo((EventInfo)fEventInfo1); |
| 317 | EventInfo info2 = new EventInfo((EventInfo)fEventInfo1); |
| 318 | EventInfo info3 = new EventInfo((EventInfo)fEventInfo1); |
| 319 | |
| 320 | assertTrue("equals", info1.equals(info2)); |
| 321 | assertTrue("equals", info2.equals(info3)); |
| 322 | assertTrue("equals", info1.equals(info3)); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Run the equals() method test. |
| 327 | */ |
| 328 | @Test |
| 329 | public void testEqualsNull() { |
| 330 | assertTrue("equals", !fEventInfo1.equals(null)); |
| 331 | assertTrue("equals", !fEventInfo2.equals(null)); |
| 332 | } |
| 333 | |
| 334 | // ------------------------------------------------------------------------ |
| 335 | // hashCode |
| 336 | // ------------------------------------------------------------------------ |
| 337 | |
| 338 | /** |
| 339 | * Run the hashCode() method test. |
| 340 | */ |
| 341 | @Test |
| 342 | public void testHashCode() { |
| 343 | EventInfo info1 = new EventInfo((EventInfo)fEventInfo1); |
| 344 | EventInfo info2 = new EventInfo((EventInfo)fEventInfo2); |
| 345 | |
| 346 | assertTrue("hashCode", fEventInfo1.hashCode() == info1.hashCode()); |
| 347 | assertTrue("hashCode", fEventInfo2.hashCode() == info2.hashCode()); |
| 348 | |
| 349 | assertTrue("hashCode", fEventInfo1.hashCode() != info2.hashCode()); |
| 350 | assertTrue("hashCode", fEventInfo2.hashCode() != info1.hashCode()); |
| 351 | } |
| 352 | } |