From d53daaa94a0a1203eb5c7a7a5e565bb94cc14ab0 Mon Sep 17 00:00:00 2001 From: Tony Cosentini Date: Thu, 14 Nov 2013 17:21:56 -0800 Subject: [PATCH 1/7] Adding tests to repro null index bug when empty index file exists. --- .../zinc/classes/ZincRepoIndexWriter.java | 4 ++ .../zinc/ZincRepoIndexWriterTest.java | 47 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/test/java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java diff --git a/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java b/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java index 7e7f5f5..4e7a052 100644 --- a/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java +++ b/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java @@ -43,6 +43,10 @@ public ZincRepoIndex getIndex() { return mRepoIndex; } + public File getIndexFile() { + return mIndexFile; + } + private ZincRepoIndex initializeIndex() { try { return mGson.fromJson(new FileReader(mIndexFile), ZincRepoIndex.class); diff --git a/src/test/java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java b/src/test/java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java new file mode 100644 index 0000000..f0c26fd --- /dev/null +++ b/src/test/java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java @@ -0,0 +1,47 @@ +package com.mindsnacks.zinc; + +import com.mindsnacks.zinc.classes.ZincRepoIndexWriter; +import com.mindsnacks.zinc.utils.ZincBaseTest; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; + +import static junit.framework.Assert.assertTrue; +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertNotNull; + +/** + * Created by Tony Cosentini + * Date: 11/14/13 + * Time: 4:55 PM + */ +public class ZincRepoIndexWriterTest extends ZincBaseTest { + private ZincRepoIndexWriter mZincRepoIndexWriter; + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private void setupZincRepoIndexWriter(File file) { + mZincRepoIndexWriter = new ZincRepoIndexWriter(file, createGson()); + } + + @Test + public void indexWriterCreatesFileAndReturnsAnIndexWhenFileDoesNotExist() throws Exception { + setupZincRepoIndexWriter(temporaryFolder.newFolder()); + assertNotNull(mZincRepoIndexWriter.getIndex()); + } + + @Test + public void indexWriterReplacesFIleAndReturnsAnIndexWhenGSONReturnsNullWhenParsingIndexJSON() throws Exception { + setupZincRepoIndexWriter(temporaryFolder.newFolder()); + File indexFile = mZincRepoIndexWriter.getIndexFile(); + + assertFalse(indexFile.exists()); + indexFile.createNewFile(); + assertTrue(indexFile.exists()); + + assertNotNull(mZincRepoIndexWriter.getIndex()); + } +} From e351fc30efec0d6e3dfb9640c223fd106d7fc7db Mon Sep 17 00:00:00 2001 From: Tony Cosentini Date: Thu, 14 Nov 2013 17:24:11 -0800 Subject: [PATCH 2/7] Fixing empty index bug. --- .../zinc/classes/ZincRepoIndexWriter.java | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java b/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java index 4e7a052..794e556 100644 --- a/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java +++ b/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java @@ -49,16 +49,27 @@ public File getIndexFile() { private ZincRepoIndex initializeIndex() { try { - return mGson.fromJson(new FileReader(mIndexFile), ZincRepoIndex.class); - } catch (FileNotFoundException fnfe) { - try { - mIndexFile.getParentFile().mkdirs(); - mIndexFile.createNewFile(); + ZincRepoIndex indexFromJSON = mGson.fromJson(new FileReader(mIndexFile), ZincRepoIndex.class); + boolean indexFromJSONIsAnEmptyFileOrCorrupted = indexFromJSON == null; - return new ZincRepoIndex(); - } catch (IOException ioe) { - throw new ZincRuntimeException("Error creating index file", ioe); + if (indexFromJSONIsAnEmptyFileOrCorrupted) { + return createNewIndexFile(); + } else { + return indexFromJSON; } + } catch (FileNotFoundException fnfe) { + return createNewIndexFile(); + } + } + + private ZincRepoIndex createNewIndexFile() { + try { + mIndexFile.getParentFile().mkdirs(); + mIndexFile.createNewFile(); + + return new ZincRepoIndex(); + } catch (IOException ioe) { + throw new ZincRuntimeException("Error creating index file", ioe); } } From a21b087d00da3f1f24eeb3abc93f9a70d3f749d2 Mon Sep 17 00:00:00 2001 From: Tony Cosentini Date: Thu, 14 Nov 2013 17:34:36 -0800 Subject: [PATCH 3/7] Using Before annotation to create ZincRepoIndexWriter. --- .../java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java b/src/test/java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java index f0c26fd..1a8b9e1 100644 --- a/src/test/java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java +++ b/src/test/java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java @@ -2,6 +2,7 @@ import com.mindsnacks.zinc.classes.ZincRepoIndexWriter; import com.mindsnacks.zinc.utils.ZincBaseTest; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -23,19 +24,18 @@ public class ZincRepoIndexWriterTest extends ZincBaseTest { @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); - private void setupZincRepoIndexWriter(File file) { - mZincRepoIndexWriter = new ZincRepoIndexWriter(file, createGson()); + @Before + public void setupZincRepoIndexWriter() throws Exception { + mZincRepoIndexWriter = new ZincRepoIndexWriter(temporaryFolder.newFolder(), createGson()); } @Test public void indexWriterCreatesFileAndReturnsAnIndexWhenFileDoesNotExist() throws Exception { - setupZincRepoIndexWriter(temporaryFolder.newFolder()); assertNotNull(mZincRepoIndexWriter.getIndex()); } @Test public void indexWriterReplacesFIleAndReturnsAnIndexWhenGSONReturnsNullWhenParsingIndexJSON() throws Exception { - setupZincRepoIndexWriter(temporaryFolder.newFolder()); File indexFile = mZincRepoIndexWriter.getIndexFile(); assertFalse(indexFile.exists()); From 46eb0d97bbdc65dbb947d358bccb50cd62a54f28 Mon Sep 17 00:00:00 2001 From: Tony Cosentini Date: Thu, 14 Nov 2013 17:43:13 -0800 Subject: [PATCH 4/7] Removing unncessary assertions. --- .../com/mindsnacks/zinc/ZincRepoIndexWriterTest.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java b/src/test/java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java index 1a8b9e1..55c1cb4 100644 --- a/src/test/java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java +++ b/src/test/java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java @@ -9,8 +9,6 @@ import java.io.File; -import static junit.framework.Assert.assertTrue; -import static junit.framework.TestCase.assertFalse; import static junit.framework.TestCase.assertNotNull; /** @@ -36,11 +34,8 @@ public void indexWriterCreatesFileAndReturnsAnIndexWhenFileDoesNotExist() throws @Test public void indexWriterReplacesFIleAndReturnsAnIndexWhenGSONReturnsNullWhenParsingIndexJSON() throws Exception { - File indexFile = mZincRepoIndexWriter.getIndexFile(); - - assertFalse(indexFile.exists()); - indexFile.createNewFile(); - assertTrue(indexFile.exists()); + File emptyIndexFile = mZincRepoIndexWriter.getIndexFile(); + emptyIndexFile.createNewFile(); assertNotNull(mZincRepoIndexWriter.getIndex()); } From e0d9140892a80916a8cf087e3c226c8151b77489 Mon Sep 17 00:00:00 2001 From: Tony Cosentini Date: Fri, 15 Nov 2013 10:33:40 -0800 Subject: [PATCH 5/7] Removing tests. --- .../zinc/classes/ZincRepoIndexWriter.java | 4 -- .../zinc/ZincRepoIndexWriterTest.java | 42 ------------------- 2 files changed, 46 deletions(-) delete mode 100644 src/test/java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java diff --git a/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java b/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java index 794e556..7776883 100644 --- a/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java +++ b/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java @@ -43,10 +43,6 @@ public ZincRepoIndex getIndex() { return mRepoIndex; } - public File getIndexFile() { - return mIndexFile; - } - private ZincRepoIndex initializeIndex() { try { ZincRepoIndex indexFromJSON = mGson.fromJson(new FileReader(mIndexFile), ZincRepoIndex.class); diff --git a/src/test/java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java b/src/test/java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java deleted file mode 100644 index 55c1cb4..0000000 --- a/src/test/java/com/mindsnacks/zinc/ZincRepoIndexWriterTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.mindsnacks.zinc; - -import com.mindsnacks.zinc.classes.ZincRepoIndexWriter; -import com.mindsnacks.zinc.utils.ZincBaseTest; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; - -import java.io.File; - -import static junit.framework.TestCase.assertNotNull; - -/** - * Created by Tony Cosentini - * Date: 11/14/13 - * Time: 4:55 PM - */ -public class ZincRepoIndexWriterTest extends ZincBaseTest { - private ZincRepoIndexWriter mZincRepoIndexWriter; - - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); - - @Before - public void setupZincRepoIndexWriter() throws Exception { - mZincRepoIndexWriter = new ZincRepoIndexWriter(temporaryFolder.newFolder(), createGson()); - } - - @Test - public void indexWriterCreatesFileAndReturnsAnIndexWhenFileDoesNotExist() throws Exception { - assertNotNull(mZincRepoIndexWriter.getIndex()); - } - - @Test - public void indexWriterReplacesFIleAndReturnsAnIndexWhenGSONReturnsNullWhenParsingIndexJSON() throws Exception { - File emptyIndexFile = mZincRepoIndexWriter.getIndexFile(); - emptyIndexFile.createNewFile(); - - assertNotNull(mZincRepoIndexWriter.getIndex()); - } -} From cd3321cf74bae483deab943d255286834ac90ce1 Mon Sep 17 00:00:00 2001 From: NachoSoto Date: Fri, 15 Nov 2013 19:07:06 +0000 Subject: [PATCH 6/7] Simplifying ZincRepoIndexWriter --- .../zinc/classes/ZincRepoIndexWriter.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java b/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java index 7776883..57932ec 100644 --- a/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java +++ b/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java @@ -1,5 +1,6 @@ package com.mindsnacks.zinc.classes; +import com.google.common.base.Preconditions; import com.google.gson.Gson; import com.mindsnacks.zinc.classes.data.ZincRepoIndex; import com.mindsnacks.zinc.exceptions.ZincRuntimeException; @@ -45,19 +46,18 @@ public ZincRepoIndex getIndex() { private ZincRepoIndex initializeIndex() { try { - ZincRepoIndex indexFromJSON = mGson.fromJson(new FileReader(mIndexFile), ZincRepoIndex.class); - boolean indexFromJSONIsAnEmptyFileOrCorrupted = indexFromJSON == null; - - if (indexFromJSONIsAnEmptyFileOrCorrupted) { - return createNewIndexFile(); - } else { - return indexFromJSON; - } + return readRepoIndexFile(); } catch (FileNotFoundException fnfe) { return createNewIndexFile(); + } catch (NullPointerException npe) { + return createNewIndexFile(); } } + private ZincRepoIndex readRepoIndexFile() throws FileNotFoundException, NullPointerException { + return Preconditions.checkNotNull(mGson.fromJson(new FileReader(mIndexFile), ZincRepoIndex.class)); + } + private ZincRepoIndex createNewIndexFile() { try { mIndexFile.getParentFile().mkdirs(); From aba248b953ab99792af0fb0023c3eed4835892eb Mon Sep 17 00:00:00 2001 From: NachoSoto Date: Fri, 15 Nov 2013 19:09:58 +0000 Subject: [PATCH 7/7] ZincRepoIndexWriter: checking for null in the caller instead. --- .../com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java b/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java index 57932ec..ea02b33 100644 --- a/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java +++ b/src/main/java/com/mindsnacks/zinc/classes/ZincRepoIndexWriter.java @@ -1,12 +1,13 @@ package com.mindsnacks.zinc.classes; -import com.google.common.base.Preconditions; import com.google.gson.Gson; import com.mindsnacks.zinc.classes.data.ZincRepoIndex; import com.mindsnacks.zinc.exceptions.ZincRuntimeException; import java.io.*; +import static com.google.common.base.Preconditions.checkNotNull; + /** * User: NachoSoto * Date: 9/3/13 @@ -46,7 +47,7 @@ public ZincRepoIndex getIndex() { private ZincRepoIndex initializeIndex() { try { - return readRepoIndexFile(); + return checkNotNull(readRepoIndexFile()); } catch (FileNotFoundException fnfe) { return createNewIndexFile(); } catch (NullPointerException npe) { @@ -54,8 +55,8 @@ private ZincRepoIndex initializeIndex() { } } - private ZincRepoIndex readRepoIndexFile() throws FileNotFoundException, NullPointerException { - return Preconditions.checkNotNull(mGson.fromJson(new FileReader(mIndexFile), ZincRepoIndex.class)); + private ZincRepoIndex readRepoIndexFile() throws FileNotFoundException { + return mGson.fromJson(new FileReader(mIndexFile), ZincRepoIndex.class); } private ZincRepoIndex createNewIndexFile() {