Remove newly-output PersistentUnitCache file if nothing was written to it.

In the case where you re-run a compile/startup without changing any code, we produce an empty cache file each time.  This change removes the file on exit if we never wrote anything to it.

http://gwt-code-reviews.appspot.com/1396804/

Review by: zundel@google.com

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@9921 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/javac/PersistentUnitCache.java b/dev/core/src/com/google/gwt/dev/javac/PersistentUnitCache.java
index 8d2a926..dcff5e2 100644
--- a/dev/core/src/com/google/gwt/dev/javac/PersistentUnitCache.java
+++ b/dev/core/src/com/google/gwt/dev/javac/PersistentUnitCache.java
@@ -184,7 +184,8 @@
         logger.log(TreeLogger.ERROR, "Error creating cache " + currentCacheFile
             + ". Disabling cache.", ex);
       }
-      int unitsWritten = 0;
+      int recentUnitsWritten = 0;
+      int totalUnitsWritten = 0;
       try {
         while (true) {
           UnitWriteMessage msg = null;
@@ -202,9 +203,9 @@
           try {
             if (msg != null) {
               if (msg == UnitWriteMessage.DELETE_OLD_CACHE_FILES) {
-                logger.log(TreeLogger.TRACE, "Wrote " + unitsWritten
+                logger.log(TreeLogger.TRACE, "Wrote " + recentUnitsWritten
                     + " units to persistent cache.");
-                unitsWritten = 0;
+                recentUnitsWritten = 0;
                 deleteOldCacheFiles(logger, currentCacheFile);
               } else if (msg == UnitWriteMessage.SHUTDOWN_THREAD) {
                 stream.flush();
@@ -214,7 +215,8 @@
                 CompilationUnit unit = msg.unitCacheEntry.getUnit();
                 assert unit != null;
                 stream.writeObject(unit);
-                unitsWritten++;
+                recentUnitsWritten++;
+                totalUnitsWritten++;
               }
             }
 
@@ -234,6 +236,10 @@
         // Paranoia - close all streams
         Utility.close(bstream);
         Utility.close(fstream);
+        if (totalUnitsWritten == 0) {
+          // Remove useless empty output.
+          currentCacheFile.delete();
+        }
         shutDownLatch.countDown();
         logger.log(TreeLogger.TRACE, "Shutting down PersistentUnitCache thread");
       }
diff --git a/dev/core/test/com/google/gwt/dev/javac/PersistentUnitCacheTest.java b/dev/core/test/com/google/gwt/dev/javac/PersistentUnitCacheTest.java
index 0cdee86..ba4fefe 100644
--- a/dev/core/test/com/google/gwt/dev/javac/PersistentUnitCacheTest.java
+++ b/dev/core/test/com/google/gwt/dev/javac/PersistentUnitCacheTest.java
@@ -153,14 +153,14 @@
     assertEquals(foo2.getContentId(), result.getContentId());
     cache.cleanup(logger);
 
-    // Now there should be 2 files.
+    // We didn't write anything, still 1 file.
     cache.shutdown();
-    assertNumCacheFiles(unitCacheDir, 2);
+    assertNumCacheFiles(unitCacheDir, 1);
 
     // keep making more files
     MockCompilationUnit lastUnit = null;
     assertTrue(PersistentUnitCache.CACHE_FILE_THRESHOLD > 3);
-    for (int i = 3; i < PersistentUnitCache.CACHE_FILE_THRESHOLD; ++i) {
+    for (int i = 2; i < PersistentUnitCache.CACHE_FILE_THRESHOLD; ++i) {
       cache = new PersistentUnitCache(logger, cacheDir);
       lastUnit = new MockCompilationUnit("com.example.Foo", "Foo Source" + i);
       cache.add(lastUnit);