You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@creadur.apache.org by po...@apache.org on 2015/05/28 00:10:28 UTC

svn commit: r1682131 - in /creadur/rat/trunk/apache-rat-core/src: main/java/org/apache/rat/report/claim/impl/xml/SimpleXmlClaimReporter.java test/java/org/apache/rat/annotation/TestLicenseAppender.java

Author: pottlinger
Date: Wed May 27 22:10:28 2015
New Revision: 1682131

URL: http://svn.apache.org/r1682131
Log:
RAT-202: Replace manual creation of temporary files with junit Rule.

* Add constants for XML report elements/attributes.

Modified:
    creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/report/claim/impl/xml/SimpleXmlClaimReporter.java
    creadur/rat/trunk/apache-rat-core/src/test/java/org/apache/rat/annotation/TestLicenseAppender.java

Modified: creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/report/claim/impl/xml/SimpleXmlClaimReporter.java
URL: http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/report/claim/impl/xml/SimpleXmlClaimReporter.java?rev=1682131&r1=1682130&r2=1682131&view=diff
==============================================================================
--- creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/report/claim/impl/xml/SimpleXmlClaimReporter.java (original)
+++ creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/report/claim/impl/xml/SimpleXmlClaimReporter.java Wed May 27 22:10:28 2015
@@ -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.api.RatException;
 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 class SimpleXmlClaimReporter exte
     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 @@ public class SimpleXmlClaimReporter exte
     @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) {

Modified: creadur/rat/trunk/apache-rat-core/src/test/java/org/apache/rat/annotation/TestLicenseAppender.java
URL: http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-core/src/test/java/org/apache/rat/annotation/TestLicenseAppender.java?rev=1682131&r1=1682130&r2=1682131&view=diff
==============================================================================
--- creadur/rat/trunk/apache-rat-core/src/test/java/org/apache/rat/annotation/TestLicenseAppender.java (original)
+++ creadur/rat/trunk/apache-rat-core/src/test/java/org/apache/rat/annotation/TestLicenseAppender.java Wed May 27 22:10:28 2015
@@ -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.FileReader;
 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 @@ public class TestLicenseAppender {
         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 @@ public class TestLicenseAppender {
                                            FileCreator creator,
                                            NewFileReader reader)
             throws IOException {
-        String name = qualify(relativeName);
+        String name = getTemporaryFileWithName(relativeName);
         try {
             createTestFile(name, creator);
 
@@ -145,8 +141,7 @@ public class TestLicenseAppender {
 
     @Test
     public void addLicenseToUnknownFile() throws IOException {
-        String filename = qualify("tmp" + random.nextLong()
-                + ".unknownType");
+        String filename = getTemporaryFileWithName(null);
         File file = null;
         File newFile = null;
         try {