93d8912458c74f808d6173579a665784b98840a3
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / src / org / eclipse / tracecompass / tmf / ui / swtbot / tests / parsers / custom / AbstractCustomParserWizard.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 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.tracecompass.tmf.ui.swtbot.tests.parsers.custom;
14
15 import java.io.File;
16 import java.io.FileNotFoundException;
17 import java.io.IOException;
18
19 import org.apache.log4j.ConsoleAppender;
20 import org.apache.log4j.Logger;
21 import org.apache.log4j.SimpleLayout;
22 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
23 import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
24 import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
25 import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile;
26 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
27 import org.junit.AfterClass;
28 import org.junit.BeforeClass;
29
30 /**
31 * Abstract custom parser
32 *
33 * @author Matthew Khouzam
34 */
35 public class AbstractCustomParserWizard {
36
37 /** The Log4j logger instance. */
38 private static final Logger fLogger = Logger.getRootLogger();
39
40 /**
41 * The SWTBot running the test
42 */
43 protected static SWTWorkbenchBot fBot;
44
45 /** Test Class setup */
46 @BeforeClass
47 public static void init() {
48 SWTBotUtils.initialize();
49 Thread.currentThread().setName("SWTBot Thread"); // for the debugger
50 /* set up for swtbot */
51 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
52 fLogger.removeAllAppenders();
53 fLogger.addAppender(new ConsoleAppender(new SimpleLayout()));
54 fBot = new SWTWorkbenchBot();
55
56 SWTBotUtils.closeView("welcome", fBot);
57
58 SWTBotUtils.switchToTracingPerspective();
59 /* finish waiting for eclipse to load */
60 SWTBotUtils.waitForJobs();
61 }
62
63 /**
64 * Test Class teardown
65 */
66 @AfterClass
67 public static void terminate() {
68 fLogger.removeAllAppenders();
69 }
70
71 /**
72 * Extract test XML file
73 *
74 * @param xmlFile
75 * the XML file to open
76 * @param category
77 * the category of the parser
78 * @param definitionName
79 * the name of the definition
80 * @return an XML string of the definition
81 * @throws IOException
82 * Error reading the file
83 * @throws FileNotFoundException
84 * File is not found
85 */
86 protected static String extractTestXml(File xmlFile, String category, String definitionName) throws IOException, FileNotFoundException {
87 StringBuilder xmlPart = new StringBuilder();
88 boolean started = false;
89 try (BufferedRandomAccessFile raf = new BufferedRandomAccessFile(xmlFile, "r");) {
90 String s = raf.readLine();
91 while (s != null) {
92 if (s.equals("<Definition category=\"" + category + "\" name=\"" + definitionName + "\">")) {
93 started = true;
94 }
95 if (started) {
96 if (s.equals("</Definition>")) {
97 break;
98 }
99 xmlPart.append(s);
100 xmlPart.append('\n');
101 }
102 s = raf.readLine();
103 }
104 }
105 return xmlPart.toString();
106 }
107
108 /**
109 * Waits until the XML file containing custom parser defintions contains the
110 * expected content for the specified trace type
111 */
112 protected static class CustomDefinitionHasContent extends DefaultCondition {
113
114 private final File fDefinitionFile;
115 private final String fCategoryName;
116 private final String fTypeName;
117 private final String fExpectedContent;
118
119 /**
120 * Creates a condition that waits until the XML file hast the expected
121 * content.
122 *
123 * @param definitionFile
124 * the XML definition file
125 * @param categoryName
126 * the category name
127 * @param typeName
128 * the trace type name
129 * @param expectedContent
130 * the expected content
131 */
132 protected CustomDefinitionHasContent(File definitionFile, String categoryName, String typeName, String expectedContent) {
133 fDefinitionFile = definitionFile;
134 fCategoryName = categoryName;
135 fTypeName = typeName;
136 fExpectedContent = expectedContent;
137 }
138
139 @Override
140 public boolean test() throws Exception {
141 return extractTestXml(fDefinitionFile, fCategoryName, fTypeName).equals(fExpectedContent);
142 }
143
144 @Override
145 public String getFailureMessage() {
146 return "The file " +fDefinitionFile + " did not contain expected content for " + fCategoryName + ":" + fTypeName + ", Expected:" + fExpectedContent;
147 }
148 }
149 }
This page took 0.035357 seconds and 4 git commands to generate.