522072fda3024bd32c897672b85868431e4b5b2a
[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 return fTrace.getNbEvents() == fNbEvents;
139 }
140
141 @Override
142 public String getFailureMessage() {
143 return fTrace.getName() + " did not contain the expected number of " + fNbEvents + " events";
144 }
145 }
146
147 /**
148 * Create a trace with a number of events.
149 *
150 * @param nbEvents
151 * the number of events to generate
152 * @return the path to the created trace
153 * @throws Exception
154 * on error
155 */
156 protected String createTrace(long nbEvents) throws Exception {
157 writeToTrace(nbEvents, false);
158 return TRACE_LOCATION;
159 }
160
161 /**
162 * Append a number of events to the trace.
163 *
164 * @param nbEvents
165 * the number of events to append
166 * @throws IOException
167 * on error
168 */
169 protected void appendToTrace(long nbEvents) throws IOException {
170 writeToTrace(nbEvents, true);
171 }
172
173 private void writeToTrace(long nbEvents, boolean append) throws IOException {
174 final File file = new File(TRACE_LOCATION);
175 try (FileWriter writer = new FileWriter(file, append)) {
176 for (int i = 0; i < nbEvents; ++i) {
177 writeEvent(writer);
178 }
179 }
180 }
181
182 private void writeEvent(FileWriter fw) throws IOException {
183 SimpleDateFormat f = new SimpleDateFormat(TIMESTAMP_FORMAT);
184 String timeStampStr = f.format(new Date(fNbWrittenEvents * SECOND_TO_MILLISECOND));
185 fw.write(timeStampStr + " HostF LoggerF: SourceFileF:9 Message F\n");
186 fNbWrittenEvents++;
187 }
188
189 /**
190 * Get the trace type for the test.
191 *
192 * @return the trace type
193 */
194 protected String getTraceType() {
195 return TRACE_TYPE_SYSLOG;
196 }
197
198 /**
199 * Get the expected time in nanosecs at the end of the trace, after
200 * refreshing.
201 *
202 * @return the expected time in nanosecs at the end of the trace
203 */
204 protected long getExpectedEndTimeStamp() {
205 Date date = new Date((fNbWrittenEvents - 1) * SECOND_TO_MILLISECOND);
206 // Syslog fills in the year when parsing so we have to do it for the
207 // expected time stamp as well
208 GregorianCalendar calendar = new GregorianCalendar();
209 calendar.setTime(date);
210 calendar.set(Calendar.YEAR, CURRENT.get(Calendar.YEAR));
211 if (calendar.after(CURRENT)) {
212 calendar.set(Calendar.YEAR, CURRENT.get(Calendar.YEAR) - 1);
213 }
214 return calendar.getTimeInMillis() * MICROSECOND_TO_NANOSECOND;
215 }
216
217 private static void goToTableEnd() {
218 SWTBotEditor eventsEditor = SWTBotUtils.activeEventsEditor(fBot);
219 eventsEditor.setFocus();
220 eventsEditor.bot().table().pressShortcut(Keystrokes.END);
221 }
222
223 /**
224 * Get the number of events written so far.
225 *
226 * @return the number of events written so far
227 */
228 protected long getNbWrittenEvents() {
229 return fNbWrittenEvents;
230 }
231 }
This page took 0.038353 seconds and 4 git commands to generate.