tmf.ui: add export to tsv events table test
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / viewers / events / ExportToTsvTest.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
3 *
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 *******************************************************************************/
9
10 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events;
11
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;
16
17 import java.io.BufferedReader;
18 import java.io.File;
19 import java.io.FileNotFoundException;
20 import java.io.FileReader;
21 import java.io.IOException;
22 import java.net.URI;
23 import java.net.URISyntaxException;
24 import java.net.URL;
25 import java.util.List;
26 import java.util.stream.Collectors;
27
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;
59
60 import com.google.common.collect.ImmutableList;
61
62 /**
63 * SWTBot test for testing export to tsv.
64 */
65 @RunWith(SWTBotJunit4ClassRunner.class)
66 @NonNullByDefault
67 public class ExportToTsvTest {
68
69 private static final class FileLargerThanZeroCondition implements ICondition {
70 private File fFile;
71
72 public FileLargerThanZeroCondition(File file) {
73 fFile = file;
74 }
75
76 @Override
77 public boolean test() throws Exception {
78 return fFile.length() >= 1;
79 }
80
81 @Override
82 public void init(@Nullable SWTBot bot) {
83 // nothing
84 }
85
86 @Override
87 public String getFailureMessage() {
88 return "File is still of length 0 : " + fFile.getAbsolutePath();
89 }
90 }
91
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 */
104
105 private @Nullable static File fTestFile = null;
106
107 private static SWTWorkbenchBot fBot = new SWTWorkbenchBot();
108 private @Nullable SWTBotEditor fEditorBot;
109 private @Nullable String fAbsolutePath;
110
111 /** The Log4j logger instance. */
112 @SuppressWarnings("null")
113 private static final Logger fLogger = Logger.getRootLogger();
114
115 /**
116 * Test Class setup
117 */
118 @BeforeClass
119 public static void beforeClass() {
120 SWTBotUtils.initialize();
121
122 /* set up test trace */
123 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TRACE_PATH), null);
124 URI uri;
125 try {
126 uri = FileLocator.toFileURL(location).toURI();
127 fTestFile = new File(uri);
128 } catch (URISyntaxException | IOException e) {
129 fail(e.getMessage());
130 }
131
132 File testFile = fTestFile;
133 assertNotNull(testFile);
134 assumeTrue(testFile.exists());
135
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();
142
143 /* Close welcome view */
144 SWTBotUtils.closeView("Welcome", fBot);
145
146 /* Switch perspectives */
147 SWTBotUtils.switchToTracingPerspective();
148
149 /* Finish waiting for eclipse to load */
150 WaitUtils.waitForJobs();
151
152 SWTBotUtils.createProject(TRACE_PROJECT_NAME);
153 }
154
155 /**
156 * Test class tear down method.
157 */
158 @AfterClass
159 public static void afterClass() {
160 SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, fBot);
161 fLogger.removeAllAppenders();
162 }
163
164 /**
165 * Before Test
166 */
167 @Before
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);
175 }
176
177 /**
178 * After Test
179 */
180 @After
181 public void after() {
182 fBot.closeAllEditors();
183 }
184
185 /**
186 * Test export a single selection
187 *
188 * @throws IOException
189 * File not found or such
190 */
191 @Test
192 public void testExportSingleSelection() throws IOException {
193 assumeTrue(!isAffectedByBug486302());
194
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));
204 }
205
206 /**
207 * Test export multiple selection
208 *
209 * @throws IOException
210 * File not found or such
211 */
212 @Test
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));
224 }
225
226 /**
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.
229 */
230 private static boolean isAffectedByBug486302() {
231 String property = System.getProperty("org.eclipse.swt.internal.gtk.version");
232 if (property != null) {
233 @NonNull
234 String @NonNull [] versionSegments = property.split("\\.");
235 if (versionSegments.length > 0) {
236 return SWT.getVersion() <= 4530 && versionSegments[0].equals("3");
237 }
238 }
239
240 return false;
241 }
242
243 /**
244 * Test full export
245 *
246 * @throws IOException
247 * File not found or such
248 */
249 @Test
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);
256
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);
263 } finally {
264 new File(fAbsolutePath).delete();
265 }
266 }
267
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);
274 } finally {
275 file.delete();
276 }
277 }
278 }
This page took 0.038825 seconds and 5 git commands to generate.