1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together.utils

File FileUtilsTest.java

 

Code metrics

6
62
17
1
180
154
20
0.32
3.65
17
1.18

Classes

Class Line # Actions
FileUtilsTest 25 62 0% 20 0
1.0100%
 

Contributing tests

This file is covered by 17 tests. .

Source view

1    package org.europa.together.utils;
2   
3    import java.io.ByteArrayInputStream;
4    import java.io.File;
5    import java.io.IOException;
6    import java.io.InputStream;
7    import java.lang.reflect.Constructor;
8    import java.nio.file.Files;
9    import java.nio.file.Paths;
10    import java.util.Collection;
11    import org.europa.together.JUnit5Preperator;
12    import org.europa.together.application.LogbackLogger;
13    import org.europa.together.business.Logger;
14    import org.europa.together.domain.ByteOrderMark;
15    import static org.junit.jupiter.api.Assertions.*;
16    import org.junit.jupiter.api.Test;
17    import org.junit.jupiter.api.extension.ExtendWith;
18    import org.springframework.test.context.ContextConfiguration;
19    import org.springframework.test.context.junit.jupiter.SpringExtension;
20   
21    @SuppressWarnings("unchecked")
22    @ExtendWith({JUnit5Preperator.class})
23    @ExtendWith(SpringExtension.class)
24    @ContextConfiguration(locations = {"/applicationContext.xml"})
 
