0d6f5811238166a4727bb996252e8aed0fbd7a3c
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / io / SharedFileBackedOutputStreamTest.java
1 /*
2  * Copyright (c) 2017 Inocybe Technologies and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.io;
9
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;
14
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;
25
26 /**
27  * Unit tests for SharedFileBackedOutputStream.
28  *
29  * @author Thomas Pantelis
30  */
31 public class SharedFileBackedOutputStreamTest {
32     private static final Logger LOG = LoggerFactory.getLogger(SharedFileBackedOutputStreamTest.class);
33     private static final String TEMP_DIR = "target/FileBackedOutputStreamTest";
34
35     @BeforeClass
36     public static void staticSetup() {
37         FileBackedOutputStreamTest.createDir(TEMP_DIR);
38     }
39
40     @AfterClass
41     public static void staticCleanup() {
42         FileBackedOutputStreamTest.deleteTempFiles(TEMP_DIR);
43         FileBackedOutputStreamTest.deleteFile(TEMP_DIR);
44     }
45
46     @Before
47     public void setup() {
48         FileBackedOutputStreamTest.deleteTempFiles(TEMP_DIR);
49         FileBackedOutputStream.REFERENCE_CACHE.clear();
50     }
51
52     @After
53     public void cleanup() {
54         FileBackedOutputStreamTest.deleteTempFiles(TEMP_DIR);
55     }
56
57     @Test
58     public void testSingleUsage() throws IOException {
59         LOG.info("testSingleUsage starting");
60         try (SharedFileBackedOutputStream fbos = new SharedFileBackedOutputStream(5, TEMP_DIR)) {
61             byte[] bytes = new byte[]{0, 1, 2, 3, 4, 5, 6};
62             fbos.write(bytes);
63
64             assertNotNull("Expected temp file created", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
65             fbos.cleanup();
66             assertNull("Found unexpected temp file", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
67         }
68
69         LOG.info("testSingleUsage ending");
70     }
71
72     @SuppressWarnings("unchecked")
73     @Test
74     public void testSharing() throws IOException {
75         LOG.info("testSharing starting");
76         try (SharedFileBackedOutputStream fbos = new SharedFileBackedOutputStream(5, TEMP_DIR)) {
77             String context = "context";
78             Consumer<String> mockCallback = Mockito.mock(Consumer.class);
79             fbos.setOnCleanupCallback(mockCallback , context);
80
81             byte[] bytes = new byte[]{0, 1, 2, 3, 4, 5, 6};
82             fbos.write(bytes);
83
84             assertNotNull("Expected temp file created", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
85
86             fbos.incrementUsageCount();
87             fbos.cleanup();
88             assertNotNull("Expected temp file exists", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
89
90             fbos.incrementUsageCount();
91             fbos.incrementUsageCount();
92
93             fbos.cleanup();
94             assertNotNull("Expected temp file exists", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
95
96             fbos.cleanup();
97             assertNotNull("Expected temp file exists", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
98
99             verify(mockCallback, never()).accept(context);
100
101             fbos.cleanup();
102             assertNull("Found unexpected temp file", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
103
104             verify(mockCallback).accept(context);
105         }
106
107         LOG.info("testSharing ending");
108     }
109 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.