Commit | Line | Data |
---|---|---|
60cabb56 PT |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 Ericsson, EfficiOS Inc. and others | |
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.statesystem.core.tests.backend; | |
11 | ||
12 | import java.io.File; | |
13 | import java.io.IOException; | |
14 | import java.util.Arrays; | |
15 | import java.util.Collection; | |
16 | import java.util.HashMap; | |
17 | import java.util.HashSet; | |
18 | import java.util.Map; | |
19 | import java.util.Set; | |
20 | ||
21 | import org.eclipse.jdt.annotation.NonNull; | |
22 | import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HistoryTreeBackend; | |
23 | import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend; | |
24 | import org.junit.After; | |
25 | import org.junit.runner.RunWith; | |
26 | import org.junit.runners.Parameterized; | |
27 | import org.junit.runners.Parameterized.Parameters; | |
28 | ||
29 | /** | |
30 | * Test the {@link HistoryTreeBackend} class. | |
31 | * | |
32 | * @author Patrick Tasse | |
33 | * @author Alexandre Montplaisir | |
34 | */ | |
35 | @RunWith(Parameterized.class) | |
36 | public class HistoryTreeBackendTest extends StateHistoryBackendTestBase { | |
37 | ||
38 | /** State system ID */ | |
39 | protected static final @NonNull String SSID = "test"; | |
40 | /** Provider version */ | |
41 | protected static final int PROVIDER_VERSION = 0; | |
42 | ||
43 | /** Default maximum number of children nodes */ | |
44 | protected static final int MAX_CHILDREN = 2; | |
45 | /** Default block size */ | |
46 | protected static final int BLOCK_SIZE = 4096; | |
47 | ||
48 | /** ReOpen test parameter */ | |
49 | protected final boolean fReOpen; | |
50 | ||
51 | /** Set of created history tree files */ | |
52 | protected Set<File> fHistoryTreeFiles = new HashSet<>(); | |
53 | /** Map of backends to history tree file */ | |
54 | protected Map<IStateHistoryBackend, File> fBackendMap = new HashMap<>(); | |
55 | /** Maximum number of children nodes */ | |
56 | protected int fMaxChildren = MAX_CHILDREN; | |
57 | /** Block size */ | |
58 | protected int fBlockSize = BLOCK_SIZE; | |
59 | ||
60 | /** | |
61 | * @return the test parameters | |
62 | */ | |
63 | @Parameters(name = "ReOpen={0}") | |
64 | public static Collection<Boolean> parameters() { | |
65 | return Arrays.asList(Boolean.FALSE, Boolean.TRUE); | |
66 | } | |
67 | ||
68 | /** | |
69 | * Constructor | |
70 | * | |
71 | * @param reOpen | |
72 | * True if the backend should be disposed and re-opened as a new | |
73 | * backend from the file, or false to use the backend as-is | |
74 | */ | |
75 | public HistoryTreeBackendTest(Boolean reOpen) { | |
76 | fReOpen = reOpen; | |
77 | } | |
78 | ||
79 | /** | |
80 | * Test cleanup | |
81 | */ | |
82 | @After | |
83 | public void teardown() { | |
84 | for (IStateHistoryBackend backend : fBackendMap.keySet()) { | |
85 | backend.dispose(); | |
86 | } | |
87 | for (File historyTreeFile : fHistoryTreeFiles) { | |
88 | historyTreeFile.delete(); | |
89 | } | |
90 | } | |
91 | ||
92 | @Override | |
93 | protected IStateHistoryBackend getBackendForBuilding(long startTime) throws IOException { | |
94 | File historyTreeFile = File.createTempFile("HistoryTreeBackendTest", ".ht"); | |
95 | fHistoryTreeFiles.add(historyTreeFile); | |
96 | HistoryTreeBackend backend = new HistoryTreeBackend(SSID, historyTreeFile, PROVIDER_VERSION, startTime, fBlockSize, fMaxChildren); | |
97 | fBackendMap.put(backend, historyTreeFile); | |
98 | return backend; | |
99 | } | |
100 | ||
101 | @Override | |
102 | protected IStateHistoryBackend getBackendForQuerying(IStateHistoryBackend backend) throws IOException { | |
103 | if (!fReOpen) { | |
104 | return backend; | |
105 | } | |
106 | File historyTreeFile = fBackendMap.remove(backend); | |
107 | backend.dispose(); | |
108 | HistoryTreeBackend reOpenedBackend = new HistoryTreeBackend(SSID, historyTreeFile, PROVIDER_VERSION); | |
109 | fBackendMap.put(reOpenedBackend, historyTreeFile); | |
110 | return reOpenedBackend; | |
111 | } | |
112 | } |