Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c9916506f0b6479e4f96c42e6309e40834139256 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*******************************************************************************
 * Copyright (c) 2009, 2013 Ericsson
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Francois Chouinard - Initial API and implementation
 *   Alexandre Montplaisir - Port to JUnit4
 *   Patrick Tasse - Add tests for TmfExperimentLocation
 *******************************************************************************/

package org.eclipse.linuxtools.tmf.core.tests.trace;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentLocation;
import org.eclipse.linuxtools.internal.tmf.core.trace.TmfLocationArray;
import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
import org.eclipse.linuxtools.tmf.core.trace.TmfLongLocation;
import org.eclipse.linuxtools.tmf.core.trace.TmfTimestampLocation;
import org.junit.Before;
import org.junit.Test;

/**
 * Test suite for the TmfLocation class.
 */
@SuppressWarnings("javadoc")
public class TmfLocationTest {

    // ------------------------------------------------------------------------
    // Variables
    // ------------------------------------------------------------------------

    private Long aLong = 12345L;
    private TmfTimestamp aTimestamp = new TmfTimestamp();
    private TmfLocationArray aLocationArray;

    private TmfLongLocation fLocation1;
    private TmfLongLocation fLocation2;
    private TmfTimestampLocation fLocation3;
    private TmfExperimentLocation fExpLocation;

    // ------------------------------------------------------------------------
    // Housekeeping
    // ------------------------------------------------------------------------

    @Before
    public void setUp() {
        fLocation1 = new TmfLongLocation((Long) null);
        fLocation2 = new TmfLongLocation(aLong);
        fLocation3 = new TmfTimestampLocation(aTimestamp);
        aLocationArray = new TmfLocationArray(
                new ITmfLocation[] { fLocation1, fLocation2, fLocation3 },
                new long[] { 1, 2, 3 }
                );
        fExpLocation = new TmfExperimentLocation(aLocationArray);
    }

    // ------------------------------------------------------------------------
    // Constructors
    // ------------------------------------------------------------------------

    @Test
    public void testTmfLocation() {
        assertNull("TmfLocation", fLocation1.getLocationInfo());
        assertEquals("TmfLocation", aLong, fLocation2.getLocationInfo());
        assertEquals("TmfLocation", aTimestamp, fLocation3.getLocationInfo());
        assertEquals("TmfLocation", aLocationArray, fExpLocation.getLocationInfo());
    }

    @Test
    public void testTmfLocationCopy() {
        TmfLongLocation location1 = new TmfLongLocation(fLocation1);
        TmfLongLocation location2 = new TmfLongLocation(fLocation2);
        TmfTimestampLocation location3 = new TmfTimestampLocation(fLocation3);
        TmfExperimentLocation expLocation = new TmfExperimentLocation(fExpLocation);

        assertNull("TmfLocation", location1.getLocationInfo());
        assertEquals("TmfLocation", aLong, location2.getLocationInfo());
        assertEquals("TmfLocation", aTimestamp, location3.getLocationInfo());
        assertEquals("TmfLocation", aLocationArray, expLocation.getLocationInfo());
    }

    // ------------------------------------------------------------------------
    // hashCode
    // ------------------------------------------------------------------------

    @Test
    public void testHashCode() {
        TmfLongLocation location1 = new TmfLongLocation((Long) null);
        TmfLongLocation location2 = new TmfLongLocation(aLong);
        TmfTimestampLocation location3 = new TmfTimestampLocation(aTimestamp);
        TmfExperimentLocation expLocation = new TmfExperimentLocation(fExpLocation);
        TmfLocationArray locationArray1 = new TmfLocationArray(aLocationArray, 2, fLocation3, 5);
        TmfExperimentLocation expLocation1 = new TmfExperimentLocation(locationArray1);
        TmfLocationArray locationArray2 = new TmfLocationArray(aLocationArray, 2, fLocation2, 4);
        TmfExperimentLocation expLocation2 = new TmfExperimentLocation(locationArray2);
        TmfLocationArray locationArray3 = new TmfLocationArray(
                new ITmfLocation[] { fLocation1, fLocation2 },
                new long[] { 1, 2 }
                );
        TmfExperimentLocation expLocation3 = new TmfExperimentLocation(locationArray3);

        assertTrue("hashCode", fLocation1.hashCode() == location1.hashCode());
        assertTrue("hashCode", fLocation2.hashCode() == location2.hashCode());
        assertTrue("hashCode", fLocation3.hashCode() == location3.hashCode());
        assertTrue("hashCode", fExpLocation.hashCode() == expLocation.hashCode());

        assertTrue("hashCode", fLocation2.hashCode() != location3.hashCode());
        assertTrue("hashCode", fLocation3.hashCode() != location2.hashCode());
        assertTrue("hashCode", fExpLocation.hashCode() != expLocation1.hashCode());
        assertTrue("hashCode", fExpLocation.hashCode() != expLocation2.hashCode());
        assertTrue("hashCode", fExpLocation.hashCode() != expLocation3.hashCode());
    }

    // ------------------------------------------------------------------------
    // toEquals
    // ------------------------------------------------------------------------

