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
.Test
;
35 * Custom text wizard tests
37 * Some reminders to help making tests (javadoc to keep formatting)
42 * 0 Time Stamp Format Help
48 * 6 Regular Expression Help
49 * 7 Remove group (group 1 toggle)
50 * 8 Remove group (group 2 toggle)
51 * 9 Add group (group 3 toggle ...)
52 * 10 Show parsing result
60 * 1 event type (message, timestamp...)
61 * 2 how to handle the data (set, append...)
65 * @author Matthew Khouzam
68 public class TestCustomTxtWizard
{
70 private static final String PROJECT_NAME
= "Test";
71 private static final String EXPECTED_TEST_DEFINITION
= "<Definition name=\"Test\">\n" +
72 "<TimeStampOutputFormat>ss</TimeStampOutputFormat>\n" +
74 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
75 "<RegEx>\\s*(\\d\\d)\\s(.*\\S)</RegEx>\n" +
76 "<InputData action=\"0\" format=\"ss\" name=\"Time Stamp\"/>\n" +
77 "<InputData action=\"0\" name=\"Message\"/>\n" +
80 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
81 "<RegEx>([^0-9]*)</RegEx>\n" +
82 "<InputData action=\"2\" name=\"Message\"/>\n" +
84 "<OutputColumn name=\"Time Stamp\"/>\n" +
85 "<OutputColumn name=\"Message\"/>\n";
87 /** The Log4j logger instance. */
88 private static final Logger fLogger
= Logger
.getRootLogger();
89 private static SWTWorkbenchBot fBot
;
91 /** Test Class setup */
93 public static void init() {
94 SWTBotUtil
.failIfUIThread();
95 Thread
.currentThread().setName("SWTBot Thread"); // for the debugger
96 /* set up for swtbot */
97 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
98 fLogger
.addAppender(new NullAppender());
99 fBot
= new SWTWorkbenchBot();
101 SWTBotUtil
.closeView("welcome", fBot
);
103 SWTBotUtil
.switchToTracingPerspective();
104 /* finish waiting for eclipse to load */
105 SWTBotUtil
.waitForJobs();
110 * Test to create a custom txt trace and compare the xml
112 * @throws IOException
113 * the xml file is not accessible, this is bad
114 * @throws FileNotFoundException
115 * the xml file wasn't written, this is bad
118 public void testNew() throws FileNotFoundException
, IOException
{
119 File xmlFile
= ResourcesPlugin
.getWorkspace().getRoot().getLocation().append(".metadata/.plugins/org.eclipse.linuxtools.tmf.core/custom_txt_parsers.xml").toFile();
120 SWTBotUtil
.createProject(PROJECT_NAME
);
121 SWTBotView proejctExplorerBot
= fBot
.viewByTitle("Project Explorer");
122 proejctExplorerBot
.show();
123 SWTBotTreeItem treeItem
= proejctExplorerBot
.bot().tree().getTreeItem(PROJECT_NAME
);
126 SWTBotTreeItem treeNode
= null;
127 for (String node
: treeItem
.getNodes()) {
128 if (node
.startsWith("Trace")) {
129 treeNode
= treeItem
.getNode(node
);
134 assertNotNull(treeNode
);
135 treeNode
.contextMenu("Manage Custom Parsers...").click();
136 fBot
.button("New...").click();
138 fBot
.textWithLabel("Log type:").setText(PROJECT_NAME
);
139 fBot
.textWithLabel("Time Stamp format:").setText("ss");
140 fBot
.comboBox(1).setSelection("Time Stamp");
141 fBot
.textWithLabel("format:").setText("ss");
142 fBot
.button(8).click();
143 fBot
.button(2).click();
144 SWTBotTreeItem
[] treeItems
= fBot
.tree().getAllItems();
145 SWTBotTreeItem eventLine
[] = new SWTBotTreeItem
[2];
146 treeItems
= fBot
.tree().getAllItems();
147 for (SWTBotTreeItem item
: treeItems
) {
148 if (item
.getText().startsWith("Root Line 1")) {
151 if (item
.getText().startsWith("Root Line 2")) {
155 assertNotNull(eventLine
[0]);
156 assertNotNull(eventLine
[1]);
157 fBot
.styledText().setText("12 Hello\nWorld\n23 Goodbye\ncruel world");
158 eventLine
[0].select();
159 SWTBotUtil
.waitForJobs();
160 fBot
.textWithLabel("Regular expression:").setText("\\s*(\\d\\d)\\s(.*\\S)");
161 eventLine
[1].select();
162 fBot
.textWithLabel("Regular expression:").setText("([^0-9]*)");
163 fBot
.button(7).click();
164 fBot
.comboBox("Set").setSelection("Append with |");
165 fBot
.button("Highlight All").click();
166 fBot
.button("Next >").click();
167 fBot
.button("Finish").click();
168 fBot
.list().select(PROJECT_NAME
);
169 String xmlPart
= extractTestXml(xmlFile
, PROJECT_NAME
);
170 assertEquals(EXPECTED_TEST_DEFINITION
, xmlPart
);
171 fBot
.button("Delete").click();
172 fBot
.button("Yes").click();
173 fBot
.button("Close").click();
174 xmlPart
= extractTestXml(xmlFile
, PROJECT_NAME
);
175 assertEquals("", xmlPart
);
179 * Test to edit a custom txt trace and compare the xml
181 * @throws IOException
182 * the xml file is not accessible, this is bad
183 * @throws FileNotFoundException
184 * the xml file wasn't written, this is bad
187 public void testEdit() throws FileNotFoundException
, IOException
{
188 File xmlFile
= ResourcesPlugin
.getWorkspace().getRoot().getLocation().append(".metadata/.plugins/org.eclipse.linuxtools.tmf.core/custom_txt_parsers.xml").toFile();
189 try (FileWriter fw
= new FileWriter(xmlFile
)) {
190 String xmlContent
= "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
191 "<CustomTxtTraceDefinitionList>\n" +
192 "<Definition name=\"Demo trace\">\n" +
193 "<TimeStampOutputFormat>sss</TimeStampOutputFormat>\n" +
195 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
196 "<RegEx>\\s*(\\d*)\\s(.*)</RegEx>\n" +
197 "<InputData action=\"0\" format=\"sss\" name=\"Time Stamp\"/>\n" +
198 "<InputData action=\"0\" name=\"Message\"/>\n" +
200 "<OutputColumn name=\"Time Stamp\"/>\n" +
201 "<OutputColumn name=\"Message\"/>\n" +
203 "<Definition name=\"dmesg\">\n" +
204 "<TimeStampOutputFormat>sssssss.ssssss</TimeStampOutputFormat>\n" +
206 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
207 "<RegEx>^[([0-9]*\\.[0.9]*)]\\s(.*)</RegEx>\n" +
208 "<InputData action=\"0\" format=\"sssss.sssss\" name=\"Time Stamp\"/>\n" +
209 "<InputData action=\"0\" name=\"Message\"/>\n" +
211 "<OutputColumn name=\"Time Stamp\"/>\n" +
212 "<OutputColumn name=\"Message\"/>\n" +
214 "</CustomTxtTraceDefinitionList>";
215 fw
.write(xmlContent
);
218 SWTBotUtil
.createProject(PROJECT_NAME
);
219 SWTBotView proejctExplorerBot
= fBot
.viewByTitle("Project Explorer");
220 proejctExplorerBot
.show();
221 SWTBotTreeItem treeItem
= proejctExplorerBot
.bot().tree().getTreeItem(PROJECT_NAME
);
224 SWTBotTreeItem treeNode
= null;
225 for (String node
: treeItem
.getNodes()) {
226 if (node
.startsWith("Trace")) {
227 treeNode
= treeItem
.getNode(node
);
232 assertNotNull(treeNode
);
233 treeNode
.contextMenu("Manage Custom Parsers...").click();
234 fBot
.list().select(fBot
.list().getItems()[0]);
235 fBot
.button("Edit...").click();
237 fBot
.textWithLabel("Log type:").setText(PROJECT_NAME
);
238 fBot
.textWithLabel("Time Stamp format:").setText("ss");
239 fBot
.comboBox(1).setSelection("Time Stamp");
240 fBot
.textWithLabel("format:").setText("ss");
241 fBot
.button(2).click();
242 SWTBotTreeItem
[] treeItems
= fBot
.tree().getAllItems();
243 SWTBotTreeItem eventLine
[] = new SWTBotTreeItem
[2];
244 for (SWTBotTreeItem item
: treeItems
) {
245 if (item
.getText().startsWith("Root Line 1")) {
248 if (item
.getText().startsWith("Root Line 2")) {
252 treeItems
= fBot
.tree().getAllItems();
253 assertNotNull(eventLine
[0]);
254 assertNotNull(eventLine
[1]);
255 fBot
.styledText().setText("12 Hello\nWorld\n23 Goodbye\ncruel world");
256 eventLine
[0].select();
257 SWTBotUtil
.waitForJobs();
258 fBot
.textWithLabel("Regular expression:").setText("\\s*(\\d\\d)\\s(.*\\S)");
259 eventLine
[1].select();
260 fBot
.textWithLabel("Regular expression:").setText("([^0-9]*)");
261 fBot
.button(7).click();
262 fBot
.comboBox("Set").setSelection("Append with |");
263 fBot
.button("Highlight All").click();
264 fBot
.button("Next >").click();
265 fBot
.button("Finish").click();
266 fBot
.list().select(PROJECT_NAME
);
267 String xmlPart
= extractTestXml(xmlFile
, PROJECT_NAME
);
268 assertEquals(EXPECTED_TEST_DEFINITION
, xmlPart
);
269 fBot
.button("Delete").click();
270 fBot
.button("Yes").click();
271 fBot
.button("Close").click();
272 xmlPart
= extractTestXml(xmlFile
, PROJECT_NAME
);
273 assertEquals("", xmlPart
);
276 private static String
extractTestXml(File xmlFile
, String definitionName
) throws IOException
, FileNotFoundException
{
277 StringBuilder xmlPart
= new StringBuilder();
278 boolean started
= false;
279 try (RandomAccessFile raf
= new RandomAccessFile(xmlFile
, "r");) {
280 String s
= raf
.readLine();
282 if (s
.equals("<Definition name=\"" + definitionName
+ "\">")) {
286 if (s
.equals("</Definition>")) {
290 xmlPart
.append('\n');
295 return xmlPart
.toString();