1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testng.keyword.TestNGBuiltinKeywords as TestNGKW
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.Keys as Keys
//Manip Fichier
import java.io.*;
import java.util.ArrayList;
//REGEX
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//Apache POI -> sortie Excel
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/* L'Objectif de ce TestCase est de récupérer uniquement les valeurs du fichier B qui sont en différence avec le fichier A en entrée.
* filea - fileb = filec
* --> On part du principe que chaque ligne comporte le même nombre de colonne, pour le fichier a comme pour le fichier B.*/
//Variables
String path = "C:\\Users\\erter\\Desktop\\Nickel\\efege\\Fichiers\\";
String filea = "11299.FR.ISIE.AUTRES.ZXIH.SALES.csv"; //[N_Unique ISIE].[CodePays].[Type SI].[Type Flux].[ZXIH?].[Fonctionnalité].csv
String fileb = "3260.FR.ISIE.AUTRES.ZXIH.SALES.csv";
String filec = "Z-Nefertiti_ResultCSV_filea-b.csv";
String filed = "Z-Nefertiti_ResultExcel_filea-b.xlsx";
int numLigneA, numLigneB, i,j,k,m;
boolean trouvee = false;
//println("0 ----- Découpage Nom 1er Fichier")
String[] tabnomFichierspliteA = filea.split("\\.", -1);
//for (int i=0; i<split.length; i++)
//System.out.println(split[i]);
//System.out.println("Done");
//println("0 ----- Découpage Nom 2ème Fichier")
String[] tabnomFichierspliteB = fileb.split("\\.", -1);
String result;
for (i=0; i<tabnomFichierspliteB.length; i++) {
if(tabnomFichierspliteB[i] != tabnomFichierspliteA[i]) {
System.out.println();
trouvee = true;
result = "1 ----- Différence dans le nom du 2ème fichier: "+tabnomFichierspliteB[i]+" au lieu de "+tabnomFichierspliteA[i];
}
}
if (trouvee == false) {
System.out.println("1 ----- Pas de Différence dans les noms de fichier = Pas de différence");
result = "1 ----- Pas de Différence dans les noms de fichier = Pas de différence";
}
ArrayList al1 = new ArrayList();
ArrayList al2 = new ArrayList();
ArrayList<String> al3 = new ArrayList<String>();
BufferedReader BRFile1 = new BufferedReader(new FileReader(path+filea));
String dataRow1 = BRFile1.readLine();
BufferedReader BRFile2 = new BufferedReader(new FileReader(path+fileb));
numLigneA = 0;
i=1;// pour récupérer la ligne
j=1;// pour récupérer le n° de colonne de l'occurence
//println("------------------------------------------------------------------ FILE B : DEBUT 1er Traitement ------------------------------------------------------------------");
while (dataRow1 != null){
//println("While Loop ="+i)
String[] dataArray1 = dataRow1.split(",");
for (String item1 : dataArray1){
//println("For Loop = "+j)
//println("L"+i+"_"+"C"+j+"_"+item1);
al1.add("L"+i+"_"+"C"+j+"_"+item1);
j++;
}
dataRow1 = BRFile1.readLine(); //Read next line of data.
i++;
numLigneA = i-1;
j=1;//Remise à 0
}
println("2 ----- NB ligne 1er fichier: ("+filea+") "+numLigneA);
BRFile1.close();
//println("------------------------------------------------------------------ FILE B : FIN 1er Traitement ------------------------------------------------------------------");
String dataRow2 = BRFile2.readLine();
i = 1;
j = 1;
numLigneB = 0;
//println("------------------------------------------------------------------ FILE A : DEBUT 1er Traitement ------------------------------------------------------------------");
while (dataRow2 != null){
//println("While Loop ="+i)
String[] dataArray2 = dataRow2.split(",");
for (String item2 : dataArray2){
//println("L"+i+"_"+"C"+j+"_"+item2);
al2.add("L"+i+"_"+"C"+j+"_"+item2);
j++;
}
dataRow2 = BRFile2.readLine(); //Read next line of data.
i++;
numLigneB = i-1;
j=1;//Remise à 0
}
println("2 ----- NB ligne 2eme fichier: ("+fileb+") "+numLigneB);
BRFile2.close();
//println("------------------------------------------------------------------ FILE A : FIN 1er Traitement ------------------------------------------------------------------");
//k = 0; // pour récupérer le n° de colonne de l'occurence supprimée
//println("------------------------------------------------------------------ REMOVE DEB ------------------------------------------------------------------");
/*for (String bs:al2){
//println("k= "+k+"_"+"bs= "+bs);
al1.remove(bs);
//k++;
}*/
//Comparer et stocker les résultats dans al3
for(i=0;i< al1.size();i++){
if(al1.get(i) == al2.get(i)){
al3.add(al2.get(i)+"_OK");
//println("OK_"+al2.get(i));
}else{
al3.add(al2.get(i)+"_KO");
//println("KO_"+al2.get(i));
}
}
//println("------------------------------------------------------------------ REMOVE FIN ------------------------------------------------------------------");
//System.out.println(size);
//println("------------------------------------------------------------------ DEB DERNIER TRAITEMENT (écriture dans le fichier de sortie) ------------------------------------------------------------------");
//println("")
//println("al1: "+al1) // va jusqu'à 10 ici
//println("al3: "+al3) // n'affiche plus
/*for(String elem: al3){
System.out.print ("boucle : "+al3.toString());
}*/
// ---------------------2ème SOLUCE : filtrer sur tous les elements du tableau al3-------------------------------------------
// si on rencontre la chaîne "END" dans al3.get(h), alors on va à la ligne.
String conc = "";
int newline = 0;
Boolean found = false;
//Initialisation Fichier EXCEL :
try{
OutputStream fileOut = new FileOutputStream(path+filed);
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Comparaison ISIE");
//écrire à la ligne 1
Row row = sheet.createRow(0);//écrire à la ligne 1
Cell cell = row.createCell(0);//écrire à la Colonne 1
cell.setCellValue(result);
//écrire à la ligne 2
row = sheet.createRow(1);
cell = row.createCell(0);//écrire à la Colonne 1
cell.setCellValue("2 ----- NB ligne 1er fichier: ("+filea+") "+numLigneA);
//écrire à la ligne 3
row = sheet.createRow(2);
cell = row.createCell(0);//écrire à la Colonne 1
cell.setCellValue("3 ----- NB ligne 2eme fichier: ("+fileb+") "+numLigneB);
//écrire à la ligne 4
row = sheet.createRow(3);
cell = row.createCell(0);//écrire à la Colonne 1
cell.setCellValue("4 ----- Ecarts constatés : ");
int count=4;
for(int h = 0 ; h < al3.size(); h++) {
if (al3.get(h).contains("END") == false) {
//System.out.println("FALSE: "+al3.get(h)+" On concatène...");
conc = conc + al3.get(h)+", ";
//System.out.println("conc"+conc);
}else{
//on concatène l'enregistrement qui contient "END"
//on la créer dans le fichier EXCEL
//
conc = conc + al3.get(h)+", "; //on concatène l'enregistrement qui contient "END"
System.out.println(conc);
//on enregistre la ligne dans le tableau à la valeur actuel
newline = newline + 1; //on itère pour créer une nouvelle ligne dans le tableau
//System.out.println("TRUE: "+al3.get(h)+" On concatène + On crée une nouvelle ligne "+newline+" ");
//et on remet à vide la variable conc
//-----------------EXCEL---------------- :
row = sheet.createRow(count); //écrire à partir de la ligne 5
cell = row.createCell(0); //écrire à la Colonne 1
cell.setCellValue(conc);
//println("start(): "+match.start());
//println("end(): "+match.end());
count++;
found = true;
conc = ""; // RESET
}
}
if(!found){
//écrire à la ligne 6
row = sheet.createRow(5);
cell = row.createCell(1);//écrire à la Colonne 2
cell.setCellValue("Aucune correspondance trouvée.");
System.out.println("Aucune correspondance trouvée.")
}
//CellStyle style = wb.createCellStyle();
// Setting Background color
//style.setFillBackgroundColor(IndexedColors.RED1.getIndex());
//style.setFillPattern(FillPatternType.BIG_SPOTS);
//Cell cell = row.createCell(0);//écrire à la Colonne 1
//cell.setCellValue(result);
//cell.setCellStyle(style);
// Setting Foreground Color
//style = wb.createCellStyle();
//style.setFillForegroundColor(IndexedColors.RED1.getIndex());
//style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//Ecrire à la 2ème ligne ?
//row = sheet.createRow(1);//écrire à la ligne 2
//cell = row.createCell(0);//écrire à la Colonne 1
//cell.setCellValue("2 ----- NB ligne 1er fichier: ("+filea+") "+numLigneA);
//cell.setCellStyle(style);
wb.write(fileOut);
}catch(Exception e) {
System.out.println(e.getMessage());
}
//System.out.println("concat : "+conc);
// ---------------------1ère SOLUCE : Utiliser une REGEX sur tous les elements du tableau al3-------------------------------------------
//N.B : Cette soluce ne fonctionne pas Regex trop compliqué à mettre en place (voir si dessous)
/*String sdonnees = String.join(",",al3);//permet de concaténer chaque
System.out.println("sdonnees: "+sdonnees); // va jusqu'à 10 ici // n'affiche plus sauf si découpage dans une fonction groovy
//println("")
//spattern REGEX qui permet de scinder ligne par ligne et de pouvoir retrouver le fichier original
//String spattern = "(L(([1-9]?[0-9])|100)_C\\d_\\d.*?L\\2_C\\d{2}_\".*?\"(?:,?\$?))(?!L\\2_)";//original // aucun résultat trouvee
String spattern = "L(([1-9]?[0-9]?[0-9])|100)_C\\d_\\W.*?L\\2_C\\d{3}_\".*?\"(?:,?\$?)"; // va jusqu'à L120 mais omet {L1,L2,L3,L4} idem {L11,L12,L13,L14} etc.. + pas de C101, C102 etc... par ligne
//String spattern = "L((?!0)\\d{1,4})_C(\\d{1,4})_\\W.*?L\\1_C\\d{3}_\".*?\"(?:,|\$)";//Pattern de Uther (// va jusqu'à L120 mais omet {L1,L2,L3,L4} idem {L11,L12,L13,L14} etc.. + pas de C101, C102 etc... par ligne)
//String spattern = "L(?:[1-9]\\d?|100)_C\\d+_(?:\"[^\"]*\")?\\w*";//stackoverflow
Pattern p = Pattern.compile(spattern);
Matcher match = p.matcher(sdonnees);
Boolean found = false;
try{
OutputStream fileOut = new FileOutputStream(path+filed);
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Comparaison ISIE");
//écrire à la ligne 1
Row row = sheet.createRow(0);//écrire à la ligne 1
Cell cell = row.createCell(0);//écrire à la Colonne 1
cell.setCellValue(result);
//écrire à la ligne 2
row = sheet.createRow(1);
cell = row.createCell(0);//écrire à la Colonne 1
cell.setCellValue("2 ----- NB ligne 1er fichier: ("+filea+") "+numLigneA);
//écrire à la ligne 3
row = sheet.createRow(2);
cell = row.createCell(0);//écrire à la Colonne 1
cell.setCellValue("3 ----- NB ligne 2eme fichier: ("+fileb+") "+numLigneB);
//écrire à la ligne 4
row = sheet.createRow(3);
cell = row.createCell(0);//écrire à la Colonne 1
cell.setCellValue("4 ----- Ecarts constatés : ");
int count=4;
while(match.find()){
row = sheet.createRow(count); //écrire à partir de la ligne 5
cell = row.createCell(0); //écrire à la Colonne 1
cell.setCellValue(sdonnees.substring(match.start(),match.end()));
//println("start(): "+match.start());
//println("end(): "+match.end());
println(sdonnees.substring(match.start(),match.end()));
count++;
found = true;
}
if(!found){
//écrire à la ligne 6
row = sheet.createRow(5);
cell = row.createCell(1);//écrire à la Colonne 2
cell.setCellValue("Aucune correspondance trouvée.");
System.out.println("Aucune correspondance trouvée.")
}
//CellStyle style = wb.createCellStyle();
// Setting Background color
//style.setFillBackgroundColor(IndexedColors.RED1.getIndex());
//style.setFillPattern(FillPatternType.BIG_SPOTS);
//Cell cell = row.createCell(0);//écrire à la Colonne 1
//cell.setCellValue(result);
//cell.setCellStyle(style);
// Setting Foreground Color
//style = wb.createCellStyle();
//style.setFillForegroundColor(IndexedColors.RED1.getIndex());
//style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//Ecrire à la 2ème ligne ?
//row = sheet.createRow(1);//écrire à la ligne 2
//cell = row.createCell(0);//écrire à la Colonne 1
//cell.setCellValue("2 ----- NB ligne 1er fichier: ("+filea+") "+numLigneA);
//cell.setCellStyle(style);
wb.write(fileOut);
}catch(Exception e) {
System.out.println(e.getMessage());
}*/ |
Partager