    private static class TmfTestLongLocation extends TmfLocation {
        public TmfTestLongLocation(Long location) {
            super(location);
        }
    }

    private static class TmfTestLongLocation2 extends TmfTestLongLocation {
        public TmfTestLongLocation2(Long location) {
            super(location);
        }
    }

    @Test
    public void testEqualsWrongTypes() {
        ITmfLocation location1 = new TmfTestLongLocation(aLong);
        TmfTestLongLocation location2 = new TmfTestLongLocation2(aLong);

        assertFalse("equals", location1.equals(location2));
        assertFalse("equals", location2.equals(location1));
    }

    @Test
    public void testEqualsWithNulls() {
        ITmfLocation location1 = new TmfLongLocation(aLong);
        ITmfLocation location2 = new TmfLongLocation((Long) null);

        assertFalse("equals", location1.equals(location2));
        assertFalse("equals", location2.equals(location1));
    }

    @Test
    public void testEqualsReflexivity() {
        assertTrue("equals", fLocation1.equals(fLocation1));
        assertTrue("equals", fLocation2.equals(fLocation2));
        assertTrue("equals", fLocation3.equals(fLocation3));
        assertTrue("equals", fExpLocation.equals(fExpLocation));

        assertTrue("equals", !fLocation2.equals(fLocation3));
        assertTrue("equals", !fLocation3.equals(fLocation2));
        TmfLocationArray locationArray1 = new TmfLocationArray(aLocationArray, 2, fLocation3, 5);
        TmfExperimentLocation expLocation1 = new TmfExperimentLocation(locationArray1);
        TmfLocationArray locationArray2 = new TmfLocationArray(aLocationArray, 2, fLocation2, 4);
        TmfExperimentLocation expLocation2 = new TmfExperimentLocation(locationArray2);
        TmfLocationArray locationArray3 = new TmfLocationArray(
                new ITmfLocation[] { fLocation1, fLocation2, fLocation3 },
                new long[] { 1, 2 }
                );
        TmfExperimentLocation expLocation3 = new TmfExperimentLocation(locationArray3);
        assertTrue("equals", !fExpLocation.equals(expLocation1));
        assertTrue("equals", !expLocation1.equals(fExpLocation));
        assertTrue("equals", !fExpLocation.equals(expLocation2));
        assertTrue("equals", !expLocation2.equals(fExpLocation));
        assertTrue("equals", !fExpLocation.equals(expLocation3));
        assertTrue("equals", !expLocation3.equals(fExpLocation));
    }

    @Test
    public void testEqualsSymmetry() {
        TmfLongLocation location2 = new TmfLongLocation(aLong);
        TmfTimestampLocation location3 = new TmfTimestampLocation(aTimestamp);
        TmfExperimentLocation expLocation = new TmfExperimentLocation(fExpLocation);

        assertTrue("equals", location2.equals(fLocation2));
        assertTrue("equals", fLocation2.equals(location2));

        assertTrue("equals", location3.equals(fLocation3));
        assertTrue("equals", fLocation3.equals(location3));

        assertTrue("equals", expLocation.equals(fExpLocation));
        assertTrue("equals", fExpLocation.equals(expLocation));
    }

    @Test
    public void testEqualsTransivity() {
        TmfLongLocation location1 = new TmfLongLocation(aLong);
        TmfLongLocation location2 = new TmfLongLocation(aLong);
        TmfLongLocation location3 = new TmfLongLocation(aLong);

        TmfExperimentLocation expLocation1 = new TmfExperimentLocation(aLocationArray);
        TmfExperimentLocation expLocation2 = new TmfExperimentLocation(aLocationArray);
        TmfExperimentLocation expLocation3 = new TmfExperimentLocation(aLocationArray);

        assertTrue("equals", location1.equals(location2));
        assertTrue("equals", location2.equals(location3));
        assertTrue("equals", location3.equals(location1));
        assertTrue("equals", expLocation1.equals(expLocation2));
        assertTrue("equals", expLocation2.equals(expLocation3));
        assertTrue("equals", expLocation3.equals(expLocation1));
    }

    @Test
    public void testEqualsNull() {
        assertTrue("equals", !fLocation1.equals(null));
        assertTrue("equals", !fLocation2.equals(null));
        assertTrue("equals", !fLocation3.equals(null));
        assertTrue("equals", !fExpLocation.equals(null));
    }

    // ------------------------------------------------------------------------
    // toString
    // ------------------------------------------------------------------------

    @Test
    public void testToString() {
        TmfTimestamp ts = new TmfTimestamp();

        TmfLongLocation location1 = new TmfLongLocation(aLong);
        TmfTimestampLocation location2 = new TmfTimestampLocation(ts);
        TmfExperimentLocation expLocation = new TmfExperimentLocation(aLocationArray);

        String expected1 = "TmfLongLocation [fLocationInfo=" + aLong + "]";
        String expected2 = "TmfTimestampLocation [fLocationInfo=" + ts + "]";
        String expected3 = "TmfExperimentLocation [" + aLocationArray + "]";

        assertEquals("toString", expected1, location1.toString());
        assertEquals("toString", expected2, location2.toString());
        assertEquals("toString", expected3, expLocation.toString());
    }

}

Back to the top