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
;
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
;
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
.eclipse
.tracecompass
.tmf
.ui
.tests
.shared
.WaitUtils
;
34 import org
.junit
.After
;
35 import org
.junit
.Before
;
36 import org
.junit
.BeforeClass
;
37 import org
.junit
.Test
;
38 import org
.junit
.runner
.RunWith
;
41 * Test refreshing a text trace after new content was added.
43 @RunWith(SWTBotJunit4ClassRunner
.class)
44 public class TestRefreshTextTrace
{
46 private static final String PROJECT_NAME
= "Test";
47 private static final String TRACE_TYPE_SYSLOG
= "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog";
48 private static final String TIMESTAMP_FORMAT
= "MMM dd HH:mm:ss";
50 private static final long INITIAL_NB_EVENTS
= 100;
51 private static final int NEW_EVENTS_PER_REFRESH
= 40000;
52 private static final int NB_REFRESH
= 3;
53 private static final long SECOND_TO_MILLISECOND
= 1000;
54 private static final long MICROSECOND_TO_NANOSECOND
= 1000000;
55 private static final int INDEXING_TIMEOUT
= 300000;
57 private static final Calendar CURRENT
= Calendar
.getInstance();
58 private static final String TRACE_LOCATION
= TmfTraceManager
.getTemporaryDirPath() + File
.separator
+ "test.txt";
59 private static SWTWorkbenchBot fBot
;
61 private long fNbWrittenEvents
= 0;
63 /** Test Class setup */
65 public static void init() {
66 SWTBotPreferences
.KEYBOARD_LAYOUT
= "EN_US";
67 SWTBotUtils
.initialize();
68 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
69 Logger
.getRootLogger().addAppender(new NullAppender());
70 fBot
= new SWTWorkbenchBot();
72 SWTBotUtils
.closeView("welcome", fBot
);
74 SWTBotUtils
.switchToTracingPerspective();
75 /* finish waiting for eclipse to load */
76 WaitUtils
.waitForJobs();
82 * @throws Exception on error
85 public void before() throws Exception
{
86 SWTBotUtils
.createProject(PROJECT_NAME
);
87 SWTBotUtils
.openTrace(PROJECT_NAME
, createTrace(INITIAL_NB_EVENTS
), getTraceType());
88 WaitUtils
.waitForJobs();
96 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
100 * Test refreshing a trace after new content was added.
102 * @throws IOException
106 public void testRefresh() throws IOException
{
107 ITmfTrace activeTrace
= TmfTraceManager
.getInstance().getActiveTrace();
109 fBot
.waitUntil(new NumberOfEventsCondition(activeTrace
, INITIAL_NB_EVENTS
));
111 for (int i
= 0; i
< NB_REFRESH
; i
++) {
112 appendToTrace(NEW_EVENTS_PER_REFRESH
);
115 SWTBotTreeItem tracesFolder
= SWTBotUtils
.selectTracesFolder(fBot
, PROJECT_NAME
);
116 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, tracesFolder
, activeTrace
.getName());
117 traceItem
.contextMenu("Refresh").click();
119 // Make sure the refresh is completed
120 fBot
.waitUntil(new NumberOfEventsCondition(activeTrace
, getNbWrittenEvents()), INDEXING_TIMEOUT
);
123 // Make sure the end of the table matches what we expect
125 fBot
.waitUntil(ConditionHelpers
.selectionInEventsTable(fBot
, getExpectedEndTimeStamp()));
128 private static class NumberOfEventsCondition
extends DefaultCondition
{
130 private ITmfTrace fTrace
;
131 private long fNbEvents
;
133 private NumberOfEventsCondition(ITmfTrace trace
, long nbEvents
) {
135 fNbEvents
= nbEvents
;
139 public boolean test() throws Exception
{
140 return fTrace
.getNbEvents() == fNbEvents
;
144 public String
getFailureMessage() {
145 return fTrace
.getName() + " did not contain the expected number of " + fNbEvents
+ " events. Current: " + fTrace
.getNbEvents();
150 * Create a trace with a number of events.
153 * the number of events to generate
154 * @return the path to the created trace
158 protected String
createTrace(long nbEvents
) throws Exception
{
159 writeToTrace(nbEvents
, false);
160 return TRACE_LOCATION
;
164 * Append a number of events to the trace.
167 * the number of events to append
168 * @throws IOException
171 protected void appendToTrace(long nbEvents
) throws IOException
{
172 writeToTrace(nbEvents
, true);
175 private void writeToTrace(long nbEvents
, boolean append
) throws IOException
{
176 final File file
= new File(TRACE_LOCATION
);
177 try (FileWriter writer
= new FileWriter(file
, append
)) {
178 for (int i
= 0; i
< nbEvents
; ++i
) {
184 private void writeEvent(FileWriter fw
) throws IOException
{
185 SimpleDateFormat f
= new SimpleDateFormat(TIMESTAMP_FORMAT
);
186 String timeStampStr
= f
.format(new Date(fNbWrittenEvents
* SECOND_TO_MILLISECOND
));
187 fw
.write(timeStampStr
+ " HostF LoggerF: SourceFileF:9 Message F\n");
192 * Get the trace type for the test.
194 * @return the trace type
196 protected String
getTraceType() {
197 return TRACE_TYPE_SYSLOG
;
201 * Get the expected time in nanosecs at the end of the trace, after
204 * @return the expected time in nanosecs at the end of the trace
206 protected long getExpectedEndTimeStamp() {
207 Date date
= new Date((fNbWrittenEvents
- 1) * SECOND_TO_MILLISECOND
);
208 // Syslog fills in the year when parsing so we have to do it for the
209 // expected time stamp as well
210 GregorianCalendar calendar
= new GregorianCalendar();
211 calendar
.setTime(date
);
212 calendar
.set(Calendar
.YEAR
, CURRENT
.get(Calendar
.YEAR
));
213 if (calendar
.after(CURRENT
)) {
214 calendar
.set(Calendar
.YEAR
, CURRENT
.get(Calendar
.YEAR
) - 1);
216 return calendar
.getTimeInMillis() * MICROSECOND_TO_NANOSECOND
;
219 private static void goToTableEnd() {
220 SWTBotEditor eventsEditor
= SWTBotUtils
.activeEventsEditor(fBot
);
221 eventsEditor
.setFocus();
222 eventsEditor
.bot().table().pressShortcut(Keystrokes
.END
);
226 * Get the number of events written so far.
228 * @return the number of events written so far
230 protected long getNbWrittenEvents() {
231 return fNbWrittenEvents
;