1 package net_alchim31_maven_yuicompressor;
2
3 import java.io.File;
4 import java.util.Collection;
5 import java.util.HashSet;
6
7 import junit.framework.TestCase;
8
9 import org.codehaus.plexus.util.FileUtils;
10 import org.sonatype.plexus.build.incremental.DefaultBuildContext;
11
12 public class AggregationTestCase extends TestCase {
13 private File dir_;
14
15 private DefaultBuildContext defaultBuildContext = new DefaultBuildContext();
16
17 @Override
18 protected void setUp() throws Exception {
19 dir_ = File.createTempFile(this.getClass().getName(), "-test");
20 dir_.delete();
21 dir_.mkdirs();
22 }
23
24 @Override
25 protected void tearDown() throws Exception {
26 FileUtils.deleteDirectory(dir_);
27 }
28
29 public void test0to1() throws Exception {
30 Aggregation target = new Aggregation();
31 target.output = new File(dir_, "output.js");
32
33 assertFalse(target.output.exists());
34 target.run(null,defaultBuildContext);
35 assertFalse(target.output.exists());
36
37 target.includes = new String[]{};
38 assertFalse(target.output.exists());
39 target.run(null,defaultBuildContext);
40 assertFalse(target.output.exists());
41
42 target.includes = new String[]{"**/*.js"};
43 assertFalse(target.output.exists());
44 target.run(null,defaultBuildContext);
45 assertFalse(target.output.exists());
46 }
47
48
49 public void test1to1() throws Exception {
50 File f1 = new File(dir_, "01.js");
51 FileUtils.fileWrite(f1.getAbsolutePath(), "1");
52 Aggregation target = new Aggregation();
53 target.output = new File(dir_, "output.js");
54 target.includes = new String[]{f1.getName()};
55
56 assertFalse(target.output.exists());
57 target.run(null,defaultBuildContext);
58 assertTrue(target.output.exists());
59 assertEquals(FileUtils.fileRead(f1), FileUtils.fileRead(target.output));
60 }
61
62 public void test2to1() throws Exception {
63 File f1 = new File(dir_, "01.js");
64 FileUtils.fileWrite(f1.getAbsolutePath(), "1");
65
66 File f2 = new File(dir_, "02.js");
67 FileUtils.fileWrite(f2.getAbsolutePath(), "22\n22");
68
69 Aggregation target = new Aggregation();
70 target.output = new File(dir_, "output.js");
71
72 target.includes = new String[]{f1.getName(), f2.getName()};
73 assertFalse(target.output.exists());
74 target.run(null,defaultBuildContext);
75 assertTrue(target.output.exists());
76 assertEquals(FileUtils.fileRead(f1) + FileUtils.fileRead(f2), FileUtils.fileRead(target.output));
77
78 target.output.delete();
79 target.includes = new String[]{"*.js"};
80 assertFalse(target.output.exists());
81 target.run(null,defaultBuildContext);
82 assertTrue(target.output.exists());
83 assertEquals(FileUtils.fileRead(f1) + FileUtils.fileRead(f2), FileUtils.fileRead(target.output));
84 }
85
86 public void testNoDuplicateAggregation() throws Exception {
87 File f1 = new File(dir_, "01.js");
88 FileUtils.fileWrite(f1.getAbsolutePath(), "1");
89
90 File f2 = new File(dir_, "02.js");
91 FileUtils.fileWrite(f2.getAbsolutePath(), "22\n22");
92
93 Aggregation target = new Aggregation();
94 target.output = new File(dir_, "output.js");
95
96 target.includes = new String[]{f1.getName(), f1.getName(), f2.getName()};
97 assertFalse(target.output.exists());
98 target.run(null,defaultBuildContext);
99 assertTrue(target.output.exists());
100 assertEquals(FileUtils.fileRead(f1) + FileUtils.fileRead(f2), FileUtils.fileRead(target.output));
101
102 target.output.delete();
103 target.includes = new String[]{f1.getName(), "*.js"};
104 assertFalse(target.output.exists());
105 target.run(null,defaultBuildContext);
106 assertTrue(target.output.exists());
107 assertEquals(FileUtils.fileRead(f1) + FileUtils.fileRead(f2), FileUtils.fileRead(target.output));
108 }
109
110 public void test2to1Order() throws Exception {
111 File f1 = new File(dir_, "01.js");
112 FileUtils.fileWrite(f1.getAbsolutePath(), "1");
113
114 File f2 = new File(dir_, "02.js");
115 FileUtils.fileWrite(f2.getAbsolutePath(), "2");
116
117 Aggregation target = new Aggregation();
118 target.output = new File(dir_, "output.js");
119
120 target.includes = new String[]{f2.getName(), f1.getName()};
121 assertFalse(target.output.exists());
122 target.run(null,defaultBuildContext);
123 assertTrue(target.output.exists());
124 assertEquals(FileUtils.fileRead(f2) + FileUtils.fileRead(f1), FileUtils.fileRead(target.output));
125 }
126
127 public void test2to1WithNewLine() throws Exception {
128 File f1 = new File(dir_, "01.js");
129 FileUtils.fileWrite(f1.getAbsolutePath(), "1");
130
131 File f2 = new File(dir_, "02.js");
132 FileUtils.fileWrite(f2.getAbsolutePath(), "22\n22");
133
134 Aggregation target = new Aggregation();
135 target.output = new File(dir_, "output.js");
136 target.insertNewLine = true;
137 target.includes = new String[]{f1.getName(), f2.getName()};
138
139 assertFalse(target.output.exists());
140 target.run(null,defaultBuildContext);
141 assertTrue(target.output.exists());
142 assertEquals(FileUtils.fileRead(f1) + "\n" + FileUtils.fileRead(f2) + "\n", FileUtils.fileRead(target.output));
143 }
144
145 public void testAbsolutePathFromInside() throws Exception {
146 File f1 = new File(dir_, "01.js");
147 FileUtils.fileWrite(f1.getAbsolutePath(), "1");
148
149 File f2 = new File(dir_, "02.js");
150 FileUtils.fileWrite(f2.getAbsolutePath(), "22\n22");
151
152 Aggregation target = new Aggregation();
153 target.output = new File(dir_, "output.js");
154
155 target.includes = new String[]{f1.getAbsolutePath(), f2.getName()};
156 assertFalse(target.output.exists());
157 target.run(null,defaultBuildContext);
158 assertTrue(target.output.exists());
159 assertEquals(FileUtils.fileRead(f1) + FileUtils.fileRead(f2), FileUtils.fileRead(target.output));
160 }
161
162 public void testAbsolutePathFromOutside() throws Exception {
163 File f1 = File.createTempFile("test-01", ".js");
164 try {
165 FileUtils.fileWrite(f1.getAbsolutePath(), "1");
166
167 File f2 = new File(dir_, "02.js");
168 FileUtils.fileWrite(f2.getAbsolutePath(), "22\n22");
169
170 Aggregation target = new Aggregation();
171 target.output = new File(dir_, "output.js");
172
173 target.includes = new String[]{f1.getAbsolutePath(), f2.getName()};
174 assertFalse(target.output.exists());
175 target.run(null,defaultBuildContext);
176 assertTrue(target.output.exists());
177 assertEquals(FileUtils.fileRead(f1) + FileUtils.fileRead(f2), FileUtils.fileRead(target.output));
178 } finally {
179 f1.delete();
180 }
181 }
182
183 public void testAutoExcludeWildcards() throws Exception {
184 File f1 = new File(dir_, "01.js");
185 FileUtils.fileWrite(f1.getAbsolutePath(), "1");
186
187 File f2 = new File(dir_, "02.js");
188 FileUtils.fileWrite(f2.getAbsolutePath(), "22\n22");
189
190 Aggregation target = new Aggregation();
191 target.autoExcludeWildcards = true;
192 target.output = new File(dir_, "output.js");
193
194 Collection<File> previouslyIncluded = new HashSet<File>();
195 previouslyIncluded.add(f1);
196
197 target.includes = new String[]{f1.getName(), f2.getName()};
198 assertFalse(target.output.exists());
199 target.run(previouslyIncluded,defaultBuildContext);
200 assertTrue(target.output.exists());
201 assertEquals(FileUtils.fileRead(f1) + FileUtils.fileRead(f2), FileUtils.fileRead(target.output));
202
203 target.output.delete();
204 target.includes = new String[]{"*.js"};
205 assertFalse(target.output.exists());
206 target.run(previouslyIncluded,defaultBuildContext);
207 assertTrue(target.output.exists());
208 assertEquals(FileUtils.fileRead(f2), FileUtils.fileRead(target.output));
209 }
210 }