analysis: combine nested ifs and merge exception handling
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / parsers / custom / TestCustomTxtWizard.java
CommitLineData
0ae97cfb 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2014, 2015 Ericsson
0ae97cfb
MK
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
fa24d78b 13package org.eclipse.tracecompass.tmf.ui.swtbot.tests.parsers.custom;
0ae97cfb
MK
14
15import static org.junit.Assert.assertEquals;
16import static org.junit.Assert.assertNotNull;
17
18import java.io.File;
19import java.io.FileNotFoundException;
20import java.io.FileWriter;
21import java.io.IOException;
0ae97cfb 22
0ae97cfb 23import org.eclipse.core.resources.ResourcesPlugin;
0ae97cfb 24import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
cbbd323f 25import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
0ae97cfb 26import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
fa24d78b 27import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
0ae97cfb 28import org.junit.Test;
cbbd323f 29import org.junit.runner.RunWith;
0ae97cfb
MK
30
31/**
32 * Custom text wizard tests
33 *
34 * Some reminders to help making tests (javadoc to keep formatting)
35 *
36 * Button reminder
37 *
38 * <pre>
39 * 0 Time Stamp Format Help
40 * 1 Remove line
41 * 2 Add next line
42 * 3 Add child line
43 * 4 Move up
44 * 5 Move down
45 * 6 Regular Expression Help
46 * 7 Remove group (group 1 toggle)
47 * 8 Remove group (group 2 toggle)
48 * 9 Add group (group 3 toggle ...)
49 * 10 Show parsing result
50 * 11 Preview Legend
51 * </pre>
52 *
53 * Combo box reminder
54 *
55 * <pre>
56 * 0 cardinality
57 * 1 event type (message, timestamp...)
58 * 2 how to handle the data (set, append...)
59 * repeat
60 * </pre>
61 *
62 * @author Matthew Khouzam
0ae97cfb 63 */
cbbd323f 64@RunWith(SWTBotJunit4ClassRunner.class)
41a0d150 65public class TestCustomTxtWizard extends AbstractCustomParserWizard {
0ae97cfb 66
d3d0f69a 67 private static final String MANAGE_CUSTOM_PARSERS_SHELL_TITLE = "Manage Custom Parsers";
41a0d150 68 private static final String PROJECT_NAME = "TestText";
c22ca172
PT
69 private static final String CATEGORY_NAME = "Test Category";
70 private static final String TRACETYPE_NAME = "Test Trace";
71 private static final String EXPECTED_TEST_DEFINITION = "<Definition category=\"Test Category\" name=\"Test Trace\">\n" +
0ae97cfb
MK
72 "<TimeStampOutputFormat>ss</TimeStampOutputFormat>\n" +
73 "<InputLine>\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" +
78 "</InputLine>\n" +
79 "<InputLine>\n" +
80 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
81 "<RegEx>([^0-9]*)</RegEx>\n" +
82 "<InputData action=\"2\" name=\"Message\"/>\n" +
83 "</InputLine>\n" +
84 "<OutputColumn name=\"Time Stamp\"/>\n" +
85 "<OutputColumn name=\"Message\"/>\n";
86
0ae97cfb
MK
87 /**
88 * Test to create a custom txt trace and compare the xml
89 *
90 * @throws IOException
91 * the xml file is not accessible, this is bad
92 * @throws FileNotFoundException
93 * the xml file wasn't written, this is bad
94 */
95 @Test
96 public void testNew() throws FileNotFoundException, IOException {
c77a695a 97 File xmlFile = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(".metadata/.plugins/org.eclipse.tracecompass.tmf.core/custom_txt_parsers.xml").toFile();
fa24d78b 98 SWTBotUtils.createProject(PROJECT_NAME);
0ae97cfb
MK
99 SWTBotView proejctExplorerBot = fBot.viewByTitle("Project Explorer");
100 proejctExplorerBot.show();
101 SWTBotTreeItem treeItem = proejctExplorerBot.bot().tree().getTreeItem(PROJECT_NAME);
102 treeItem.select();
103 treeItem.expand();
104 SWTBotTreeItem treeNode = null;
105 for (String node : treeItem.getNodes()) {
106 if (node.startsWith("Trace")) {
107 treeNode = treeItem.getNode(node);
108 break;
109 }
110
111 }
112 assertNotNull(treeNode);
113 treeNode.contextMenu("Manage Custom Parsers...").click();
8af624a8 114 fBot.shell(MANAGE_CUSTOM_PARSERS_SHELL_TITLE).setFocus();
fca952c3 115
0ae97cfb
MK
116 fBot.button("New...").click();
117
c22ca172
PT
118 fBot.textWithLabel("Category:").setText(CATEGORY_NAME);
119 fBot.textWithLabel("Trace type:").setText(TRACETYPE_NAME);
0ae97cfb
MK
120 fBot.textWithLabel("Time Stamp format:").setText("ss");
121 fBot.comboBox(1).setSelection("Time Stamp");
122 fBot.textWithLabel("format:").setText("ss");
123 fBot.button(8).click();
124 fBot.button(2).click();
125 SWTBotTreeItem[] treeItems = fBot.tree().getAllItems();
126 SWTBotTreeItem eventLine[] = new SWTBotTreeItem[2];
127 treeItems = fBot.tree().getAllItems();
128 for (SWTBotTreeItem item : treeItems) {
129 if (item.getText().startsWith("Root Line 1")) {
130 eventLine[0] = item;
131 }
132 if (item.getText().startsWith("Root Line 2")) {
133 eventLine[1] = item;
134 }
135 }
136 assertNotNull(eventLine[0]);
137 assertNotNull(eventLine[1]);
138 fBot.styledText().setText("12 Hello\nWorld\n23 Goodbye\ncruel world");
139 eventLine[0].select();
fa24d78b 140 SWTBotUtils.waitForJobs();
0ae97cfb
MK
141 fBot.textWithLabel("Regular expression:").setText("\\s*(\\d\\d)\\s(.*\\S)");
142 eventLine[1].select();
143 fBot.textWithLabel("Regular expression:").setText("([^0-9]*)");
144 fBot.button(7).click();
145 fBot.comboBox("Set").setSelection("Append with |");
146 fBot.button("Highlight All").click();
147 fBot.button("Next >").click();
148 fBot.button("Finish").click();
c22ca172 149 String xmlPart = extractTestXml(xmlFile, CATEGORY_NAME, TRACETYPE_NAME);
0ae97cfb 150 assertEquals(EXPECTED_TEST_DEFINITION, xmlPart);
c22ca172 151 fBot.list().select(CATEGORY_NAME + " : " + TRACETYPE_NAME);
0ae97cfb
MK
152 fBot.button("Delete").click();
153 fBot.button("Yes").click();
154 fBot.button("Close").click();
c22ca172 155 xmlPart = extractTestXml(xmlFile, CATEGORY_NAME, TRACETYPE_NAME);
0ae97cfb 156 assertEquals("", xmlPart);
93c91230 157
fa24d78b 158 SWTBotUtils.deleteProject(PROJECT_NAME, fBot);
0ae97cfb
MK
159 }
160
161 /**
162 * Test to edit a custom txt trace and compare the xml
163 *
164 * @throws IOException
165 * the xml file is not accessible, this is bad
166 * @throws FileNotFoundException
167 * the xml file wasn't written, this is bad
168 */
169 @Test
170 public void testEdit() throws FileNotFoundException, IOException {
c77a695a 171 File xmlFile = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(".metadata/.plugins/org.eclipse.tracecompass.tmf.core/custom_txt_parsers.xml").toFile();
0ae97cfb
MK
172 try (FileWriter fw = new FileWriter(xmlFile)) {
173 String xmlContent = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
174 "<CustomTxtTraceDefinitionList>\n" +
c22ca172 175 "<Definition category=\"Demo Category\" name=\"Demo trace\">\n" +
0ae97cfb
MK
176 "<TimeStampOutputFormat>sss</TimeStampOutputFormat>\n" +
177 "<InputLine>\n" +
178 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
179 "<RegEx>\\s*(\\d*)\\s(.*)</RegEx>\n" +
180 "<InputData action=\"0\" format=\"sss\" name=\"Time Stamp\"/>\n" +
181 "<InputData action=\"0\" name=\"Message\"/>\n" +
182 "</InputLine>\n" +
183 "<OutputColumn name=\"Time Stamp\"/>\n" +
184 "<OutputColumn name=\"Message\"/>\n" +
185 "</Definition>\n" +
186 "<Definition name=\"dmesg\">\n" +
187 "<TimeStampOutputFormat>sssssss.ssssss</TimeStampOutputFormat>\n" +
188 "<InputLine>\n" +
189 "<Cardinality max=\"2147483647\" min=\"0\"/>\n" +
190 "<RegEx>^[([0-9]*\\.[0.9]*)]\\s(.*)</RegEx>\n" +
191 "<InputData action=\"0\" format=\"sssss.sssss\" name=\"Time Stamp\"/>\n" +
192 "<InputData action=\"0\" name=\"Message\"/>\n" +
193 "</InputLine>\n" +
194 "<OutputColumn name=\"Time Stamp\"/>\n" +
195 "<OutputColumn name=\"Message\"/>\n" +
196 "</Definition>\n" +
197 "</CustomTxtTraceDefinitionList>";
198 fw.write(xmlContent);
199 fw.flush();
200 }
fa24d78b 201 SWTBotUtils.createProject(PROJECT_NAME);
0ae97cfb
MK
202 SWTBotView proejctExplorerBot = fBot.viewByTitle("Project Explorer");
203 proejctExplorerBot.show();
204 SWTBotTreeItem treeItem = proejctExplorerBot.bot().tree().getTreeItem(PROJECT_NAME);
205 treeItem.select();
206 treeItem.expand();
207 SWTBotTreeItem treeNode = null;
208 for (String node : treeItem.getNodes()) {
209 if (node.startsWith("Trace")) {
210 treeNode = treeItem.getNode(node);
211 break;
212 }
213
214 }
215 assertNotNull(treeNode);
216 treeNode.contextMenu("Manage Custom Parsers...").click();
8af624a8 217 fBot.shell(MANAGE_CUSTOM_PARSERS_SHELL_TITLE).setFocus();
c22ca172 218 fBot.list().select("Demo Category : Demo trace");
0ae97cfb
MK
219 fBot.button("Edit...").click();
220
c22ca172
PT
221 fBot.textWithLabel("Category:").setText(CATEGORY_NAME);
222 fBot.textWithLabel("Trace type:").setText(TRACETYPE_NAME);
0ae97cfb
MK
223 fBot.textWithLabel("Time Stamp format:").setText("ss");
224 fBot.comboBox(1).setSelection("Time Stamp");
225 fBot.textWithLabel("format:").setText("ss");
226 fBot.button(2).click();
227 SWTBotTreeItem[] treeItems = fBot.tree().getAllItems();
228 SWTBotTreeItem eventLine[] = new SWTBotTreeItem[2];
229 for (SWTBotTreeItem item : treeItems) {
230 if (item.getText().startsWith("Root Line 1")) {
231 eventLine[0] = item;
232 }
233 if (item.getText().startsWith("Root Line 2")) {
234 eventLine[1] = item;
235 }
236 }
237 treeItems = fBot.tree().getAllItems();
238 assertNotNull(eventLine[0]);
239 assertNotNull(eventLine[1]);
240 fBot.styledText().setText("12 Hello\nWorld\n23 Goodbye\ncruel world");
241 eventLine[0].select();
fa24d78b 242 SWTBotUtils.waitForJobs();
0ae97cfb
MK
243 fBot.textWithLabel("Regular expression:").setText("\\s*(\\d\\d)\\s(.*\\S)");
244 eventLine[1].select();
245 fBot.textWithLabel("Regular expression:").setText("([^0-9]*)");
246 fBot.button(7).click();
247 fBot.comboBox("Set").setSelection("Append with |");
248 fBot.button("Highlight All").click();
249 fBot.button("Next >").click();
250 fBot.button("Finish").click();
c22ca172 251 String xmlPart = extractTestXml(xmlFile, CATEGORY_NAME, TRACETYPE_NAME);
0ae97cfb 252 assertEquals(EXPECTED_TEST_DEFINITION, xmlPart);
c22ca172 253 fBot.list().select(CATEGORY_NAME + " : " + TRACETYPE_NAME);
0ae97cfb
MK
254 fBot.button("Delete").click();
255 fBot.button("Yes").click();
256 fBot.button("Close").click();
c22ca172 257 xmlPart = extractTestXml(xmlFile, CATEGORY_NAME, TRACETYPE_NAME);
0ae97cfb 258 assertEquals("", xmlPart);
93c91230 259
fa24d78b 260 SWTBotUtils.deleteProject(PROJECT_NAME, fBot);
0ae97cfb 261 }
0ae97cfb 262}
This page took 0.060411 seconds and 5 git commands to generate.