1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 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
.tracecompass
.tmf
.ui
.swtbot
.tests
.parsers
.custom
;
16 import java
.io
.FileNotFoundException
;
17 import java
.io
.IOException
;
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
.eclipse
.tracecompass
.tmf
.ui
.tests
.shared
.WaitUtils
;
28 import org
.junit
.AfterClass
;
29 import org
.junit
.BeforeClass
;
32 * Abstract custom parser
34 * @author Matthew Khouzam
36 public class AbstractCustomParserWizard
{
38 /** The Log4j logger instance. */
39 private static final Logger fLogger
= Logger
.getRootLogger();
42 * The SWTBot running the test
44 protected static SWTWorkbenchBot fBot
;
46 /** Test Class setup */
48 public static void init() {
49 SWTBotUtils
.initialize();
50 Thread
.currentThread().setName("SWTBot Thread"); // for the debugger
51 /* set up for swtbot */
52 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
53 fLogger
.removeAllAppenders();
54 fLogger
.addAppender(new ConsoleAppender(new SimpleLayout()));
55 fBot
= new SWTWorkbenchBot();
57 SWTBotUtils
.closeView("welcome", fBot
);
59 SWTBotUtils
.switchToTracingPerspective();
60 /* finish waiting for eclipse to load */
61 WaitUtils
.waitForJobs();
68 public static void terminate() {
69 fLogger
.removeAllAppenders();
73 * Extract test XML file
76 * the XML file to open
78 * the category of the parser
79 * @param definitionName
80 * the name of the definition
81 * @return an XML string of the definition
83 * Error reading the file
84 * @throws FileNotFoundException
87 protected static String
extractTestXml(File xmlFile
, String category
, String definitionName
) throws IOException
, FileNotFoundException
{
88 StringBuilder xmlPart
= new StringBuilder();
89 boolean started
= false;
90 try (BufferedRandomAccessFile raf
= new BufferedRandomAccessFile(xmlFile
, "r");) {
91 String s
= raf
.readLine();
93 if (s
.equals("<Definition category=\"" + category
+ "\" name=\"" + definitionName
+ "\">")) {
97 if (s
.equals("</Definition>")) {
101 xmlPart
.append('\n');
106 return xmlPart
.toString();
110 * Waits until the XML file containing custom parser defintions contains the
111 * expected content for the specified trace type
113 protected static class CustomDefinitionHasContent
extends DefaultCondition
{
115 private final File fDefinitionFile
;
116 private final String fCategoryName
;
117 private final String fTypeName
;
118 private final String fExpectedContent
;
121 * Creates a condition that waits until the XML file hast the expected
124 * @param definitionFile
125 * the XML definition file
126 * @param categoryName
129 * the trace type name
130 * @param expectedContent
131 * the expected content
133 protected CustomDefinitionHasContent(File definitionFile
, String categoryName
, String typeName
, String expectedContent
) {
134 fDefinitionFile
= definitionFile
;
135 fCategoryName
= categoryName
;
136 fTypeName
= typeName
;
137 fExpectedContent
= expectedContent
;
141 public boolean test() throws Exception
{
142 return extractTestXml(fDefinitionFile
, fCategoryName
, fTypeName
).equals(fExpectedContent
);
146 public String
getFailureMessage() {
147 return "The file " +fDefinitionFile
+ " did not contain expected content for " + fCategoryName
+ ":" + fTypeName
+ ", Expected:" + fExpectedContent
;