ss: historyTreeFile can be null
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core.tests / src / org / eclipse / tracecompass / statesystem / core / tests / backend / HistoryTreeBackendTest.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson 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 static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.Map;
21 import java.util.Set;
22
23 import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HistoryTreeBackend;
24 import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend;
25 import org.junit.After;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.Parameterized;
28 import org.junit.runners.Parameterized.Parameters;
29
30 /**
31 * Test the {@link HistoryTreeBackend} class.
32 *
33 * @author Patrick Tasse
34 */
35 @RunWith(Parameterized.class)
36 public class HistoryTreeBackendTest extends StateHistoryBackendTestBase {
37
38 /** State system ID */
39 protected static final 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 = checkNotNull(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
107 File historyTreeFile = fBackendMap.remove(backend);
108
109 if (historyTreeFile == null) {
110 throw new IllegalStateException();
111 }
112
113 backend.dispose();
114 HistoryTreeBackend reOpenedBackend = new HistoryTreeBackend(SSID, historyTreeFile, PROVIDER_VERSION);
115 fBackendMap.put(reOpenedBackend, historyTreeFile);
116 return reOpenedBackend;
117 }
118 }
This page took 0.034478 seconds and 5 git commands to generate.