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
.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
.junit
.SWTBotJunit4ClassRunner
;
30 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
31 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
32 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
33 import org
.junit
.BeforeClass
;
34 import org
.junit
.Ignore
;
35 import org
.junit
.Test
;
36 import org
.junit
.runner
.RunWith
;
39 * Custom text wizard tests
41 * Some reminders to help making tests (javadoc to keep formatting)
46 * 0 Time Stamp Format Help
52 * 6 Regular Expression Help
53 * 7 Remove group (group 1 toggle)
54 * 8 Remove group (group 2 toggle)
55 * 9 Add group (group 3 toggle ...)
56 * 10 Show parsing result
64 * 1 event type (message, timestamp...)
65 * 2 how to handle the data (set, append...)
69 * @author Matthew Khouzam
72 @RunWith(SWTBotJunit4ClassRunner
.class)
73 public class TestCustomTxtWizard
{
75 private static final String MANAGE_CUSTOM_PARSERS_SHELL_TITLE
= "Manage Custom Parsers";
76 private static final String PROJECT_NAME
= "Test";
77 private static final String EXPECTED_TEST_DEFINITION
= "<Definition name=\"Test\">\n" +
78 "<TimeStampOutputFormat>ss</TimeStampOutputFormat>\n" +
80 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
81 "<RegEx>\\s*(\\d\\d)\\s(.*\\S)</RegEx>\n" +
82 "<InputData action=\"0\" format=\"ss\" name=\"Time Stamp\"/>\n" +
83 "<InputData action=\"0\" name=\"Message\"/>\n" +
86 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
87 "<RegEx>([^0-9]*)</RegEx>\n" +
88 "<InputData action=\"2\" name=\"Message\"/>\n" +
90 "<OutputColumn name=\"Time Stamp\"/>\n" +
91 "<OutputColumn name=\"Message\"/>\n";
93 /** The Log4j logger instance. */
94 private static final Logger fLogger
= Logger
.getRootLogger();
95 private static SWTWorkbenchBot fBot
;
97 /** Test Class setup */
99 public static void init() {
100 SWTBotUtil
.failIfUIThread();
101 Thread
.currentThread().setName("SWTBot Thread"); // for the debugger
102 /* set up for swtbot */
103 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
104 fLogger
.addAppender(new NullAppender());
105 fBot
= new SWTWorkbenchBot();
107 SWTBotUtil
.closeView("welcome", fBot
);
109 SWTBotUtil
.switchToTracingPerspective();
110 /* finish waiting for eclipse to load */
111 SWTBotUtil
.waitForJobs();
116 * Test to create a custom txt trace and compare the xml
118 * @throws IOException
119 * the xml file is not accessible, this is bad
120 * @throws FileNotFoundException
121 * 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
.waitUntil(Conditions
.shellIsActive(MANAGE_CUSTOM_PARSERS_SHELL_TITLE
));
144 fBot
.button("New...").click();
146 fBot
.textWithLabel("Log type:").setText(PROJECT_NAME
);
147 fBot
.textWithLabel("Time Stamp format:").setText("ss");
148 fBot
.comboBox(1).setSelection("Time Stamp");
149 fBot
.textWithLabel("format:").setText("ss");
150 fBot
.button(8).click();
151 fBot
.button(2).click();
152 SWTBotTreeItem
[] treeItems
= fBot
.tree().getAllItems();
153 SWTBotTreeItem eventLine
[] = new SWTBotTreeItem
[2];
154 treeItems
= fBot
.tree().getAllItems();
155 for (SWTBotTreeItem item
: treeItems
) {
156 if (item
.getText().startsWith("Root Line 1")) {
159 if (item
.getText().startsWith("Root Line 2")) {
163 assertNotNull(eventLine
[0]);
164 assertNotNull(eventLine
[1]);
165 fBot
.styledText().setText("12 Hello\nWorld\n23 Goodbye\ncruel world");
166 eventLine
[0].select();
167 SWTBotUtil
.waitForJobs();
168 fBot
.textWithLabel("Regular expression:").setText("\\s*(\\d\\d)\\s(.*\\S)");
169 eventLine
[1].select();
170 fBot
.textWithLabel("Regular expression:").setText("([^0-9]*)");
171 fBot
.button(7).click();
172 fBot
.comboBox("Set").setSelection("Append with |");
173 fBot
.button("Highlight All").click();
174 fBot
.button("Next >").click();
175 fBot
.button("Finish").click();
176 fBot
.list().select(PROJECT_NAME
);
177 String xmlPart
= extractTestXml(xmlFile
, PROJECT_NAME
);
178 assertEquals(EXPECTED_TEST_DEFINITION
, xmlPart
);
179 fBot
.button("Delete").click();
180 fBot
.button("Yes").click();
181 fBot
.button("Close").click();
182 xmlPart
= extractTestXml(xmlFile
, PROJECT_NAME
);
183 assertEquals("", xmlPart
);
187 * Test to edit a custom txt trace and compare the xml
189 * @throws IOException
190 * the xml file is not accessible, this is bad
191 * @throws FileNotFoundException
192 * the xml file wasn't written, this is bad
196 public void testEdit() throws FileNotFoundException
, IOException
{
197 File xmlFile
= ResourcesPlugin
.getWorkspace().getRoot().getLocation().append(".metadata/.plugins/org.eclipse.linuxtools.tmf.core/custom_txt_parsers.xml").toFile();
198 try (FileWriter fw
= new FileWriter(xmlFile
)) {
199 String xmlContent
= "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
200 "<CustomTxtTraceDefinitionList>\n" +
201 "<Definition name=\"Demo trace\">\n" +
202 "<TimeStampOutputFormat>sss</TimeStampOutputFormat>\n" +
204 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
205 "<RegEx>\\s*(\\d*)\\s(.*)</RegEx>\n" +
206 "<InputData action=\"0\" format=\"sss\" name=\"Time Stamp\"/>\n" +
207 "<InputData action=\"0\" name=\"Message\"/>\n" +
209 "<OutputColumn name=\"Time Stamp\"/>\n" +
210 "<OutputColumn name=\"Message\"/>\n" +
212 "<Definition name=\"dmesg\">\n" +
213 "<TimeStampOutputFormat>sssssss.ssssss</TimeStampOutputFormat>\n" +
215 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
216 "<RegEx>^[([0-9]*\\.[0.9]*)]\\s(.*)</RegEx>\n" +
217 "<InputData action=\"0\" format=\"sssss.sssss\" name=\"Time Stamp\"/>\n" +
218 "<InputData action=\"0\" name=\"Message\"/>\n" +
220 "<OutputColumn name=\"Time Stamp\"/>\n" +
221 "<OutputColumn name=\"Message\"/>\n" +
223 "</CustomTxtTraceDefinitionList>";
224 fw
.write(xmlContent
);
227 SWTBotUtil
.createProject(PROJECT_NAME
);
228 SWTBotView proejctExplorerBot
= fBot
.viewByTitle("Project Explorer");
229 proejctExplorerBot
.show();
230 SWTBotTreeItem treeItem
= proejctExplorerBot
.bot().tree().getTreeItem(PROJECT_NAME
);
233 SWTBotTreeItem treeNode
= null;
234 for (String node
: treeItem
.getNodes()) {
235 if (node
.startsWith("Trace")) {
236 treeNode
= treeItem
.getNode(node
);
241 assertNotNull(treeNode
);
242 treeNode
.contextMenu("Manage Custom Parsers...").click();
243 fBot
.waitUntil(Conditions
.shellIsActive(MANAGE_CUSTOM_PARSERS_SHELL_TITLE
));
244 fBot
.list().select(fBot
.list().getItems()[0]);
245 fBot
.button("Edit...").click();
247 fBot
.textWithLabel("Log type:").setText(PROJECT_NAME
);
248 fBot
.textWithLabel("Time Stamp format:").setText("ss");
249 fBot
.comboBox(1).setSelection("Time Stamp");
250 fBot
.textWithLabel("format:").setText("ss");
251 fBot
.button(2).click();
252 SWTBotTreeItem
[] treeItems
= fBot
.tree().getAllItems();
253 SWTBotTreeItem eventLine
[] = new SWTBotTreeItem
[2];
254 for (SWTBotTreeItem item
: treeItems
) {
255 if (item
.getText().startsWith("Root Line 1")) {
258 if (item
.getText().startsWith("Root Line 2")) {
262 treeItems
= fBot
.tree().getAllItems();
263 assertNotNull(eventLine
[0]);
264 assertNotNull(eventLine
[1]);
265 fBot
.styledText().setText("12 Hello\nWorld\n23 Goodbye\ncruel world");
266 eventLine
[0].select();
267 SWTBotUtil
.waitForJobs();
268 fBot
.textWithLabel("Regular expression:").setText("\\s*(\\d\\d)\\s(.*\\S)");
269 eventLine
[1].select();
270 fBot
.textWithLabel("Regular expression:").setText("([^0-9]*)");
271 fBot
.button(7).click();
272 fBot
.comboBox("Set").setSelection("Append with |");
273 fBot
.button("Highlight All").click();
274 fBot
.button("Next >").click();
275 fBot
.button("Finish").click();
276 fBot
.list().select(PROJECT_NAME
);
277 String xmlPart
= extractTestXml(xmlFile
, PROJECT_NAME
);
278 assertEquals(EXPECTED_TEST_DEFINITION
, xmlPart
);
279 fBot
.button("Delete").click();
280 fBot
.button("Yes").click();
281 fBot
.button("Close").click();
282 xmlPart
= extractTestXml(xmlFile
, PROJECT_NAME
);
283 assertEquals("", xmlPart
);
286 private static String
extractTestXml(File xmlFile
, String definitionName
) throws IOException
, FileNotFoundException
{
287 StringBuilder xmlPart
= new StringBuilder();
288 boolean started
= false;
289 try (RandomAccessFile raf
= new RandomAccessFile(xmlFile
, "r");) {
290 String s
= raf
.readLine();
292 if (s
.equals("<Definition name=\"" + definitionName
+ "\">")) {
296 if (s
.equals("</Definition>")) {
300 xmlPart
.append('\n');
305 return xmlPart
.toString();