1 /*******************************************************************************
2 * Copyright (c) 2014 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
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.swtbot
.tests
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertNotNull
;
19 import java
.io
.FileNotFoundException
;
20 import java
.io
.FileWriter
;
21 import java
.io
.IOException
;
22 import java
.io
.RandomAccessFile
;
24 import org
.apache
.log4j
.ConsoleAppender
;
25 import org
.apache
.log4j
.Logger
;
26 import org
.apache
.log4j
.SimpleLayout
;
27 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
28 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
29 import org
.eclipse
.swtbot
.eclipse
.finder
.widgets
.SWTBotView
;
30 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
31 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
32 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
33 import org
.junit
.BeforeClass
;
34 import org
.junit
.Test
;
35 import org
.junit
.runner
.RunWith
;
38 * Custom text wizard tests
40 * Some reminders to help making tests (javadoc to keep formatting)
45 * 0 Time Stamp Format Help
51 * 6 Regular Expression Help
52 * 7 Remove group (group 1 toggle)
53 * 8 Remove group (group 2 toggle)
54 * 9 Add group (group 3 toggle ...)
55 * 10 Show parsing result
63 * 1 event type (message, timestamp...)
64 * 2 how to handle the data (set, append...)
68 * @author Matthew Khouzam
71 @RunWith(SWTBotJunit4ClassRunner
.class)
72 public class TestCustomTxtWizard
{
74 private static final String MANAGE_CUSTOM_PARSERS_SHELL_TITLE
= "Manage Custom Parsers";
75 private static final String PROJECT_NAME
= "Test";
76 private static final String CATEGORY_NAME
= "Test Category";
77 private static final String TRACETYPE_NAME
= "Test Trace";
78 private static final String EXPECTED_TEST_DEFINITION
= "<Definition category=\"Test Category\" name=\"Test Trace\">\n" +
79 "<TimeStampOutputFormat>ss</TimeStampOutputFormat>\n" +
81 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
82 "<RegEx>\\s*(\\d\\d)\\s(.*\\S)</RegEx>\n" +
83 "<InputData action=\"0\" format=\"ss\" name=\"Time Stamp\"/>\n" +
84 "<InputData action=\"0\" name=\"Message\"/>\n" +
87 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
88 "<RegEx>([^0-9]*)</RegEx>\n" +
89 "<InputData action=\"2\" name=\"Message\"/>\n" +
91 "<OutputColumn name=\"Time Stamp\"/>\n" +
92 "<OutputColumn name=\"Message\"/>\n";
94 /** The Log4j logger instance. */
95 private static final Logger fLogger
= Logger
.getRootLogger();
96 private static SWTWorkbenchBot fBot
;
98 /** Test Class setup */
100 public static void init() {
101 SWTBotUtil
.failIfUIThread();
102 Thread
.currentThread().setName("SWTBot Thread"); // for the debugger
103 /* set up for swtbot */
104 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
105 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout()));
106 fBot
= new SWTWorkbenchBot();
108 SWTBotUtil
.closeView("welcome", fBot
);
110 SWTBotUtil
.switchToTracingPerspective();
111 /* finish waiting for eclipse to load */
112 SWTBotUtil
.waitForJobs();
117 * Test to create a custom txt trace and compare the xml
119 * @throws IOException
120 * the xml file is not accessible, this is bad
121 * @throws FileNotFoundException
122 * the xml file wasn't written, this is bad
125 public void testNew() throws FileNotFoundException
, IOException
{
126 File xmlFile
= ResourcesPlugin
.getWorkspace().getRoot().getLocation().append(".metadata/.plugins/org.eclipse.linuxtools.tmf.core/custom_txt_parsers.xml").toFile();
127 SWTBotUtil
.createProject(PROJECT_NAME
);
128 SWTBotView proejctExplorerBot
= fBot
.viewByTitle("Project Explorer");
129 proejctExplorerBot
.show();
130 SWTBotTreeItem treeItem
= proejctExplorerBot
.bot().tree().getTreeItem(PROJECT_NAME
);
133 SWTBotTreeItem treeNode
= null;
134 for (String node
: treeItem
.getNodes()) {
135 if (node
.startsWith("Trace")) {
136 treeNode
= treeItem
.getNode(node
);
141 assertNotNull(treeNode
);
142 treeNode
.contextMenu("Manage Custom Parsers...").click();
143 fBot
.shell(MANAGE_CUSTOM_PARSERS_SHELL_TITLE
).setFocus();
145 fBot
.button("New...").click();
147 fBot
.textWithLabel("Category:").setText(CATEGORY_NAME
);
148 fBot
.textWithLabel("Trace type:").setText(TRACETYPE_NAME
);
149 fBot
.textWithLabel("Time Stamp format:").setText("ss");
150 fBot
.comboBox(1).setSelection("Time Stamp");
151 fBot
.textWithLabel("format:").setText("ss");
152 fBot
.button(8).click();
153 fBot
.button(2).click();
154 SWTBotTreeItem
[] treeItems
= fBot
.tree().getAllItems();
155 SWTBotTreeItem eventLine
[] = new SWTBotTreeItem
[2];
156 treeItems
= fBot
.tree().getAllItems();
157 for (SWTBotTreeItem item
: treeItems
) {
158 if (item
.getText().startsWith("Root Line 1")) {
161 if (item
.getText().startsWith("Root Line 2")) {
165 assertNotNull(eventLine
[0]);
166 assertNotNull(eventLine
[1]);
167 fBot
.styledText().setText("12 Hello\nWorld\n23 Goodbye\ncruel world");
168 eventLine
[0].select();
169 SWTBotUtil
.waitForJobs();
170 fBot
.textWithLabel("Regular expression:").setText("\\s*(\\d\\d)\\s(.*\\S)");
171 eventLine
[1].select();
172 fBot
.textWithLabel("Regular expression:").setText("([^0-9]*)");
173 fBot
.button(7).click();
174 fBot
.comboBox("Set").setSelection("Append with |");
175 fBot
.button("Highlight All").click();
176 fBot
.button("Next >").click();
177 fBot
.button("Finish").click();
178 String xmlPart
= extractTestXml(xmlFile
, CATEGORY_NAME
, TRACETYPE_NAME
);
179 assertEquals(EXPECTED_TEST_DEFINITION
, xmlPart
);
180 fBot
.list().select(CATEGORY_NAME
+ " : " + TRACETYPE_NAME
);
181 fBot
.button("Delete").click();
182 fBot
.button("Yes").click();
183 fBot
.button("Close").click();
184 xmlPart
= extractTestXml(xmlFile
, CATEGORY_NAME
, TRACETYPE_NAME
);
185 assertEquals("", xmlPart
);
187 SWTBotUtil
.deleteProject(PROJECT_NAME
, fBot
);
191 * Test to edit a custom txt trace and compare the xml
193 * @throws IOException
194 * the xml file is not accessible, this is bad
195 * @throws FileNotFoundException
196 * the xml file wasn't written, this is bad
199 public void testEdit() throws FileNotFoundException
, IOException
{
200 File xmlFile
= ResourcesPlugin
.getWorkspace().getRoot().getLocation().append(".metadata/.plugins/org.eclipse.linuxtools.tmf.core/custom_txt_parsers.xml").toFile();
201 try (FileWriter fw
= new FileWriter(xmlFile
)) {
202 String xmlContent
= "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
203 "<CustomTxtTraceDefinitionList>\n" +
204 "<Definition category=\"Demo Category\" name=\"Demo trace\">\n" +
205 "<TimeStampOutputFormat>sss</TimeStampOutputFormat>\n" +
207 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
208 "<RegEx>\\s*(\\d*)\\s(.*)</RegEx>\n" +
209 "<InputData action=\"0\" format=\"sss\" name=\"Time Stamp\"/>\n" +
210 "<InputData action=\"0\" name=\"Message\"/>\n" +
212 "<OutputColumn name=\"Time Stamp\"/>\n" +
213 "<OutputColumn name=\"Message\"/>\n" +
215 "<Definition name=\"dmesg\">\n" +
216 "<TimeStampOutputFormat>sssssss.ssssss</TimeStampOutputFormat>\n" +
218 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
219 "<RegEx>^[([0-9]*\\.[0.9]*)]\\s(.*)</RegEx>\n" +
220 "<InputData action=\"0\" format=\"sssss.sssss\" name=\"Time Stamp\"/>\n" +
221 "<InputData action=\"0\" name=\"Message\"/>\n" +
223 "<OutputColumn name=\"Time Stamp\"/>\n" +
224 "<OutputColumn name=\"Message\"/>\n" +
226 "</CustomTxtTraceDefinitionList>";
227 fw
.write(xmlContent
);
230 SWTBotUtil
.createProject(PROJECT_NAME
);
231 SWTBotView proejctExplorerBot
= fBot
.viewByTitle("Project Explorer");
232 proejctExplorerBot
.show();
233 SWTBotTreeItem treeItem
= proejctExplorerBot
.bot().tree().getTreeItem(PROJECT_NAME
);
236 SWTBotTreeItem treeNode
= null;
237 for (String node
: treeItem
.getNodes()) {
238 if (node
.startsWith("Trace")) {
239 treeNode
= treeItem
.getNode(node
);
244 assertNotNull(treeNode
);
245 treeNode
.contextMenu("Manage Custom Parsers...").click();
246 fBot
.shell(MANAGE_CUSTOM_PARSERS_SHELL_TITLE
).setFocus();
247 fBot
.list().select("Demo Category : Demo trace");
248 fBot
.button("Edit...").click();
250 fBot
.textWithLabel("Category:").setText(CATEGORY_NAME
);
251 fBot
.textWithLabel("Trace type:").setText(TRACETYPE_NAME
);
252 fBot
.textWithLabel("Time Stamp format:").setText("ss");
253 fBot
.comboBox(1).setSelection("Time Stamp");
254 fBot
.textWithLabel("format:").setText("ss");
255 fBot
.button(2).click();
256 SWTBotTreeItem
[] treeItems
= fBot
.tree().getAllItems();
257 SWTBotTreeItem eventLine
[] = new SWTBotTreeItem
[2];
258 for (SWTBotTreeItem item
: treeItems
) {
259 if (item
.getText().startsWith("Root Line 1")) {
262 if (item
.getText().startsWith("Root Line 2")) {
266 treeItems
= fBot
.tree().getAllItems();
267 assertNotNull(eventLine
[0]);
268 assertNotNull(eventLine
[1]);
269 fBot
.styledText().setText("12 Hello\nWorld\n23 Goodbye\ncruel world");
270 eventLine
[0].select();
271 SWTBotUtil
.waitForJobs();
272 fBot
.textWithLabel("Regular expression:").setText("\\s*(\\d\\d)\\s(.*\\S)");
273 eventLine
[1].select();
274 fBot
.textWithLabel("Regular expression:").setText("([^0-9]*)");
275 fBot
.button(7).click();
276 fBot
.comboBox("Set").setSelection("Append with |");
277 fBot
.button("Highlight All").click();
278 fBot
.button("Next >").click();
279 fBot
.button("Finish").click();
280 String xmlPart
= extractTestXml(xmlFile
, CATEGORY_NAME
, TRACETYPE_NAME
);
281 assertEquals(EXPECTED_TEST_DEFINITION
, xmlPart
);
282 fBot
.list().select(CATEGORY_NAME
+ " : " + TRACETYPE_NAME
);
283 fBot
.button("Delete").click();
284 fBot
.button("Yes").click();
285 fBot
.button("Close").click();
286 xmlPart
= extractTestXml(xmlFile
, CATEGORY_NAME
, TRACETYPE_NAME
);
287 assertEquals("", xmlPart
);
289 SWTBotUtil
.deleteProject(PROJECT_NAME
, fBot
);
292 private static String
extractTestXml(File xmlFile
, String category
, String definitionName
) throws IOException
, FileNotFoundException
{
293 StringBuilder xmlPart
= new StringBuilder();
294 boolean started
= false;
295 try (RandomAccessFile raf
= new RandomAccessFile(xmlFile
, "r");) {
296 String s
= raf
.readLine();
298 if (s
.equals("<Definition category=\"" + category
+ "\" name=\"" + definitionName
+ "\">")) {
302 if (s
.equals("</Definition>")) {
306 xmlPart
.append('\n');
311 return xmlPart
.toString();