Add timezones to preference page
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / event / TmfTimestampFormatTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013 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 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.tests.event;
14
15 import static org.junit.Assert.assertEquals;
16
17 import java.util.TimeZone;
18
19 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
20 import org.eclipse.core.runtime.preferences.InstanceScope;
21 import org.eclipse.linuxtools.internal.tmf.core.Activator;
22 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimePreferencesConstants;
23 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimePreferences;
24 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestampFormat;
25 import org.junit.Test;
26 import org.osgi.service.prefs.BackingStoreException;
27
28 /**
29 * Test suite for the TmfTimestampFormat class.
30 */
31 public class TmfTimestampFormatTest {
32
33 private static final String TEST_PATTERN = "HH:mm:ss.SSS";
34 private static final TimeZone TEST_TIME_ZONE = TimeZone.getTimeZone(TimeZone.getAvailableIDs(0)[0]);
35
36 private final TmfTimestampFormat tsf1 = new TmfTimestampFormat(TEST_PATTERN);
37 private final TmfTimestampFormat tsf2 = new TmfTimestampFormat(TEST_PATTERN, TEST_TIME_ZONE);
38
39 /**
40 * Test that the default value is loaded when using the default constructor
41 */
42 @Test
43 public void testDefaultConstructor() {
44 TmfTimestampFormat ts0 = new TmfTimestampFormat();
45 assertEquals("HH:mm:ss.SSS CCC NNN", ts0.toPattern());
46 }
47
48 /**
49 * Test that the value constructor properly assigns the value
50 */
51 @Test
52 public void testValueConstructor() {
53 assertEquals(TEST_PATTERN, tsf1.toPattern());
54 }
55
56 /**
57 * Test that the value constructor using a time zone properly assigns the pattern and time zone
58 */
59 @Test
60 public void testValueTimeZoneConstructor() {
61 assertEquals(TEST_PATTERN, tsf2.toPattern());
62 assertEquals(TEST_TIME_ZONE, tsf2.getTimeZone());
63 }
64
65 /**
66 * Make sure that the default formats in TmfTimestampFormat get updated when
67 * updateDefaultFormats is called.
68 */
69 @Test
70 public void testUpdateDefaultFormats() {
71 IEclipsePreferences node = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
72
73 String dateTimeTestValue = ITmfTimePreferencesConstants.TIME_HOUR_FMT + ":";
74 node.put(ITmfTimePreferencesConstants.DATIME, dateTimeTestValue);
75
76 String subSecTestValue = ITmfTimePreferencesConstants.SUBSEC_NANO_FMT + ":";
77 node.put(ITmfTimePreferencesConstants.SUBSEC, subSecTestValue);
78 try {
79 node.flush();
80 } catch (BackingStoreException e) {
81 }
82 TmfTimestampFormat.updateDefaultFormats();
83 String expected = dateTimeTestValue + "." + subSecTestValue;
84 String expected2 = "TTT." + subSecTestValue;
85 assertEquals(expected, TmfTimestampFormat.getDefaulTimeFormat().toPattern());
86 assertEquals(expected2, TmfTimestampFormat.getDefaulIntervalFormat().toPattern());
87 // Revert preferences
88 node.put(ITmfTimePreferencesConstants.DATIME, ITmfTimePreferencesConstants.TIME_HOUR_FMT);
89 node.put(ITmfTimePreferencesConstants.SUBSEC, ITmfTimePreferencesConstants.SUBSEC_NANO_FMT);
90 try {
91 node.flush();
92 } catch (BackingStoreException e) {
93 }
94 TmfTimestampFormat.updateDefaultFormats();
95 }
96
97 /**
98 * Test that getDefaulTimeFormat returns the appropriate value (from the default)
99 */
100 @Test
101 public void testGetDefaulTimeFormat() {
102 assertEquals(TmfTimestampFormat.getDefaulTimeFormat().toPattern(), TmfTimePreferences.getInstance().getTimePattern());
103 }
104
105 /**
106 * Test that getDefaulIntervalFormat returns the appropriate value (from the default)
107 */
108 @Test
109 public void testGetDefaulIntervalFormat() {
110 assertEquals(TmfTimestampFormat.getDefaulIntervalFormat().toPattern(), TmfTimePreferences.getInstance().getIntervalPattern());
111 }
112 }
This page took 0.036258 seconds and 6 git commands to generate.