Use java.lang.ref.Cleaner in controller.cluster.io
[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     }
50
51     @After
52     public void cleanup() {
53         FileBackedOutputStreamTest.deleteTempFiles(TEMP_DIR);
54     }
55
56     @Test
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};
61             fbos.write(bytes);
62
63             assertNotNull("Expected temp file created", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
64             fbos.cleanup();
65             assertNull("Found unexpected temp file", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
66         }
67
68         LOG.info("testSingleUsage ending");
69     }
70
71     @SuppressWarnings("unchecked")
72     @Test
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);
79
80             byte[] bytes = new byte[]{0, 1, 2, 3, 4, 5, 6};
81             fbos.write(bytes);
82
83             assertNotNull("Expected temp file created", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
84
85             fbos.incrementUsageCount();
86             fbos.cleanup();
87             assertNotNull("Expected temp file exists", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
88
89             fbos.incrementUsageCount();
90             fbos.incrementUsageCount();
91
92             fbos.cleanup();
93             assertNotNull("Expected temp file exists", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
94
95             fbos.cleanup();
96             assertNotNull("Expected temp file exists", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
97
98             verify(mockCallback, never()).accept(context);
99
100             fbos.cleanup();
101             assertNull("Found unexpected temp file", FileBackedOutputStreamTest.findTempFileName(TEMP_DIR));
102
103             verify(mockCallback).accept(context);
104         }
105
106         LOG.info("testSharing ending");
107     }
108 }

©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.