tmf: Mark TmfTraceManager @NonNullByDefault
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / viewers / events / TestRefreshTextTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2015 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
10 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events;
11
12 import static org.junit.Assert.assertNotNull;
13
14 import java.io.File;
15 import java.io.FileWriter;
16 import java.io.IOException;
17 import java.text.SimpleDateFormat;
18 import java.util.Calendar;
19 import java.util.Date;
20 import java.util.GregorianCalendar;
21
22 import org.apache.log4j.Logger;
23 import org.apache.log4j.varia.NullAppender;
24 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
25 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
26 import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
27 import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes;
28 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
29 import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
30 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
31 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
32 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
33 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
34 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
35 import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41
42 /**
43 * Test refreshing a text trace after new content was added.
44 */
45 @RunWith(SWTBotJunit4ClassRunner.class)
46 public class TestRefreshTextTrace {
47
48 private static final String PROJECT_NAME = "Test";
49 private static final String TRACE_TYPE_SYSLOG = "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog";
50 private static final String TIMESTAMP_FORMAT = "MMM dd HH:mm:ss";
51
52 private static final long INITIAL_NB_EVENTS = 100;
53 private static final int NEW_EVENTS_PER_REFRESH = 40000;
54 private static final int NB_REFRESH = 3;
55 private static final long SECOND_TO_MILLISECOND = 1000;
56 private static final long MICROSECOND_TO_NANOSECOND = 1000000;
57 private static final int INDEXING_TIMEOUT = 300000;
58
59 private static final Calendar CURRENT = Calendar.getInstance();
60 private static final String TRACE_LOCATION = TmfTraceManager.getTemporaryDirPath() + File.separator + "test.txt";
61 private static SWTWorkbenchBot fBot;
62
63 private long fNbWrittenEvents = 0;
64
65 /** Test Class setup */
66 @BeforeClass
67 public static void init() {
68 SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
69 SWTBotUtils.initialize();
70 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
71 Logger.getRootLogger().addAppender(new NullAppender());
72 fBot = new SWTWorkbenchBot();
73
74 SWTBotUtils.closeView("welcome", fBot);
75
76 SWTBotUtils.switchToTracingPerspective();
77 /* finish waiting for eclipse to load */
78 WaitUtils.waitForJobs();
79 }
80
81 /**
82 * Test setup
83 *
84 * @throws Exception on error
85 */
86 @Before
87 public void before() throws Exception {
88 SWTBotUtils.createProject(PROJECT_NAME);
89 SWTBotUtils.openTrace(PROJECT_NAME, createTrace(INITIAL_NB_EVENTS), getTraceType());
90 WaitUtils.waitForJobs();
91 }
92
93 /**
94 * Test tear down
95 */
96 @After
97 public void after() {
98 SWTBotUtils.deleteProject(PROJECT_NAME, fBot);
99 }
100
101 /**
102 * Test refreshing a trace after new content was added.
103 *
104 * @throws IOException
105 * on error
106 */
107 @Test
108 public void testRefresh() throws IOException {
109 ITmfTrace activeTrace = TmfTraceManager.getInstance().getActiveTrace();
110 assertNotNull(activeTrace);
111
112 fBot.waitUntil(new NumberOfEventsCondition(activeTrace, INITIAL_NB_EVENTS));
113
114 for (int i = 0; i < NB_REFRESH; i++) {
115 appendToTrace(NEW_EVENTS_PER_REFRESH);
116
117 // Refresh
118 SWTBotTreeItem tracesFolder = SWTBotUtils.selectTracesFolder(fBot, PROJECT_NAME);
119 SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, tracesFolder, activeTrace.getName());
120 traceItem.contextMenu("Refresh").click();
121
122 // Make sure the refresh is completed
123 fBot.waitUntil(new NumberOfEventsCondition(activeTrace, getNbWrittenEvents()), INDEXING_TIMEOUT);
124 }
125
126 // Make sure the end of the table matches what we expect
127 goToTableEnd();
128 fBot.waitUntil(ConditionHelpers.selectionInEventsTable(fBot, getExpectedEndTimeStamp()));
129 }
130
131 private static class NumberOfEventsCondition extends DefaultCondition {
132
133 private ITmfTrace fTrace;
134 private long fNbEvents;
135
136 private NumberOfEventsCondition(ITmfTrace trace, long nbEvents) {
137 fTrace = trace;
138 fNbEvents = nbEvents;
139 }
140
141 @Override
142 public boolean test() throws Exception {
143 return fTrace.getNbEvents() == fNbEvents;
144 }
145
146 @Override
147 public String getFailureMessage() {
148 return fTrace.getName() + " did not contain the expected number of " + fNbEvents + " events. Current: " + fTrace.getNbEvents();
149 }
150 }
151
152 /**
153 * Create a trace with a number of events.
154 *
155 * @param nbEvents
156 * the number of events to generate
157 * @return the path to the created trace
158 * @throws Exception
159 * on error
160 */
161 protected String createTrace(long nbEvents) throws Exception {
162 writeToTrace(nbEvents, false);
163 return TRACE_LOCATION;
164 }
165
166 /**
167 * Append a number of events to the trace.
168 *
169 * @param nbEvents
170 * the number of events to append
171 * @throws IOException
172 * on error
173 */
174 protected void appendToTrace(long nbEvents) throws IOException {
175 writeToTrace(nbEvents, true);
176 }
177
178 private void writeToTrace(long nbEvents, boolean append) throws IOException {
179 final File file = new File(TRACE_LOCATION);
180 try (FileWriter writer = new FileWriter(file, append)) {
181 for (int i = 0; i < nbEvents; ++i) {
182 writeEvent(writer);
183 }
184 }
185 }
186
187 private void writeEvent(FileWriter fw) throws IOException {
188 SimpleDateFormat f = new SimpleDateFormat(TIMESTAMP_FORMAT);
189 String timeStampStr = f.format(new Date(fNbWrittenEvents * SECOND_TO_MILLISECOND));
190 fw.write(timeStampStr + " HostF LoggerF: SourceFileF:9 Message F\n");
191 fNbWrittenEvents++;
192 }
193
194 /**
195 * Get the trace type for the test.
196 *
197 * @return the trace type
198 */
199 protected String getTraceType() {
200 return TRACE_TYPE_SYSLOG;
201 }
202
203 /**
204 * Get the expected time in nanosecs at the end of the trace, after
205 * refreshing.
206 *
207 * @return the expected time in nanosecs at the end of the trace
208 */
209 protected long getExpectedEndTimeStamp() {
210 Date date = new Date((fNbWrittenEvents - 1) * SECOND_TO_MILLISECOND);
211 // Syslog fills in the year when parsing so we have to do it for the
212 // expected time stamp as well
213 GregorianCalendar calendar = new GregorianCalendar();
214 calendar.setTime(date);
215 calendar.set(Calendar.YEAR, CURRENT.get(Calendar.YEAR));
216 if (calendar.after(CURRENT)) {
217 calendar.set(Calendar.YEAR, CURRENT.get(Calendar.YEAR) - 1);
218 }
219 return calendar.getTimeInMillis() * MICROSECOND_TO_NANOSECOND;
220 }
221
222 private static void goToTableEnd() {
223 SWTBotEditor eventsEditor = SWTBotUtils.activeEventsEditor(fBot);
224 eventsEditor.setFocus();
225 eventsEditor.bot().table().pressShortcut(Keystrokes.END);
226 }
227
228 /**
229 * Get the number of events written so far.
230 *
231 * @return the number of events written so far
232 */
233 protected long getNbWrittenEvents() {
234 return fNbWrittenEvents;
235 }
236 }
This page took 0.035944 seconds and 5 git commands to generate.