Commit | Line | Data |
---|---|---|
03c71d1e ASL |
1 | package org.eclipse.linuxtools.lttng.tests.event; |
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; | |
03c71d1e | 10 | import org.eclipse.linuxtools.lttng.event.LttngTimestamp; |
e1ab8984 | 11 | import org.eclipse.linuxtools.lttng.tests.LTTngCoreTestPlugin; |
03c71d1e | 12 | import org.eclipse.linuxtools.lttng.trace.LTTngTextTrace; |
9f584e4c | 13 | import org.eclipse.linuxtools.tmf.trace.TmfContext; |
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() { |
a610acec FC |
42 | if (testStream == null) { |
43 | try { | |
44 | URL location = FileLocator.find(LTTngCoreTestPlugin.getPlugin().getBundle(), new Path(tracepath1), null); | |
45 | File testfile = new File(FileLocator.toFileURL(location).toURI()); | |
46 | LTTngTextTrace tmpStream = new LTTngTextTrace(testfile.getPath(), skipIndexing); | |
47 | testStream = tmpStream; | |
48 | } catch (Exception e) { | |
49 | System.out.println("ERROR : Could not open " + tracepath1); | |
50 | testStream = null; | |
51 | } | |
52 | } | |
53 | return testStream; | |
54 | } | |
e1ab8984 | 55 | |
03c71d1e ASL |
56 | private LttngTimestamp prepareToTest() { |
57 | LttngTimestamp tmpTime = null; | |
58 | ||
59 | // This trace should be valid | |
60 | try { | |
61 | LTTngTextTrace tmpStream = initializeEventStream(); | |
a610acec FC |
62 | tmpTime = (LttngTimestamp) tmpStream.getNextEvent(new TmfContext(null, 0)).getTimestamp(); |
63 | } catch (Exception e) { | |
03c71d1e ASL |
64 | fail("ERROR : Failed to get reference!"); |
65 | } | |
66 | ||
67 | return tmpTime; | |
68 | } | |
a610acec | 69 | |
03c71d1e ASL |
70 | public void testConstructors() { |
71 | LttngTimestamp tmpTime = null; | |
72 | @SuppressWarnings("unused") | |
73 | LttngTimestamp tmpTime2 = null; | |
a610acec | 74 | |
a5ec08e5 WB |
75 | // Default construction with no argument |
76 | try { | |
77 | tmpTime = new LttngTimestamp(); | |
a610acec | 78 | } catch (Exception e) { |
a5ec08e5 WB |
79 | fail("Construction failed!"); |
80 | } | |
a610acec | 81 | |
03c71d1e ASL |
82 | // Default construction with good argument |
83 | try { | |
84 | tmpTime = new LttngTimestamp(1); | |
a610acec | 85 | } catch (Exception e) { |
03c71d1e ASL |
86 | fail("Construction failed!"); |
87 | } | |
a610acec | 88 | |
03c71d1e ASL |
89 | // Copy constructor |
90 | try { | |
91 | tmpTime = new LttngTimestamp(1); | |
92 | tmpTime2 = new LttngTimestamp(tmpTime); | |
a610acec | 93 | } catch (Exception e) { |
03c71d1e ASL |
94 | fail("Construction failed!"); |
95 | } | |
96 | } | |
a610acec | 97 | |
cb866e08 FC |
98 | public void testGetter() { |
99 | LttngTimestamp tmpTime = prepareToTest(); | |
a610acec FC |
100 | |
101 | assertEquals("Time in second is wrong", firstEventTimeSecond, tmpTime.getSeconds()); | |
102 | assertEquals("Time in nano second is wrong", firstEventTimeNano, tmpTime.getNanoSeconds()); | |
103 | ||
104 | assertEquals("Full time is wrong", firstEventTimeFull, tmpTime.getValue()); | |
cb866e08 | 105 | } |
a610acec | 106 | |
cb866e08 FC |
107 | public void testSetter() { |
108 | LttngTimestamp tmpTime = prepareToTest(); | |
a610acec | 109 | |
cb866e08 FC |
110 | // We will set a time and we will make sure the set is working then |
111 | tmpTime.setValue(1); | |
a610acec | 112 | assertEquals("Full time is wrong after set", 1, tmpTime.getValue()); |
cb866e08 | 113 | } |
a610acec | 114 | |
cb866e08 FC |
115 | public void testToString() { |
116 | LttngTimestamp tmpTime = prepareToTest(); | |
a610acec | 117 | |
cb866e08 | 118 | // Just make sure toString() does not return null or the java reference |
a610acec FC |
119 | assertNotSame("toString returned null", null, tmpTime.toString()); |
120 | assertNotSame("toString is not overridded!", tmpTime.getClass().getName() + '@' + Integer.toHexString(tmpTime.hashCode()), tmpTime.toString()); | |
cb866e08 | 121 | } |
a610acec FC |
122 | |
123 | // Better test... | |
124 | public void testToString2() { | |
125 | LttngTimestamp ts1 = new LttngTimestamp(2064357056377L); | |
126 | String expectedTS1 = "2064.357056377"; | |
127 | ||
128 | LttngTimestamp ts2 = new LttngTimestamp(1L); | |
129 | String expectedTS2 = "0.000000001"; | |
130 | ||
131 | LttngTimestamp ts3 = new LttngTimestamp(123456789L); | |
132 | String expectedTS3 = "0.123456789"; | |
133 | ||
134 | LttngTimestamp ts4 = new LttngTimestamp(1234567890L); | |
135 | String expectedTS4 = "1.234567890"; | |
136 | ||
137 | assertEquals("toString()", expectedTS1, ts1.toString()); | |
138 | assertEquals("toString()", expectedTS2, ts2.toString()); | |
139 | assertEquals("toString()", expectedTS3, ts3.toString()); | |
140 | assertEquals("toString()", expectedTS4, ts4.toString()); | |
73005152 BH |
141 | |
142 | LttngTimestamp ts5 = new LttngTimestamp(2234567890L); | |
143 | LttngTimestamp delta = ts4.getDelta(ts5); | |
144 | String expectedDelta = "-1.000000000"; | |
145 | assertEquals("toString()", expectedDelta, delta.toString()); | |
a610acec | 146 | } |
03c71d1e | 147 | } |