From 71e626c05421293e126aa3d11b812fa10a821f8a Mon Sep 17 00:00:00 2001 From: Louis Calas Date: Thu, 10 Oct 2019 09:30:35 +0200 Subject: [PATCH] Fix string validity check --- NGCC/resources/log4j2.properties | 2 +- NGCC/src/commands/Read.java | 19 +++--- NGCC/src/csv/GenerateCSV.java | 12 ++-- NGCC/tests/commands/TestRead.java | 1 - NGCC/tests/csv/TestCSV.java | 99 +++++++++++++++++++------------ 5 files changed, 78 insertions(+), 55 deletions(-) diff --git a/NGCC/resources/log4j2.properties b/NGCC/resources/log4j2.properties index c7dfd1f..81eb8f1 100644 --- a/NGCC/resources/log4j2.properties +++ b/NGCC/resources/log4j2.properties @@ -16,7 +16,7 @@ appender.file.layout.type=PatternLayout appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n -#Création des loggers +#Création des loggers (par package) loggers = console, commands, csv diff --git a/NGCC/src/commands/Read.java b/NGCC/src/commands/Read.java index c60f402..bb9f493 100644 --- a/NGCC/src/commands/Read.java +++ b/NGCC/src/commands/Read.java @@ -65,11 +65,11 @@ public class Read implements Callable { public Read(PrintStream out) { } - -// public boolean isCsv(String file) { -// return file.endsWith(".csv"); -// -// } + // Check de l'extension csv (à améliorer) + public boolean isCsv(String file) { + return file.endsWith(".csv"); + + } @Override @@ -138,7 +138,10 @@ public class Read implements Callable { logger.debug("Source : "+source_path); - + if (!isCsv(result_name)) { + result_name = result_name+".csv"; + logger.info("Result file name changed to '"+result_name+"'"); + } @@ -147,9 +150,9 @@ public class Read implements Callable { String filePath = new File("").getAbsolutePath(); - + // Instantie l'OCR GestionnaireCopies ocr = new GestionnaireCopies("../"+directory_name); - // Instantie l'ocr + logger.debug("CSV initialized with : "+ocr.createHashMapforCSV()+" , "+config.getParam().get("Code")+" , "+result_name); diff --git a/NGCC/src/csv/GenerateCSV.java b/NGCC/src/csv/GenerateCSV.java index a8a5e59..70b569e 100644 --- a/NGCC/src/csv/GenerateCSV.java +++ b/NGCC/src/csv/GenerateCSV.java @@ -36,7 +36,7 @@ public class GenerateCSV { int nb = Character.getNumericValue(s.charAt(i)); - if (nb <= 0 || nb >= 9) { + if (nb < 0 || nb > 9) { logger.fatal("Student id's characters are not recognized"); return false; @@ -72,12 +72,12 @@ public class GenerateCSV { // Si etudiant HashMap est null, pas ecrit if (etud != null) { - //if (this.isValid(etud)) { + if (this.isValid(etud)) { writer.write(etud + ";" + etudiants.get(etud) + System.getProperty("line.separator")); - //} -// else { -// logger.debug("Invalid id not added to csv"); -// } + } + else { + logger.debug("Invalid id not added to csv"); + } } else { diff --git a/NGCC/tests/commands/TestRead.java b/NGCC/tests/commands/TestRead.java index 1191b69..0a168c1 100644 --- a/NGCC/tests/commands/TestRead.java +++ b/NGCC/tests/commands/TestRead.java @@ -8,7 +8,6 @@ import org.junit.jupiter.api.Test; import ch.qos.logback.classic.Level; import picocli.CommandLine; import picocli.CommandLine.Command; -import picocli.CommandLine.HelpCommand; @Command( name = "TestRead", diff --git a/NGCC/tests/csv/TestCSV.java b/NGCC/tests/csv/TestCSV.java index 35e9370..d03f5dc 100644 --- a/NGCC/tests/csv/TestCSV.java +++ b/NGCC/tests/csv/TestCSV.java @@ -1,47 +1,68 @@ package csv; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; +import java.util.HashMap; +import java.util.Map; + +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class TestCSV { + + Map map = new HashMap(); + String length = "8"; + String path = "../export/result.csv"; + GenerateCSV csv = new GenerateCSV(map,length,path); + + @BeforeEach + void setUp() { + map = new HashMap(); + length = "8"; + path = "../export/result.csv"; + } + + @Test + void testNotNull() { + + map.put("21705239", "17"); + csv = new GenerateCSV(map,length,path); + + assertFalse(csv == null); + } + + @Test + void testValid() { + + map.put("21435712", "9"); + map.put("21705239", "17"); + csv = new GenerateCSV(map,length,path); + + for (String etud : csv.etudiants.keySet()) { + assertTrue(csv.isValid(etud)); + } + + } + + + @Test + void testNotValid() { + + map.put("21435", "9"); + map.put("1705239", "17"); + csv = new GenerateCSV(map,length,path); + + for (String etud : csv.etudiants.keySet()) { + assertFalse(csv.isValid(etud)); + } + + } + + + @Test + void testGeneration() { + + } -// @Test -// public void createNullCSV() { -// //We check if a CSV object cannot be created ! -// CSV c = new CSV(); -// assertEquals(null, c.getStudentNumber()); -// assertEquals(.0, c.getGrade()); -// } -// -// @Test -// public void checkFalseStudentnumber() { -// //We check if the student number is full composed of numeric digits ! -// String s = "056498a198"; -// assertEquals(false, CSV.checkStudentNumber(s)); -// } -// -// @Test -// public void checkStudentNumberTooShort() { -// //We check if the student number is enought long ! -// String s = ""; -// assertEquals(false, CSV.checkStudentNumber(s)); -// } -// -// -// @Test -// public void checkStudentNumberTooLong() { -// //We check if the student number is enought short ! -// String s = "012345678910111213141516"; -// assertEquals(false, CSV.checkStudentNumber(s)); -// } -// -// @Test -// public void checkCorrectStudentNumber() { -// //We check if the student number is correct ! -// String s = "123456789"; -// assertEquals(true, CSV.checkStudentNumber(s)); -// } - -} +} \ No newline at end of file