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;
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
;
59 private long fNbWrittenEvents
= 0;
61 /** Test Class setup */
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();
70 SWTBotUtils
.closeView("welcome", fBot
);
72 SWTBotUtils
.switchToTracingPerspective();
73 /* finish waiting for eclipse to load */
74 SWTBotUtils
.waitForJobs();
80 * @throws Exception on error
83 public void before() throws Exception
{
84 SWTBotUtils
.createProject(PROJECT_NAME
);
85 SWTBotUtils
.openTrace(PROJECT_NAME
, createTrace(INITIAL_NB_EVENTS
), getTraceType());
86 SWTBotUtils
.waitForJobs();
94 SWTBotUtils
.deleteProject(PROJECT_NAME
, fBot
);
98 * Test refreshing a trace after new content was added.
100 * @throws IOException
104 public void testRefresh() throws IOException
{
105 ITmfTrace activeTrace
= TmfTraceManager
.getInstance().getActiveTrace();
107 fBot
.waitUntil(new NumberOfEventsCondition(activeTrace
, INITIAL_NB_EVENTS
));
109 for (int i
= 0; i
< NB_REFRESH
; i
++) {
110 appendToTrace(NEW_EVENTS_PER_REFRESH
);
113 SWTBotTreeItem tracesFolder
= SWTBotUtils
.selectTracesFolder(fBot
, PROJECT_NAME
);
114 SWTBotTreeItem traceItem
= SWTBotUtils
.getTraceProjectItem(fBot
, tracesFolder
, activeTrace
.getName());
115 traceItem
.contextMenu("Refresh").click();
117 // Make sure the refresh is completed
118 fBot
.waitUntil(new NumberOfEventsCondition(activeTrace
, getNbWrittenEvents()));
121 // Make sure the end of the table matches what we expect
123 fBot
.waitUntil(ConditionHelpers
.selectionInEventsTable(fBot
, getExpectedEndTimeStamp()));
126 private static class NumberOfEventsCondition
extends DefaultCondition
{
128 private ITmfTrace fTrace
;
129 private long fNbEvents
;
131 private NumberOfEventsCondition(ITmfTrace trace
, long nbEvents
) {
133 fNbEvents
= nbEvents
;
137 public boolean test() throws Exception
{
138 return fTrace
.getNbEvents() == fNbEvents
;
142 public String
getFailureMessage() {
143 return fTrace
.getName() + " did not contain the expected number of " + fNbEvents
+ " events";
148 * Create a trace with a number of events.
151 * the number of events to generate
152 * @return the path to the created trace
156 protected String
createTrace(long nbEvents
) throws Exception
{
157 writeToTrace(nbEvents
, false);
158 return TRACE_LOCATION
;
162 * Append a number of events to the trace.
165 * the number of events to append
166 * @throws IOException
169 protected void appendToTrace(long nbEvents
) throws IOException
{
170 writeToTrace(nbEvents
, true);
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
) {
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");
190 * Get the trace type for the test.
192 * @return the trace type
194 protected String
getTraceType() {
195 return TRACE_TYPE_SYSLOG
;
199 * Get the expected time in nanosecs at the end of the trace, after
202 * @return the expected time in nanosecs at the end of the trace
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);
214 return calendar
.getTimeInMillis() * MICROSECOND_TO_NANOSECOND
;
217 private static void goToTableEnd() {
218 SWTBotEditor eventsEditor
= SWTBotUtils
.activeEventsEditor(fBot
);
219 eventsEditor
.setFocus();
220 eventsEditor
.bot().table().pressShortcut(Keystrokes
.END
);
224 * Get the number of events written so far.
226 * @return the number of events written so far
228 protected long getNbWrittenEvents() {
229 return fNbWrittenEvents
;