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