Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.idea
*.iml
.classpath
.project
.settings
/target/
/bin/
5 changes: 0 additions & 5 deletions README.md

This file was deleted.

42 changes: 42 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>devinstitut</groupId>
<artifactId>java-java17</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
</properties>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.17</source>
<target>1.17</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
</dependency>
</dependencies>

</project>
23 changes: 23 additions & 0 deletions src/test/java/java17/data/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package java17.data;

public class Account {

private Person owner;
private Integer balance;

public Person getOwner() {
return owner;
}

public void setOwner(Person owner) {
this.owner = owner;
}

public Integer getBalance() {
return balance;
}

public void setBalance(Integer balance) {
this.balance = balance;
}
}
15 changes: 15 additions & 0 deletions src/test/java/java17/data/Data.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package java17.data;


import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Data {

public static List<Person> buildPersonList(int nb) {
return IntStream.rangeClosed(1,nb)
.mapToObj(i -> new Person("first_" + i, "last_" + i, i, i % 9 == 0 ? "test": "password"+i))
.collect(Collectors.toList());
}
}
51 changes: 51 additions & 0 deletions src/test/java/java17/data/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package java17.data;

public class Person {

private String firstname;
private String lastname;
private Integer age;
private String password;

public Person() {
}

public Person(String firstname, String lastname, Integer age, String password) {
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
this.password = password;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}
53 changes: 53 additions & 0 deletions src/test/java/java17/ex01/Optional_01_Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package java17.ex01;


import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.junit.Test;

import java17.data.Data;
import java17.data.Person;

/**
* Exercice 02 - Filter, Map
*/
public class Optional_01_Test {

class NotPresentException extends RuntimeException {

}

@Test
public void test_optional_ifPresent() throws Exception {

List<Person> persons = Data.buildPersonList(10);

// TODO rechercher dans la liste ci-dessus la 1ère personne ayant 18 ans
// TODO utiliser la méthode "findFirst"
Optional<Person> optPerson = persons.stream().findFirst();
assertThat(optPerson.isPresent(), is(true));

// TODO afficher la personne en question si l'optional contient une personne
System.out.println(optPerson.get().getFirstname());
}

@Test(expected=NotPresentException.class)
public void test_optional_notPresent() throws Exception {
List<Person> persons = Data.buildPersonList(50);

// TODO rechercher dans la liste ci-dessus la 1ère personne ayant 75 ans
// TODO utiliser la méthode "findFirst"
Optional<Person> optPerson = persons.stream().filter(person->person.getAge()==75).findFirst();
assertThat(optPerson.isPresent(), is(false));

Person person = optPerson.orElseThrow(NotPresentException::new);
// TODO si la personne n'existe pas, jeter une exception NotPresentException
// TODO utiliser la méthode "orElseThrow"

}
}