From 645a44129a4d6c68ffe2bff02652a96a812ecb18 Mon Sep 17 00:00:00 2001 From: fb084 <fb084@hdm-stuttgart.de> Date: Fri, 15 Jun 2018 11:35:20 +0200 Subject: [PATCH] More unit tests and code cleanup --- .../mi/sd1/weather/Forecast.java | 2 +- .../mi/sd1/weather/model/CopyToFile.java | 14 +++++----- .../weather/model/SearchingAndComparing.java | 4 +-- .../mi/sd1/weather/ErrorInput.java | 26 +++++++++++++++++++ .../mi/sd1/weather/GetIDTest.java | 13 ++++++++++ .../mi/sd1/weather/WeatherDataParseTest.java | 3 ++- 6 files changed, 51 insertions(+), 11 deletions(-) create mode 100755 src/test/java/de/hdm_stuttgart/mi/sd1/weather/ErrorInput.java create mode 100755 src/test/java/de/hdm_stuttgart/mi/sd1/weather/GetIDTest.java diff --git a/src/main/java/de/hdm_stuttgart/mi/sd1/weather/Forecast.java b/src/main/java/de/hdm_stuttgart/mi/sd1/weather/Forecast.java index 2635917..49cf6c5 100755 --- a/src/main/java/de/hdm_stuttgart/mi/sd1/weather/Forecast.java +++ b/src/main/java/de/hdm_stuttgart/mi/sd1/weather/Forecast.java @@ -23,7 +23,7 @@ public class Forecast { - CopyToFile.CopyURLToFile(); + CopyToFile.CopyURLToFile("weatherCache\\" + SearchingAndComparing.GetID() + ".json"); CopyToFile.CopyFileToWeather(); diff --git a/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/CopyToFile.java b/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/CopyToFile.java index 89e8ff3..7f7099c 100755 --- a/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/CopyToFile.java +++ b/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/CopyToFile.java @@ -11,24 +11,24 @@ public class CopyToFile { public static String filePath; - public static void CopyURLToFile() { + public static void CopyURLToFile(String pathname) { - if (new File("weatherCache\\" + SearchingAndComparing.GetID() + ".json").isFile() || - (Calendar.getInstance().getTimeInMillis() - new File("weatherCache\\" + SearchingAndComparing.GetID() + ".json").lastModified() > 600000) && - new File("weatherCache\\" + SearchingAndComparing.GetID() + ".json").lastModified() > 0) { + if (new File(pathname).isFile() || + (Calendar.getInstance().getTimeInMillis() - new File(pathname).lastModified() > 600000) && + new File(pathname).lastModified() > 0) { System.out.println("Loading file from cache..."); } else { try { FileUtils.copyURLToFile( URLCreator.CreateUrl(), - new File("weatherCache\\" + SearchingAndComparing.GetID() + ".json")); + new File(pathname)); } catch (IOException e) { System.out.println("ERROR"); } } - filePath = "weatherCache\\" + SearchingAndComparing.GetID() + ".json"; - //return "weatherCache\\" + SearchingAndComparing.GetID() + ".json"; + filePath = pathname; + //return pathname; } diff --git a/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/SearchingAndComparing.java b/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/SearchingAndComparing.java index 0d25bff..6c67780 100755 --- a/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/SearchingAndComparing.java +++ b/src/main/java/de/hdm_stuttgart/mi/sd1/weather/model/SearchingAndComparing.java @@ -19,7 +19,7 @@ public class SearchingAndComparing { System.out.println("Please enter your City: "); Scanner scanCity = new Scanner(System.in); String searchedCity = scanCity.nextLine(); - //String searchedCity = "Republic of India"; + // String searchedCity = "Republic of India"; /** * Searching and comparing cities @@ -49,7 +49,7 @@ public class SearchingAndComparing { City[] multipleResults = Arrays.copyOf(citySelector, k); for (int i = 0; i < multipleResults.length; i++) { - System.out.println(i + 1 + ". " + multipleResults[i].getName() + " \n country: " + multipleResults[i].getCountry() + " | coordinates: " + citySelector[0].getAdditionalProperties()); + System.out.println(i + 1 + ". " + multipleResults[i].getName() + " \n country: " + multipleResults[i].getCountry() + " | coordinates: " + citySelector[i].getAdditionalProperties()); } System.out.println("Please enter the number of the city you want weatherdata for: "); Scanner scanCityChoice = new Scanner(System.in); diff --git a/src/test/java/de/hdm_stuttgart/mi/sd1/weather/ErrorInput.java b/src/test/java/de/hdm_stuttgart/mi/sd1/weather/ErrorInput.java new file mode 100755 index 0000000..a08e514 --- /dev/null +++ b/src/test/java/de/hdm_stuttgart/mi/sd1/weather/ErrorInput.java @@ -0,0 +1,26 @@ +/* + +package de.hdm_stuttgart.mi.sd1.weather; + +import org.junit.Assert; +import org.junit.Test; +public class ErrorInput { + @Test + int SkyPercent = 50; + int SkyPercent2 = 101; + public void TestErrorInput(){ + Assert.assertEquals(" Something wrong with the sky - just run" + "\n", ); + } + + + + + + + + + + + +} +*/ \ No newline at end of file diff --git a/src/test/java/de/hdm_stuttgart/mi/sd1/weather/GetIDTest.java b/src/test/java/de/hdm_stuttgart/mi/sd1/weather/GetIDTest.java new file mode 100755 index 0000000..ad0c7b4 --- /dev/null +++ b/src/test/java/de/hdm_stuttgart/mi/sd1/weather/GetIDTest.java @@ -0,0 +1,13 @@ +package de.hdm_stuttgart.mi.sd1.weather; + +import de.hdm_stuttgart.mi.sd1.weather.model.SearchingAndComparing; +import org.junit.Assert; +import org.junit.Test; + +public class GetIDTest { + @Test + public void GetIDTest() { + Assert.assertEquals(0,SearchingAndComparing.GetID()); + } + +} diff --git a/src/test/java/de/hdm_stuttgart/mi/sd1/weather/WeatherDataParseTest.java b/src/test/java/de/hdm_stuttgart/mi/sd1/weather/WeatherDataParseTest.java index 161556c..e03bf2f 100755 --- a/src/test/java/de/hdm_stuttgart/mi/sd1/weather/WeatherDataParseTest.java +++ b/src/test/java/de/hdm_stuttgart/mi/sd1/weather/WeatherDataParseTest.java @@ -6,7 +6,8 @@ import org.junit.Test; import java.io.IOException; -public class WeatherDataParseTest { +public class +WeatherDataParseTest { @Test public void testParseWeatherData() { final String sampleWeatherDataFileName = "src/main/resources/stuttgart.weather.json"; -- GitLab