| 1 | /********************************************************************** |
| 2 | * Copyright (c) 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 | * Bernd Hufmann - Initial API and implementation |
| 11 | **********************************************************************/ |
| 12 | package org.eclipse.linuxtools.lttng.ui.tests.control.model.impl; |
| 13 | |
| 14 | import junit.framework.TestCase; |
| 15 | |
| 16 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.IProbeEventInfo; |
| 17 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.TraceEnablement; |
| 18 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.TraceEventType; |
| 19 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.impl.BaseEventInfo; |
| 20 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.impl.EventInfo; |
| 21 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.impl.ProbeEventInfo; |
| 22 | |
| 23 | /** |
| 24 | * The class <code>BaseEventInfoTest</code> contains test for the class <code>{@link BaseEventInfo}</code>. |
| 25 | */ |
| 26 | @SuppressWarnings("nls") |
| 27 | public class ProbeEventInfoTest extends TestCase { |
| 28 | |
| 29 | // ------------------------------------------------------------------------ |
| 30 | // Test data |
| 31 | // ------------------------------------------------------------------------ |
| 32 | private IProbeEventInfo fEventInfo1 = null; |
| 33 | private IProbeEventInfo fEventInfo2 = null; |
| 34 | |
| 35 | // ------------------------------------------------------------------------ |
| 36 | // Housekeeping |
| 37 | // ------------------------------------------------------------------------ |
| 38 | /** |
| 39 | * Perform pre-test initialization. |
| 40 | * |
| 41 | * @throws Exception if the initialization fails for some reason |
| 42 | * |
| 43 | */ |
| 44 | @Override |
| 45 | public void setUp() throws Exception { |
| 46 | ModelImplFactory factory = new ModelImplFactory(); |
| 47 | fEventInfo1 = factory.getProbeEventInfo1(); |
| 48 | fEventInfo2 = factory.getProbeEventInfo2(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Perform post-test clean-up. |
| 53 | * |
| 54 | * @throws Exception if the clean-up fails for some reason |
| 55 | * |
| 56 | */ |
| 57 | @Override |
| 58 | public void tearDown() throws Exception { |
| 59 | } |
| 60 | |
| 61 | // ------------------------------------------------------------------------ |
| 62 | // Tests |
| 63 | // ------------------------------------------------------------------------ |
| 64 | |
| 65 | /** |
| 66 | * Run the BaseEventInfo() constructor test. |
| 67 | * |
| 68 | */ |
| 69 | public void testBaseEventInfo() { |
| 70 | ProbeEventInfo fixture = new ProbeEventInfo("event"); |
| 71 | assertNotNull(fixture); |
| 72 | |
| 73 | TraceEventType result = fixture.getEventType(); |
| 74 | |
| 75 | assertEquals("event", fixture.getName()); |
| 76 | assertEquals("unknown", result.getInName()); |
| 77 | assertEquals("UNKNOWN", result.name()); |
| 78 | assertEquals("UNKNOWN", result.toString()); |
| 79 | assertEquals(3, result.ordinal()); |
| 80 | |
| 81 | TraceEnablement state = fixture.getState(); |
| 82 | assertEquals("disabled", state.getInName()); |
| 83 | assertEquals("DISABLED", state.name()); |
| 84 | assertEquals("DISABLED", state.toString()); |
| 85 | assertEquals(0, state.ordinal()); |
| 86 | |
| 87 | assertNull(fixture.getAddress()); |
| 88 | assertNull(fixture.getOffset()); |
| 89 | assertNull(fixture.getSymbol()); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Test Copy Constructor |
| 94 | */ |
| 95 | public void testEventInfoCopy() { |
| 96 | ProbeEventInfo info = new ProbeEventInfo((ProbeEventInfo)fEventInfo1); |
| 97 | |
| 98 | assertEquals(fEventInfo1.getName(), info.getName()); |
| 99 | assertEquals(fEventInfo1.getEventType(), info.getEventType()); |
| 100 | assertEquals(fEventInfo1.getState(), info.getState()); |
| 101 | assertEquals(fEventInfo1.getAddress(), info.getAddress()); |
| 102 | assertEquals(fEventInfo1.getOffset(), info.getOffset()); |
| 103 | assertEquals(fEventInfo1.getSymbol(), info.getSymbol()); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Test Copy Constructor |
| 108 | */ |
| 109 | public void testEventCopy2() { |
| 110 | try { |
| 111 | ProbeEventInfo info = null; |
| 112 | new ProbeEventInfo(info); |
| 113 | fail("null copy"); |
| 114 | } |
| 115 | catch (IllegalArgumentException e) { |
| 116 | // Success |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Getter/Setter tests |
| 122 | */ |
| 123 | public void testGetAndSetter() { |
| 124 | ProbeEventInfo fixture = new ProbeEventInfo("event"); |
| 125 | |
| 126 | // Make sure that ProbeEventInfo extends EventInfo |
| 127 | // -> so we don't need to re-test common parts |
| 128 | assertTrue(fixture instanceof EventInfo); |
| 129 | |
| 130 | fixture.setAddress("0xc12344321"); |
| 131 | String result = fixture.getAddress(); |
| 132 | |
| 133 | assertNotNull(result); |
| 134 | assertEquals("0xc12344321", result); |
| 135 | |
| 136 | fixture.setOffset("0x1000"); |
| 137 | result = fixture.getOffset(); |
| 138 | |
| 139 | assertNotNull(result); |
| 140 | assertEquals("0x1000", result); |
| 141 | |
| 142 | fixture.setSymbol("cpu_idle"); |
| 143 | result = fixture.getSymbol(); |
| 144 | |
| 145 | assertNotNull(result); |
| 146 | assertEquals("cpu_idle", result); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Run the String toString() method test. |
| 151 | */ |
| 152 | public void testToString_1() { |
| 153 | assertEquals("[ProbeEventInfo([EventInfo([BaseEventInfo([TraceInfo(Name=probeEvent1)],type=TRACEPOINT,level=TRACE_DEBUG)],State=ENABLED)],fAddress=0xc1231234)]", fEventInfo1.toString()); |
| 154 | assertEquals("[ProbeEventInfo([EventInfo([BaseEventInfo([TraceInfo(Name=probeEvent2)],type=UNKNOWN,level=TRACE_DEBUG)],State=DISABLED)],fOffset=0x100,fSymbol=init_post)]", fEventInfo2.toString()); |
| 155 | } |
| 156 | |
| 157 | // ------------------------------------------------------------------------ |
| 158 | // equals |
| 159 | // ------------------------------------------------------------------------ |
| 160 | public void testEqualsReflexivity() { |
| 161 | assertTrue("equals", fEventInfo1.equals(fEventInfo1)); |
| 162 | assertTrue("equals", fEventInfo2.equals(fEventInfo2)); |
| 163 | |
| 164 | assertTrue("equals", !fEventInfo1.equals(fEventInfo2)); |
| 165 | assertTrue("equals", !fEventInfo2.equals(fEventInfo1)); |
| 166 | } |
| 167 | |
| 168 | public void testEqualsSymmetry() { |
| 169 | ProbeEventInfo info1 = new ProbeEventInfo((ProbeEventInfo)fEventInfo1); |
| 170 | ProbeEventInfo info2 = new ProbeEventInfo((ProbeEventInfo)fEventInfo2); |
| 171 | |
| 172 | assertTrue("equals", info1.equals(fEventInfo1)); |
| 173 | assertTrue("equals", fEventInfo1.equals(info1)); |
| 174 | |
| 175 | assertTrue("equals", info2.equals(fEventInfo2)); |
| 176 | assertTrue("equals", fEventInfo2.equals(info2)); |
| 177 | } |
| 178 | |
| 179 | public void testEqualsTransivity() { |
| 180 | ProbeEventInfo info1 = new ProbeEventInfo((ProbeEventInfo)fEventInfo1); |
| 181 | ProbeEventInfo info2 = new ProbeEventInfo((ProbeEventInfo)fEventInfo1); |
| 182 | ProbeEventInfo info3 = new ProbeEventInfo((ProbeEventInfo)fEventInfo1); |
| 183 | |
| 184 | assertTrue("equals", info1.equals(info2)); |
| 185 | assertTrue("equals", info2.equals(info3)); |
| 186 | assertTrue("equals", info1.equals(info3)); |
| 187 | } |
| 188 | |
| 189 | public void testEqualsNull() { |
| 190 | assertTrue("equals", !fEventInfo1.equals(null)); |
| 191 | assertTrue("equals", !fEventInfo2.equals(null)); |
| 192 | } |
| 193 | |
| 194 | // ------------------------------------------------------------------------ |
| 195 | // hashCode |
| 196 | // ------------------------------------------------------------------------ |
| 197 | |
| 198 | public void testHashCode() { |
| 199 | ProbeEventInfo info1 = new ProbeEventInfo((ProbeEventInfo)fEventInfo1); |
| 200 | ProbeEventInfo info2 = new ProbeEventInfo((ProbeEventInfo)fEventInfo2); |
| 201 | |
| 202 | assertTrue("hashCode", fEventInfo1.hashCode() == info1.hashCode()); |
| 203 | assertTrue("hashCode", fEventInfo2.hashCode() == info2.hashCode()); |
| 204 | |
| 205 | assertTrue("hashCode", fEventInfo1.hashCode() != info2.hashCode()); |
| 206 | assertTrue("hashCode", fEventInfo2.hashCode() != info1.hashCode()); |
| 207 | } |
| 208 | } |