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
.utils
.SWTBotPreferences
;
30 import org
.eclipse
.swtbot
.swt
.finder
.waits
.Conditions
;
31 import org
.eclipse
.swtbot
.swt
.finder
.widgets
.SWTBotTreeItem
;
32 import org
.junit
.BeforeClass
;
33 import org
.junit
.Ignore
;
34 import org
.junit
.Test
;
37 * Custom text wizard tests
39 * Some reminders to help making tests (javadoc to keep formatting)
44 * 0 Time Stamp Format Help
50 * 6 Regular Expression Help
51 * 7 Remove group (group 1 toggle)
52 * 8 Remove group (group 2 toggle)
53 * 9 Add group (group 3 toggle ...)
54 * 10 Show parsing result
62 * 1 event type (message, timestamp...)
63 * 2 how to handle the data (set, append...)
67 * @author Matthew Khouzam
70 public class TestCustomTxtWizard
{
72 private static final String MANAGE_CUSTOM_PARSERS_SHELL_TITLE
= "Manage Custom Parsers";
73 private static final String PROJECT_NAME
= "Test";
74 private static final String EXPECTED_TEST_DEFINITION
= "<Definition name=\"Test\">\n" +
75 "<TimeStampOutputFormat>ss</TimeStampOutputFormat>\n" +
77 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
78 "<RegEx>\\s*(\\d\\d)\\s(.*\\S)</RegEx>\n" +
79 "<InputData action=\"0\" format=\"ss\" name=\"Time Stamp\"/>\n" +
80 "<InputData action=\"0\" name=\"Message\"/>\n" +
83 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
84 "<RegEx>([^0-9]*)</RegEx>\n" +
85 "<InputData action=\"2\" name=\"Message\"/>\n" +
87 "<OutputColumn name=\"Time Stamp\"/>\n" +
88 "<OutputColumn name=\"Message\"/>\n";
90 /** The Log4j logger instance. */
91 private static final Logger fLogger
= Logger
.getRootLogger();
92 private static SWTWorkbenchBot fBot
;
94 /** Test Class setup */
96 public static void init() {
97 SWTBotUtil
.failIfUIThread();
98 Thread
.currentThread().setName("SWTBot Thread"); // for the debugger
99 /* set up for swtbot */
100 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
101 fLogger
.addAppender(new NullAppender());
102 fBot
= new SWTWorkbenchBot();
104 SWTBotUtil
.closeView("welcome", fBot
);
106 SWTBotUtil
.switchToTracingPerspective();
107 /* finish waiting for eclipse to load */
108 SWTBotUtil
.waitForJobs();
113 * Test to create a custom txt trace and compare the xml
115 * @throws IOException
116 * the xml file is not accessible, this is bad
117 * @throws FileNotFoundException
118 * the xml file wasn't written, this is bad
122 public void testNew() throws FileNotFoundException
, IOException
{
123 File xmlFile
= ResourcesPlugin
.getWorkspace().getRoot().getLocation().append(".metadata/.plugins/org.eclipse.linuxtools.tmf.core/custom_txt_parsers.xml").toFile();
124 SWTBotUtil
.createProject(PROJECT_NAME
);
125 SWTBotView proejctExplorerBot
= fBot
.viewByTitle("Project Explorer");
126 proejctExplorerBot
.show();
127 SWTBotTreeItem treeItem
= proejctExplorerBot
.bot().tree().getTreeItem(PROJECT_NAME
);
130 SWTBotTreeItem treeNode
= null;
131 for (String node
: treeItem
.getNodes()) {
132 if (node
.startsWith("Trace")) {
133 treeNode
= treeItem
.getNode(node
);
138 assertNotNull(treeNode
);
139 treeNode
.contextMenu("Manage Custom Parsers...").click();
140 fBot
.waitUntil(Conditions
.shellIsActive(MANAGE_CUSTOM_PARSERS_SHELL_TITLE
));
141 fBot
.button("New...").click();
143 fBot
.textWithLabel("Log type:").setText(PROJECT_NAME
);
144 fBot
.textWithLabel("Time Stamp format:").setText("ss");
145 fBot
.comboBox(1).setSelection("Time Stamp");
146 fBot
.textWithLabel("format:").setText("ss");
147 fBot
.button(8).click();
148 fBot
.button(2).click();
149 SWTBotTreeItem
[] treeItems
= fBot
.tree().getAllItems();
150 SWTBotTreeItem eventLine
[] = new SWTBotTreeItem
[2];
151 treeItems
= fBot
.tree().getAllItems();
152 for (SWTBotTreeItem item
: treeItems
) {
153 if (item
.getText().startsWith("Root Line 1")) {
156 if (item
.getText().startsWith("Root Line 2")) {
160 assertNotNull(eventLine
[0]);
161 assertNotNull(eventLine
[1]);
162 fBot
.styledText().setText("12 Hello\nWorld\n23 Goodbye\ncruel world");
163 eventLine
[0].select();
164 SWTBotUtil
.waitForJobs();
165 fBot
.textWithLabel("Regular expression:").setText("\\s*(\\d\\d)\\s(.*\\S)");
166 eventLine
[1].select();
167 fBot
.textWithLabel("Regular expression:").setText("([^0-9]*)");
168 fBot
.button(7).click();
169 fBot
.comboBox("Set").setSelection("Append with |");
170 fBot
.button("Highlight All").click();
171 fBot
.button("Next >").click();
172 fBot
.button("Finish").click();
173 fBot
.list().select(PROJECT_NAME
);
174 String xmlPart
= extractTestXml(xmlFile
, PROJECT_NAME
);
175 assertEquals(EXPECTED_TEST_DEFINITION
, xmlPart
);
176 fBot
.button("Delete").click();
177 fBot
.button("Yes").click();
178 fBot
.button("Close").click();
179 xmlPart
= extractTestXml(xmlFile
, PROJECT_NAME
);
180 assertEquals("", xmlPart
);
184 * Test to edit a custom txt trace and compare the xml
186 * @throws IOException
187 * the xml file is not accessible, this is bad
188 * @throws FileNotFoundException
189 * the xml file wasn't written, this is bad
193 public void testEdit() throws FileNotFoundException
, IOException
{
194 File xmlFile
= ResourcesPlugin
.getWorkspace().getRoot().getLocation().append(".metadata/.plugins/org.eclipse.linuxtools.tmf.core/custom_txt_parsers.xml").toFile();
195 try (FileWriter fw
= new FileWriter(xmlFile
)) {
196 String xmlContent
= "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
197 "<CustomTxtTraceDefinitionList>\n" +
198 "<Definition name=\"Demo trace\">\n" +
199 "<TimeStampOutputFormat>sss</TimeStampOutputFormat>\n" +
201 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
202 "<RegEx>\\s*(\\d*)\\s(.*)</RegEx>\n" +
203 "<InputData action=\"0\" format=\"sss\" name=\"Time Stamp\"/>\n" +
204 "<InputData action=\"0\" name=\"Message\"/>\n" +
206 "<OutputColumn name=\"Time Stamp\"/>\n" +
207 "<OutputColumn name=\"Message\"/>\n" +
209 "<Definition name=\"dmesg\">\n" +
210 "<TimeStampOutputFormat>sssssss.ssssss</TimeStampOutputFormat>\n" +
212 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
213 "<RegEx>^[([0-9]*\\.[0.9]*)]\\s(.*)</RegEx>\n" +
214 "<InputData action=\"0\" format=\"sssss.sssss\" name=\"Time Stamp\"/>\n" +
215 "<InputData action=\"0\" name=\"Message\"/>\n" +
217 "<OutputColumn name=\"Time Stamp\"/>\n" +
218 "<OutputColumn name=\"Message\"/>\n" +
220 "</CustomTxtTraceDefinitionList>";
221 fw
.write(xmlContent
);
224 SWTBotUtil
.createProject(PROJECT_NAME
);
225 SWTBotView proejctExplorerBot
= fBot
.viewByTitle("Project Explorer");
226 proejctExplorerBot
.show();
227 SWTBotTreeItem treeItem
= proejctExplorerBot
.bot().tree().getTreeItem(PROJECT_NAME
);
230 SWTBotTreeItem treeNode
= null;
231 for (String node
: treeItem
.getNodes()) {
232 if (node
.startsWith("Trace")) {
233 treeNode
= treeItem
.getNode(node
);
238 assertNotNull(treeNode
);
239 treeNode
.contextMenu("Manage Custom Parsers...").click();
240 fBot
.waitUntil(Conditions
.shellIsActive(MANAGE_CUSTOM_PARSERS_SHELL_TITLE
));
241 fBot
.list().select(fBot
.list().getItems()[0]);
242 fBot
.button("Edit...").click();
244 fBot
.textWithLabel("Log type:").setText(PROJECT_NAME
);
245 fBot
.textWithLabel("Time Stamp format:").setText("ss");
246 fBot
.comboBox(1).setSelection("Time Stamp");
247 fBot
.textWithLabel("format:").setText("ss");
248 fBot
.button(2).click();
249 SWTBotTreeItem
[] treeItems
= fBot
.tree().getAllItems();
250 SWTBotTreeItem eventLine
[] = new SWTBotTreeItem
[2];
251 for (SWTBotTreeItem item
: treeItems
) {
252 if (item
.getText().startsWith("Root Line 1")) {
255 if (item
.getText().startsWith("Root Line 2")) {
259 treeItems
= fBot
.tree().getAllItems();
260 assertNotNull(eventLine
[0]);
261 assertNotNull(eventLine
[1]);
262 fBot
.styledText().setText("12 Hello\nWorld\n23 Goodbye\ncruel world");
263 eventLine
[0].select();
264 SWTBotUtil
.waitForJobs();
265 fBot
.textWithLabel("Regular expression:").setText("\\s*(\\d\\d)\\s(.*\\S)");
266 eventLine
[1].select();
267 fBot
.textWithLabel("Regular expression:").setText("([^0-9]*)");
268 fBot
.button(7).click();
269 fBot
.comboBox("Set").setSelection("Append with |");
270 fBot
.button("Highlight All").click();
271 fBot
.button("Next >").click();
272 fBot
.button("Finish").click();
273 fBot
.list().select(PROJECT_NAME
);
274 String xmlPart
= extractTestXml(xmlFile
, PROJECT_NAME
);
275 assertEquals(EXPECTED_TEST_DEFINITION
, xmlPart
);
276 fBot
.button("Delete").click();
277 fBot
.button("Yes").click();
278 fBot
.button("Close").click();
279 xmlPart
= extractTestXml(xmlFile
, PROJECT_NAME
);
280 assertEquals("", xmlPart
);
283 private static String
extractTestXml(File xmlFile
, String definitionName
) throws IOException
, FileNotFoundException
{
284 StringBuilder xmlPart
= new StringBuilder();
285 boolean started
= false;
286 try (RandomAccessFile raf
= new RandomAccessFile(xmlFile
, "r");) {
287 String s
= raf
.readLine();
289 if (s
.equals("<Definition name=\"" + definitionName
+ "\">")) {
293 if (s
.equals("</Definition>")) {
297 xmlPart
.append('\n');
302 return xmlPart
.toString();