Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d869ce6652ddeba85bd8ac31cf4d8cc0b82e241b (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*******************************************************************************
 * 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
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Francois Chouinard - Initial API and implementation
 *******************************************************************************/

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;

/**
 * Test suite for the TmfLocation class.
 */
@SuppressWarnings({"nls","javadoc"})
public class TmfLocationTest extends TestCase {

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

    String aString = "some location";
    Long aLong = 12345L;
    TmfTimestamp aTimestamp = new TmfTimestamp();

    TmfLocation<String> fLocation1;
    TmfLocation<String> fLocation2;
    TmfLocation<Long> fLocation3;
    TmfLocation<ITmfTimestamp> fLocation4;

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

    /**
     * @param name
     *            the test name
     */
    public TmfLocationTest(String name) {
        super(name);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        fLocation1 = new TmfLocation<String>((String) null);
        fLocation2 = new TmfLocation<String>(aString);
        fLocation3 = new TmfLocation<Long>(aLong);
        fLocation4 = new TmfLocation<ITmfTimestamp>(aTimestamp);
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

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

    public void testTmfLocation() {
        assertNull("TmfLocation", fLocation1.getLocation());
        assertEquals("TmfLocation", aString, fLocation2.getLocation());
        assertEquals("TmfLocation", aLong, fLocation3.getLocation());
        assertEquals("TmfLocation", aTimestamp, fLocation4.getLocation());
    }

    public void testTmfLocationCopy() {
        TmfLocation<String> location1 = new TmfLocation<String>(fLocation1);
        TmfLocation<String> location2 = new TmfLocation<String>(fLocation2);
        TmfLocation<Long> location3 = new TmfLocation<Long>(fLocation3);
        TmfLocation<ITmfTimestamp> location4 = new TmfLocation<ITmfTimestamp>(fLocation4);

        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<String> location1 = fLocation1.clone();
            TmfLocation<String> location2 = fLocation2.clone();
            TmfLocation<Long> location3 = fLocation3.clone();
            TmfLocation<ITmfTimestamp> 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()");
        }
    }

    private static class MyCloneableClass implements Cloneable, Comparable<MyCloneableClass> {
        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 fName.compareTo(o.fName);
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((fName == null) ? 0 : fName.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (!(obj instanceof MyCloneableClass)) {
                return false;
            }
            MyCloneableClass other = (MyCloneableClass) obj;
            if (fName == null) {
                if (other.fName != null) {
                    return false;
                }
            } else if (!fName.equals(other.fName)) {
                return false;
            }
            return true;
        }
    }

    public void testCloneCloneable() {
        try {
            MyCloneableClass myClass = new MyCloneableClass("myCloneableClass");
            TmfLocation<MyCloneableClass> location = new TmfLocation<MyCloneableClass>(myClass);
            TmfLocation<MyCloneableClass> 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");
        }
    }

    private static class MyUnCloneableClass implements Comparable<MyUnCloneableClass> {
        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 fName.compareTo(o.fName);
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((fName == null) ? 0 : fName.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (!(obj instanceof MyUnCloneableClass)) {
                return false;
            }
            MyUnCloneableClass other = (MyUnCloneableClass) obj;
            if (fName == null) {
                if (other.fName != null) {
                    return false;
                }
            } else if (!fName.equals(other.fName)) {
                return false;
            }
            return true;
        }
    }

    public void testCloneUncloneable() {
        try {
            MyUnCloneableClass myClass = new MyUnCloneableClass("myUncloneableClass");
            TmfLocation<MyUnCloneableClass> myLocation = new TmfLocation<MyUnCloneableClass>(myClass);
            myLocation.clone();
            fail("clone an uncloneable class");
        } catch (InternalError e) {
        }
    }

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

    public void testHashCode() {
        TmfLocation<String> location1 = new TmfLocation<String>((String) null);
        TmfLocation<String> location2 = new TmfLocation<String>(aString);
        TmfLocation<Long> location3 = new TmfLocation<Long>(aLong);

        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
    // ------------------------------------------------------------------------

    private static class TmfLocation2 extends TmfLocation<String> {
        public TmfLocation2(String location) {
            super(location);
        }
    }

    public void testEqualsWrongTypes() {
        TmfLocation<String> location1 = new TmfLocation<String>(aString);
        TmfLocation2 location2 = new TmfLocation2(aString);

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

    public void testEqualsWithNulls() {
        TmfLocation<String> location1 = new TmfLocation<String>(aString);
        TmfLocation<String> location2 = new TmfLocation<String>((String) null);

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

    public void testEqualsReflexivity() {
        assertTrue("equals", fLocation2.equals(fLocation2));
        assertTrue("equals", fLocation3.equals(fLocation3));

        assertTrue("equals", !fLocation2.equals(fLocation3));
        assertTrue("equals", !fLocation3.equals(fLocation2));
    }

    public void testEqualsSymmetry() {
        TmfLocation<String> location2 = new TmfLocation<String>(aString);
        TmfLocation<Long> location3 = new TmfLocation<Long>(aLong);

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

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

    public void testEqualsTransivity() {
        TmfLocation<String> location1 = new TmfLocation<String>(aString);
        TmfLocation<String> location2 = new TmfLocation<String>(aString);
        TmfLocation<String> location3 = new TmfLocation<String>(aString);

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

    public void testEqualsNull() {
        assertTrue("equals", !fLocation2.equals(null));
        assertTrue("equals", !fLocation2.equals(null));
    }

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

    @SuppressWarnings("hiding")
    public void testToString() {
        String aString = "some location";
        Long aLong = 12345L;
        TmfTimestamp aTimestamp = new TmfTimestamp();

        TmfLocation<String> location1 = new TmfLocation<String>(aString);
        TmfLocation<Long> location2 = new TmfLocation<Long>(aLong);
        TmfLocation<ITmfTimestamp> location3 = new TmfLocation<ITmfTimestamp>(aTimestamp);

        String expected1 = "TmfLocation [fLocation=" + aString + "]";
        String expected2 = "TmfLocation [fLocation=" + aLong + "]";
        String expected3 = "TmfLocation [fLocation=" + aTimestamp + "]";

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

}

Back to the top