/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Copyright (C) 2011-2013 Marchand Eric <ricoh51@free.fr>
This file is part of Freegressi.
Freegressi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Freegressi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Freegressi. If not, see <http://www.gnu.org/licenses/>.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package freegressi.main;
import freegressi.tableur.Colonne;
import freegressi.tableur.Tableur;
import java.util.ArrayList;
import static java.util.Arrays.asList;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
*
* @author marchand
*/
public class CSVHandlerTest {
private static int goods = 0, all = 0;
public CSVHandlerTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
/**
* Test of stringToSheet method, of class CSVHandler.
*/
@Test
public void testStringToSheet() {
System.out.println("******* CSVHandler *******");
String str;
str = "a b c\nd e f \n1. 2\t3\n4 5;6"; // ok
testOk(str, 3, 2, new ArrayList<>(asList("a", "b", "c")) , new ArrayList<>(asList("d", "e", "f")), false);
str = "toto\na b c\nd e f \n1. 2\t3\n4 5;6"; // ok
testOk(str, 3, 2, new ArrayList<>(asList( "a", "b", "c")) , new ArrayList<>(asList("d", "e", "f")), false);
str = "toto tata"; // Exception
testOk(str, 3, 2, new ArrayList<>(asList( "a", "b", "c")) , new ArrayList<>(asList("d", "e", "f")), true);
str = ""; // Exception, aucune donnée
testOk(str, 0, 0, new ArrayList<>(asList( "var0")) , new ArrayList<>(asList("")), true);
str = "\n"; // Exception, aucune donnée
testOk(str, 0, 0, new ArrayList<>(asList( "var0")) , new ArrayList<>(asList("")), true);
str = "a b c\nd e f \n1. 2\t3\n4 5;"; // Exception, une ligne incomplète
testOk(str, 3, 2, new ArrayList<>(asList("a", "b", "c")) , new ArrayList<>(asList("d", "e", "f")), true);
str = "a b c\nd e f g\n1. 2\t3\n4 5;6"; // Exception, une ligne incomplète
testOk(str, 3, 2, new ArrayList<>(asList("a", "b", "c")) , new ArrayList<>(asList("d", "e", "f", "g")), true);
str = "description\na b c\nd e f\n1. 2 3\n4 5;6"; // ok
testOk(str, 3, 2, new ArrayList<>(asList("a", "b", "c")) , new ArrayList<>(asList("d", "e", "f")), false);
str = "description\na b c\n\t\t\n1. 2 3\n4 5;6"; // ok
testOk(str, 3, 2, new ArrayList<>(asList("a", "b", "c")) , new ArrayList<>(asList("", "", "")), false);
str = "test\ndescription\na b c\n\t\t\n1. 2 3\n4 5;6"; // Exception trop de lignes
testOk(str, 3, 2, new ArrayList<>(asList("a", "b", "c")) , new ArrayList<>(asList("", "", "")), true);
str = "1 b c\nd e f\n1. 2\t3\n4 5;6"; // Exception, un nom incorrect
testOk(str, 3, 2, new ArrayList<>(asList("a", "b", "c")) , new ArrayList<>(asList("d", "e", "f")), true);
str = "1 b c\nd e f\n1. 2\t3\n4 5;6"; // Exception, une unité incorrecte
testOk(str, 3, 2, new ArrayList<>(asList("a", "b", "c")) , new ArrayList<>(asList("@d", "e", "f")), true);
str = "a b c\nd e f \n1. 2\t3\n4 5;NaN"; // ok avec un NaN
testOk(str, 3, 2, new ArrayList<>(asList("a", "b", "c")) , new ArrayList<>(asList("d", "e", "f")), false);
str = "1\t2\n2\t3\n4\t5"; // ok que des doubles
testOk(str, 2, 3, new ArrayList<>(asList("var0", "var1")) , new ArrayList<>(asList("", "")), false);
System.out.println("CSVHandler : " + goods + "/"+all+" tests passés avec succès!");
}
private boolean listEquals(List<String> a, List<String> b){
if (a.size() != b.size()){
return false;
}
for (int i = 0; i < a.size(); i++){
if (!a.get(i).equals(b.get(i))){
return false;
}
}
return true;
}
private List<String> getUnits(Tableur sheet){
List<String> units = new ArrayList<>();
for (Colonne c : sheet.getListeColonnes()){
units.add(c.getColonneDescription().getUnite());
}
return units;
}
private void testOk(String str, int columns, int lines, List<String> names, List<String> units, boolean ex){
all++;
CSVHandler csv = new CSVHandler();
String err = "";
try{
Tableur sheet = csv.stringToSheet(str);
if (sheet.donneNombreColonne() != columns){
err = "Erreur dans le nombre de colonnes";
}
if (sheet.getListeInteger().size() != lines){
err = "Erreur dans le nombre de lignes";
}
if (!listEquals(sheet.donneListeNoms(), names)){
err = "Erreur dans les noms de variables";
}
if (!listEquals(getUnits(sheet), units)){
err = "Erreur dans les noms d'unités";
}
if (err.equals("")){
goods++;
}else{
System.out.println("************* Erreur dans : \n" + str + " : " + err);
System.out.println("*************");
}
} catch (Exception e){
if (ex){
goods++;
} else {
err = "Exception non traitée : " + e.getMessage();
System.out.println("************* Erreur dans : \n" + str + " : " + err);
}
}
}
}