You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@creadur.apache.org by rd...@apache.org on 2008/07/06 23:06:06 UTC
svn commit: r674346 - in /incubator/rat/main/trunk/rat/rat-core: ./
src/main/java/org/apache/rat/ src/main/java/org/apache/rat/annotation/
src/test/java/org/apache/rat/annotation/
Author: rdonkin
Date: Sun Jul 6 14:06:05 2008
New Revision: 674346
URL: http://svn.apache.org/viewvc?rev=674346&view=rev
Log:
RAT-30 Optionally add licence headers to those currently without one, https://issues.apache.org/jira/browse/RAT-30. Contributed by Ross Gardler.
Added:
incubator/rat/main/trunk/rat/rat-core/src/main/java/org/apache/rat/annotation/
incubator/rat/main/trunk/rat/rat-core/src/main/java/org/apache/rat/annotation/AbstractLicenceAppender.java
incubator/rat/main/trunk/rat/rat-core/src/main/java/org/apache/rat/annotation/ApacheV2LicenceAppender.java
incubator/rat/main/trunk/rat/rat-core/src/test/java/org/apache/rat/annotation/
incubator/rat/main/trunk/rat/rat-core/src/test/java/org/apache/rat/annotation/TestLicenceAppender.java
Modified:
incubator/rat/main/trunk/rat/rat-core/pom.xml
incubator/rat/main/trunk/rat/rat-core/src/main/java/org/apache/rat/Report.java
Modified: incubator/rat/main/trunk/rat/rat-core/pom.xml
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/rat/rat-core/pom.xml?rev=674346&r1=674345&r2=674346&view=diff
==============================================================================
--- incubator/rat/main/trunk/rat/rat-core/pom.xml (original)
+++ incubator/rat/main/trunk/rat/rat-core/pom.xml Sun Jul 6 14:06:05 2008
@@ -39,14 +39,23 @@
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
+ <version>3.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
+ <version>2.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
+ <version>3.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-cli</groupId>
+ <artifactId>commons-cli</artifactId>
+ <version>1.1</version>
</dependency>
</dependencies>
</project>
Modified: incubator/rat/main/trunk/rat/rat-core/src/main/java/org/apache/rat/Report.java
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/rat/rat-core/src/main/java/org/apache/rat/Report.java?rev=674346&r1=674345&r2=674346&view=diff
==============================================================================
--- incubator/rat/main/trunk/rat/rat-core/src/main/java/org/apache/rat/Report.java (original)
+++ incubator/rat/main/trunk/rat/rat-core/src/main/java/org/apache/rat/Report.java Sun Jul 6 14:06:05 2008
@@ -18,19 +18,31 @@
*/
package org.apache.rat;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.PrintStream;
import java.io.Writer;
+import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerConfigurationException;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PosixParser;
import org.apache.rat.analysis.IHeaderMatcher;
+import org.apache.rat.annotation.AbstractLicenceAppender;
+import org.apache.rat.annotation.ApacheV2LicenceAppender;
import org.apache.rat.license.ILicenseFamily;
import org.apache.rat.report.IReportable;
import org.apache.rat.report.RatReport;
@@ -38,31 +50,135 @@
import org.apache.rat.report.xml.XmlReportFactory;
import org.apache.rat.report.xml.writer.IXmlWriter;
import org.apache.rat.report.xml.writer.impl.base.XmlWriter;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
public class Report {
- public static final void main(String args[]) throws Exception {
- if (args == null || args.length != 1) {
- printUsage();
+ //@SuppressWarnings("unchecked")
+ public static final void main(String args[]) throws Exception {
+ Options opts = new Options();
+
+ Option help = new Option("h", "help", false,
+ "Print help for the RAT command line interface and exit");
+ opts.addOption(help);
+
+ Option addLicence = new Option(
+ "a",
+ "addLicence",
+ false,
+ "Add the default licence header to any file with an unknown licence that is not in the exclusion list. By default new files will be created with the licence header, to force the modification of existing files use the --force option.");
+ opts.addOption(addLicence);
+
+ Option write = new Option(
+ "f",
+ "force",
+ false,
+ "Forces any changes in files to be written without confirmation");
+ opts.addOption(write);
+
+ Option copyright = new Option(
+ "c",
+ "copyright",
+ true,
+ "The copyright message to use in the licence headers, usually in the form of \"Copyright 2008 Foo\"");
+ opts.addOption(copyright);
+
+ Option xml = new Option(
+ "x",
+ "xml",
+ false,
+ "Output the report in XML format");
+ opts.addOption(xml);
+
+ PosixParser parser = new PosixParser();
+ CommandLine cl = null;
+ try {
+ cl = parser.parse(opts, args);
+ } catch (ParseException e) {
+ System.err.println("Please use the \"--help\" option to see a list of valid commands and options");
+ System.exit(1);
+ }
+
+ if (cl.hasOption('h')) {
+ printUsage(opts);
+ }
+
+
+ args = cl.getArgs();
+ if (args == null || args.length != 1) {
+ printUsage(opts);
} else {
- Report report = new Report(args[0]);
- //report.report(System.out);
- report.styleReport(System.out);
- }
+ Report report = new Report(args[0]);
+
+ if (cl.hasOption('a')) {
+ OutputStream reportOutput = new ByteArrayOutputStream();
+ PrintStream stream = new PrintStream(reportOutput, true);
+ report.report(stream);
+
+ AbstractLicenceAppender appender;
+ String copyrightMsg = cl.getOptionValue("c");
+ if ( copyrightMsg != null) {
+ appender = new ApacheV2LicenceAppender(copyrightMsg);
+ } else {
+ appender = new ApacheV2LicenceAppender();
+ }
+ if (cl.hasOption("f")) {
+ appender.setForce(true);
+ }
+
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setValidating(false);
+ ByteArrayInputStream xmlStream = new ByteArrayInputStream(reportOutput.toString().getBytes("UTF-8"));
+ Document doc = factory.newDocumentBuilder().parse(xmlStream);
+
+ NodeList resourceHeaders = doc.getElementsByTagName("header-type");
+ String value = null;
+ for (int i = 0; i < resourceHeaders.getLength(); i++) {
+ Node headerType = resourceHeaders.item(i).getAttributes().getNamedItem("name");
+ if(headerType != null) {
+ value = headerType.getNodeValue();
+ } else {
+ value = null;
+ }
+ if (value != null &&value.equals("?????")) {
+ Node resource = resourceHeaders.item(i).getParentNode();
+ String filename = resource.getAttributes().getNamedItem("name").getNodeValue();
+ File document = new File(filename);
+ appender.append(document);
+ }
+ }
+ }
+
+ if (cl.hasOption('x')) {
+ report.report(System.out);
+ } else {
+ report.styleReport(System.out);
+ }
+ }
}
- private static final void printUsage() {
- System.out.println("Usage: <directory>");
- System.out.println(" where ");
- System.out.println( "<directory> is the base directory to be audited.");
- System.out.println("NOTE: RAT is really little more than a grep ATM");
- System.out.println(" RAT is also rather memory hungry ATM");
- System.out.println(" RAT is very basic ATM");
- System.out.println(" RAT ATM runs on unpacked releases");
- System.out.println(" RAT highlights possible issues");
- System.out.println(" RAT reports require intepretation");
- System.out.println(" RAT often requires some tuning before it runs well against a project");
- System.out.println(" RAT relies on heuristics: it may miss issues");
+ private static final void printUsage(Options opts) {
+ HelpFormatter f = new HelpFormatter();
+
+ String header = "Options";
+
+ StringBuffer footer = new StringBuffer("\n");
+ footer.append("NOTE:\n");
+ footer.append("RAT is really little more than a grep ATM\n");
+ footer.append("RAT is also rather memory hungry ATM\n");
+ footer.append("RAT is very basic ATM\n");
+ footer.append("RAT ATM runs on unpacked releases\n");
+ footer.append("RAT highlights possible issues\n");
+ footer.append("RAT reports require intepretation\n");
+ footer.append("RAT often requires some tuning before it runs well against a project\n");
+ footer.append("RAT relies on heuristics: it may miss issues\n");
+
+ f.printHelp("java rat.report [options] [DIR]",
+ header, opts, footer.toString(), false);
+ System.exit(0);
}
private final String baseDirectory;
Added: incubator/rat/main/trunk/rat/rat-core/src/main/java/org/apache/rat/annotation/AbstractLicenceAppender.java
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/rat/rat-core/src/main/java/org/apache/rat/annotation/AbstractLicenceAppender.java?rev=674346&view=auto
==============================================================================
--- incubator/rat/main/trunk/rat/rat-core/src/main/java/org/apache/rat/annotation/AbstractLicenceAppender.java (added)
+++ incubator/rat/main/trunk/rat/rat-core/src/main/java/org/apache/rat/annotation/AbstractLicenceAppender.java Sun Jul 6 14:06:05 2008
@@ -0,0 +1,205 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ */
+package org.apache.rat.annotation;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+
+/**
+ * Add a licence header to a document. This appender does not check for the
+ * existence of an existing licence header, it is assumed that either a second
+ * licence header is intentional or that there is no licence header present
+ * already.
+ *
+ */
+public abstract class AbstractLicenceAppender {
+ private static final int TYPE_UNKNOWN = 0;
+ private static final int TYPE_JAVA = 1;
+ private static final int TYPE_XML = 2;
+ private static final int TYPE_HTML = 3;
+ private static final int TYPE_CSS = 4;
+ private static final int TYPE_JAVASCRIPT = 5;
+ private static final int TYPE_APT = 6;
+ private static final int TYPE_PROPERTIES = 7;
+ private boolean isForced;
+
+ public AbstractLicenceAppender() {
+ super();
+ }
+
+ /**
+ * Append the default licence header to the supplied document.
+ *
+ * @param document
+ * @throws IOException
+ * if there is a problem either reading or writing the file
+ */
+ public void append(File document) throws IOException {
+ int type = getType(document);
+ if (type == TYPE_UNKNOWN) {
+ return;
+ }
+ File newDocument = new File(document.getAbsolutePath() + ".new");
+ FileWriter writer = new FileWriter(newDocument);
+
+ FileReader fr = new FileReader(document);
+ BufferedReader br = new BufferedReader(fr);
+
+ if (type == TYPE_CSS
+ || type == TYPE_JAVASCRIPT
+ || type == TYPE_APT
+ || type == TYPE_PROPERTIES
+ || type == TYPE_HTML) {
+ writer.append(getLicenceHeader(document));
+ writer.append("\n");
+ }
+
+ String line;
+ while ((line = br.readLine()) != null) {
+ writer.append(line);
+ writer.append("\n");
+ if (type == TYPE_JAVA && line.startsWith("package ")) {
+ writer.append(getLicenceHeader(document));
+ writer.append("\n");
+ }
+ if (type == TYPE_XML && line.startsWith("<?xml ")) {
+ writer.append(getLicenceHeader(document));
+ writer.append("\n");
+ }
+ }
+ br.close();
+ writer.close();
+
+ if (isForced) {
+ document.delete();
+ boolean renamed = newDocument.renameTo(document.getAbsoluteFile());
+ if (!renamed) {
+ System.err.println("Failed to rename new file, original file remains unchanged.");
+ }
+ }
+ }
+
+ /**
+ * Detect the type of document.
+ *
+ * @param document
+ * @return
+ * @TODO use existing mechanism to detect the type of a file and record it in the report output, thus we will not need this duplication here.
+ */
+ protected int getType(File document) {
+ if (document.getPath().endsWith(".java")) {
+ return TYPE_JAVA;
+ } else if (document.getPath().endsWith(".xml") || document.getPath().endsWith(".xsl")) {
+ return TYPE_XML;
+ } else if (document.getPath().endsWith(".html") || document.getPath().endsWith(".htm")) {
+ return TYPE_HTML;
+ } else if (document.getPath().endsWith(".css")) {
+ return TYPE_CSS;
+ } else if (document.getPath().endsWith(".js")) {
+ return TYPE_JAVASCRIPT;
+ } else if (document.getPath().endsWith(".apt")) {
+ return TYPE_APT;
+ } else if (document.getPath().endsWith(".properties")) {
+ return TYPE_PROPERTIES;
+ }
+ return TYPE_UNKNOWN;
+ }
+
+ /**
+ * Set the force flag on this appender. If this flag is set
+ * to true then files will be modified directly, otherwise
+ * new files will be created alongside the existing files.
+ *
+ * @param b
+ */
+ public void setForce(boolean b) {
+ isForced = b;
+ }
+
+ /**
+ * Get the licence header for a document.
+ */
+ public abstract String getLicenceHeader(File document);
+
+ /**
+ * Get the first line of the licence header formatted
+ * for the given type of file.
+ *
+ * @param type the type of file, see the TYPE_* constants
+ * @return
+ */
+ protected String getFirstLine(int type) {
+ switch (type) {
+ case TYPE_JAVA: return "/*\n";
+ case TYPE_XML: return "<!--\n";
+ case TYPE_HTML: return "<!--\n";
+ case TYPE_CSS: return "/*\n";
+ case TYPE_JAVASCRIPT: return "/*\n";
+ case TYPE_APT: return "~~\n";
+ case TYPE_PROPERTIES: return "#\n";
+ default: return "";
+ }
+ }
+
+
+ /**
+ * Get the last line of the licence header formatted
+ * for the given type of file.
+ *
+ * @param type the type of file, see the TYPE_* constants
+ * @return
+ */
+ protected String getLastLine(int type) {
+ switch (type) {
+ case TYPE_JAVA: return " */\n";
+ case TYPE_XML: return "-->\n";
+ case TYPE_HTML: return "-->\n";
+ case TYPE_CSS: return " */\n";
+ case TYPE_JAVASCRIPT: return " */\n";
+ case TYPE_APT: return "~~\n";
+ case TYPE_PROPERTIES: return "#\n";
+ default: return "";
+ }
+ }
+
+
+ /**
+ * Get a line of the licence header formatted
+ * for the given type of file.
+ *
+ * @param type the type of file, see the TYPE_* constants
+ * @param content the content for this line
+ * @return
+ */
+ protected String getLine(int type, String content) {
+ switch (type) {
+ case TYPE_JAVA: return " * " + content + "\n";
+ case TYPE_XML: return " " + content + "\n";
+ case TYPE_HTML: return " " + content + "\n";
+ case TYPE_CSS: return " * " + content + "\n";
+ case TYPE_JAVASCRIPT: return " * " + content + "\n";
+ case TYPE_APT: return "~~ " + content + "\n";
+ case TYPE_PROPERTIES: return "# " + content + "\n";
+ default: return "";
+ }
+ }
+}
\ No newline at end of file
Added: incubator/rat/main/trunk/rat/rat-core/src/main/java/org/apache/rat/annotation/ApacheV2LicenceAppender.java
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/rat/rat-core/src/main/java/org/apache/rat/annotation/ApacheV2LicenceAppender.java?rev=674346&view=auto
==============================================================================
--- incubator/rat/main/trunk/rat/rat-core/src/main/java/org/apache/rat/annotation/ApacheV2LicenceAppender.java (added)
+++ incubator/rat/main/trunk/rat/rat-core/src/main/java/org/apache/rat/annotation/ApacheV2LicenceAppender.java Sun Jul 6 14:06:05 2008
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ */
+package org.apache.rat.annotation;
+
+import java.io.File;
+
+
+/**
+ * Add an Apache Software Licence V2 licence header to a
+ * document. This appender does not check for the
+ * existence of an existing licence header, it is assumed that either a second
+ * licence header is intentional or that there is no licence header present
+ * already.
+ *
+ */
+public class ApacheV2LicenceAppender extends AbstractLicenceAppender {
+
+ private String copyright;
+
+ /**
+ * Create a licence appender with the standard ASF licence header.
+ */
+ public ApacheV2LicenceAppender() {
+ super();
+ }
+
+ /**
+ * Create a licence appender with the given copyright line. This should be of
+ * the form "Copyright 2008 Foo"
+ *
+ * @param copyright
+ */
+ public ApacheV2LicenceAppender(String copyright) {
+ super();
+ this.copyright = copyright;
+ }
+
+ //@Override
+ public String getLicenceHeader(File document) {
+ int type = getType(document);
+ StringBuffer sb = new StringBuffer();
+ if (copyright == null) {
+ sb.append(getFirstLine(type));
+ sb.append(getLine(type, ""));
+ sb.append(getLine(type, "Licensed to the Apache Software Foundation (ASF) under one"));
+ sb.append(getLine(type, "or more contributor license agreements. See the NOTICE file"));
+ sb.append(getLine(type, "distributed with this work for additional information"));
+ sb.append(getLine(type, "regarding copyright ownership. The ASF licenses this file"));
+ sb.append(getLine(type, "to you under the Apache License, Version 2.0 (the"));
+ sb.append(getLine(type, "\"License\"); you may not use this file except in compliance"));
+ sb.append(getLine(type, "with the License. You may obtain a copy of the License at"));
+ sb.append(getLine(type, ""));
+ sb.append(getLine(type, " http://www.apache.org/licenses/LICENSE-2.0"));
+ sb.append(getLine(type, ""));
+ sb.append(getLine(type, "Unless required by applicable law or agreed to in writing,"));
+ sb.append(getLine(type, "software distributed under the License is distributed on an"));
+ sb.append(getLine(type, "\"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY"));
+ sb.append(getLine(type, "KIND, either express or implied. See the License for the"));
+ sb.append(getLine(type, "specific language governing permissions and limitations"));
+ sb.append(getLine(type, "under the License."));
+ sb.append(getLine(type, ""));
+ sb.append(getLastLine(type));
+ } else {
+ sb.append(getFirstLine(type));
+ sb.append(getLine(type, ""));
+ sb.append(copyright);
+ sb.append(getLine(type, ""));
+ sb.append(getLine(type, "Licensed under the Apache License, Version 2.0 (the \"License\");"));
+ sb.append(getLine(type, "you may not use this file except in compliance with the License."));
+ sb.append(getLine(type, "You may obtain a copy of the License at"));
+ sb.append(getLine(type, ""));
+ sb.append(getLine(type, " http://www.apache.org/licenses/LICENSE-2.0"));
+ sb.append(getLine(type, ""));
+ sb.append(getLine(type, "Unless required by applicable law or agreed to in writing,"));
+ sb.append(getLine(type, "software distributed under the License is distributed on an"));
+ sb.append(getLine(type, "\"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY"));
+ sb.append(getLine(type, "KIND, either express or implied. See the License for the"));
+ sb.append(getLine(type, "specific language governing permissions and limitations"));
+ sb.append(getLine(type, "under the License."));
+ sb.append(getLine(type, ""));
+ sb.append(getLastLine(type));
+ }
+ return sb.toString();
+ }
+
+
+
+}
Added: incubator/rat/main/trunk/rat/rat-core/src/test/java/org/apache/rat/annotation/TestLicenceAppender.java
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/rat/rat-core/src/test/java/org/apache/rat/annotation/TestLicenceAppender.java?rev=674346&view=auto
==============================================================================
--- incubator/rat/main/trunk/rat/rat-core/src/test/java/org/apache/rat/annotation/TestLicenceAppender.java (added)
+++ incubator/rat/main/trunk/rat/rat-core/src/test/java/org/apache/rat/annotation/TestLicenceAppender.java Sun Jul 6 14:06:05 2008
@@ -0,0 +1,218 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ */
+package org.apache.rat.annotation;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+public class TestLicenceAppender extends TestCase {
+
+ public void testAddLicenceToUnknownFile() throws IOException {
+ String filename = "tmp.unkownTyoe";
+ File file = new File(System.getProperty("java.io.tmpdir") + File.separator + filename);
+ FileWriter writer = new FileWriter(file);
+ writer.append("Unkown file type\n");
+ writer.close();
+
+ ApacheV2LicenceAppender appender = new ApacheV2LicenceAppender();
+ appender.append(file);
+
+ File newFile = new File(System.getProperty("java.io.tmpdir") + File.separator + filename + ".new");
+ assertFalse("No new file should have been written", newFile.exists());
+ }
+
+ public void testAddLicenceToJava() throws IOException {
+ String filename = "tmp.java";
+ String firstLine = "package foo;";
+ String secondLine = "/*";
+
+ File file = new File(System.getProperty("java.io.tmpdir") + File.separator + filename);
+ FileWriter writer = new FileWriter(file);
+ writer.append(firstLine + "\n");
+ writer.append("\n");
+ writer.append("public class test {\n");
+ writer.append("}\n");
+ writer.close();
+
+ ApacheV2LicenceAppender appender = new ApacheV2LicenceAppender();
+ appender.append(file);
+
+ File newFile = new File(System.getProperty("java.io.tmpdir") + File.separator + filename + ".new");
+ BufferedReader reader = new BufferedReader(new FileReader(newFile));
+ String line = reader.readLine();
+ assertEquals("First line is incorrect", firstLine, line);
+ line = reader.readLine();
+ assertEquals("Second line is incorrect", secondLine, line);
+
+ file.delete();
+ newFile.delete();
+ }
+
+ public void testAddLicenceToXML() throws IOException {
+ String filename = "tmp.xml";
+ String firstLine = "<?xml version='1.0'?>";
+ String secondLine = "<!--";
+
+ File file = new File(System.getProperty("java.io.tmpdir") + File.separator + filename);
+ FileWriter writer = new FileWriter(file);
+ writer.append(firstLine + "\n");
+ writer.append("\n");
+ writer.append("<xml>\n");
+ writer.append("</xml>\n");
+ writer.close();
+
+ ApacheV2LicenceAppender appender = new ApacheV2LicenceAppender();
+ appender.append(file);
+
+ File newFile = new File(System.getProperty("java.io.tmpdir") + File.separator + filename + ".new");
+
+ BufferedReader reader = new BufferedReader(new FileReader(newFile));
+ String line = reader.readLine();
+ assertEquals("First line is incorrect", firstLine, line);
+ line = reader.readLine();
+ assertEquals("Second line is incorrect", secondLine, line);
+
+ file.delete();
+ newFile.delete();
+ }
+ public void testAddLicenceToHTML() throws IOException {
+ String filename = "tmp.html";
+ String commentLine = "<!--";
+
+ File file = new File(System.getProperty("java.io.tmpdir") + File.separator + filename);
+ FileWriter writer = new FileWriter(file);
+ writer.append("<html>\n");
+ writer.append("\n");
+ writer.append("</html>\n");
+ writer.close();
+
+ ApacheV2LicenceAppender appender = new ApacheV2LicenceAppender();
+ appender.append(file);
+
+ File newFile = new File(System.getProperty("java.io.tmpdir") + File.separator + filename + ".new");
+
+ BufferedReader reader = new BufferedReader(new FileReader(newFile));
+ String line = reader.readLine();
+ assertEquals("First line is incorrect", commentLine, line);
+
+ file.delete();
+ newFile.delete();
+ }
+
+ public void testAddLicenceToCSS() throws IOException {
+ String filename = "tmp.css";
+ String firstLine = "/*";
+
+ File file = new File(System.getProperty("java.io.tmpdir") + File.separator + filename);
+ FileWriter writer = new FileWriter(file);
+ writer.append(".class {\n");
+ writer.append(" background-color: red;");
+ writer.append("}\n");
+ writer.close();
+
+ ApacheV2LicenceAppender appender = new ApacheV2LicenceAppender();
+ appender.append(file);
+
+ File newFile = new File(System.getProperty("java.io.tmpdir") + File.separator + filename + ".new");
+
+ BufferedReader reader = new BufferedReader(new FileReader(newFile));
+ String line = reader.readLine();
+ assertEquals("First line is incorrect", firstLine, line);
+
+ file.delete();
+ newFile.delete();
+ }
+
+ public void testAddLicenceToJavascript() throws IOException {
+ String filename = "tmp.js";
+ String firstLine = "/*";
+
+ File file = new File(System.getProperty("java.io.tmpdir") + File.separator + filename);
+ FileWriter writer = new FileWriter(file);
+ writer.append("if (a ==b) {>\n");
+ writer.append(" alert(\"how useful!\");");
+ writer.append("}\n");
+ writer.close();
+
+ ApacheV2LicenceAppender appender = new ApacheV2LicenceAppender();
+ appender.append(file);
+
+ File newFile = new File(System.getProperty("java.io.tmpdir") + File.separator + filename + ".new");
+
+ BufferedReader reader = new BufferedReader(new FileReader(newFile));
+ String line = reader.readLine();
+ assertEquals("First line is incorrect", firstLine, line);
+
+ file.delete();
+ newFile.delete();
+ }
+
+ public void testAddLicenceToAPT() throws IOException {
+ String filename = "tmp.apt";
+ String firstLine = "~~";
+
+ File file = new File(System.getProperty("java.io.tmpdir") + File.separator + filename);
+ FileWriter writer = new FileWriter(file);
+ writer.append("A Simple APT file");
+ writer.append(" This file contains nothing\n");
+ writer.append(" of any importance\n");
+ writer.close();
+
+ ApacheV2LicenceAppender appender = new ApacheV2LicenceAppender();
+ appender.append(file);
+
+ File newFile = new File(System.getProperty("java.io.tmpdir") + File.separator + filename + ".new");
+
+ BufferedReader reader = new BufferedReader(new FileReader(newFile));
+ String line = reader.readLine();
+ assertEquals("First line is incorrect", firstLine, line);
+
+ file.delete();
+ newFile.delete();
+ }
+
+ public void testAddLicenceToProperties() throws IOException {
+ String filename = "tmp.properties";
+ String firstLine = "#";
+
+ File file = new File(System.getProperty("java.io.tmpdir") + File.separator + filename);
+ FileWriter writer = new FileWriter(file);
+ writer.append("property = value\n");
+ writer.append("fun = true\n");
+ writer.append("cool = true\n");
+ writer.close();
+
+ ApacheV2LicenceAppender appender = new ApacheV2LicenceAppender();
+ appender.append(file);
+
+ File newFile = new File(System.getProperty("java.io.tmpdir") + File.separator + filename + ".new");
+
+ BufferedReader reader = new BufferedReader(new FileReader(newFile));
+ String line = reader.readLine();
+ assertEquals("First line is incorrect", firstLine, line);
+
+ file.delete();
+ newFile.delete();
+ }
+}