From ea9cae98fb153aee3726601120d022cc64ec3709 Mon Sep 17 00:00:00 2001 From: AlexStudent Date: Mon, 2 Apr 2018 01:48:28 +0300 Subject: [PATCH] Fixed toArray where it's needed (probably). Rewritten getPageTags() methods so that the result depends on the data structure we pass in. --- src/edu/launua/URLManager.java | 19 ++++++------------- tests/edu/launua/URLManagerTest.java | 8 +++++++- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/edu/launua/URLManager.java b/src/edu/launua/URLManager.java index 45e8e6d..dc18357 100644 --- a/src/edu/launua/URLManager.java +++ b/src/edu/launua/URLManager.java @@ -38,24 +38,17 @@ public String getPageContent(){ return res; } - public Object[] getPageTags() { + public String[] getPageTags(Collection collection) { String res = getPageContent(); Pattern pattern = Pattern.compile("<[\\w]+"); Matcher matcher = pattern.matcher(res); - ArrayList tags = new ArrayList<>(); while(matcher.find()) - tags.add(res.substring(matcher.start(), matcher.end()) + ">"); - return tags.toArray(); + collection.add(res.substring(matcher.start(), matcher.end()) + ">"); + return collection.toArray(new String[collection.size()]); } - public Object[] getPageTagsSortedSet() { - String res = getPageContent(); - Pattern pattern = Pattern.compile("<[\\w]+"); - Matcher matcher = pattern.matcher(res); - Set tags = new TreeSet<>(); - while(matcher.find()) - tags.add(res.substring(matcher.start(), matcher.end()) + ">"); - return tags.toArray(); + public String[] getPageTagsSortedSet() { + return getPageTags(new TreeSet<>()); } public Object[] getPageTagsSortedByCountSetOld() { @@ -85,7 +78,7 @@ public int compare(Map.Entry a, Map.Entry b) { // More clean version public Object[] getPageTagsSortedByCountSet() { - Object[] tags = getPageTags(); + Object[] tags = getPageTags(new ArrayList()); Map tagsCount= new HashMap<>(); for (Object tag : tags) { if(tagsCount.containsKey(tag)) diff --git a/tests/edu/launua/URLManagerTest.java b/tests/edu/launua/URLManagerTest.java index 77e6f0a..396e87b 100644 --- a/tests/edu/launua/URLManagerTest.java +++ b/tests/edu/launua/URLManagerTest.java @@ -4,16 +4,20 @@ import org.junit.Before; import org.junit.Test; +import java.net.MalformedURLException; import java.util.Map; import java.util.regex.Pattern; import static org.junit.Assert.*; public class URLManagerTest { + String url; URLManager urlManager; + @Before public void testSetUp() throws Exception { - urlManager = new URLManager("file:////C://Users//San//IdeaProjects//Collector//index.html"); + url = "file:////C://Users//San//IdeaProjects//Collector//index.html"; + urlManager = new URLManager(url); } @After @@ -22,10 +26,12 @@ public void testTearDown() throws Exception { @Test public void testGetUrl() { + assertEquals(url, urlManager.getUrl()); } @Test public void testSetUrl() { + urlManager.setUrl("something wrong"); } @Test