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
.widgets
.SWTBotTreeItem
;
31 import org
.junit
.BeforeClass
;
32 import org
.junit
.Ignore
;
33 import org
.junit
.Test
;
36 * Custom text wizard tests
38 * Some reminders to help making tests (javadoc to keep formatting)
43 * 0 Time Stamp Format Help
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
61 * 1 event type (message, timestamp...)
62 * 2 how to handle the data (set, append...)
66 * @author Matthew Khouzam
69 public class TestCustomTxtWizard
{
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" +
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" +
81 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
82 "<RegEx>([^0-9]*)</RegEx>\n" +
83 "<InputData action=\"2\" name=\"Message\"/>\n" +
85 "<OutputColumn name=\"Time Stamp\"/>\n" +
86 "<OutputColumn name=\"Message\"/>\n";
88 /** The Log4j logger instance. */
89 private static final Logger fLogger
= Logger
.getRootLogger();
90 private static SWTWorkbenchBot fBot
;
92 /** Test Class setup */
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();
102 SWTBotUtil
.closeView("welcome", fBot
);
104 SWTBotUtil
.switchToTracingPerspective();
105 /* finish waiting for eclipse to load */
106 SWTBotUtil
.waitForJobs();
111 * Test to create a custom txt trace and compare the xml
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
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
);
128 SWTBotTreeItem treeNode
= null;
129 for (String node
: treeItem
.getNodes()) {
130 if (node
.startsWith("Trace")) {
131 treeNode
= treeItem
.getNode(node
);
136 assertNotNull(treeNode
);
137 treeNode
.contextMenu("Manage Custom Parsers...").click();
138 fBot
.button("New...").click();
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")) {
153 if (item
.getText().startsWith("Root Line 2")) {
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
);
181 * Test to edit a custom txt trace and compare the xml
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
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" +
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" +
203 "<OutputColumn name=\"Time Stamp\"/>\n" +
204 "<OutputColumn name=\"Message\"/>\n" +
206 "<Definition name=\"dmesg\">\n" +
207 "<TimeStampOutputFormat>sssssss.ssssss</TimeStampOutputFormat>\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" +
214 "<OutputColumn name=\"Time Stamp\"/>\n" +
215 "<OutputColumn name=\"Message\"/>\n" +
217 "</CustomTxtTraceDefinitionList>";
218 fw
.write(xmlContent
);
221 SWTBotUtil
.createProject(PROJECT_NAME
);
222 SWTBotView proejctExplorerBot
= fBot
.viewByTitle("Project Explorer");
223 proejctExplorerBot
.show();
224 SWTBotTreeItem treeItem
= proejctExplorerBot
.bot().tree().getTreeItem(PROJECT_NAME
);
227 SWTBotTreeItem treeNode
= null;
228 for (String node
: treeItem
.getNodes()) {
229 if (node
.startsWith("Trace")) {
230 treeNode
= treeItem
.getNode(node
);
235 assertNotNull(treeNode
);
236 treeNode
.contextMenu("Manage Custom Parsers...").click();
237 fBot
.list().select(fBot
.list().getItems()[0]);
238 fBot
.button("Edit...").click();
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")) {
251 if (item
.getText().startsWith("Root Line 2")) {
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
);
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();
285 if (s
.equals("<Definition name=\"" + definitionName
+ "\">")) {
289 if (s
.equals("</Definition>")) {
293 xmlPart
.append('\n');
298 return xmlPart
.toString();