25    public class FileUtilsTest {
26   
27    private static final Logger LOGGER = new LogbackLogger(FileUtilsTest.class);
28   
29    private static final String DIRECTORY
30    = Constraints.SYSTEM_APP_DIR + "/target/test-classes/";
31   
 
32  1 toggle @Test
33    void privateConstructor() throws Exception {
34  1 Constructor<FileUtils> clazz
35    = FileUtils.class.getDeclaredConstructor();
36  1 clazz.setAccessible(true);
37  1 assertThrows(Exception.class, () -> {
38  1 FileUtils call = clazz.newInstance();
39    });
40    }
41   
 
42  1 toggle @Test
43    void getFileSize() {
44  1 assertEquals(146329, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", null));
45  1 assertEquals(146329, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", ""));
46  1 assertEquals(146329, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", " "));
47  1 assertEquals(142, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", "kilo"));
48   
49  1 assertEquals(0, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", "mega"));
50  1 assertEquals(0, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", "giga"));
51  1 assertEquals(0, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", "tera"));
52    }
53   
 
54  1 toggle @Test
55    void failGetFileSize() {
56  1 assertEquals(0, FileUtils.getFileSize(DIRECTORY + "no_file", null));
57    }
58   
 
59  1 toggle @Test
60    void writeEmptyFile() throws Exception {
61  1 assertTrue(FileUtils.writeStringToFile("", DIRECTORY + "/empty.txt"));
62  1 assertEquals(0, FileUtils.getFileSize(DIRECTORY + "/empty.txt", null));
63  1 Files.delete(Paths.get(DIRECTORY + "/empty.txt"));
64    }
65   
 
66  1 toggle @Test
67    void writeStringToFile() throws Exception {
68  1 String fileContent = "Content of the written File";
69  1 assertTrue(FileUtils.writeStringToFile(fileContent, DIRECTORY + "/test.txt"));
70  1 assertEquals(27, FileUtils.getFileSize(DIRECTORY + "/test.txt", null));
71  1 Files.delete(Paths.get(DIRECTORY + "/test.txt"));
72    }
73   
 
74  1 toggle @Test
75    void failWriteStringToFile() throws IOException {
76  1 assertThrows(IOException.class, () -> {
77  1 FileUtils.writeStringToFile("content", "");
78    });
79    }
80   
 
81  1 toggle @Test
82    void readFileStream() throws Exception {
83  1 String file = DIRECTORY + "TestFile";
84  1 assertEquals("Hello World!", FileUtils.readFileStream(new File(file)));
85  1 assertEquals("Hello World!", FileUtils.readFileStream(new File(file), ByteOrderMark.NONE));
86  1 assertEquals("Hello World!", FileUtils.readFileStream(new File(file), ByteOrderMark.UTF_8));
87    }
88   
 
89  1 toggle @Test
90    void failReadFileStream() throws Exception {
91  1 assertThrows(Exception.class, () -> {
92  1 FileUtils.readFileStream(new File("no_file"));
93    });
94    }
95   
 
96  1 toggle @Test
97    void appendFile() throws Exception {
98  1 String file = DIRECTORY + "AppendTestFile.txt";
99  1 FileUtils.writeStringToFile("Hello World!", file);
100   
101  1 String append = " more content.";
102  1 FileUtils.appendFile(file, append);
103   
104  1 assertEquals("Hello World! more content.", FileUtils.readFileStream(new File(file)));
105  1 Files.delete(Paths.get(file));
106    }
107   
 
108  1 toggle @Test
109    void failAppendFile() throws Exception {
110  1 assertThrows(Exception.class, () -> {
111  1 FileUtils.appendFile("no_file_to_append", "appending string.");
112    });
113    }
114   
 
115  1 toggle @Test
116    void listFileTree() {
117  1 File file = new File(DIRECTORY + "dir-test");
118  1 Collection<File> filelist = FileUtils.listFileTree(file);
119   
120  1 assertEquals(3, filelist.size());
121  1 for (File entry : filelist) {
122  3 if (entry.getName().equalsIgnoreCase("root")) {
123  1 assertEquals(DIRECTORY + "dir-test/root",
124    entry.getAbsolutePath());
125    }
126  3 if (entry.getName().equalsIgnoreCase("file.sql")) {
127  1 assertEquals(DIRECTORY + "dir-test/level_2.0/level_2.1/file.sql",
128    entry.getAbsolutePath());
129    }
130  3 if (entry.getName().equalsIgnoreCase("file_a.txt")) {
131  1 assertEquals(DIRECTORY + "dir-test/level_1.0/file_a.txt",
132    entry.getAbsolutePath());
133    }
134    }
135    }
136   
 
137  1 toggle @Test
138    void listFileTreeWithEmptyDir() {
139  1 File file = new File(DIRECTORY + "dir-test/empty");
140  1 assertTrue(file.mkdir());
141  1 assertEquals(0, FileUtils.listFileTree(file).size());
142  1 assertTrue(file.delete());
143    }
144   
 
145  1 toggle @Test
146    void failListFileTree() {
147  1 assertNotNull(FileUtils.listFileTree(new File("")));
148  1 assertNotNull(FileUtils.listFileTree(null));
149    }
150   
 
151  1 toggle @Test
152    void copyFile() throws Exception {
153  1 File source = new File(Constraints.SYSTEM_APP_DIR + "/README.md");
154  1 File destination = new File(Constraints.SYSTEM_APP_DIR + "/target/README-COPY.md");
155  1 FileUtils.copyFile(source, destination);
156  1 assertTrue(destination.exists());
157    }
158   
 
159  1 toggle @Test
160    void failCopyFile() throws Exception {
161  1 assertThrows(Exception.class, () -> {
162  1 FileUtils.copyFile(new File("empty/dontExist.txt"), new File("empty/dontExist-copy.txt"));
163    });
164    }
165   
 
166  1 toggle @Test
167    void inputStreamToByteArray() throws Exception {
168  1 byte[] byteArray = {23, 12, 1, 0, 12, 34, 9};
169  1 InputStream input = new ByteArrayInputStream(byteArray);
170   
171  1 assertArrayEquals(byteArray, FileUtils.inputStreamToByteArray(input));
172    }
173   
 
174  1 toggle @Test
175    void failInputStreamToByteArray() throws Exception {
176  1 assertThrows(Exception.class, () -> {
177  1 FileUtils.inputStreamToByteArray(null);
178    });
179    }
180    }