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