2 * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.io;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Mockito.never;
13 import static org.mockito.Mockito.verify;
15 import java.io.IOException;
16 import java.util.function.Consumer;
17 import org.junit.After;
18 import org.junit.AfterClass;
19 import org.junit.Before;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.mockito.Mockito;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Unit tests for SharedFileBackedOutputStream.
29 * @author Thomas Pantelis
31 public class SharedFileBackedOutputStreamTest {
32 private static final Logger LOG = LoggerFactory.getLogger(SharedFileBackedOutputStreamTest.class);
33 private static final String TEMP_DIR = "target/FileBackedOutputStreamTest";
36 public static void staticSetup() {
37 FileBackedOutputStreamTest.createDir(TEMP_DIR);
41 public static void staticCleanup() {
42 FileBackedOutputStreamTest.deleteTempFiles(TEMP_DIR);
43 FileBackedOutputStreamTest.deleteFile(TEMP_DIR);
48 FileBackedOutputStreamTest.deleteTempFiles(TEMP_DIR);
52 public void cleanup() {
53 FileBackedOutputStreamTest.deleteTempFiles(TEMP_DIR);
57 public void testSingleUsage() throws IOException {
58 LOG.info("testSingleUsage starting");
59 try (SharedFileBackedOutputStream fbos = new SharedFileBackedOutputStream(5, TEMP_DIR)) {
60 byte[] bytes = new byte[]{0, 1, 2, 3, 4, 5, 6};
63 assertNotNull("Expected temp file created", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
65 assertNull("Found unexpected temp file", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
68 LOG.info("testSingleUsage ending");
71 @SuppressWarnings("unchecked")
73 public void testSharing() throws IOException {
74 LOG.info("testSharing starting");
75 try (SharedFileBackedOutputStream fbos = new SharedFileBackedOutputStream(5, TEMP_DIR)) {
76 String context = "context";
77 Consumer<String> mockCallback = Mockito.mock(Consumer.class);
78 fbos.setOnCleanupCallback(mockCallback , context);
80 byte[] bytes = new byte[]{0, 1, 2, 3, 4, 5, 6};
83 assertNotNull("Expected temp file created", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
85 fbos.incrementUsageCount();
87 assertNotNull("Expected temp file exists", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
89 fbos.incrementUsageCount();
90 fbos.incrementUsageCount();
93 assertNotNull("Expected temp file exists", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
96 assertNotNull("Expected temp file exists", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
98 verify(mockCallback, never()).accept(context);
101 assertNull("Found unexpected temp file", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
103 verify(mockCallback).accept(context);
106 LOG.info("testSharing ending");