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