Commit | Line | Data |
---|---|---|
573087b4 MAL |
1 | /******************************************************************************* |
2 | * Copyright (c) 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 | ||
10 | package org.eclipse.tracecompass.tmf.ui.swtbot.tests.viewers.events; | |
11 | ||
12 | import java.io.File; | |
13 | import java.io.FileWriter; | |
14 | import java.io.IOException; | |
15 | import java.text.SimpleDateFormat; | |
16 | import java.util.Calendar; | |
17 | import java.util.Date; | |
18 | import java.util.GregorianCalendar; | |
19 | ||
20 | import org.apache.log4j.Logger; | |
21 | import org.apache.log4j.varia.NullAppender; | |
22 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; | |
23 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor; | |
24 | import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; | |
25 | import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes; | |
26 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; | |
27 | import org.eclipse.swtbot.swt.finder.waits.DefaultCondition; | |
28 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; | |
29 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
30 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; | |
31 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers; | |
32 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; | |
f0beeb4a | 33 | import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils; |
573087b4 MAL |
34 | import org.junit.After; |
35 | import org.junit.Before; | |
36 | import org.junit.BeforeClass; | |
37 | import org.junit.Test; | |
38 | import org.junit.runner.RunWith; | |
39 | ||
40 | /** | |
41 | * Test refreshing a text trace after new content was added. | |
42 | */ | |
43 | @RunWith(SWTBotJunit4ClassRunner.class) | |
44 | public class TestRefreshTextTrace { | |
45 | ||
46 | private static final String PROJECT_NAME = "Test"; | |
47 | private static final String TRACE_TYPE_SYSLOG = "org.eclipse.linuxtools.tmf.tests.stubs.trace.text.testsyslog"; | |
48 | private static final String TIMESTAMP_FORMAT = "MMM dd HH:mm:ss"; | |
49 | ||
50 | private static final long INITIAL_NB_EVENTS = 100; | |
51 | private static final int NEW_EVENTS_PER_REFRESH = 40000; | |
52 | private static final int NB_REFRESH = 3; | |
53 | private static final long SECOND_TO_MILLISECOND = 1000; | |
54 | private static final long MICROSECOND_TO_NANOSECOND = 1000000; | |
9a869f27 | 55 | private static final int INDEXING_TIMEOUT = 300000; |
573087b4 MAL |
56 | |
57 | private static final Calendar CURRENT = Calendar.getInstance(); | |
58 | private static final String TRACE_LOCATION = TmfTraceManager.getTemporaryDirPath() + File.separator + "test.txt"; | |
59 | private static SWTWorkbenchBot fBot; | |
60 | ||
61 | private long fNbWrittenEvents = 0; | |
62 | ||
63 | /** Test Class setup */ | |
64 | @BeforeClass | |
65 | public static void init() { | |
66 | SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US"; | |
5785ab49 | 67 | SWTBotUtils.initialize(); |
573087b4 MAL |
68 | SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */ |
69 | Logger.getRootLogger().addAppender(new NullAppender()); | |
70 | fBot = new SWTWorkbenchBot(); | |
71 | ||
72 | SWTBotUtils.closeView("welcome", fBot); | |
73 | ||
74 | SWTBotUtils.switchToTracingPerspective(); | |
75 | /* finish waiting for eclipse to load */ | |
f0beeb4a | 76 | WaitUtils.waitForJobs(); |
573087b4 MAL |
77 | } |
78 | ||
79 | /** | |
80 | * Test setup | |
81 | * | |
82 | * @throws Exception on error | |
83 | */ | |
84 | @Before | |
85 | public void before() throws Exception { | |
86 | SWTBotUtils.createProject(PROJECT_NAME); | |
87 | SWTBotUtils.openTrace(PROJECT_NAME, createTrace(INITIAL_NB_EVENTS), getTraceType()); | |
f0beeb4a | 88 | WaitUtils.waitForJobs(); |
573087b4 MAL |
89 | } |
90 | ||
91 | /** | |
92 | * Test tear down | |
93 | */ | |
94 | @After | |
95 | public void after() { | |
96 | SWTBotUtils.deleteProject(PROJECT_NAME, fBot); | |
97 | } | |
98 | ||
99 | /** | |
100 | * Test refreshing a trace after new content was added. | |
101 | * | |
102 | * @throws IOException | |
103 | * on error | |
104 | */ | |
105 | @Test | |
106 | public void testRefresh() throws IOException { | |
107 | ITmfTrace activeTrace = TmfTraceManager.getInstance().getActiveTrace(); | |
108 | ||
109 | fBot.waitUntil(new NumberOfEventsCondition(activeTrace, INITIAL_NB_EVENTS)); | |
110 | ||
111 | for (int i = 0; i < NB_REFRESH; i++) { | |
112 | appendToTrace(NEW_EVENTS_PER_REFRESH); | |
113 | ||
114 | // Refresh | |
115 | SWTBotTreeItem tracesFolder = SWTBotUtils.selectTracesFolder(fBot, PROJECT_NAME); | |
116 | SWTBotTreeItem traceItem = SWTBotUtils.getTraceProjectItem(fBot, tracesFolder, activeTrace.getName()); | |
117 | traceItem.contextMenu("Refresh").click(); | |
118 | ||
119 | // Make sure the refresh is completed | |
9a869f27 | 120 | fBot.waitUntil(new NumberOfEventsCondition(activeTrace, getNbWrittenEvents()), INDEXING_TIMEOUT); |
573087b4 MAL |
121 | } |
122 | ||
123 | // Make sure the end of the table matches what we expect | |
124 | goToTableEnd(); | |
125 | fBot.waitUntil(ConditionHelpers.selectionInEventsTable(fBot, getExpectedEndTimeStamp())); | |
126 | } | |
127 | ||
128 | private static class NumberOfEventsCondition extends DefaultCondition { | |
129 | ||
130 | private ITmfTrace fTrace; | |
131 | private long fNbEvents; | |
132 | ||
133 | private NumberOfEventsCondition(ITmfTrace trace, long nbEvents) { | |
134 | fTrace = trace; | |
135 | fNbEvents = nbEvents; | |
136 | } | |
137 | ||
138 | @Override | |
139 | public boolean test() throws Exception { | |
9a869f27 | 140 | return fTrace.getNbEvents() == fNbEvents; |
573087b4 MAL |
141 | } |
142 | ||
143 | @Override | |
144 | public String getFailureMessage() { | |
2501b01e | 145 | return fTrace.getName() + " did not contain the expected number of " + fNbEvents + " events. Current: " + fTrace.getNbEvents(); |
573087b4 MAL |
146 | } |
147 | } | |
148 | ||
149 | /** | |
150 | * Create a trace with a number of events. | |
151 | * | |
152 | * @param nbEvents | |
153 | * the number of events to generate | |
154 | * @return the path to the created trace | |
155 | * @throws Exception | |
156 | * on error | |
157 | */ | |
158 | protected String createTrace(long nbEvents) throws Exception { | |
159 | writeToTrace(nbEvents, false); | |
160 | return TRACE_LOCATION; | |
161 | } | |
162 | ||
163 | /** | |
164 | * Append a number of events to the trace. | |
165 | * | |
166 | * @param nbEvents | |
167 | * the number of events to append | |
168 | * @throws IOException | |
169 | * on error | |
170 | */ | |
171 | protected void appendToTrace(long nbEvents) throws IOException { | |
172 | writeToTrace(nbEvents, true); | |
173 | } | |
174 | ||
175 | private void writeToTrace(long nbEvents, boolean append) throws IOException { | |
176 | final File file = new File(TRACE_LOCATION); | |
177 | try (FileWriter writer = new FileWriter(file, append)) { | |
178 | for (int i = 0; i < nbEvents; ++i) { | |
179 | writeEvent(writer); | |
180 | } | |
181 | } | |
182 | } | |
183 | ||
184 | private void writeEvent(FileWriter fw) throws IOException { | |
185 | SimpleDateFormat f = new SimpleDateFormat(TIMESTAMP_FORMAT); | |
186 | String timeStampStr = f.format(new Date(fNbWrittenEvents * SECOND_TO_MILLISECOND)); | |
187 | fw.write(timeStampStr + " HostF LoggerF: SourceFileF:9 Message F\n"); | |
188 | fNbWrittenEvents++; | |
189 | } | |
190 | ||
191 | /** | |
192 | * Get the trace type for the test. | |
193 | * | |
194 | * @return the trace type | |
195 | */ | |
196 | protected String getTraceType() { | |
197 | return TRACE_TYPE_SYSLOG; | |
198 | } | |
199 | ||
200 | /** | |
201 | * Get the expected time in nanosecs at the end of the trace, after | |
202 | * refreshing. | |
203 | * | |
204 | * @return the expected time in nanosecs at the end of the trace | |
205 | */ | |
206 | protected long getExpectedEndTimeStamp() { | |
207 | Date date = new Date((fNbWrittenEvents - 1) * SECOND_TO_MILLISECOND); | |
208 | // Syslog fills in the year when parsing so we have to do it for the | |
209 | // expected time stamp as well | |
210 | GregorianCalendar calendar = new GregorianCalendar(); | |
211 | calendar.setTime(date); | |
212 | calendar.set(Calendar.YEAR, CURRENT.get(Calendar.YEAR)); | |
213 | if (calendar.after(CURRENT)) { | |
214 | calendar.set(Calendar.YEAR, CURRENT.get(Calendar.YEAR) - 1); | |
215 | } | |
216 | return calendar.getTimeInMillis() * MICROSECOND_TO_NANOSECOND; | |
217 | } | |
218 | ||
219 | private static void goToTableEnd() { | |
220 | SWTBotEditor eventsEditor = SWTBotUtils.activeEventsEditor(fBot); | |
221 | eventsEditor.setFocus(); | |
222 | eventsEditor.bot().table().pressShortcut(Keystrokes.END); | |
223 | } | |
224 | ||
225 | /** | |
226 | * Get the number of events written so far. | |
227 | * | |
228 | * @return the number of events written so far | |
229 | */ | |
230 | protected long getNbWrittenEvents() { | |
231 | return fNbWrittenEvents; | |
232 | } | |
233 | } |