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