diff --git a/src/main/java/fr/u_paris/gla/project/io/NetworkFormat.java b/src/main/java/fr/u_paris/gla/project/io/NetworkFormat.java index 2cb4bbd..8dadf35 100644 --- a/src/main/java/fr/u_paris/gla/project/io/NetworkFormat.java +++ b/src/main/java/fr/u_paris/gla/project/io/NetworkFormat.java @@ -33,10 +33,10 @@ public final class NetworkFormat { private static final DateTimeFormatter DURATION_FORMATTER = DateTimeFormatter .ofPattern("HH:mm:ss"); - private static final NumberFormat DURATION_SECONDS_FORMATTER = NumberFormat + private static final NumberFormat DURATION_INDIVIDUAL_FORMATTER = NumberFormat .getIntegerInstance(Locale.ENGLISH); static { - DURATION_SECONDS_FORMATTER.setMinimumIntegerDigits(2); + DURATION_INDIVIDUAL_FORMATTER.setMinimumIntegerDigits(2); } private static final Temporal ZERO = LocalTime.parse("00:00:00"); @@ -45,14 +45,22 @@ public final class NetworkFormat { // Tool class } + /** Convert a {@link java.lang.String} into a {@link java.time.Duration} + * @param duration the {@link java.lang.String} + * @return the {@link java.time.Duration} object + */ public static Duration parseDuration(String duration) { LocalTime time = LocalTime.parse("00:" + duration, DURATION_FORMATTER); return Duration.between(time, ZERO); } + /** Format a {@link java.time.Duration} into a {@link java.lang.String} + * @param duration an object of type {@link java.time.Duration} + * @return a String that depicts the duration in format MM:SS + */ public static String formatDuration(Duration duration) { - return Long.toString(duration.toMinutes()) + ":" - + DURATION_SECONDS_FORMATTER.format(duration.toSecondsPart()); + return DURATION_INDIVIDUAL_FORMATTER.format(duration.toMinutes()) + ":" + + DURATION_INDIVIDUAL_FORMATTER.format(duration.toSecondsPart()); } /** Get a formatter for the numbers in a GPS coordinate pair diff --git a/src/main/java/fr/u_paris/gla/project/io/ScheduleFormat.java b/src/main/java/fr/u_paris/gla/project/io/ScheduleFormat.java index fc4d4ba..c97b233 100644 --- a/src/main/java/fr/u_paris/gla/project/io/ScheduleFormat.java +++ b/src/main/java/fr/u_paris/gla/project/io/ScheduleFormat.java @@ -5,6 +5,7 @@ package fr.u_paris.gla.project.io; import java.time.format.DateTimeFormatter; import java.time.format.ResolverStyle; +import java.util.ArrayList; import java.util.List; /** @@ -32,11 +33,17 @@ public final class ScheduleFormat { * @return the sequence of branching */ public static List getTripSequence(String representation) { - // TODO Read a trip sequence from a string - throw new RuntimeException("Not implemented yet"); - } + List l = new ArrayList<>(); + for(String s : representation.split(",")) + l.add(Integer.parseInt(s)); + return l; + } + + /** Create a {@link java.time.format.DateTimeFormatter} object used to format Dates + * @return the formatter + */ public static DateTimeFormatter getTimeFormatter() { return DateTimeFormatter.ofPattern("HH:mm").withResolverStyle(ResolverStyle.LENIENT); } diff --git a/src/main/java/fr/u_paris/gla/project/utils/CSVTools.java b/src/main/java/fr/u_paris/gla/project/utils/CSVTools.java index 7d50613..d87442d 100644 --- a/src/main/java/fr/u_paris/gla/project/utils/CSVTools.java +++ b/src/main/java/fr/u_paris/gla/project/utils/CSVTools.java @@ -45,18 +45,28 @@ public final class CSVTools { throw new IOException("Invalid csv file", e); //$NON-NLS-1$ } } - + public static void readCSVFromFile(String filename, Consumer contentLineConsumer) throws IOException { File file = new File(filename); readCSVFromInputStream(new FileInputStream(file), contentLineConsumer); } + /** get a CSV file from a URL, download and parse it, and keep values in memory + * @param url the address of the CSV file + * @param contentLineConsumer the variable used to store the data + * @throws IOException if it's impossible to download the file + */ public static void readCSVFromURL(String url, Consumer contentLineConsumer) throws IOException { readCSVFromInputStream(new URL(url).openStream(), contentLineConsumer); } + /** Save our current CSV variable's data into an actual file + * @param filename the saved file's name and path + * @param contentLineConsumer our data variable + * @throws IOException if we can't write the data into the file + */ public static void writeCSVToFile(String filename, Stream contentLineConsumer) throws IOException { try (FileWriter writer = new FileWriter(filename, StandardCharsets.UTF_8)) { diff --git a/src/test/java/fr/u_paris/gla/project/io/NetworkFormatTest.java b/src/test/java/fr/u_paris/gla/project/io/NetworkFormatTest.java new file mode 100644 index 0000000..a46f12d --- /dev/null +++ b/src/test/java/fr/u_paris/gla/project/io/NetworkFormatTest.java @@ -0,0 +1,80 @@ +package fr.u_paris.gla.project.io; + +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.text.NumberFormat; +import java.time.Duration; +import java.time.LocalTime; +import java.time.format.DateTimeParseException; + +import static org.junit.jupiter.api.Assertions.*; + +class NetworkFormatTest { + + String t = "00:00"; + NumberFormat GPS_test = NetworkFormat.getGPSFormatter(); + + @Test + void parseDurationEqual() { + + assertEquals(Duration.ZERO, NetworkFormat.parseDuration(t)); + } + + @Test + void parseDurationTooBig() { + String y = "119:00"; + assertThrows(DateTimeParseException.class, () -> NetworkFormat.parseDuration(y)); + } + + @Test + void formatDuration() { + assertEquals(t, NetworkFormat.formatDuration(Duration.ZERO)); + } + + @Test + void parseThenFormatDuration(){ + String t = "00:00"; + assertEquals(t, NetworkFormat.formatDuration(NetworkFormat.parseDuration(t))); + } + + @Test + void getGPSFormatterPos() { + double GPS_pos = 1.456489615649813; + assertEquals(String.valueOf(GPS_pos), GPS_test.format(GPS_pos)); + + + } + + @Test + void getGPSFormatterNeg() { + double GPS_neg = -1.456489615649813; + assertEquals(String.valueOf(GPS_neg), GPS_test.format(GPS_neg)); + + + } + @Test + void getGPSFormatterNul() { + int GPS_nul = 0; + assertEquals(String.valueOf(GPS_nul), GPS_test.format(GPS_nul)); + + + } + + @Test + void getGPSFormatterBig() { + String string_int = "4565156498156489"; + String string_float = "5675747274674276474267479751262167"; + BigDecimal GPS_big = new BigDecimal(string_int + "." + string_float); + + + assertEquals(string_int + "." + string_float.substring(0, NetworkFormat.GPS_PRECISION), + GPS_test.format(GPS_big).replace(",", "").replace(" ","")); + + + } + + + +} \ No newline at end of file diff --git a/src/test/java/fr/u_paris/gla/project/io/ScheduleFormatTest.java b/src/test/java/fr/u_paris/gla/project/io/ScheduleFormatTest.java new file mode 100644 index 0000000..f9eae35 --- /dev/null +++ b/src/test/java/fr/u_paris/gla/project/io/ScheduleFormatTest.java @@ -0,0 +1,33 @@ +package fr.u_paris.gla.project.io; + +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class ScheduleFormatTest { + + @Test + public void getTripSequence() { + String rpz = "4,5,19,21"; + List test = Arrays.asList(4, 5, 19, 21); + + assertEquals(test, ScheduleFormat.getTripSequence(rpz)); + } + + @Test + void getTimeFormatter() { + DateTimeFormatter formatter = ScheduleFormat.getTimeFormatter(); + LocalDateTime date = LocalDateTime.now(); + String test = date.format(formatter); + //format date: YYYY-MM-DDTHH:MM:SS.DECIMAL + assertEquals(date.toString().substring(11, 16), test); + + + } +} \ No newline at end of file diff --git a/src/test/java/fr/u_paris/gla/project/utils/CSVToolsTest.java b/src/test/java/fr/u_paris/gla/project/utils/CSVToolsTest.java new file mode 100644 index 0000000..0fbd4c8 --- /dev/null +++ b/src/test/java/fr/u_paris/gla/project/utils/CSVToolsTest.java @@ -0,0 +1,76 @@ +package fr.u_paris.gla.project.utils; + + +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.Arrays; + +import java.util.function.Consumer; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.*; + +class CSVToolsTest { + + + + + @Test + void readCSVFromURL_invalid() { + assertThrows(IOException.class,() -> { + Consumer test = s -> System.out.println(Arrays.toString(s)); + CSVTools.readCSVFromURL("https://google.fr", + test); + + } + ); + } + + @Test + void readCSVFromURL_valid() { + assertDoesNotThrow(() -> { + Consumer test = s -> System.out.println(Arrays.toString(s)); + CSVTools.readCSVFromURL("https://people.sc.fsu.edu/~jburkardt/data/csv/addresses.csv", + test); + + } + ); + } + + @Test + void writeCSVToFile() { + + assertDoesNotThrow(() -> { + String[] stuff = {"jsqdsqdsqsqffdfgzava", "pfezegrrbeebn", "dfbsduifzegbczi", "sdfsdfcy"}; + String[][] t = {stuff, stuff}; + Stream test = Arrays.stream(t); + CSVTools.writeCSVToFile("test.csv", test); + + }); + } + + @Test + void writeCSVToFile_specialName() { + + assertDoesNotThrow(() -> { + String[] stuff = {"jsqdsqdsqsqffdfgzava", "pfezegrrbeebn", "dfbsduifzegbczi", "sdfsdfcy"}; + String[][] t = {stuff, stuff}; + Stream test = Arrays.stream(t); + CSVTools.writeCSVToFile("éè'-'_-éè_à.csv", test); + + }); + } + + @Test + void writeCSVToFile_invalidName() { + + assertThrows( IOException.class ,() -> { + String[] stuff = {"jsqdsqdsqsqffdfgzava", "pfezegrrbeebn", "dfbsduifzegbczi", "sdfsdfcy"}; + String[][] t = {stuff, stuff}; + Stream test = Arrays.stream(t); + CSVTools.writeCSVToFile(".", test); + + }); + } +} \ No newline at end of file diff --git a/src/test/java/fr/u_paris/gla/project/utils/GPSTest.java b/src/test/java/fr/u_paris/gla/project/utils/GPSTest.java new file mode 100644 index 0000000..de63357 --- /dev/null +++ b/src/test/java/fr/u_paris/gla/project/utils/GPSTest.java @@ -0,0 +1,39 @@ +package fr.u_paris.gla.project.utils; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class GPSTest { + + + @Test + void distance_SameLat(){ + assertDoesNotThrow( + () -> { + GPS.distance(5, 3, 5, 11); + } + ); + } + + @Test + void distance_SameLon(){ + assertDoesNotThrow( + () -> { + GPS.distance(5, 3, 7, 3); + } + ); + } + + @Test + void distance_SamePoint() { + assertEquals(0.0, GPS.distance(5, 3, 5, 3) ); + } + + @Test + void distance_NegativePoint(){ + assertNotEquals(0.0, GPS.distance(-5, 7, -13, 4)); + } + + +} \ No newline at end of file