tmf: add swtbot tests for custom text parser
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui.swtbot.tests / src / org / eclipse / linuxtools / tmf / ui / swtbot / tests / TestCustomTxtWizard.java
1 /*******************************************************************************
2 * Copyright (c) 2014 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 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.swtbot.tests;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotNull;
17
18 import java.io.File;
19 import java.io.FileNotFoundException;
20 import java.io.FileWriter;
21 import java.io.IOException;
22 import java.io.RandomAccessFile;
23
24 import org.apache.log4j.Logger;
25 import org.apache.log4j.varia.NullAppender;
26 import org.eclipse.core.resources.ResourcesPlugin;
27 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
28 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
29 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
30 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33
34 /**
35 * Custom text wizard tests
36 *
37 * Some reminders to help making tests (javadoc to keep formatting)
38 *
39 * Button reminder
40 *
41 * <pre>
42 * 0 Time Stamp Format Help
43 * 1 Remove line
44 * 2 Add next line
45 * 3 Add child line
46 * 4 Move up
47 * 5 Move down
48 * 6 Regular Expression Help
49 * 7 Remove group (group 1 toggle)
50 * 8 Remove group (group 2 toggle)
51 * 9 Add group (group 3 toggle ...)
52 * 10 Show parsing result
53 * 11 Preview Legend
54 * </pre>
55 *
56 * Combo box reminder
57 *
58 * <pre>
59 * 0 cardinality
60 * 1 event type (message, timestamp...)
61 * 2 how to handle the data (set, append...)
62 * repeat
63 * </pre>
64 *
65 * @author Matthew Khouzam
66 *
67 */
68 public class TestCustomTxtWizard {
69
70 private static final String PROJECT_NAME = "Test";
71 private static final String EXPECTED_TEST_DEFINITION = "<Definition name=\"Test\">\n" +
72 "<TimeStampOutputFormat>ss</TimeStampOutputFormat>\n" +
73 "<InputLine>\n" +
74 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
75 "<RegEx>\\s*(\\d\\d)\\s(.*\\S)</RegEx>\n" +
76 "<InputData action=\"0\" format=\"ss\" name=\"Time Stamp\"/>\n" +
77 "<InputData action=\"0\" name=\"Message\"/>\n" +
78 "</InputLine>\n" +
79 "<InputLine>\n" +
80 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
81 "<RegEx>([^0-9]*)</RegEx>\n" +
82 "<InputData action=\"2\" name=\"Message\"/>\n" +
83 "</InputLine>\n" +
84 "<OutputColumn name=\"Time Stamp\"/>\n" +
85 "<OutputColumn name=\"Message\"/>\n";
86
87 /** The Log4j logger instance. */
88 private static final Logger fLogger = Logger.getRootLogger();
89 private static SWTWorkbenchBot fBot;
90
91 /** Test Class setup */
92 @BeforeClass
93 public static void init() {
94 SWTBotUtil.failIfUIThread();
95 Thread.currentThread().setName("SWTBot Thread"); // for the debugger
96 /* set up for swtbot */
97 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
98 fLogger.addAppender(new NullAppender());
99 fBot = new SWTWorkbenchBot();
100
101 SWTBotUtil.closeView("welcome", fBot);
102
103 SWTBotUtil.switchToTracingPerspective();
104 /* finish waiting for eclipse to load */
105 SWTBotUtil.waitForJobs();
106
107 }
108
109 /**
110 * Test to create a custom txt trace and compare the xml
111 *
112 * @throws IOException
113 * the xml file is not accessible, this is bad
114 * @throws FileNotFoundException
115 * the xml file wasn't written, this is bad
116 */
117 @Test
118 public void testNew() throws FileNotFoundException, IOException {
119 File xmlFile = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(".metadata/.plugins/org.eclipse.linuxtools.tmf.core/custom_txt_parsers.xml").toFile();
120 SWTBotUtil.createProject(PROJECT_NAME);
121 SWTBotView proejctExplorerBot = fBot.viewByTitle("Project Explorer");
122 proejctExplorerBot.show();
123 SWTBotTreeItem treeItem = proejctExplorerBot.bot().tree().getTreeItem(PROJECT_NAME);
124 treeItem.select();
125 treeItem.expand();
126 SWTBotTreeItem treeNode = null;
127 for (String node : treeItem.getNodes()) {
128 if (node.startsWith("Trace")) {
129 treeNode = treeItem.getNode(node);
130 break;
131 }
132
133 }
134 assertNotNull(treeNode);
135 treeNode.contextMenu("Manage Custom Parsers...").click();
136 fBot.button("New...").click();
137
138 fBot.textWithLabel("Log type:").setText(PROJECT_NAME);
139 fBot.textWithLabel("Time Stamp format:").setText("ss");
140 fBot.comboBox(1).setSelection("Time Stamp");
141 fBot.textWithLabel("format:").setText("ss");
142 fBot.button(8).click();
143 fBot.button(2).click();
144 SWTBotTreeItem[] treeItems = fBot.tree().getAllItems();
145 SWTBotTreeItem eventLine[] = new SWTBotTreeItem[2];
146 treeItems = fBot.tree().getAllItems();
147 for (SWTBotTreeItem item : treeItems) {
148 if (item.getText().startsWith("Root Line 1")) {
149 eventLine[0] = item;
150 }
151 if (item.getText().startsWith("Root Line 2")) {
152 eventLine[1] = item;
153 }
154 }
155 assertNotNull(eventLine[0]);
156 assertNotNull(eventLine[1]);
157 fBot.styledText().setText("12 Hello\nWorld\n23 Goodbye\ncruel world");
158 eventLine[0].select();
159 SWTBotUtil.waitForJobs();
160 fBot.textWithLabel("Regular expression:").setText("\\s*(\\d\\d)\\s(.*\\S)");
161 eventLine[1].select();
162 fBot.textWithLabel("Regular expression:").setText("([^0-9]*)");
163 fBot.button(7).click();
164 fBot.comboBox("Set").setSelection("Append with |");
165 fBot.button("Highlight All").click();
166 fBot.button("Next >").click();
167 fBot.button("Finish").click();
168 fBot.list().select(PROJECT_NAME);
169 String xmlPart = extractTestXml(xmlFile, PROJECT_NAME);
170 assertEquals(EXPECTED_TEST_DEFINITION, xmlPart);
171 fBot.button("Delete").click();
172 fBot.button("Yes").click();
173 fBot.button("Close").click();
174 xmlPart = extractTestXml(xmlFile, PROJECT_NAME);
175 assertEquals("", xmlPart);
176 }
177
178 /**
179 * Test to edit a custom txt trace and compare the xml
180 *
181 * @throws IOException
182 * the xml file is not accessible, this is bad
183 * @throws FileNotFoundException
184 * the xml file wasn't written, this is bad
185 */
186 @Test
187 public void testEdit() throws FileNotFoundException, IOException {
188 File xmlFile = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(".metadata/.plugins/org.eclipse.linuxtools.tmf.core/custom_txt_parsers.xml").toFile();
189 try (FileWriter fw = new FileWriter(xmlFile)) {
190 String xmlContent = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
191 "<CustomTxtTraceDefinitionList>\n" +
192 "<Definition name=\"Demo trace\">\n" +
193 "<TimeStampOutputFormat>sss</TimeStampOutputFormat>\n" +
194 "<InputLine>\n" +
195 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
196 "<RegEx>\\s*(\\d*)\\s(.*)</RegEx>\n" +
197 "<InputData action=\"0\" format=\"sss\" name=\"Time Stamp\"/>\n" +
198 "<InputData action=\"0\" name=\"Message\"/>\n" +
199 "</InputLine>\n" +
200 "<OutputColumn name=\"Time Stamp\"/>\n" +
201 "<OutputColumn name=\"Message\"/>\n" +
202 "</Definition>\n" +
203 "<Definition name=\"dmesg\">\n" +
204 "<TimeStampOutputFormat>sssssss.ssssss</TimeStampOutputFormat>\n" +
205 "<InputLine>\n" +
206 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
207 "<RegEx>^[([0-9]*\\.[0.9]*)]\\s(.*)</RegEx>\n" +
208 "<InputData action=\"0\" format=\"sssss.sssss\" name=\"Time Stamp\"/>\n" +
209 "<InputData action=\"0\" name=\"Message\"/>\n" +
210 "</InputLine>\n" +
211 "<OutputColumn name=\"Time Stamp\"/>\n" +
212 "<OutputColumn name=\"Message\"/>\n" +
213 "</Definition>\n" +
214 "</CustomTxtTraceDefinitionList>";
215 fw.write(xmlContent);
216 fw.flush();
217 }
218 SWTBotUtil.createProject(PROJECT_NAME);
219 SWTBotView proejctExplorerBot = fBot.viewByTitle("Project Explorer");
220 proejctExplorerBot.show();
221 SWTBotTreeItem treeItem = proejctExplorerBot.bot().tree().getTreeItem(PROJECT_NAME);
222 treeItem.select();
223 treeItem.expand();
224 SWTBotTreeItem treeNode = null;
225 for (String node : treeItem.getNodes()) {
226 if (node.startsWith("Trace")) {
227 treeNode = treeItem.getNode(node);
228 break;
229 }
230
231 }
232 assertNotNull(treeNode);
233 treeNode.contextMenu("Manage Custom Parsers...").click();
234 fBot.list().select(fBot.list().getItems()[0]);
235 fBot.button("Edit...").click();
236
237 fBot.textWithLabel("Log type:").setText(PROJECT_NAME);
238 fBot.textWithLabel("Time Stamp format:").setText("ss");
239 fBot.comboBox(1).setSelection("Time Stamp");
240 fBot.textWithLabel("format:").setText("ss");
241 fBot.button(2).click();
242 SWTBotTreeItem[] treeItems = fBot.tree().getAllItems();
243 SWTBotTreeItem eventLine[] = new SWTBotTreeItem[2];
244 for (SWTBotTreeItem item : treeItems) {
245 if (item.getText().startsWith("Root Line 1")) {
246 eventLine[0] = item;
247 }
248 if (item.getText().startsWith("Root Line 2")) {
249 eventLine[1] = item;
250 }
251 }
252 treeItems = fBot.tree().getAllItems();
253 assertNotNull(eventLine[0]);
254 assertNotNull(eventLine[1]);
255 fBot.styledText().setText("12 Hello\nWorld\n23 Goodbye\ncruel world");
256 eventLine[0].select();
257 SWTBotUtil.waitForJobs();
258 fBot.textWithLabel("Regular expression:").setText("\\s*(\\d\\d)\\s(.*\\S)");
259 eventLine[1].select();
260 fBot.textWithLabel("Regular expression:").setText("([^0-9]*)");
261 fBot.button(7).click();
262 fBot.comboBox("Set").setSelection("Append with |");
263 fBot.button("Highlight All").click();
264 fBot.button("Next >").click();
265 fBot.button("Finish").click();
266 fBot.list().select(PROJECT_NAME);
267 String xmlPart = extractTestXml(xmlFile, PROJECT_NAME);
268 assertEquals(EXPECTED_TEST_DEFINITION, xmlPart);
269 fBot.button("Delete").click();
270 fBot.button("Yes").click();
271 fBot.button("Close").click();
272 xmlPart = extractTestXml(xmlFile, PROJECT_NAME);
273 assertEquals("", xmlPart);
274 }
275
276 private static String extractTestXml(File xmlFile, String definitionName) throws IOException, FileNotFoundException {
277 StringBuilder xmlPart = new StringBuilder();
278 boolean started = false;
279 try (RandomAccessFile raf = new RandomAccessFile(xmlFile, "r");) {
280 String s = raf.readLine();
281 while (s != null) {
282 if (s.equals("<Definition name=\"" + definitionName + "\">")) {
283 started = true;
284 }
285 if (started) {
286 if (s.equals("</Definition>")) {
287 break;
288 }
289 xmlPart.append(s);
290 xmlPart.append('\n');
291 }
292 s = raf.readLine();
293 }
294 }
295 return xmlPart.toString();
296 }
297 }
This page took 0.045581 seconds and 5 git commands to generate.