Commit | Line | Data |
---|---|---|
6c13869b | 1 | package org.eclipse.linuxtools.lttng.core.tests.event; |
03c71d1e | 2 | |
e1ab8984 FC |
3 | import java.io.File; |
4 | import java.net.URL; | |
5 | ||
03c71d1e | 6 | import junit.framework.TestCase; |
cbd4ad82 | 7 | |
e1ab8984 FC |
8 | import org.eclipse.core.runtime.FileLocator; |
9 | import org.eclipse.core.runtime.Path; | |
5945cec9 FC |
10 | import org.eclipse.linuxtools.internal.lttng.core.event.LttngTimestamp; |
11 | import org.eclipse.linuxtools.internal.lttng.core.trace.LTTngTextTrace; | |
6c13869b | 12 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
9269df72 | 13 | import org.osgi.framework.FrameworkUtil; |
03c71d1e ASL |
14 | |
15 | /* | |
16 | Functions tested here : | |
a610acec FC |
17 | public LttngTimestamp() |
18 | public LttngTimestamp(long newEventTime) | |
19 | public LttngTimestamp(TmfTimestamp oldEventTime) | |
20 | ||
21 | public long getValue() | |
22 | public String getSeconds() | |
23 | public String getNanoSeconds() | |
24 | ||
25 | public void setValue(long newValue) | |
26 | ||
27 | public String toString() | |
03c71d1e ASL |
28 | */ |
29 | ||
3b38ea61 | 30 | @SuppressWarnings("nls") |
03c71d1e | 31 | public class LttngTimestampTest extends TestCase { |
a610acec FC |
32 | private final static String tracepath1 = "traceset/trace-15316events_nolost_newformat.txt"; |
33 | private final static boolean skipIndexing = true; | |
34 | ||
35 | private final static String firstEventTimeSecond = "13589"; | |
36 | private final static String firstEventTimeNano = "759412128"; | |
37 | private final static long firstEventTimeFull = 13589759412128L; | |
38 | ||
e1ab8984 | 39 | private static LTTngTextTrace testStream = null; |
a610acec | 40 | |
03c71d1e | 41 | private LTTngTextTrace initializeEventStream() { |
25e48683 | 42 | if (testStream == null) |
a610acec | 43 | try { |
25e48683 FC |
44 | final URL location = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), new Path(tracepath1), null); |
45 | final File testfile = new File(FileLocator.toFileURL(location).toURI()); | |
46 | final LTTngTextTrace tmpStream = new LTTngTextTrace(null, testfile.getPath(), skipIndexing); | |
a610acec | 47 | testStream = tmpStream; |
25e48683 | 48 | } catch (final Exception e) { |
a610acec FC |
49 | System.out.println("ERROR : Could not open " + tracepath1); |
50 | testStream = null; | |
51 | } | |
a610acec FC |
52 | return testStream; |
53 | } | |
e1ab8984 | 54 | |
03c71d1e ASL |
55 | private LttngTimestamp prepareToTest() { |
56 | LttngTimestamp tmpTime = null; | |
57 | ||
58 | // This trace should be valid | |
59 | try { | |
25e48683 | 60 | final LTTngTextTrace tmpStream = initializeEventStream(); |
c32744d6 | 61 | tmpTime = (LttngTimestamp) tmpStream.getNext(new TmfContext(null, 0)).getTimestamp(); |
25e48683 | 62 | } catch (final Exception e) { |
03c71d1e ASL |
63 | fail("ERROR : Failed to get reference!"); |
64 | } | |
65 | ||
66 | return tmpTime; | |
67 | } | |
a610acec | 68 | |
03c71d1e ASL |
69 | public void testConstructors() { |
70 | LttngTimestamp tmpTime = null; | |
a610acec | 71 | |
a5ec08e5 WB |
72 | // Default construction with no argument |
73 | try { | |
74 | tmpTime = new LttngTimestamp(); | |
25e48683 | 75 | } catch (final Exception e) { |
a5ec08e5 WB |
76 | fail("Construction failed!"); |
77 | } | |
a610acec | 78 | |
03c71d1e ASL |
79 | // Default construction with good argument |
80 | try { | |
81 | tmpTime = new LttngTimestamp(1); | |
25e48683 | 82 | } catch (final Exception e) { |
03c71d1e ASL |
83 | fail("Construction failed!"); |
84 | } | |
a610acec | 85 | |
03c71d1e ASL |
86 | // Copy constructor |
87 | try { | |
88 | tmpTime = new LttngTimestamp(1); | |
f9a8715c | 89 | new LttngTimestamp(tmpTime); |
25e48683 | 90 | } catch (final Exception e) { |
03c71d1e ASL |
91 | fail("Construction failed!"); |
92 | } | |
93 | } | |
a610acec | 94 | |
cb866e08 | 95 | public void testGetter() { |
25e48683 | 96 | final LttngTimestamp tmpTime = prepareToTest(); |
a610acec FC |
97 | |
98 | assertEquals("Time in second is wrong", firstEventTimeSecond, tmpTime.getSeconds()); | |
99 | assertEquals("Time in nano second is wrong", firstEventTimeNano, tmpTime.getNanoSeconds()); | |
100 | ||
101 | assertEquals("Full time is wrong", firstEventTimeFull, tmpTime.getValue()); | |
cb866e08 | 102 | } |
a610acec | 103 | |
cb866e08 | 104 | public void testSetter() { |
25e48683 | 105 | final LttngTimestamp tmpTime = prepareToTest(); |
a610acec | 106 | |
cb866e08 FC |
107 | // We will set a time and we will make sure the set is working then |
108 | tmpTime.setValue(1); | |
a610acec | 109 | assertEquals("Full time is wrong after set", 1, tmpTime.getValue()); |
cb866e08 | 110 | } |
a610acec | 111 | |
cb866e08 | 112 | public void testToString() { |
25e48683 | 113 | final LttngTimestamp tmpTime = prepareToTest(); |
a610acec | 114 | |
cb866e08 | 115 | // Just make sure toString() does not return null or the java reference |
a610acec FC |
116 | assertNotSame("toString returned null", null, tmpTime.toString()); |
117 | assertNotSame("toString is not overridded!", tmpTime.getClass().getName() + '@' + Integer.toHexString(tmpTime.hashCode()), tmpTime.toString()); | |
cb866e08 | 118 | } |
a610acec FC |
119 | |
120 | // Better test... | |
121 | public void testToString2() { | |
25e48683 FC |
122 | final LttngTimestamp ts1 = new LttngTimestamp(2064357056377L); |
123 | final String expectedTS1 = "2064.357056377"; | |
a610acec | 124 | |
25e48683 FC |
125 | final LttngTimestamp ts2 = new LttngTimestamp(1L); |
126 | final String expectedTS2 = "0.000000001"; | |
a610acec | 127 | |
25e48683 FC |
128 | final LttngTimestamp ts3 = new LttngTimestamp(123456789L); |
129 | final String expectedTS3 = "0.123456789"; | |
a610acec | 130 | |
25e48683 FC |
131 | final LttngTimestamp ts4 = new LttngTimestamp(1234567890L); |
132 | final String expectedTS4 = "1.234567890"; | |
a610acec FC |
133 | |
134 | assertEquals("toString()", expectedTS1, ts1.toString()); | |
135 | assertEquals("toString()", expectedTS2, ts2.toString()); | |
136 | assertEquals("toString()", expectedTS3, ts3.toString()); | |
137 | assertEquals("toString()", expectedTS4, ts4.toString()); | |
25e48683 FC |
138 | |
139 | final LttngTimestamp ts5 = new LttngTimestamp(2234567890L); | |
140 | final LttngTimestamp delta = ts4.getDelta(ts5); | |
141 | final String expectedDelta = "-1.000000000"; | |
73005152 | 142 | assertEquals("toString()", expectedDelta, delta.toString()); |
a610acec | 143 | } |
03c71d1e | 144 | } |