From 3915b18aba352c96fc610511f3fc15e6a2f38b46 Mon Sep 17 00:00:00 2001 From: yann_ Date: Mon, 21 Dec 2020 19:24:45 +0100 Subject: [PATCH] ImportData + test ImportData Le code est fonctionnel --- ProjetGenieLogicielLoto.iml | 11 +++--- pom.xml | 6 ++++ src/main/java/fr/myny/data/ImportData.java | 35 ++++++++----------- .../java/fr/myny/data/ImportDataTest.java | 17 +++++++++ 4 files changed, 44 insertions(+), 25 deletions(-) create mode 100644 src/test/java/fr/myny/data/ImportDataTest.java diff --git a/ProjetGenieLogicielLoto.iml b/ProjetGenieLogicielLoto.iml index ade512f..4766121 100644 --- a/ProjetGenieLogicielLoto.iml +++ b/ProjetGenieLogicielLoto.iml @@ -6,16 +6,19 @@ + + + - + - - - + + + diff --git a/pom.xml b/pom.xml index 3abf41e..396b357 100644 --- a/pom.xml +++ b/pom.xml @@ -67,6 +67,12 @@ slf4j-simple 1.7.25 + + org.junit.jupiter + junit-jupiter-api + 5.7.0 + compile + diff --git a/src/main/java/fr/myny/data/ImportData.java b/src/main/java/fr/myny/data/ImportData.java index 205fb2c..75ec0b2 100644 --- a/src/main/java/fr/myny/data/ImportData.java +++ b/src/main/java/fr/myny/data/ImportData.java @@ -7,12 +7,14 @@ import java.net.MalformedURLException; public class ImportData { protected String url; + protected String repDes; /** * Le constructeur de ImportData */ ImportData(){ this.url = "https://www.fdj.fr/jeux-de-tirage/loto/statistiques"; + this.repDes = "src/test/resources/"; } /** @@ -23,6 +25,10 @@ public class ImportData { this.url=url; } + public void getUrl() { + + } + private boolean UrlExist() { try { URL site = new URL(url); @@ -37,16 +43,6 @@ public class ImportData { } } - private boolean ZipNotExist(String filename) { - File directory = new File("src/main/java/resources/"); - File[] files = directory.listFiles(); - for (File f : files) { - if (f.getName().equals(filename)) - return false; - } - return true; - } - public void DownloadCsvZip() { if (UrlExist()) { BufferedReader in = null; @@ -63,19 +59,16 @@ public class ImportData { if (test.equals(urlZipLink.substring(50, 55))) { nameZip = urlZipLink.substring(50,65); urlZipLink = urlZipLink.substring(13,65); - if (ZipNotExist(nameZip)) { - try (BufferedInputStream bis = new BufferedInputStream(new URL(urlZipLink).openStream()); - FileOutputStream fos = new FileOutputStream("src/main/java/resources/" + nameZip)) { - byte data[] = new byte[1024]; - int byteContent; - while ((byteContent = bis.read(data, 0, 1024)) != -1) { - fos.write(data, 0, byteContent); - } - } catch (IOException e) { - e.printStackTrace(System.out); + try (BufferedInputStream bis = new BufferedInputStream(new URL(urlZipLink).openStream()); + FileOutputStream fos = new FileOutputStream(repDes + nameZip)) { + byte data[] = new byte[1024]; + int byteContent; + while ((byteContent = bis.read(data, 0, 1024)) != -1) { + fos.write(data, 0, byteContent); } + } catch (IOException e) { + e.printStackTrace(System.out); } - } } } diff --git a/src/test/java/fr/myny/data/ImportDataTest.java b/src/test/java/fr/myny/data/ImportDataTest.java new file mode 100644 index 0000000..a791d73 --- /dev/null +++ b/src/test/java/fr/myny/data/ImportDataTest.java @@ -0,0 +1,17 @@ +package fr.myny.data; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ImportDataTest { + + @Test + void downloadCsvZip() { + + ImportData csv = new ImportData(); + + csv.DownloadCsvZip(); + + } +} \ No newline at end of file