RAT-202: Replace manual creation of temporary files with junit Rule.
* Add constants for XML report elements/attributes.
git-svn-id: https://svn.apache.org/repos/asf/creadur/rat/trunk@1682131 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/apache-rat-core/src/main/java/org/apache/rat/report/claim/impl/xml/SimpleXmlClaimReporter.java b/apache-rat-core/src/main/java/org/apache/rat/report/claim/impl/xml/SimpleXmlClaimReporter.java
index 2d71ec0..02e21d6 100644
--- a/apache-rat-core/src/main/java/org/apache/rat/report/claim/impl/xml/SimpleXmlClaimReporter.java
+++ b/apache-rat-core/src/main/java/org/apache/rat/report/claim/impl/xml/SimpleXmlClaimReporter.java
@@ -18,9 +18,6 @@
*/
package org.apache.rat.report.claim.impl.xml;
-import java.io.IOException;
-import java.util.Calendar;
-
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.rat.api.Document;
import org.apache.rat.api.MetaData;
@@ -28,7 +25,12 @@
import org.apache.rat.report.AbstractReport;
import org.apache.rat.report.xml.writer.IXmlWriter;
+import java.io.IOException;
+import java.util.Calendar;
+
public class SimpleXmlClaimReporter extends AbstractReport {
+ public static final String RAT_REPORT = "rat-report";
+ public static final String TIMESTAMP = "timestamp";
public static final String LICENSE_APPROVAL_PREDICATE = "license-approval";
public static final String LICENSE_FAMILY_PREDICATE = "license-family";
public static final String HEADER_SAMPLE_PREDICATE = "header-sample";
@@ -38,8 +40,8 @@
public static final String ARCHIVE_TYPE_UNREADABLE = "unreadable";
public static final String ARCHIVE_TYPE_READABLE = "readable";
- private static final String NAME = "name";
private final IXmlWriter writer;
+ private static final String NAME = "name";
private boolean firstTime = true;
public SimpleXmlClaimReporter(final IXmlWriter writer) {
@@ -128,8 +130,8 @@
@Override
public void startReport() throws RatException {
try {
- writer.openElement("rat-report")
- .attribute("timestamp",
+ writer.openElement(RAT_REPORT)
+ .attribute(TIMESTAMP,
DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT
.format(Calendar.getInstance()));
} catch (IOException e) {
diff --git a/apache-rat-core/src/test/java/org/apache/rat/annotation/TestLicenseAppender.java b/apache-rat-core/src/test/java/org/apache/rat/annotation/TestLicenseAppender.java
index 21e9e90..a76488a 100644
--- a/apache-rat-core/src/test/java/org/apache/rat/annotation/TestLicenseAppender.java
+++ b/apache-rat-core/src/test/java/org/apache/rat/annotation/TestLicenseAppender.java
@@ -19,7 +19,9 @@
package org.apache.rat.annotation;
import org.apache.rat.test.utils.Resources;
+import org.junit.ClassRule;
import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
import java.io.BufferedReader;
import java.io.File;
@@ -27,24 +29,16 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
-import java.util.Random;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
public class TestLicenseAppender {
-
- // TODO pottlinger refactor test to use:
- //@Rule
- //TemporaryFolder baseTempFolder = new TemporaryFolder();
+ @ClassRule
+ public static TemporaryFolder baseTempFolder = new TemporaryFolder();
private static final String FIRST_LICENSE_LINE = " Licensed to the Apache Software Foundation (ASF) under one";
- /**
- * Used to ensure that temporary files have unq
- */
- private Random random = new Random();
-
private interface FileCreator {
void createFile(Writer w) throws IOException;
}
@@ -53,10 +47,12 @@
void readFile(BufferedReader r) throws IOException;
}
- private static String qualify(String fileName) {
- return new File(new File(System.getProperty("java.io.tmpdir")),
- fileName)
- .getAbsolutePath();
+ private static String getTemporaryFileWithName(String fileName) throws IOException {
+ if (fileName != null) {
+ return baseTempFolder.newFile(fileName).getAbsolutePath();
+ } else {
+ return baseTempFolder.newFile().getAbsolutePath();
+ }
}
private static void createTestFile(String fileName,
@@ -82,7 +78,7 @@
FileCreator creator,
NewFileReader reader)
throws IOException {
- String name = qualify(relativeName);
+ String name = getTemporaryFileWithName(relativeName);
try {
createTestFile(name, creator);
@@ -145,8 +141,7 @@
@Test
public void addLicenseToUnknownFile() throws IOException {
- String filename = qualify("tmp" + random.nextLong()
- + ".unknownType");
+ String filename = getTemporaryFileWithName(null);
File file = null;
File newFile = null;
try {