tmf: add TmfTimestampFormat tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / event / TmfTimestampFormatTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013 - 2014 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 * Marc-Andre Laperle - Initial API and implementation
11 * Matthew Khouzam - Added timestamp string tests
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.tests.event;
15
16 import static org.junit.Assert.assertEquals;
17
18 import java.text.ParseException;
19 import java.util.TimeZone;
20
21 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
22 import org.eclipse.core.runtime.preferences.InstanceScope;
23 import org.eclipse.linuxtools.internal.tmf.core.Activator;
24 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimePreferencesConstants;
25 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimePreferences;
26 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestampFormat;
27 import org.junit.Test;
28 import org.osgi.service.prefs.BackingStoreException;
29
30 /**
31 * Test suite for the TmfTimestampFormat class.
32 */
33 public class TmfTimestampFormatTest {
34
35 private static final String TEST_PATTERN = "HH:mm:ss.SSS";
36 private static final String TEST_PATTERN_2 = "TTT.SSSCCCNNN";
37 private static final String TEST_PATTERN_3 = "TTT.SSS";
38 private static final String TEST_PATTERN_4 = "TTT.SSS CCC NNN";
39 private static final TimeZone TEST_TIME_ZONE = TimeZone.getTimeZone(TimeZone.getAvailableIDs(0)[0]);
40
41 private static final TmfTimestampFormat tsf1 = new TmfTimestampFormat(TEST_PATTERN);
42 private static final TmfTimestampFormat tsf2 = new TmfTimestampFormat(TEST_PATTERN, TEST_TIME_ZONE);
43 private static final TmfTimestampFormat tsf3 = new TmfTimestampFormat(TEST_PATTERN_2);
44 private static final TmfTimestampFormat tsf4 = new TmfTimestampFormat(TEST_PATTERN_3);
45 private static final TmfTimestampFormat tsf5 = new TmfTimestampFormat(TEST_PATTERN_4);
46
47 /**
48 * Test that the default value is loaded when using the default constructor
49 */
50 @Test
51 public void testDefaultConstructor() {
52 TmfTimestampFormat ts0 = new TmfTimestampFormat();
53 assertEquals("HH:mm:ss.SSS CCC NNN", ts0.toPattern());
54 }
55
56 /**
57 * Test that the value constructor properly assigns the value
58 */
59 @Test
60 public void testValueConstructor() {
61 assertEquals(TEST_PATTERN, tsf1.toPattern());
62 }
63
64 /**
65 * Test that the value constructor using a time zone properly assigns the
66 * pattern and time zone
67 */
68 @Test
69 public void testValueTimeZoneConstructor() {
70 assertEquals(TEST_PATTERN, tsf2.toPattern());
71 assertEquals(TEST_TIME_ZONE, tsf2.getTimeZone());
72 }
73
74 /**
75 * Make sure that the default formats in TmfTimestampFormat get updated when
76 * updateDefaultFormats is called.
77 */
78 @Test
79 public void testUpdateDefaultFormats() {
80 IEclipsePreferences node = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
81
82 String dateTimeTestValue = ITmfTimePreferencesConstants.TIME_HOUR_FMT + ":";
83 node.put(ITmfTimePreferencesConstants.DATIME, dateTimeTestValue);
84
85 String subSecTestValue = ITmfTimePreferencesConstants.SUBSEC_NANO_FMT + ":";
86 node.put(ITmfTimePreferencesConstants.SUBSEC, subSecTestValue);
87 try {
88 node.flush();
89 } catch (BackingStoreException e) {
90 }
91 TmfTimestampFormat.updateDefaultFormats();
92 String expected = dateTimeTestValue + "." + subSecTestValue;
93 String expected2 = "TTT." + subSecTestValue;
94 assertEquals(expected, TmfTimestampFormat.getDefaulTimeFormat().toPattern());
95 assertEquals(expected2, TmfTimestampFormat.getDefaulIntervalFormat().toPattern());
96 // Revert preferences
97 node.put(ITmfTimePreferencesConstants.DATIME, ITmfTimePreferencesConstants.TIME_HOUR_FMT);
98 node.put(ITmfTimePreferencesConstants.SUBSEC, ITmfTimePreferencesConstants.SUBSEC_NANO_FMT);
99 try {
100 node.flush();
101 } catch (BackingStoreException e) {
102 }
103 TmfTimestampFormat.updateDefaultFormats();
104 }
105
106 /**
107 * Test that getDefaulTimeFormat returns the appropriate value (from the
108 * default)
109 */
110 @Test
111 public void testGetDefaulTimeFormat() {
112 assertEquals(TmfTimestampFormat.getDefaulTimeFormat().toPattern(), TmfTimePreferences.getInstance().getTimePattern());
113 }
114
115 /**
116 * Test that getDefaulIntervalFormat returns the appropriate value (from the
117 * default)
118 */
119 @Test
120 public void testGetDefaulIntervalFormat() {
121 assertEquals(TmfTimestampFormat.getDefaulIntervalFormat().toPattern(), TmfTimePreferences.getInstance().getIntervalPattern());
122 }
123
124 /**
125 * Test the time value 007, should return 7 seconds
126 *
127 * @throws ParseException
128 * should not happen, if it does, the test is a failure
129 */
130 @Test
131 public void testParseStringTime() throws ParseException {
132 long result = tsf3.parseValue("07");
133 assertEquals(7000000000L, result);
134 }
135
136 /**
137 * Test the time value 007, should return 7 seconds
138 *
139 * @throws ParseException
140 * should not happen, if it does, the test is a failure
141 */
142 @Test
143 public void testParseStringCompleteTime() throws ParseException {
144 long result = tsf3.parseValue("07.00");
145 assertEquals(7000000000L, result);
146 }
147
148 /**
149 * Test the time value 007, should return 7 seconds
150 *
151 * @throws ParseException
152 * should not happen, if it does, the test is a failure
153 */
154 @Test
155 public void testParseStringCompleteMilliTime() throws ParseException {
156 long result = tsf3.parseValue("0.07");
157 assertEquals(70000000L, result);
158 }
159
160 /**
161 * Test the time value 007, should return 7 miliseconds
162 *
163 * @throws ParseException
164 * should not happen, if it does, the test is a failure
165 */
166 @Test
167 public void testParseStringDecimalTime() throws ParseException {
168 long result = tsf3.parseValue(".007");
169 assertEquals(7000000L, result);
170 }
171
172 /**
173 * Test the time value 007, should return 7 miliseconds
174 *
175 * @throws ParseException
176 * should not happen, if it does, the test is a failure
177 */
178 @Test
179 public void testParseStringCompleteDecimalTime() throws ParseException {
180 long result = tsf3.parseValue("0.007");
181 assertEquals(7000000L, result);
182 }
183
184 /**
185 * Tests the time value of 70 ns
186 *
187 * @throws ParseException
188 * should not happen, if it does, the test is a failure
189 */
190 @Test
191 public void testParseStringNanoTime() throws ParseException {
192 long result = tsf3.parseValue("0.00000007");
193 assertEquals(70L, result);
194 }
195
196 /**
197 * Tests the time value of 70 ns
198 *
199 * @throws ParseException
200 * should not happen, if it does, the test is a failure
201 */
202 @Test
203 public void testCustomParseStringNanoTime() throws ParseException {
204 long result = tsf3.parseValue("0.00000007");
205 assertEquals(70L, result);
206 }
207
208 /**
209 * Tests the time value of 70 ns
210 *
211 * @throws ParseException
212 * should not happen, if it does, the test is a failure
213 */
214 @Test
215 public void testCustomParseStringNanoSeparatorTime() throws ParseException {
216 long result = tsf5.parseValue("0.000 000 07");
217 assertEquals(70L, result);
218 }
219
220 /**
221 * Tests the time value of 70 ns
222 *
223 * @throws ParseException
224 * should not happen, if it does, the test is a failure
225 */
226 @Test
227 public void testCustomParseStringNanoSeparatorTime2() throws ParseException {
228 long result = tsf5.parseValue("0.00000007");
229 assertEquals(70L, result);
230 }
231
232 /**
233 * Tests the time value of 123 ms
234 *
235 * @throws ParseException
236 * should not happen, if it does, the test is a failure
237 */
238 @Test
239 public void testCustomParseStringMiliOK() throws ParseException {
240 long result = tsf4.parseValue("0.123");
241 assertEquals(123000000L, result);
242 }
243
244 /**
245 * Tests the time value of 123.456 ms
246 *
247 * @throws ParseException
248 * should not happen, if it does, the test is a failure
249 */
250 @Test
251 public void testCustomParseStringMiliLong() throws ParseException {
252 long result = tsf4.parseValue("0.12345");
253 assertEquals(123000000L, result);
254 }
255
256 /**
257 * Tests the time value of 123 ms as .123, no zero
258 *
259 * @throws ParseException
260 * should not happen, if it does, the test is a failure
261 */
262 @Test
263 public void testCustomParseStringMiliNoZero() throws ParseException {
264 long result = tsf4.parseValue(".123");
265 assertEquals(123000000L, result);
266 }
267 }
This page took 0.062898 seconds and 6 git commands to generate.