1
2
3
4
5
6
7 package net.alchim31.maven.yuicompressor;
8
9 import java.io.File;
10 import java.io.IOException;
11 import java.nio.charset.StandardCharsets;
12 import java.nio.file.Files;
13 import java.nio.file.Path;
14
15 import org.junit.jupiter.api.AfterEach;
16 import org.junit.jupiter.api.Assertions;
17 import org.junit.jupiter.api.BeforeEach;
18 import org.junit.jupiter.api.Test;
19
20
21
22
23 class SourceFileTest {
24
25
26 private Path srcRoot;
27
28
29 private Path destRoot;
30
31
32
33
34
35
36
37 @BeforeEach
38 void setUp() throws IOException {
39 this.srcRoot = Files.createTempDirectory("srcRoot");
40 this.destRoot = Files.createTempDirectory("destRoot");
41 }
42
43
44
45
46
47
48
49 @AfterEach
50 void tearDown() throws IOException {
51 try (var srcStream = Files.walk(this.srcRoot)) {
52 srcStream.map(Path::toFile).forEach(File::delete);
53 }
54 try (var destStream = Files.walk(this.destRoot)) {
55 destStream.map(Path::toFile).forEach(File::delete);
56 }
57 }
58
59
60
61
62 @Test
63 void testConstructorWithExtension() {
64 final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "foo.js", false);
65 Assertions.assertEquals(".js", sf.getExtension());
66 }
67
68
69
70
71 @Test
72 void testConstructorWithoutExtension() {
73 final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "foo", false);
74 Assertions.assertEquals("", sf.getExtension());
75 }
76
77
78
79
80
81
82
83 @Test
84 void testToFileSrcRoot() throws IOException {
85 final var filePath = this.srcRoot.resolve("bar.js");
86 Files.write(filePath, "test".getBytes(StandardCharsets.UTF_8));
87 final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "bar.js", false);
88 Assertions.assertEquals(filePath.toFile(), sf.toFile());
89 }
90
91
92
93
94
95
96
97 @Test
98 void testToFileDestRootWhenDestAsSourceAndFileExists() throws IOException {
99 final var filePath = this.destRoot.resolve("baz.js");
100 Files.write(filePath, "test".getBytes(StandardCharsets.UTF_8));
101 final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "baz.js", true);
102 Assertions.assertEquals(filePath.toFile(), sf.toFile());
103 }
104
105
106
107
108 @Test
109 void testToFileDestRootWhenDestAsSourceAndFileDoesNotExist() {
110 final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "qux.js", true);
111
112 final var expected = this.srcRoot.resolve("qux.js").toFile();
113 Assertions.assertEquals(expected, sf.toFile());
114 }
115
116
117
118
119 @Test
120 void testToDestFileWithSuffix() {
121 final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "foo.js", false);
122 final var expected = this.destRoot.resolve("foo-min.js").toFile();
123 Assertions.assertEquals(expected, sf.toDestFile("-min"));
124 }
125
126
127
128
129 @Test
130 void testToDestFileWithSuffixNoExtension() {
131 final var sf = new SourceFile(this.srcRoot.toFile(), this.destRoot.toFile(), "foo", false);
132 final var expected = this.destRoot.resolve("foo-min").toFile();
133 Assertions.assertEquals(expected, sf.toDestFile("-min"));
134 }
135
136 }