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
.junit
.After
;
34 import org
.junit
.Before
;
35 import org
.junit
.BeforeClass
;
36 import org
.junit
.Test
;
37 import org
.junit
.runner
.RunWith
;
40 * Test refreshing a text trace after new content was added.
42 @RunWith(SWTBotJunit4ClassRunner
.class)
43 public class TestRefreshTextTrace
{
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";
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;
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
;
60 private long fNbWrittenEvents
= 0;
62 /** Test Class setup */
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();
71 SWTBotUtils
.closeView("welcome", fBot
);
73 SWTBotUtils
.switchToTracingPerspective();
74 /* finish waiting for eclipse to load */
75 SWTBotUtils
.waitForJobs();
81 * @throws Exception on error
84 public void before() throws Exception
{
85 SWTBotUtils
.createProject(PROJECT_NAME
);
86 SWTBotUtils
.openTrace(PROJECT_NAME
, createTrace(INITIAL_NB_EVENTS
), getTraceType());
87 SWTBotUtils
.waitForJobs();
95 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
99 * Test refreshing a trace after new content was added.
101 * @throws IOException
105 public void testRefresh() throws IOException
{
106 ITmfTrace activeTrace
= TmfTraceManager
.getInstance().getActiveTrace();
108 fBot
.waitUntil(new NumberOfEventsCondition(activeTrace
, INITIAL_NB_EVENTS
));
110 for (int i
= 0; i
< NB_REFRESH
; i
++) {
111 appendToTrace(NEW_EVENTS_PER_REFRESH
);
114 SWTBotTreeItem tracesFolder
= SWTBotUtils
.selectTracesFolder(fBot
, PROJECT_NAME
);
115 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, tracesFolder
, activeTrace
.getName());
116 traceItem
.contextMenu("Refresh").click();
118 // Make sure the refresh is completed
119 fBot
.waitUntil(new NumberOfEventsCondition(activeTrace
, getNbWrittenEvents()), INDEXING_TIMEOUT
);
122 // Make sure the end of the table matches what we expect
124 fBot
.waitUntil(ConditionHelpers
.selectionInEventsTable(fBot
, getExpectedEndTimeStamp()));
127 private static class NumberOfEventsCondition
extends DefaultCondition
{
129 private ITmfTrace fTrace
;
130 private long fNbEvents
;
132 private NumberOfEventsCondition(ITmfTrace trace
, long nbEvents
) {
134 fNbEvents
= nbEvents
;
138 public boolean test() throws Exception
{
139 return fTrace
.getNbEvents() == fNbEvents
;
143 public String
getFailureMessage() {
144 return fTrace
.getName() + " did not contain the expected number of " + fNbEvents
+ " events. Current: " + fTrace
.getNbEvents();
149 * Create a trace with a number of events.
152 * the number of events to generate
153 * @return the path to the created trace
157 protected String
createTrace(long nbEvents
) throws Exception
{
158 writeToTrace(nbEvents
, false);
159 return TRACE_LOCATION
;
163 * Append a number of events to the trace.
166 * the number of events to append
167 * @throws IOException
170 protected void appendToTrace(long nbEvents
) throws IOException
{
171 writeToTrace(nbEvents
, true);
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
) {
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");
191 * Get the trace type for the test.
193 * @return the trace type
195 protected String
getTraceType() {
196 return TRACE_TYPE_SYSLOG
;
200 * Get the expected time in nanosecs at the end of the trace, after
203 * @return the expected time in nanosecs at the end of the trace
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);
215 return calendar
.getTimeInMillis() * MICROSECOND_TO_NANOSECOND
;
218 private static void goToTableEnd() {
219 SWTBotEditor eventsEditor
= SWTBotUtils
.activeEventsEditor(fBot
);
220 eventsEditor
.setFocus();
221 eventsEditor
.bot().table().pressShortcut(Keystrokes
.END
);
225 * Get the number of events written so far.
227 * @return the number of events written so far
229 protected long getNbWrittenEvents() {
230 return fNbWrittenEvents
;