Skip to content

Commit d75db8a

Browse files
author
Fabien "BioTheWolff" Zoccola
authored
Merge pull request #8 from DevLab-umontp/debug
🚑 Patch du package
2 parents 4c8efd9 + 5286d6e commit d75db8a

File tree

7 files changed

+134
-28
lines changed

7 files changed

+134
-28
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- [![Maven Central](https://img.shields.io/maven-central/v/com.github.devlab-umontp/villes-et-departements-de-france.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.github.devlab-umontp%22%20AND%20a:%22villes-et-departements-de-france%22) -->
1+
[![Maven Central](https://img.shields.io/maven-central/v/com.github.devlab-umontp/villes-et-departements-de-france.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.github.devlab-umontp%22%20AND%20a:%22villes-et-departements-de-france%22)
22
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=DevLab-umontp_Librairie-Java-Villes-de-France&metric=alert_status)](https://sonarcloud.io/dashboard?id=DevLab-umontp_Librairie-Java-Villes-de-France)
33
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=DevLab-umontp_Librairie-Java-Villes-de-France&metric=coverage)](https://sonarcloud.io/dashboard?id=DevLab-umontp_Librairie-Java-Villes-de-France)
44
![GitHub Actions](https://github.com/DevLab-umontp/Librairie-Java-Villes-de-France/workflows/Java%20CI%20with%20Maven/badge.svg)
@@ -23,7 +23,7 @@ Si vous avez Maven, ajoutez ce qui suit aux dépendances de votre fichier `pom.x
2323
<dependency>
2424
<groupId>com.github.devlab-umontp</groupId>
2525
<artifactId>villes-et-departements-de-france</artifactId>
26-
<version>1.0.0</version>
26+
<version>1.0.1</version>
2727
</dependency>
2828
```
2929
>*Voir un exemple de fichier [pom.xml](https://github.com/DevLab-umontp/Librarie-Java-Villes-de-France/blob/main/ressources/pom.xml#L20L24) avec la librairie Villes de France*
@@ -32,7 +32,7 @@ Si vous avez Maven, ajoutez ce qui suit aux dépendances de votre fichier `pom.x
3232
Si vous utilisez Gradle, ajoutez ce qui suit aux dépendances de votre fichier `build.gradle`:
3333

3434
```
35-
implementation 'com.github.devlab-umontp:villes-et-departements-de-france:1.0.0'
35+
implementation 'com.github.devlab-umontp:villes-et-departements-de-france:1.0.1'
3636
```
3737

3838
## Exemple de code d'utilisation de la Librairie Java Villes de France

pom.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>com.github.devlab-umontp</groupId>
44
<artifactId>villes-et-departements-de-france</artifactId>
5-
<version>1.0.0</version>
5+
<version>1.0.1</version>
66
<packaging>jar</packaging>
77

88
<name>Code postaux villes et départements de France</name>
@@ -36,6 +36,7 @@
3636
<groupId>org.hamcrest</groupId>
3737
<artifactId>hamcrest</artifactId>
3838
<version>2.2</version>
39+
<scope>test</scope>
3940
</dependency>
4041

4142
<dependency>
@@ -76,6 +77,12 @@
7677
<organization>com.github.devlab-umontp</organization>
7778
<organizationUrl>https://github.com/DevLab-umontp</organizationUrl>
7879
</developer>
80+
<developer>
81+
<name>Fabien ZOCCOLA</name>
82+
<email>[email protected]</email>
83+
<organization>com.github.devlab-umontp</organization>
84+
<organizationUrl>https://github.com/DevLab-umontp</organizationUrl>
85+
</developer>
7986
</developers>
8087

8188
<issueManagement>
@@ -191,6 +198,16 @@
191198
</profiles>
192199

193200
<build>
201+
<!-- Resources (the .CSV) -->
202+
<resources>
203+
<resource>
204+
<directory>${project.basedir}</directory>
205+
<includes>
206+
<include>communes.csv</include>
207+
</includes>
208+
</resource>
209+
</resources>
210+
194211
<plugins>
195212

196213
<plugin>
@@ -262,4 +279,5 @@
262279

263280
</plugins>
264281
</build>
282+
265283
</project>

ressources/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>com.github.devlab-umontp</groupId>
2222
<artifactId>villes-et-departements-de-france</artifactId>
23-
<version>1.0.0</version>
23+
<version>1.0.1</version>
2424
</dependency>
2525
<dependency>
2626
<groupId>org.junit.jupiter</groupId>

src/main/java/fr/france/RepertoireCommune.java

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package fr.france;
22

33
import java.io.FileNotFoundException;
4-
import java.io.FileReader;
4+
import java.io.Reader;
55
import java.util.ArrayList;
66
import java.util.Collections;
77
import java.util.HashMap;
88
import java.util.List;
99
import java.util.Map;
1010

11-
import com.opencsv.bean.CsvToBeanBuilder;
11+
import org.apache.commons.logging.Log;
12+
import org.apache.commons.logging.LogFactory;
1213

14+
import fr.france.outils.OutilsRepertoireCommune;
1315
import fr.france.outils.OutilsString;
1416

1517
/**
@@ -31,19 +33,21 @@
3133
*/
3234
public class RepertoireCommune {
3335

36+
private static final Log log = LogFactory.getLog(RepertoireCommune.class);
37+
3438
private RepertoireCommune() {
35-
throw new IllegalStateException("Class utilitaire");
39+
throw new IllegalStateException("Classe utilitaire");
3640
}
3741

3842
private static final List<Commune> COMMUNES = initCommunes();
3943

4044
private static List<Commune> initCommunes() {
4145
List<Commune> result = null;
4246
try {
43-
result = new CsvToBeanBuilder<Commune>(new FileReader("communes.csv")).withType(Commune.class).build()
44-
.parse();
47+
Reader reader = OutilsRepertoireCommune.getRessource("communes.csv");
48+
result = OutilsRepertoireCommune.convertirCsvEnCommune(reader);
4549
} catch (IllegalStateException | FileNotFoundException e) {
46-
System.err.println("Le fichier contenant les communes n'a pas été trouvé");
50+
log.error(e.getMessage(), e);
4751
}
4852
return result;
4953
}
@@ -53,23 +57,13 @@ private static List<Commune> initCommunes() {
5357
private static final Map<String, Commune> REPERTOIRE_NOM_ET_CODE_POSTAL;
5458

5559
static {
56-
Map<Integer, List<Commune>> codePostaux = new HashMap<>();
57-
Map<String, List<Commune>> noms = new HashMap<>();
58-
Map<String, Commune> nomEtCodePostal = new HashMap<>();
59-
for (Commune Commune : COMMUNES) {
60-
if (Commune.getNom() == null)
61-
continue;
62-
nomEtCodePostal.put(OutilsString.formater(Commune.getNom()) + Commune.getCodePostal(), Commune);
63-
List<Commune> villesNom = noms.computeIfAbsent(OutilsString.formater(Commune.getNom()),
64-
k -> new ArrayList<>());
65-
List<Commune> villesCodePostal = codePostaux.computeIfAbsent(Commune.getCodePostal(),
66-
k -> new ArrayList<>());
67-
villesNom.add(Commune);
68-
villesCodePostal.add(Commune);
69-
}
70-
REPERTOIRE_CODE_POSTAL = Collections.unmodifiableMap(codePostaux);
71-
REPERTOIRE_NOM = Collections.unmodifiableMap(noms);
72-
REPERTOIRE_NOM_ET_CODE_POSTAL = Collections.unmodifiableMap(nomEtCodePostal);
60+
Map<Integer, List<Commune>> repCodePostaux = new HashMap<>();
61+
Map<String, List<Commune>> repNoms = new HashMap<>();
62+
Map<String, Commune> repNomEtCodePostal = new HashMap<>();
63+
OutilsRepertoireCommune.initialiserRepertoires(repCodePostaux, repNoms, repNomEtCodePostal, COMMUNES);
64+
REPERTOIRE_CODE_POSTAL = Collections.unmodifiableMap(repCodePostaux);
65+
REPERTOIRE_NOM = Collections.unmodifiableMap(repNoms);
66+
REPERTOIRE_NOM_ET_CODE_POSTAL = Collections.unmodifiableMap(repNomEtCodePostal);
7367
}
7468

7569
/**
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package fr.france.outils;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.InputStream;
5+
import java.io.InputStreamReader;
6+
import java.io.Reader;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
import com.opencsv.bean.CsvToBeanBuilder;
12+
13+
import fr.france.Commune;
14+
import fr.france.RepertoireCommune;
15+
16+
/**
17+
* <b>OutilsRepertoireCommune est la classe utilitaire de la class
18+
* RepertoireCommune.</b>
19+
* <p>
20+
* Elle permet d'alléger la class {@link fr.france.RepertoireCommune}
21+
* </p>
22+
*
23+
* @see RepertoireCommune
24+
*
25+
* @author MathieuSoysal
26+
* @version 1.0.0
27+
*/
28+
public class OutilsRepertoireCommune {
29+
30+
private OutilsRepertoireCommune() {
31+
throw new IllegalStateException("Class utilitaire");
32+
}
33+
34+
public static List<Commune> convertirCsvEnCommune(Reader reader) {
35+
return new CsvToBeanBuilder<Commune>(reader).withType(Commune.class).build().parse();
36+
}
37+
38+
public static Reader getRessource(String fileName) throws FileNotFoundException {
39+
InputStream resource = RepertoireCommune.class.getClassLoader().getResourceAsStream(fileName);
40+
if (resource == null)
41+
throw new FileNotFoundException("Le fichier contenant les communes n'a pas été trouvé");
42+
return new InputStreamReader(resource);
43+
}
44+
45+
public static void initialiserRepertoires(Map<Integer, List<Commune>> codePostaux, Map<String, List<Commune>> noms,
46+
Map<String, Commune> nomEtCodePostal, final List<Commune> communes) {
47+
for (Commune Commune : communes) {
48+
nomEtCodePostal.put(OutilsString.formater(Commune.getNom()) + Commune.getCodePostal(), Commune);
49+
List<Commune> villesNom = noms.computeIfAbsent(OutilsString.formater(Commune.getNom()),
50+
k -> new ArrayList<>());
51+
List<Commune> villesCodePostal = codePostaux.computeIfAbsent(Commune.getCodePostal(),
52+
k -> new ArrayList<>());
53+
villesNom.add(Commune);
54+
villesCodePostal.add(Commune);
55+
}
56+
}
57+
58+
}

src/test/java/fr/france/RepertoireCaumuneTest.java renamed to src/test/java/fr/france/RepertoireCommuneTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,32 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertNotEquals;
55
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
67

8+
import java.lang.reflect.Constructor;
9+
import java.lang.reflect.InvocationTargetException;
710
import java.util.Collections;
811
import java.util.List;
912

1013
import org.junit.jupiter.api.Test;
1114

1215
class RepertoireCommuneTest {
1316

17+
@Test
18+
void test_ConstructionJetteIllegalStateException() throws NoSuchMethodException {
19+
Constructor<RepertoireCommune> constructor = RepertoireCommune.class.getDeclaredConstructor();
20+
constructor.setAccessible(true);
21+
22+
// On vérifie qu'il jette une erreur
23+
assertThrows(InvocationTargetException.class, constructor::newInstance);
24+
try {
25+
constructor.newInstance();
26+
} catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
27+
// On vérifie que la cause est bien une IllegalStateException
28+
assertEquals("java.lang.IllegalStateException: Classe utilitaire", e.getCause().toString());
29+
}
30+
}
31+
1432
@Test
1533
void test_CommunesInitialise() {
1634
assertNotEquals(Collections.emptyList(), RepertoireCommune.getCommunes());
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package fr.france.outils;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.io.FileNotFoundException;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
class OutilsRepertoireCommuneTest {
11+
12+
@Test
13+
void test_getRessources() {
14+
Throwable e = assertThrows(FileNotFoundException.class, () -> OutilsRepertoireCommune.getRessource("noFile"));
15+
assertEquals("Le fichier contenant les communes n'a pas été trouvé", e.getMessage());
16+
}
17+
18+
}

0 commit comments

Comments
 (0)