Merge branch 'tests-idfm' into 'idf-mobilite-net'

Implemented tests for GPS and CSVTools classes

See merge request gla-groupe-3/projet!2
This commit is contained in:
RODRIGUEZ lucas 2024-04-06 10:36:36 +02:00
commit c81504b8ec
7 changed files with 261 additions and 8 deletions

View file

@ -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

View file

@ -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<Integer> getTripSequence(String representation) {
// TODO Read a trip sequence from a string
throw new RuntimeException("Not implemented yet");
}
List<Integer> 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);
}

View file

@ -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<String[]> 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<String[]> 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<String[]> contentLineConsumer) throws IOException {
try (FileWriter writer = new FileWriter(filename, StandardCharsets.UTF_8)) {

View file

@ -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(" ",""));
}
}

View file

@ -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<Integer> 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);
}
}

View file

@ -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<String[]> test = s -> System.out.println(Arrays.toString(s));
CSVTools.readCSVFromURL("https://google.fr",
test);
}
);
}
@Test
void readCSVFromURL_valid() {
assertDoesNotThrow(() -> {
Consumer<String[]> 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<String[]> 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<String[]> 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<String[]> test = Arrays.stream(t);
CSVTools.writeCSVToFile(".", test);
});
}
}

View file

@ -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));
}
}