1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.annotation;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23
24 import java.io.BufferedReader;
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.io.Writer;
30
31 import org.apache.commons.io.FileUtils;
32 import org.apache.rat.test.utils.Resources;
33 import org.apache.rat.utils.DefaultLog;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.io.TempDir;
36
37 public class TestLicenseAppender {
38
39 @TempDir
40 private File baseTempFolder;
41
42
43 private static final String FIRST_LICENSE_LINE = " Licensed to the Apache Software Foundation (ASF) under one";
44
45 private interface FileCreator {
46 void createFile(Writer w) throws IOException;
47 }
48
49 private interface NewFileReader {
50 void readFile(BufferedReader r) throws IOException;
51 }
52
53 private String getTemporaryFileWithName(String fileName) throws IOException {
54 if (fileName != null) {
55 return new File(baseTempFolder,fileName).getAbsolutePath();
56 }
57 return File.createTempFile("tla", null, baseTempFolder).getAbsolutePath();
58 }
59
60 private static void createTestFile(String fileName,
61 FileCreator creator)
62 throws IOException {
63 try (FileWriter w = new FileWriter(fileName)) {
64 creator.createFile(w);
65 }
66 }
67
68 private void commonTestTemplate(String relativeName,
69 FileCreator creator,
70 NewFileReader reader)
71 throws IOException {
72 String name = getTemporaryFileWithName(relativeName);
73
74 createTestFile(name, creator);
75
76 ApacheV2LicenseAppender appender =
77 new ApacheV2LicenseAppender(DefaultLog.INSTANCE);
78 appender.append(new File(name));
79
80 try (BufferedReader r = new BufferedReader(new FileReader(name + ".new"))){
81 reader.readFile(r);
82 }
83 FileUtils.delete( new File(name+".new"));
84 FileUtils.delete( new File(name));
85 }
86
87 private static NewFileReader checkLines(final String ... lines) {
88 return new NewFileReader() {
89 public void readFile(BufferedReader r) throws IOException {
90 for (int i=0; i<lines.length; i++) {
91 String line = r.readLine();
92 assertEquals(lines[i], line, String.format("Line %s is incorrect",i));
93 }
94 }
95 };
96 }
97
98 @Test
99 public void addLicenseToUnknownFile() throws IOException {
100 String filename = getTemporaryFileWithName(null);
101 createTestFile(filename, new FileCreator() {
102 public void createFile(Writer writer)
103 throws IOException {
104 writer.write("Unknown file type\n");
105 }
106 });
107
108 File file = new File(filename);
109 file.deleteOnExit();
110 ApacheV2LicenseAppender appender =
111 new ApacheV2LicenseAppender(DefaultLog.INSTANCE);
112 appender.append(file);
113
114 File newFile = new File(filename + ".new");
115 newFile.deleteOnExit();
116 assertFalse(newFile.exists(), "No new file should have been written");
117 }
118
119 @Test
120 public void addLicenseToJava() throws IOException {
121 String filename = "tmp.java";
122 final String firstLine = "package foo;";
123 final String secondLine = "";
124 final String thirdLine = "/*";
125 commonTestTemplate(filename, new FileCreator() {
126 public void createFile(Writer writer)
127 throws IOException {
128 writer.write(firstLine + "\n");
129 writer.write("\n");
130 writer.write("public class test {\n");
131 writer.write("}\n");
132 }
133 },
134 checkLines(firstLine, secondLine, thirdLine));
135 }
136
137 @Test
138 public void addLicenseToJavaWithoutPackage() throws IOException {
139 String filename = "tmp.java";
140 String commentLine = "/*";
141 commonTestTemplate(filename, new FileCreator() {
142 public void createFile(Writer writer)
143 throws IOException {
144 writer.write("public class test {\n");
145 writer.write("}\n");
146 }
147 },
148 checkLines(commentLine));
149 }
150
151 @Test
152 public void addLicenseToXML() throws IOException {
153 String filename = "tmp.xml";
154 final String firstLine = "<?xml version='1.0'?>";
155 final String secondLine = "";
156 final String thirdLine = "<!--";
157
158 commonTestTemplate(filename, new FileCreator() {
159 public void createFile(Writer writer)
160 throws IOException {
161 writer.write(firstLine + "\n");
162 writer.write("\n");
163 writer.write("<xml>\n");
164 writer.write("</xml>\n");
165 }
166 },
167 checkLines(firstLine, secondLine, thirdLine));
168 }
169
170 @Test
171 public void addLicenseToXMLWithoutDecl() throws IOException {
172 String filename = "tmp.xml";
173 final String firstLine = "<?xml version='1.0'?>";
174 final String secondLine = "<!--";
175
176 commonTestTemplate(filename, new FileCreator() {
177 public void createFile(Writer writer)
178 throws IOException {
179 writer.write("<xml>\n");
180 writer.write("</xml>\n");
181 }
182 },
183 checkLines(firstLine, secondLine));
184 }
185
186 @Test
187 public void addLicenseToHTML() throws IOException {
188 String filename = "tmp.html";
189 String commentLine = "<!--";
190
191 commonTestTemplate(filename, new FileCreator() {
192 public void createFile(Writer writer)
193 throws IOException {
194 writer.write("<html>\n");
195 writer.write("\n");
196 writer.write("</html>\n");
197 }
198 },
199 checkLines(commentLine));
200 }
201
202 @Test
203 public void addLicenseToCSS() throws IOException {
204 String filename = "tmp.css";
205 String firstLine = "/*";
206
207 commonTestTemplate(filename, new FileCreator() {
208 public void createFile(Writer writer)
209 throws IOException {
210 writer.write(".class {\n");
211 writer.write(" background-color: red;");
212 writer.write("}\n");
213 }
214 },
215 checkLines(firstLine));
216 }
217
218 @Test
219 public void addLicenseToJavascript() throws IOException {
220 String filename = "tmp.js";
221 String firstLine = "/*";
222
223 commonTestTemplate(filename, new FileCreator() {
224 public void createFile(Writer writer)
225 throws IOException {
226 writer.write("if (a ==b) {>\n");
227 writer.write(" alert(\"how useful!\");");
228 writer.write("}\n");
229 }
230 },
231 checkLines(firstLine));
232 }
233
234 @Test
235 public void addLicenseToAPT() throws IOException {
236 String filename = "tmp.apt";
237 String firstLine = "~~" + FIRST_LICENSE_LINE;
238
239 commonTestTemplate(filename, new FileCreator() {
240 public void createFile(Writer writer)
241 throws IOException {
242 writer.write("A Simple APT file");
243 writer.write(" This file contains nothing\n");
244 writer.write(" of any importance\n");
245 }
246 },
247 checkLines(firstLine));
248 }
249
250 @Test
251 public void addLicenseToProperties() throws IOException {
252 String filename = "tmp.properties";
253 String firstLine = "#" + FIRST_LICENSE_LINE;
254
255 commonTestTemplate(filename, new FileCreator() {
256 public void createFile(Writer writer)
257 throws IOException {
258 writer.write("property = value\n");
259 writer.write("fun = true\n");
260 writer.write("cool = true\n");
261 }
262 },
263 checkLines(firstLine));
264 }
265
266 @Test
267 public void addLicenseToScala() throws IOException {
268 String filename = "tmp.scala";
269 final String firstLine = "package foo {";
270 final String newFirstLine = "/*";
271
272 commonTestTemplate(filename, new FileCreator() {
273 public void createFile(Writer writer)
274 throws IOException {
275 writer.write(firstLine + "\n");
276 writer.write("\n");
277 writer.write(" object X { val x = 1; }\n");
278 writer.write("}\n");
279 }
280 },
281 new NewFileReader() {
282 public void readFile(BufferedReader reader)
283 throws IOException {
284 String line = reader.readLine();
285 assertEquals(
286 newFirstLine, line, "First line is incorrect");
287 while ((line = reader.readLine()) != null) {
288 if (line.length() == 0) {
289 line = reader.readLine();
290 break;
291 }
292 }
293 assertEquals(
294 firstLine, line, "Package line is incorrect");
295 }
296 });
297 }
298
299 @Test
300 public void addLicenseToRubyWithoutHashBang()
301 throws IOException {
302 String filename = "tmp.rb";
303 String firstLine = "#" + FIRST_LICENSE_LINE;
304
305 commonTestTemplate(filename, new FileCreator() {
306 public void createFile(Writer writer)
307 throws IOException {
308 writer.write("class Foo\n");
309 writer.write("end\n");
310 }
311 },
312 checkLines(firstLine));
313 }
314
315 @Test
316 public void addLicenseToRubyWithHashBang() throws IOException {
317 String filename = "tmp.rb";
318 final String firstLine = "#!/usr/bin/env ruby";
319 String secondLine = "#" + FIRST_LICENSE_LINE;
320
321 commonTestTemplate(filename, new FileCreator() {
322 public void createFile(Writer writer)
323 throws IOException {
324 writer.write(firstLine + "\n");
325 writer.write("class Foo\n");
326 writer.write("end\n");
327 }
328 },
329 checkLines(firstLine, secondLine));
330 }
331
332 @Test
333 public void addLicenseToPerlWithoutHashBang()
334 throws IOException {
335 String filename = "tmp.pl";
336 String firstLine = "#" + FIRST_LICENSE_LINE;
337
338 commonTestTemplate(filename, new FileCreator() {
339 public void createFile(Writer writer)
340 throws IOException {
341 writer.write("print \"Hello world\"\n");
342 }
343 },
344 checkLines(firstLine));
345 }
346
347 @Test
348 public void addLicenseToPerlWithHashBang() throws IOException {
349 String filename = "tmp.pl";
350 final String firstLine = "#!/usr/bin/env perl";
351 String secondLine = "#" + FIRST_LICENSE_LINE;
352
353 commonTestTemplate(filename, new FileCreator() {
354 public void createFile(Writer writer)
355 throws IOException {
356 writer.write(firstLine + "\n");
357 writer.write("print \"Hello world\"\n");
358 }
359 },
360 checkLines(firstLine, secondLine));
361 }
362
363 @Test
364 public void addLicenseToPerlModule() throws IOException {
365 String filename = "tmp.pm";
366 final String firstLine = "package API::TestAPI;";
367 final String secondLine = "";
368 final String thirdLine = "#" + FIRST_LICENSE_LINE;
369
370 commonTestTemplate(filename, new FileCreator() {
371 public void createFile(Writer writer)
372 throws IOException {
373 writer.write(firstLine + "\n");
374 writer.write("print \"Hello world\"\n");
375 }
376 },
377 checkLines(firstLine, secondLine, thirdLine));
378 }
379
380 @Test
381 public void addLicenseToTclWithoutHashBang()
382 throws IOException {
383 String filename = "tmp.tcl";
384 String firstLine = "#" + FIRST_LICENSE_LINE;
385
386 commonTestTemplate(filename, new FileCreator() {
387 public void createFile(Writer writer)
388 throws IOException {
389 writer.write("puts \"Hello world\"\n");
390 }
391 },
392 checkLines(firstLine));
393 }
394
395 @Test
396 public void addLicenseToTclWithHashBang() throws IOException {
397 String filename = "tmp.tcl";
398 final String firstLine = "#!/usr/bin/env tcl";
399 String secondLine = "#" + FIRST_LICENSE_LINE;
400
401 commonTestTemplate(filename, new FileCreator() {
402 public void createFile(Writer writer)
403 throws IOException {
404 writer.write(firstLine + "\n");
405 writer.write("puts \"Hello world\"\n");
406 }
407 },
408 checkLines(firstLine, secondLine));
409 }
410
411 @Test
412 public void addLicenseToPHP() throws IOException {
413 String filename = "tmp.php";
414 final String firstLine = "<?php";
415 final String secondLine = "";
416 final String thirdLine = "/*";
417
418 commonTestTemplate(filename, new FileCreator() {
419 public void createFile(Writer writer)
420 throws IOException {
421 writer.write(firstLine + "\n");
422 writer.write("echo 'Hello World'\n");
423 writer.write("?>\n");
424 }
425 },
426 checkLines(firstLine, secondLine, thirdLine));
427 }
428
429 @Test
430 public void addLicenseToCSharp() throws IOException {
431 String filename = "tmp.cs";
432 String firstLine = "/*";
433
434 commonTestTemplate(filename, new FileCreator() {
435 public void createFile(Writer writer)
436 throws IOException {
437 writer.write("namespace org.example {\n");
438 writer.write(" public class Foo {\n");
439 writer.write(" }\n");
440 writer.write("}\n");
441 }
442 },
443 checkLines(firstLine));
444 }
445
446 @Test
447 public void addLicenseToGroovy() throws IOException {
448 String filename = "tmp.groovy";
449 String firstLine = "/*";
450
451 commonTestTemplate(filename, new FileCreator() {
452 public void createFile(Writer writer)
453 throws IOException {
454 writer.write("package org.example \n");
455 writer.write(" class Foo {\n");
456 writer.write(" }\n");
457 }
458 },
459 checkLines(firstLine));
460 }
461
462 @Test
463 public void addLicenseToCPlusPlus() throws IOException {
464 String filename = "tmp.cpp";
465 String firstLine = "/*";
466
467 commonTestTemplate(filename, new FileCreator() {
468 public void createFile(Writer writer)
469 throws IOException {
470 writer.write("namespace org.example {\n");
471 writer.write(" public class Foo {\n");
472 writer.write(" }\n");
473 writer.write("}\n");
474 }
475 },
476 checkLines(firstLine));
477 }
478
479 @Test
480 public void addLicenseToGo() throws IOException {
481 String filename = "tmp.go";
482 final String firstLine = "package main";
483 String secondLine = "";
484 String thirdLine = "/*";
485
486
487
488 commonTestTemplate(filename, new FileCreator() {
489 public void createFile(Writer writer)
490 throws IOException {
491 writer.write(firstLine + "\n");
492 writer.write("import (\n");
493 writer.write(" log\n");
494 writer.write(")\n");
495 }
496 },
497 checkLines(firstLine, secondLine, thirdLine));
498 }
499
500 @Test
501 public void fileWithBOM() throws IOException {
502 File f = Resources.getResourceFile("violations/FilterTest.cs");
503
504 ApacheV2LicenseAppender appender =
505 new ApacheV2LicenseAppender(DefaultLog.INSTANCE);
506 appender.append(f);
507
508 try (BufferedReader r = new BufferedReader(new FileReader(f.getAbsolutePath()
509 + ".new"))) {
510 assertEquals("/*", r.readLine());
511 String line = null;
512 while ((line = r.readLine()) != null) {
513 if (line.trim().length() == 0) {
514 break;
515 }
516 }
517 assertEquals("#if NET_2_0", r.readLine());
518 }
519 }
520
521 @Test
522 public void addLicenseToVS2003solution() throws IOException {
523 String filename = "tmp.sln";
524 final String firstLine = "Microsoft Visual Studio Solution File,"
525 + " Format Version 8.0";
526 String secondLine = "#" + FIRST_LICENSE_LINE;
527
528 commonTestTemplate(filename, new FileCreator() {
529 public void createFile(Writer writer)
530 throws IOException {
531 writer.write(firstLine + "\n");
532 writer.write("Project(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"ConsoleApp\", \"Tutorials\\ConsoleApp\\cs\\src\\ConsoleApp.csproj\", \"{933969DF-2BC5-44E6-8B1A-400FC276A23F}\"\n");
533 writer.write("\tProjectSection(WebsiteProperties) = preProject\n");
534 writer.write("\t\tDebug.AspNetCompiler.Debug = \"True\"\n");
535 writer.write("\t\tRelease.AspNetCompiler.Debug = \"False\"\n");
536 writer.write("\tEndProjectSection\n");
537 writer.write("EndProject\n");
538 }
539 },
540 checkLines(firstLine, secondLine));
541 }
542
543 @Test
544 public void addLicenseToVS2005solution() throws IOException {
545 String filename = "tmp.sln";
546 final String firstLine = "Microsoft Visual Studio Solution File,"
547 + " Format Version 9.0";
548 final String secondLine = "# Visual Studio 2005";
549 final String thirdLine = "#" + FIRST_LICENSE_LINE;
550
551 commonTestTemplate(filename, new FileCreator() {
552 public void createFile(Writer writer)
553 throws IOException {
554 writer.write(firstLine + "\n");
555 writer.write(secondLine + "\n");
556 writer.write("Project(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"ConsoleApp\", \"Tutorials\\ConsoleApp\\cs\\src\\ConsoleApp.csproj\", \"{933969DF-2BC5-44E6-8B1A-400FC276A23F}\"\n");
557 writer.write("\tProjectSection(WebsiteProperties) = preProject\n");
558 writer.write("\t\tDebug.AspNetCompiler.Debug = \"True\"\n");
559 writer.write("\t\tRelease.AspNetCompiler.Debug = \"False\"\n");
560 writer.write("\tEndProjectSection\n");
561 writer.write("EndProject\n");
562 }
563 },
564 checkLines(firstLine, secondLine, thirdLine)
565 );
566 }
567
568 @Test
569 public void addLicenseToVS2010ExpressSolution() throws IOException {
570 String filename = "tmp.sln";
571 final String firstLine = "Microsoft Visual Studio Solution File, "
572 + "Format Version 11.00";
573 final String secondLine = "# Visual C# Express 2010";
574 final String thirdLine = "#" + FIRST_LICENSE_LINE;
575
576 commonTestTemplate(filename, new FileCreator() {
577 public void createFile(Writer writer)
578 throws IOException {
579 writer.write(firstLine + "\n");
580 writer.write(secondLine + "\n");
581 writer.write("Project(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"Lucene.Net\", \"..\\..\\..\\src\\core\\Lucene.Net.csproj\", \"{5D4AD9BE-1FFB-41AB-9943-25737971BF57}\"\n");
582 writer.write("EndProject\n");
583 writer.write("Project(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"Contrib.Highlighter\", \"..\\..\\..\\src\\contrib\\Highlighter\\Contrib.Highlighter.csproj\", \"{901D5415-383C-4AA6-A256-879558841BEA}\"\n");
584 writer.write("EndProject\n");
585 writer.write("Global\n");
586 writer.write("GlobalSection(SolutionConfigurationPlatforms) = preSolution\n");
587 writer.write("Debug|Any CPU = Debug|Any CPU\n");
588 writer.write("Release|Any CPU = Release|Any CPU\n");
589 writer.write("EndGlobalSection\n");
590 writer.write("GlobalSection(ProjectConfigurationPlatforms) = postSolution\n");
591 writer.write("{5D4AD9BE-1FFB-41AB-9943-25737971BF57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n");
592 writer.write("{5D4AD9BE-1FFB-41AB-9943-25737971BF57}.Debug|Any CPU.Build.0 = Debug|Any CPU\n");
593 writer.write("{5D4AD9BE-1FFB-41AB-9943-25737971BF57}.Release|Any CPU.ActiveCfg = Release|Any CPU\n");
594 writer.write("{5D4AD9BE-1FFB-41AB-9943-25737971BF57}.Release|Any CPU.Build.0 = Release|Any CPU\n");
595 writer.write("{901D5415-383C-4AA6-A256-879558841BEA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n");
596 writer.write("{901D5415-383C-4AA6-A256-879558841BEA}.Debug|Any CPU.Build.0 = Debug|Any CPU\n");
597 writer.write("{901D5415-383C-4AA6-A256-879558841BEA}.Release|Any CPU.ActiveCfg = Release|Any CPU\n");
598 writer.write("{901D5415-383C-4AA6-A256-879558841BEA}.Release|Any CPU.Build.0 = Release|Any CPU\n");
599 writer.write("EndGlobalSection\n");
600 writer.write("GlobalSection(SolutionProperties) = preSolution\n");
601 writer.write("HideSolutionNode = FALSE\n");
602 writer.write("EndGlobalSection\n");
603 writer.write("EndGlobal \n");
604 }
605 },
606 checkLines(firstLine, secondLine, thirdLine)
607 );
608 }
609
610 @Test
611 public void addLicenseToVS2010SolutionWithBlankLine() throws IOException {
612 String filename = "tmp.sln";
613 final String firstLine = "";
614 final String secondLine = "Microsoft Visual Studio Solution File, "
615 + "Format Version 11.00";
616 final String thirdLine = "# Visual C# Express 2010";
617 final String forthLine = "#" + FIRST_LICENSE_LINE;
618
619 commonTestTemplate(filename, new FileCreator() {
620 public void createFile(Writer writer)
621 throws IOException {
622 writer.write(firstLine + "\n");
623 writer.write(secondLine + "\n");
624 writer.write(thirdLine + "\n");
625 writer.write("Project(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"Lucene.Net\", \"..\\..\\..\\src\\core\\Lucene.Net.csproj\", \"{5D4AD9BE-1FFB-41AB-9943-25737971BF57}\"\n");
626 writer.write("EndProject\n");
627 writer.write("Project(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"Contrib.Highlighter\", \"..\\..\\..\\src\\contrib\\Highlighter\\Contrib.Highlighter.csproj\", \"{901D5415-383C-4AA6-A256-879558841BEA}\"\n");
628 writer.write("EndProject\n");
629 writer.write("Global\n");
630 writer.write("GlobalSection(SolutionConfigurationPlatforms) = preSolution\n");
631 writer.write("Debug|Any CPU = Debug|Any CPU\n");
632 writer.write("Release|Any CPU = Release|Any CPU\n");
633 writer.write("EndGlobalSection\n");
634 writer.write("GlobalSection(ProjectConfigurationPlatforms) = postSolution\n");
635 writer.write("{5D4AD9BE-1FFB-41AB-9943-25737971BF57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n");
636 writer.write("{5D4AD9BE-1FFB-41AB-9943-25737971BF57}.Debug|Any CPU.Build.0 = Debug|Any CPU\n");
637 writer.write("{5D4AD9BE-1FFB-41AB-9943-25737971BF57}.Release|Any CPU.ActiveCfg = Release|Any CPU\n");
638 writer.write("{5D4AD9BE-1FFB-41AB-9943-25737971BF57}.Release|Any CPU.Build.0 = Release|Any CPU\n");
639 writer.write("{901D5415-383C-4AA6-A256-879558841BEA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n");
640 writer.write("{901D5415-383C-4AA6-A256-879558841BEA}.Debug|Any CPU.Build.0 = Debug|Any CPU\n");
641 writer.write("{901D5415-383C-4AA6-A256-879558841BEA}.Release|Any CPU.ActiveCfg = Release|Any CPU\n");
642 writer.write("{901D5415-383C-4AA6-A256-879558841BEA}.Release|Any CPU.Build.0 = Release|Any CPU\n");
643 writer.write("EndGlobalSection\n");
644 writer.write("GlobalSection(SolutionProperties) = preSolution\n");
645 writer.write("HideSolutionNode = FALSE\n");
646 writer.write("EndGlobalSection\n");
647 writer.write("EndGlobal \n");
648 }
649 },
650 checkLines(firstLine, secondLine, thirdLine, forthLine)
651 );
652 }
653
654 @Test
655 public void addLicenseMarkdown() throws IOException {
656 String filename = "tmp.md";
657
658 commonTestTemplate(filename, new FileCreator() {
659 public void createFile(Writer writer)
660 throws IOException {
661 writer.write("## This is the first header\n");
662 writer.write(" * this is a list entry\n");
663 writer.write(" * this is another list entry\n");
664 writer.write(" <!-- this is a comment line -->");
665 writer.write("## This is the second header\n");
666 }
667 },
668 new NewFileReader() {
669 public void readFile(BufferedReader r) throws IOException {
670 String line = r.readLine();
671 assertEquals(
672 "<!--", line, "First line is incorrect");
673 line = r.readLine();
674 }
675 });
676 }
677 }