Changes for checkstyle 8.32
[policy/apex-pdp.git] / tools / tools-common / src / test / java / org / onap / policy / apex / tools / common / OutputFileTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (c) 2020 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.tools.common;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26
27 import java.io.File;
28 import java.nio.file.FileSystems;
29 import java.nio.file.Path;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33
34 public class OutputFileTest {
35
36     final String testFileName = "testing.txt";
37     final Path fp = FileSystems.getDefault().getPath(testFileName);
38     File file = fp.toFile();
39
40     @Before
41     public void beforeSetUp() {
42         if (file.exists()) {
43             file.delete();
44         }
45     }
46
47     @Test
48     public void testToWriter() {
49         OutputFile testFile = new OutputFile(testFileName, false);
50         testFile.validate();
51         file.setReadable(false);
52         file.setWritable(false);
53         assertNull(testFile.toWriter());
54         file.setWritable(true);
55         assertNotNull(testFile.toWriter());
56     }
57
58     @Test
59     public void testValidate() {
60         OutputFile testFile = new OutputFile(testFileName, true);
61         assertNull(testFile.validate());
62         file.setReadable(false);
63         file.setWritable(false);
64         assertNotNull(testFile.validate());
65         OutputFile testFile2 = new OutputFile(testFileName);
66         assertNotNull(testFile2.validate());
67         assertEquals("file already exists", testFile2.validate());
68     }
69
70     @Test
71     public void testToOutputStream() {
72         OutputFile testFile = new OutputFile(testFileName, true);
73         assertNotNull(testFile.toOutputStream());
74         file.setReadable(false);
75         file.setWritable(false);
76         assertNull(testFile.toOutputStream());
77     }
78
79     @After
80     public void testDown() {
81         if (file.exists()) {
82             file.delete();
83         }
84     }
85 }