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