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