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
.junit
.AfterClass
;
28 import org
.junit
.BeforeClass
;
31 * Abstract custom parser
33 * @author Matthew Khouzam
35 public class AbstractCustomParserWizard
{
37 /** The Log4j logger instance. */
38 private static final Logger fLogger
= Logger
.getRootLogger();
41 * The SWTBot running the test
43 protected static SWTWorkbenchBot fBot
;
45 /** Test Class setup */
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();
56 SWTBotUtils
.closeView("welcome", fBot
);
58 SWTBotUtils
.switchToTracingPerspective();
59 /* finish waiting for eclipse to load */
60 SWTBotUtils
.waitForJobs();
67 public static void terminate() {
68 fLogger
.removeAllAppenders();
72 * Extract test XML file
75 * the XML file to open
77 * the category of the parser
78 * @param definitionName
79 * the name of the definition
80 * @return an XML string of the definition
82 * Error reading the file
83 * @throws FileNotFoundException
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();
92 if (s
.equals("<Definition category=\"" + category
+ "\" name=\"" + definitionName
+ "\">")) {
96 if (s
.equals("</Definition>")) {
100 xmlPart
.append('\n');
105 return xmlPart
.toString();
109 * Waits until the XML file containing custom parser defintions contains the
110 * expected content for the specified trace type
112 protected static class CustomDefinitionHasContent
extends DefaultCondition
{
114 private final File fDefinitionFile
;
115 private final String fCategoryName
;
116 private final String fTypeName
;
117 private final String fExpectedContent
;
120 * Creates a condition that waits until the XML file hast the expected
123 * @param definitionFile
124 * the XML definition file
125 * @param categoryName
128 * the trace type name
129 * @param expectedContent
130 * the expected content
132 protected CustomDefinitionHasContent(File definitionFile
, String categoryName
, String typeName
, String expectedContent
) {
133 fDefinitionFile
= definitionFile
;
134 fCategoryName
= categoryName
;
135 fTypeName
= typeName
;
136 fExpectedContent
= expectedContent
;
140 public boolean test() throws Exception
{
141 return extractTestXml(fDefinitionFile
, fCategoryName
, fTypeName
).equals(fExpectedContent
);
145 public String
getFailureMessage() {
146 return "The file " +fDefinitionFile
+ " did not contain expected content for " + fCategoryName
+ ":" + fTypeName
+ ", Expected:" + fExpectedContent
;