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