1 /*******************************************************************************
2 * Copyright (c) 2013 - 2014 Ericsson
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
10 * Marc-Andre Laperle - Initial API and implementation
11 * Matthew Khouzam - Added timestamp string tests
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.event
;
16 import static org
.junit
.Assert
.assertEquals
;
18 import java
.text
.ParseException
;
19 import java
.util
.TimeZone
;
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
;
31 * Test suite for the TmfTimestampFormat class.
33 public class TmfTimestampFormatTest
{
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]);
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
);
48 * Test that the default value is loaded when using the default constructor
51 public void testDefaultConstructor() {
52 TmfTimestampFormat ts0
= new TmfTimestampFormat();
53 assertEquals("HH:mm:ss.SSS CCC NNN", ts0
.toPattern());
57 * Test that the value constructor properly assigns the value
60 public void testValueConstructor() {
61 assertEquals(TEST_PATTERN
, tsf1
.toPattern());
65 * Test that the value constructor using a time zone properly assigns the
66 * pattern and time zone
69 public void testValueTimeZoneConstructor() {
70 assertEquals(TEST_PATTERN
, tsf2
.toPattern());
71 assertEquals(TEST_TIME_ZONE
, tsf2
.getTimeZone());
75 * Make sure that the default formats in TmfTimestampFormat get updated when
76 * updateDefaultFormats is called.
79 public void testUpdateDefaultFormats() {
80 IEclipsePreferences node
= InstanceScope
.INSTANCE
.getNode(Activator
.PLUGIN_ID
);
82 String dateTimeTestValue
= ITmfTimePreferencesConstants
.TIME_HOUR_FMT
+ ":";
83 node
.put(ITmfTimePreferencesConstants
.DATIME
, dateTimeTestValue
);
85 String subSecTestValue
= ITmfTimePreferencesConstants
.SUBSEC_NANO_FMT
+ ":";
86 node
.put(ITmfTimePreferencesConstants
.SUBSEC
, subSecTestValue
);
89 } catch (BackingStoreException e
) {
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());
97 node
.put(ITmfTimePreferencesConstants
.DATIME
, ITmfTimePreferencesConstants
.TIME_HOUR_FMT
);
98 node
.put(ITmfTimePreferencesConstants
.SUBSEC
, ITmfTimePreferencesConstants
.SUBSEC_NANO_FMT
);
101 } catch (BackingStoreException e
) {
103 TmfTimestampFormat
.updateDefaultFormats();
107 * Test that getDefaulTimeFormat returns the appropriate value (from the
111 public void testGetDefaulTimeFormat() {
112 assertEquals(TmfTimestampFormat
.getDefaulTimeFormat().toPattern(), TmfTimePreferences
.getInstance().getTimePattern());
116 * Test that getDefaulIntervalFormat returns the appropriate value (from the
120 public void testGetDefaulIntervalFormat() {
121 assertEquals(TmfTimestampFormat
.getDefaulIntervalFormat().toPattern(), TmfTimePreferences
.getInstance().getIntervalPattern());
125 * Test the time value 007, should return 7 seconds
127 * @throws ParseException
128 * should not happen, if it does, the test is a failure
131 public void testParseStringTime() throws ParseException
{
132 long result
= tsf3
.parseValue("07");
133 assertEquals(7000000000L, result
);
137 * Test the time value 007, should return 7 seconds
139 * @throws ParseException
140 * should not happen, if it does, the test is a failure
143 public void testParseStringCompleteTime() throws ParseException
{
144 long result
= tsf3
.parseValue("07.00");
145 assertEquals(7000000000L, result
);
149 * Test the time value 007, should return 7 seconds
151 * @throws ParseException
152 * should not happen, if it does, the test is a failure
155 public void testParseStringCompleteMilliTime() throws ParseException
{
156 long result
= tsf3
.parseValue("0.07");
157 assertEquals(70000000L, result
);
161 * Test the time value 007, should return 7 miliseconds
163 * @throws ParseException
164 * should not happen, if it does, the test is a failure
167 public void testParseStringDecimalTime() throws ParseException
{
168 long result
= tsf3
.parseValue(".007");
169 assertEquals(7000000L, result
);
173 * Test the time value 007, should return 7 miliseconds
175 * @throws ParseException
176 * should not happen, if it does, the test is a failure
179 public void testParseStringCompleteDecimalTime() throws ParseException
{
180 long result
= tsf3
.parseValue("0.007");
181 assertEquals(7000000L, result
);
185 * Tests the time value of 70 ns
187 * @throws ParseException
188 * should not happen, if it does, the test is a failure
191 public void testParseStringNanoTime() throws ParseException
{
192 long result
= tsf3
.parseValue("0.00000007");
193 assertEquals(70L, result
);
197 * Tests the time value of 70 ns
199 * @throws ParseException
200 * should not happen, if it does, the test is a failure
203 public void testCustomParseStringNanoTime() throws ParseException
{
204 long result
= tsf3
.parseValue("0.00000007");
205 assertEquals(70L, result
);
209 * Tests the time value of 70 ns
211 * @throws ParseException
212 * should not happen, if it does, the test is a failure
215 public void testCustomParseStringNanoSeparatorTime() throws ParseException
{
216 long result
= tsf5
.parseValue("0.000 000 07");
217 assertEquals(70L, result
);
221 * Tests the time value of 70 ns
223 * @throws ParseException
224 * should not happen, if it does, the test is a failure
227 public void testCustomParseStringNanoSeparatorTime2() throws ParseException
{
228 long result
= tsf5
.parseValue("0.00000007");
229 assertEquals(70L, result
);
233 * Tests the time value of 123 ms
235 * @throws ParseException
236 * should not happen, if it does, the test is a failure
239 public void testCustomParseStringMiliOK() throws ParseException
{
240 long result
= tsf4
.parseValue("0.123");
241 assertEquals(123000000L, result
);
245 * Tests the time value of 123.456 ms
247 * @throws ParseException
248 * should not happen, if it does, the test is a failure
251 public void testCustomParseStringMiliLong() throws ParseException
{
252 long result
= tsf4
.parseValue("0.12345");
253 assertEquals(123000000L, result
);
257 * Tests the time value of 123 ms as .123, no zero
259 * @throws ParseException
260 * should not happen, if it does, the test is a failure
263 public void testCustomParseStringMiliNoZero() throws ParseException
{
264 long result
= tsf4
.parseValue(".123");
265 assertEquals(123000000L, result
);