Commit | Line | Data |
---|---|---|
0ae97cfb MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Matthew Khouzam - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.swtbot.tests; | |
14 | ||
15 | import static org.junit.Assert.assertEquals; | |
16 | import static org.junit.Assert.assertNotNull; | |
17 | ||
18 | import java.io.File; | |
19 | import java.io.FileNotFoundException; | |
20 | import java.io.FileWriter; | |
21 | import java.io.IOException; | |
22 | import java.io.RandomAccessFile; | |
23 | ||
fca952c3 | 24 | import org.apache.log4j.ConsoleAppender; |
0ae97cfb | 25 | import org.apache.log4j.Logger; |
fca952c3 | 26 | import org.apache.log4j.SimpleLayout; |
0ae97cfb MK |
27 | import org.eclipse.core.resources.ResourcesPlugin; |
28 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; | |
29 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; | |
cbbd323f | 30 | import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; |
0ae97cfb MK |
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; | |
cbbd323f | 35 | import org.junit.runner.RunWith; |
0ae97cfb MK |
36 | |
37 | /** | |
38 | * Custom text wizard tests | |
39 | * | |
40 | * Some reminders to help making tests (javadoc to keep formatting) | |
41 | * | |
42 | * Button reminder | |
43 | * | |
44 | * <pre> | |
45 | * 0 Time Stamp Format Help | |
46 | * 1 Remove line | |
47 | * 2 Add next line | |
48 | * 3 Add child line | |
49 | * 4 Move up | |
50 | * 5 Move down | |
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 | |
56 | * 11 Preview Legend | |
57 | * </pre> | |
58 | * | |
59 | * Combo box reminder | |
60 | * | |
61 | * <pre> | |
62 | * 0 cardinality | |
63 | * 1 event type (message, timestamp...) | |
64 | * 2 how to handle the data (set, append...) | |
65 | * repeat | |
66 | * </pre> | |
67 | * | |
68 | * @author Matthew Khouzam | |
69 | * | |
70 | */ | |
cbbd323f | 71 | @RunWith(SWTBotJunit4ClassRunner.class) |
0ae97cfb MK |
72 | public class TestCustomTxtWizard { |
73 | ||
d3d0f69a | 74 | private static final String MANAGE_CUSTOM_PARSERS_SHELL_TITLE = "Manage Custom Parsers"; |
0ae97cfb | 75 | private static final String PROJECT_NAME = "Test"; |
c22ca172 PT |
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" + | |
0ae97cfb MK |
79 | "<TimeStampOutputFormat>ss</TimeStampOutputFormat>\n" + |
80 | "<InputLine>\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" + | |
85 | "</InputLine>\n" + | |
86 | "<InputLine>\n" + | |
87 | "<Cardinality max=\"2147483647\" min=\"0\"/>\n" + | |
88 | "<RegEx>([^0-9]*)</RegEx>\n" + | |
89 | "<InputData action=\"2\" name=\"Message\"/>\n" + | |
90 | "</InputLine>\n" + | |
91 | "<OutputColumn name=\"Time Stamp\"/>\n" + | |
92 | "<OutputColumn name=\"Message\"/>\n"; | |
93 | ||
94 | /** The Log4j logger instance. */ | |
95 | private static final Logger fLogger = Logger.getRootLogger(); | |
96 | private static SWTWorkbenchBot fBot; | |
97 | ||
98 | /** Test Class setup */ | |
99 | @BeforeClass | |
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 */ | |
fca952c3 | 105 | fLogger.addAppender(new ConsoleAppender(new SimpleLayout())); |
0ae97cfb MK |
106 | fBot = new SWTWorkbenchBot(); |
107 | ||
108 | SWTBotUtil.closeView("welcome", fBot); | |
109 | ||
110 | SWTBotUtil.switchToTracingPerspective(); | |
111 | /* finish waiting for eclipse to load */ | |
112 | SWTBotUtil.waitForJobs(); | |
113 | ||
114 | } | |
115 | ||
116 | /** | |
117 | * Test to create a custom txt trace and compare the xml | |
118 | * | |
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 | |
123 | */ | |
124 | @Test | |
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); | |
131 | treeItem.select(); | |
132 | treeItem.expand(); | |
133 | SWTBotTreeItem treeNode = null; | |
134 | for (String node : treeItem.getNodes()) { | |
135 | if (node.startsWith("Trace")) { | |
136 | treeNode = treeItem.getNode(node); | |
137 | break; | |
138 | } | |
139 | ||
140 | } | |
141 | assertNotNull(treeNode); | |
142 | treeNode.contextMenu("Manage Custom Parsers...").click(); | |
8af624a8 | 143 | fBot.shell(MANAGE_CUSTOM_PARSERS_SHELL_TITLE).setFocus(); |
fca952c3 | 144 | |
0ae97cfb MK |
145 | fBot.button("New...").click(); |
146 | ||
c22ca172 PT |
147 | fBot.textWithLabel("Category:").setText(CATEGORY_NAME); |
148 | fBot.textWithLabel("Trace type:").setText(TRACETYPE_NAME); | |
0ae97cfb MK |
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")) { | |
159 | eventLine[0] = item; | |
160 | } | |
161 | if (item.getText().startsWith("Root Line 2")) { | |
162 | eventLine[1] = item; | |
163 | } | |
164 | } | |
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(); | |
c22ca172 | 178 | String xmlPart = extractTestXml(xmlFile, CATEGORY_NAME, TRACETYPE_NAME); |
0ae97cfb | 179 | assertEquals(EXPECTED_TEST_DEFINITION, xmlPart); |
c22ca172 | 180 | fBot.list().select(CATEGORY_NAME + " : " + TRACETYPE_NAME); |
0ae97cfb MK |
181 | fBot.button("Delete").click(); |
182 | fBot.button("Yes").click(); | |
183 | fBot.button("Close").click(); | |
c22ca172 | 184 | xmlPart = extractTestXml(xmlFile, CATEGORY_NAME, TRACETYPE_NAME); |
0ae97cfb | 185 | assertEquals("", xmlPart); |
93c91230 MAL |
186 | |
187 | SWTBotUtil.deleteProject(PROJECT_NAME, fBot); | |
0ae97cfb MK |
188 | } |
189 | ||
190 | /** | |
191 | * Test to edit a custom txt trace and compare the xml | |
192 | * | |
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 | |
197 | */ | |
198 | @Test | |
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" + | |
c22ca172 | 204 | "<Definition category=\"Demo Category\" name=\"Demo trace\">\n" + |
0ae97cfb MK |
205 | "<TimeStampOutputFormat>sss</TimeStampOutputFormat>\n" + |
206 | "<InputLine>\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" + | |
211 | "</InputLine>\n" + | |
212 | "<OutputColumn name=\"Time Stamp\"/>\n" + | |
213 | "<OutputColumn name=\"Message\"/>\n" + | |
214 | "</Definition>\n" + | |
215 | "<Definition name=\"dmesg\">\n" + | |
216 | "<TimeStampOutputFormat>sssssss.ssssss</TimeStampOutputFormat>\n" + | |
217 | "<InputLine>\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" + | |
222 | "</InputLine>\n" + | |
223 | "<OutputColumn name=\"Time Stamp\"/>\n" + | |
224 | "<OutputColumn name=\"Message\"/>\n" + | |
225 | "</Definition>\n" + | |
226 | "</CustomTxtTraceDefinitionList>"; | |
227 | fw.write(xmlContent); | |
228 | fw.flush(); | |
229 | } | |
230 | SWTBotUtil.createProject(PROJECT_NAME); | |
231 | SWTBotView proejctExplorerBot = fBot.viewByTitle("Project Explorer"); | |
232 | proejctExplorerBot.show(); | |
233 | SWTBotTreeItem treeItem = proejctExplorerBot.bot().tree().getTreeItem(PROJECT_NAME); | |
234 | treeItem.select(); | |
235 | treeItem.expand(); | |
236 | SWTBotTreeItem treeNode = null; | |
237 | for (String node : treeItem.getNodes()) { | |
238 | if (node.startsWith("Trace")) { | |
239 | treeNode = treeItem.getNode(node); | |
240 | break; | |
241 | } | |
242 | ||
243 | } | |
244 | assertNotNull(treeNode); | |
245 | treeNode.contextMenu("Manage Custom Parsers...").click(); | |
8af624a8 | 246 | fBot.shell(MANAGE_CUSTOM_PARSERS_SHELL_TITLE).setFocus(); |
c22ca172 | 247 | fBot.list().select("Demo Category : Demo trace"); |
0ae97cfb MK |
248 | fBot.button("Edit...").click(); |
249 | ||
c22ca172 PT |
250 | fBot.textWithLabel("Category:").setText(CATEGORY_NAME); |
251 | fBot.textWithLabel("Trace type:").setText(TRACETYPE_NAME); | |
0ae97cfb MK |
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")) { | |
260 | eventLine[0] = item; | |
261 | } | |
262 | if (item.getText().startsWith("Root Line 2")) { | |
263 | eventLine[1] = item; | |
264 | } | |
265 | } | |
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(); | |
c22ca172 | 280 | String xmlPart = extractTestXml(xmlFile, CATEGORY_NAME, TRACETYPE_NAME); |
0ae97cfb | 281 | assertEquals(EXPECTED_TEST_DEFINITION, xmlPart); |
c22ca172 | 282 | fBot.list().select(CATEGORY_NAME + " : " + TRACETYPE_NAME); |
0ae97cfb MK |
283 | fBot.button("Delete").click(); |
284 | fBot.button("Yes").click(); | |
285 | fBot.button("Close").click(); | |
c22ca172 | 286 | xmlPart = extractTestXml(xmlFile, CATEGORY_NAME, TRACETYPE_NAME); |
0ae97cfb | 287 | assertEquals("", xmlPart); |
93c91230 MAL |
288 | |
289 | SWTBotUtil.deleteProject(PROJECT_NAME, fBot); | |
0ae97cfb MK |
290 | } |
291 | ||
332527a4 | 292 | private static String extractTestXml(File xmlFile, String category, String definitionName) throws IOException, FileNotFoundException { |
0ae97cfb MK |
293 | StringBuilder xmlPart = new StringBuilder(); |
294 | boolean started = false; | |
295 | try (RandomAccessFile raf = new RandomAccessFile(xmlFile, "r");) { | |
296 | String s = raf.readLine(); | |
297 | while (s != null) { | |
332527a4 | 298 | if (s.equals("<Definition category=\"" + category + "\" name=\"" + definitionName + "\">")) { |
0ae97cfb MK |
299 | started = true; |
300 | } | |
301 | if (started) { | |
302 | if (s.equals("</Definition>")) { | |
303 | break; | |
304 | } | |
305 | xmlPart.append(s); | |
306 | xmlPart.append('\n'); | |
307 | } | |
308 | s = raf.readLine(); | |
309 | } | |
310 | } | |
311 | return xmlPart.toString(); | |
312 | } | |
313 | } |