1 /*******************************************************************************
2 * Copyright (c) 2016 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
.assertEquals
;
13 import static org
.junit
.Assert
.assertNotNull
;
14 import static org
.junit
.Assert
.fail
;
15 import static org
.junit
.Assume
.assumeTrue
;
17 import java
.io
.BufferedReader
;
19 import java
.io
.FileNotFoundException
;
20 import java
.io
.FileReader
;
21 import java
.io
.IOException
;
23 import java
.net
.URISyntaxException
;
25 import java
.util
.List
;
26 import java
.util
.stream
.Collectors
;
28 import org
.apache
.log4j
.ConsoleAppender
;
29 import org
.apache
.log4j
.Logger
;
30 import org
.apache
.log4j
.SimpleLayout
;
31 import org
.eclipse
.core
.runtime
.FileLocator
;
32 import org
.eclipse
.core
.runtime
.Path
;
33 import org
.eclipse
.jdt
.annotation
.NonNull
;
34 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
35 import org
.eclipse
.jdt
.annotation
.Nullable
;
36 import org
.eclipse
.swt
.SWT
;
37 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
38 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotEditor
;
39 import org
.eclipse
.swtbot
.swt
.finder
.SWTBot
;
40 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
41 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keyboard
;
42 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.KeyboardFactory
;
43 import org
.eclipse
.swtbot
.swt
.finder
.keyboard
.Keystrokes
;
44 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
45 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
46 import org
.eclipse
.swtbot
.swt
.finder
.waits
.ICondition
;
47 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTable
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.TmfCoreTestPlugin
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
50 import org
.eclipse
.tracecompass
.tmf
.ui
.dialog
.TmfFileDialogFactory
;
51 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
52 import org
.eclipse
.tracecompass
.tmf
.ui
.tests
.shared
.WaitUtils
;
53 import org
.junit
.After
;
54 import org
.junit
.AfterClass
;
55 import org
.junit
.Before
;
56 import org
.junit
.BeforeClass
;
57 import org
.junit
.Test
;
58 import org
.junit
.runner
.RunWith
;
60 import com
.google
.common
.collect
.ImmutableList
;
63 * SWTBot test for testing export to tsv.
65 @RunWith(SWTBotJunit4ClassRunner
.class)
67 public class ExportToTsvTest
{
69 private static final class FileLargerThanZeroCondition
implements ICondition
{
72 public FileLargerThanZeroCondition(File file
) {
77 public boolean test() throws Exception
{
78 return fFile
.length() >= 1;
82 public void init(@Nullable SWTBot bot
) {
87 public String
getFailureMessage() {
88 return "File is still of length 0 : " + fFile
.getAbsolutePath();
92 private static final String HEADER_TEXT
= "Timestamp\tHost\tLogger\tFile\tLine\tMessage";
93 private static final String EVENT1_TEXT
= "01:01:01.000 000 000\tHostA\tLoggerA\tSourceFile\t4\tMessage A";
94 private static final String EVENT2_TEXT
= "02:02:02.000 000 000\tHostB\tLoggerB\tSourceFile\t5\tMessage B";
95 private static final String EVENT3_TEXT
= "03:03:03.000 000 000\tHostC\tLoggerC\tSourceFile\t6\tMessage C";
96 @SuppressWarnings("null")
97 private static final Keyboard KEYBOARD
= KeyboardFactory
.getSWTKeyboard();
98 private static final String TRACE_PROJECT_NAME
= "test";
99 private static final String TRACE_NAME
= "syslog_collapse";
100 private static final String TRACE_PATH
= "testfiles/" + TRACE_NAME
;
101 private static final String TRACE_TYPE
= "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog";
102 private static final String EXPORT_TO_TSV
= "Export To Text...";
103 private static final int TIMEOUT
= 2000; /* 20 second timeout */
105 private @Nullable static File fTestFile
= null;
107 private static SWTWorkbenchBot fBot
= new SWTWorkbenchBot();
108 private @Nullable SWTBotEditor fEditorBot
;
109 private @Nullable String fAbsolutePath
;
111 /** The Log4j logger instance. */
112 @SuppressWarnings("null")
113 private static final Logger fLogger
= Logger
.getRootLogger();
119 public static void beforeClass() {
120 SWTBotUtils
.initialize();
122 /* set up test trace */
123 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(TRACE_PATH
), null);
126 uri
= FileLocator
.toFileURL(location
).toURI();
127 fTestFile
= new File(uri
);
128 } catch (URISyntaxException
| IOException e
) {
129 fail(e
.getMessage());
132 File testFile
= fTestFile
;
133 assertNotNull(testFile
);
134 assumeTrue(testFile
.exists());
136 /* Set up for swtbot */
137 SWTBotPreferences
.TIMEOUT
= TIMEOUT
;
138 SWTBotPreferences
.KEYBOARD_LAYOUT
= "EN_US";
139 fLogger
.removeAllAppenders();
140 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender
.SYSTEM_OUT
));
141 fBot
= new SWTWorkbenchBot();
143 /* Close welcome view */
144 SWTBotUtils
.closeView("Welcome", fBot
);
146 /* Switch perspectives */
147 SWTBotUtils
.switchToTracingPerspective();
149 /* Finish waiting for eclipse to load */
150 WaitUtils
.waitForJobs();
152 SWTBotUtils
.createProject(TRACE_PROJECT_NAME
);
156 * Test class tear down method.
159 public static void afterClass() {
160 SWTBotUtils
.deleteProject(TRACE_PROJECT_NAME
, fBot
);
161 fLogger
.removeAllAppenders();
168 public void before() {
169 File testFile
= fTestFile
;
170 assertNotNull(testFile
);
171 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, testFile
.getAbsolutePath(), TRACE_TYPE
);
172 fEditorBot
= SWTBotUtils
.activateEditor(fBot
, testFile
.getName());
173 fAbsolutePath
= TmfTraceManager
.getTemporaryDirPath() + File
.separator
+ "exportToTsvTest.tsv";
174 TmfFileDialogFactory
.setOverrideFiles(fAbsolutePath
);
181 public void after() {
182 fBot
.closeAllEditors();
186 * Test export a single selection
188 * @throws IOException
189 * File not found or such
192 public void testExportSingleSelection() throws IOException
{
193 assumeTrue(!isAffectedByBug486302());
195 SWTBotEditor editorBot
= fEditorBot
;
196 assertNotNull(editorBot
);
197 final SWTBotTable tableBot
= editorBot
.bot().table();
198 tableBot
.getTableItem(0).click(3);
199 KEYBOARD
.typeText("LoggerA");
200 KEYBOARD
.pressShortcut(Keystrokes
.CTRL
, Keystrokes
.CR
);
201 fBot
.waitUntil(Conditions
.tableHasRows(tableBot
, 4), 5000);
202 tableBot
.contextMenu(EXPORT_TO_TSV
).click();
203 assertTsvContentsEquals(ImmutableList
.of(HEADER_TEXT
, EVENT1_TEXT
));
207 * Test export multiple selection
209 * @throws IOException
210 * File not found or such
213 public void testExportMultipleSelection() throws IOException
{
214 assumeTrue(!isAffectedByBug486302());
215 SWTBotEditor editorBot
= fEditorBot
;
216 assertNotNull(editorBot
);
217 final SWTBotTable tableBot
= editorBot
.bot().table();
218 tableBot
.getTableItem(0).click(3);
219 KEYBOARD
.typeText("LoggerA|LoggerB|LoggerC");
220 KEYBOARD
.pressShortcut(Keystrokes
.CTRL
, Keystrokes
.CR
);
221 fBot
.waitUntil(Conditions
.tableHasRows(tableBot
, 6), 5000);
222 tableBot
.contextMenu(EXPORT_TO_TSV
).click();
223 assertTsvContentsEquals(ImmutableList
.of(HEADER_TEXT
, EVENT1_TEXT
, EVENT2_TEXT
, EVENT3_TEXT
));
227 * Returns whether or not the running Eclipse is affected by Bug 486302. The
228 * bug is present in Eclipse 4.5.2 and earlier running GTK3.
230 private static boolean isAffectedByBug486302() {
231 String property
= System
.getProperty("org.eclipse.swt.internal.gtk.version");
232 if (property
!= null) {
234 String
@NonNull [] versionSegments
= property
.split("\\.");
235 if (versionSegments
.length
> 0) {
236 return SWT
.getVersion() <= 4530 && versionSegments
[0].equals("3");
246 * @throws IOException
247 * File not found or such
250 public void testExportNoSelection() throws IOException
{
251 SWTBotEditor editorBot
= fEditorBot
;
252 assertNotNull(editorBot
);
253 final SWTBotTable tableBot
= editorBot
.bot().table();
254 tableBot
.getTableItem(1).click();
255 KEYBOARD
.pressShortcut(Keystrokes
.SHIFT
, Keystrokes
.UP
);
257 tableBot
.contextMenu(EXPORT_TO_TSV
).click();
258 File file
= new File(fAbsolutePath
);
259 fBot
.waitUntil(new FileLargerThanZeroCondition(file
));
260 try (BufferedReader br
= new BufferedReader(new FileReader(file
))) {
261 long lines
= br
.lines().count();
262 assertEquals("Both reads", 23, lines
);
264 new File(fAbsolutePath
).delete();
268 private void assertTsvContentsEquals(final List
<String
> expected
) throws FileNotFoundException
, IOException
{
269 File file
= new File(fAbsolutePath
);
270 fBot
.waitUntil(new FileLargerThanZeroCondition(file
));
271 try (BufferedReader br
= new BufferedReader(new FileReader(file
))) {
272 List
<String
> lines
= br
.lines().collect(Collectors
.toList());
273 assertEquals("Both reads", expected
, lines
);