1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.viewers
.events
;
12 import static org
.junit
.Assert
.assertNotNull
;
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
;
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
;
43 * Test refreshing a text trace after new content was added.
45 @RunWith(SWTBotJunit4ClassRunner
.class)
46 public class TestRefreshTextTrace
{
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";
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;
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
;
63 private long fNbWrittenEvents
= 0;
65 /** Test Class setup */
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();
74 SWTBotUtils
.closeView("welcome", fBot
);
76 SWTBotUtils
.switchToTracingPerspective();
77 /* finish waiting for eclipse to load */
78 WaitUtils
.waitForJobs();
84 * @throws Exception on error
87 public void before() throws Exception
{
88 SWTBotUtils
.createProject(PROJECT_NAME
);
89 SWTBotUtils
.openTrace(PROJECT_NAME
, createTrace(INITIAL_NB_EVENTS
), getTraceType());
90 WaitUtils
.waitForJobs();
98 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
102 * Test refreshing a trace after new content was added.
104 * @throws IOException
108 public void testRefresh() throws IOException
{
109 ITmfTrace activeTrace
= TmfTraceManager
.getInstance().getActiveTrace();
110 assertNotNull(activeTrace
);
112 fBot
.waitUntil(new NumberOfEventsCondition(activeTrace
, INITIAL_NB_EVENTS
));
114 for (int i
= 0; i
< NB_REFRESH
; i
++) {
115 appendToTrace(NEW_EVENTS_PER_REFRESH
);
118 SWTBotTreeItem tracesFolder
= SWTBotUtils
.selectTracesFolder(fBot
, PROJECT_NAME
);
119 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, tracesFolder
, activeTrace
.getName());
120 traceItem
.contextMenu("Refresh").click();
122 // Make sure the refresh is completed
123 fBot
.waitUntil(new NumberOfEventsCondition(activeTrace
, getNbWrittenEvents()), INDEXING_TIMEOUT
);
126 // Make sure the end of the table matches what we expect
128 fBot
.waitUntil(ConditionHelpers
.selectionInEventsTable(fBot
, getExpectedEndTimeStamp()));
131 private static class NumberOfEventsCondition
extends DefaultCondition
{
133 private ITmfTrace fTrace
;
134 private long fNbEvents
;
136 private NumberOfEventsCondition(ITmfTrace trace
, long nbEvents
) {
138 fNbEvents
= nbEvents
;
142 public boolean test() throws Exception
{
143 return fTrace
.getNbEvents() == fNbEvents
;
147 public String
getFailureMessage() {
148 return fTrace
.getName() + " did not contain the expected number of " + fNbEvents
+ " events. Current: " + fTrace
.getNbEvents();
153 * Create a trace with a number of events.
156 * the number of events to generate
157 * @return the path to the created trace
161 protected String
createTrace(long nbEvents
) throws Exception
{
162 writeToTrace(nbEvents
, false);
163 return TRACE_LOCATION
;
167 * Append a number of events to the trace.
170 * the number of events to append
171 * @throws IOException
174 protected void appendToTrace(long nbEvents
) throws IOException
{
175 writeToTrace(nbEvents
, true);
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
) {
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");
195 * Get the trace type for the test.
197 * @return the trace type
199 protected String
getTraceType() {
200 return TRACE_TYPE_SYSLOG
;
204 * Get the expected time in nanosecs at the end of the trace, after
207 * @return the expected time in nanosecs at the end of the trace
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);
219 return calendar
.getTimeInMillis() * MICROSECOND_TO_NANOSECOND
;
222 private static void goToTableEnd() {
223 SWTBotEditor eventsEditor
= SWTBotUtils
.activeEventsEditor(fBot
);
224 eventsEditor
.setFocus();
225 eventsEditor
.bot().table().pressShortcut(Keystrokes
.END
);
229 * Get the number of events written so far.
231 * @return the number of events written so far
233 protected long getNbWrittenEvents() {
234 return fNbWrittenEvents
;