From 8ee6023597b91ec9626f674d516ba2bdde3b71e9 Mon Sep 17 00:00:00 2001 From: Kenneth McFarland Date: Thu, 7 Mar 2019 16:36:39 -0800 Subject: [PATCH 1/5] Initial test for the PageLoader class that covers null and empty checks for factory methods and constructors. --- .../webindex/data/fluo/PageLoaderTest.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java diff --git a/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java b/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java new file mode 100644 index 0000000..1aed3c2 --- /dev/null +++ b/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java @@ -0,0 +1,63 @@ +package webindex.data.fluo; + +import java.net.MalformedURLException; + +import org.junit.Assert; +import org.junit.Test; + +import webindex.core.models.Page; +import webindex.core.models.URL; + +@SuppressWarnings("unused") +public class PageLoaderTest { + + @Test + public void testUpdatePageWithNullPage() { + Page p = null; + PageLoader loader = PageLoader.updatePage(p); + loader = null; + } + + @Test(expected = IllegalArgumentException.class) + public void testEmptyPageThrowsIllegalArgument() { + Page p = Page.EMPTY; + PageLoader loader = PageLoader.updatePage(p); + loader = null; + } + + @Test(expected = NullPointerException.class) + public void testDeleteFailsWithNullUrl() { + try { + PageLoader loader = PageLoader.deletePage(null); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } + + @Test + public void testDeletePage() { + URL url = new URL("","","",0,false,false); + try { + PageLoader loader = PageLoader.deletePage(url); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } + + @Test + public void testLoadWithNullTxCtx() { + URL url = new URL("","","",0,false,false); + PageLoader loader; + try { + loader = PageLoader.deletePage(url); + loader.load(null, null); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (NullPointerException e) { + e.printStackTrace(); + Assert.fail(); + } catch (Exception e) { + e.printStackTrace(); + } + } +} From 80e10334356ea2da9bb6cf19d9e9404785088600 Mon Sep 17 00:00:00 2001 From: Kenneth McFarland Date: Fri, 15 Mar 2019 11:44:28 -0700 Subject: [PATCH 2/5] Refactoring of PageLoaderTest Added Apache License header Renamed methods with better descriptives Removed unused local variable warnings --- .../webindex/data/fluo/PageLoaderTest.java | 51 ++++++++++++------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java b/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java index 1aed3c2..c988c70 100644 --- a/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java +++ b/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java @@ -1,3 +1,20 @@ +/* + * 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 webindex.data.fluo; import java.net.MalformedURLException; @@ -8,42 +25,41 @@ import webindex.core.models.Page; import webindex.core.models.URL; -@SuppressWarnings("unused") + public class PageLoaderTest { @Test public void testUpdatePageWithNullPage() { - Page p = null; - PageLoader loader = PageLoader.updatePage(p); - loader = null; + PageLoader.updatePage((Page) null); } @Test(expected = IllegalArgumentException.class) public void testEmptyPageThrowsIllegalArgument() { Page p = Page.EMPTY; - PageLoader loader = PageLoader.updatePage(p); - loader = null; + PageLoader.updatePage(p); } @Test(expected = NullPointerException.class) - public void testDeleteFailsWithNullUrl() { + public void testDeletePageThrowsNPEifNullURL() { try { - PageLoader loader = PageLoader.deletePage(null); + PageLoader.deletePage((URL)null); } catch (MalformedURLException e) { - e.printStackTrace(); + } + } @Test - public void testDeletePage() { + public void testDeletePageWithBlankURL() { URL url = new URL("","","",0,false,false); try { - PageLoader loader = PageLoader.deletePage(url); + PageLoader.deletePage(url); } catch (MalformedURLException e) { - e.printStackTrace(); + } } + //This test wont make sense until PageLoader.deletePage() handles non null empty URLS @Test public void testLoadWithNullTxCtx() { URL url = new URL("","","",0,false,false); @@ -51,13 +67,12 @@ public void testLoadWithNullTxCtx() { try { loader = PageLoader.deletePage(url); loader.load(null, null); - } catch (MalformedURLException e) { - e.printStackTrace(); - } catch (NullPointerException e) { - e.printStackTrace(); + } catch (NullPointerException e) { Assert.fail(); + } catch (MalformedURLException e) { + // from PageLoader.deletePage() } catch (Exception e) { - e.printStackTrace(); - } + // from PageLoader.load() + } } } From 216dccd082bc2ac55b002a997f4fdc07e52fee5f Mon Sep 17 00:00:00 2001 From: Kenneth McFarland Date: Fri, 15 Mar 2019 15:36:36 -0700 Subject: [PATCH 3/5] Update Exception Handling Improve handling of unexpected exceptions and cast them to the appropriate type --- .../webindex/data/fluo/PageLoaderTest.java | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java b/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java index c988c70..7c0a673 100644 --- a/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java +++ b/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java @@ -19,7 +19,6 @@ import java.net.MalformedURLException; -import org.junit.Assert; import org.junit.Test; import webindex.core.models.Page; @@ -34,7 +33,7 @@ public void testUpdatePageWithNullPage() { } @Test(expected = IllegalArgumentException.class) - public void testEmptyPageThrowsIllegalArgument() { + public void testUpdatePageWithEmptyPage() { Page p = Page.EMPTY; PageLoader.updatePage(p); } @@ -46,33 +45,27 @@ public void testDeletePageThrowsNPEifNullURL() { } catch (MalformedURLException e) { } - } @Test public void testDeletePageWithBlankURL() { - URL url = new URL("","","",0,false,false); try { + URL url = new URL("","","",0,false,false); PageLoader.deletePage(url); } catch (MalformedURLException e) { } } - //This test wont make sense until PageLoader.deletePage() handles non null empty URLS @Test public void testLoadWithNullTxCtx() { - URL url = new URL("","","",0,false,false); - PageLoader loader; try { - loader = PageLoader.deletePage(url); + URL url = new URL("www.apache.org", "www.apache.org", "http://www.apache.org", 80, false, false); + PageLoader loader = PageLoader.deletePage(url); loader.load(null, null); - } catch (NullPointerException e) { - Assert.fail(); - } catch (MalformedURLException e) { - // from PageLoader.deletePage() } catch (Exception e) { - // from PageLoader.load() + if(e instanceof NullPointerException) + throw new NullPointerException(e.getMessage()); } } } From 7d94a3c079d49ad05eb90299a592f8c5c07ea9a7 Mon Sep 17 00:00:00 2001 From: Kenneth McFarland Date: Mon, 18 Mar 2019 08:58:13 -0700 Subject: [PATCH 4/5] Update Exception Handling Change method signature to eliminate catch blocks --- .../webindex/data/fluo/PageLoaderTest.java | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java b/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java index 7c0a673..80de916 100644 --- a/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java +++ b/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java @@ -34,31 +34,22 @@ public void testUpdatePageWithNullPage() { @Test(expected = IllegalArgumentException.class) public void testUpdatePageWithEmptyPage() { - Page p = Page.EMPTY; - PageLoader.updatePage(p); + PageLoader.updatePage(Page.EMPTY); } @Test(expected = NullPointerException.class) - public void testDeletePageThrowsNPEifNullURL() { - try { - PageLoader.deletePage((URL)null); - } catch (MalformedURLException e) { - - } + public void testDeletePageThrowsNPEifNullURL() throws MalformedURLException{ + PageLoader.deletePage((URL)null); } @Test - public void testDeletePageWithBlankURL() { - try { + public void testDeletePageWithBlankURL() throws MalformedURLException{ URL url = new URL("","","",0,false,false); PageLoader.deletePage(url); - } catch (MalformedURLException e) { - - } } @Test - public void testLoadWithNullTxCtx() { + public void testLoadWithNullTxCtx() throws MalformedURLException{ try { URL url = new URL("www.apache.org", "www.apache.org", "http://www.apache.org", 80, false, false); PageLoader loader = PageLoader.deletePage(url); From abd15cc7de9e6d490b7242706368be670fbb63e3 Mon Sep 17 00:00:00 2001 From: Kenneth McFarland Date: Tue, 19 Mar 2019 13:42:15 -0700 Subject: [PATCH 5/5] Update exception handling method signature --- .../java/webindex/data/fluo/PageLoaderTest.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java b/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java index 80de916..94d9f0d 100644 --- a/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java +++ b/webindex/modules/data/src/test/java/webindex/data/fluo/PageLoaderTest.java @@ -49,14 +49,9 @@ public void testDeletePageWithBlankURL() throws MalformedURLException{ } @Test - public void testLoadWithNullTxCtx() throws MalformedURLException{ - try { - URL url = new URL("www.apache.org", "www.apache.org", "http://www.apache.org", 80, false, false); - PageLoader loader = PageLoader.deletePage(url); - loader.load(null, null); - } catch (Exception e) { - if(e instanceof NullPointerException) - throw new NullPointerException(e.getMessage()); - } + public void testLoadWithNullTxCtx() throws Exception{ + URL url = new URL("www.apache.org", "www.apache.org", "http://www.apache.org", 80, false, false); + PageLoader loader = PageLoader.deletePage(url); + loader.load(null, null); } }