Skip to content

Commit 146b633

Browse files
Makoto GoEugen
authored andcommitted
New artifacts for BAEL-1081. (eugenp#2392)
* New artifacts for BAEL-1081. * Consolidate code to new package.
1 parent 71ef6b2 commit 146b633

6 files changed

Lines changed: 391 additions & 4 deletions

File tree

‎junit5/pom.xml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>com.baeldung</groupId>
6+
<!-- NOT NEEDED - JSP <groupId>com.baeldung</groupId>-->
77
<artifactId>junit5</artifactId>
88
<version>1.0-SNAPSHOT</version>
99

@@ -19,9 +19,9 @@
1919
<properties>
2020
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2121
<java.version>1.8</java.version>
22-
<junit.jupiter.version>5.0.0-M5</junit.jupiter.version>
23-
<junit.platform.version>1.0.0-M5</junit.platform.version>
24-
<junit.vintage.version>4.12.0-M5</junit.vintage.version>
22+
<junit.jupiter.version>5.0.0-RC2</junit.jupiter.version>
23+
<junit.platform.version>1.0.0-RC2</junit.platform.version>
24+
<junit.vintage.version>4.12.0-RC2</junit.vintage.version>
2525
<log4j2.version>2.8.2</log4j2.version>
2626
<h2.version>1.4.196</h2.version>
2727

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.param;
2+
3+
import java.util.Random;
4+
5+
import org.junit.jupiter.api.extension.ExtensionContext;
6+
import org.junit.jupiter.api.extension.ParameterContext;
7+
import org.junit.jupiter.api.extension.ParameterResolutionException;
8+
import org.junit.jupiter.api.extension.ParameterResolver;
9+
10+
public class InvalidPersonParameterResolver implements ParameterResolver {
11+
12+
/**
13+
* The "bad" (invalid) data for testing purposes has to go somewhere, right?
14+
*/
15+
public static Person[] INVALID_PERSONS = {
16+
new Person().setId(1L).setLastName("Ad_ams").setFirstName("Jill,"),
17+
new Person().setId(2L).setLastName(",Baker").setFirstName(""),
18+
new Person().setId(3L).setLastName(null).setFirstName(null),
19+
new Person().setId(4L).setLastName("Daniel&").setFirstName("{Joseph}"),
20+
new Person().setId(5L).setLastName("").setFirstName("English, Jane"),
21+
new Person()/* .setId(6L).setLastName("Fontana").setFirstName("Enrique") */,
22+
// TODO: ADD MORE DATA HERE
23+
};
24+
25+
@Override
26+
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
27+
throws ParameterResolutionException {
28+
Object ret = null;
29+
//
30+
// Return a random, valid Person object if Person.class is the type of Parameter
31+
/// to be resolved. Otherwise return null.
32+
if (parameterContext.getParameter().getType() == Person.class) {
33+
ret = INVALID_PERSONS[new Random().nextInt(INVALID_PERSONS.length)];
34+
}
35+
return ret;
36+
}
37+
38+
@Override
39+
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
40+
throws ParameterResolutionException {
41+
boolean ret = false;
42+
//
43+
// If the Parameter.type == Person.class, then we support it, otherwise, get outta here!
44+
if (parameterContext.getParameter().getType() == Person.class) {
45+
ret = true;
46+
}
47+
return ret;
48+
}
49+
50+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.param;
2+
3+
/**
4+
* Very simple Person entity.
5+
* Use the Fluent-style interface to set properties.
6+
*
7+
* @author J Steven Perry
8+
*
9+
*/
10+
public class Person {
11+
12+
private Long id;
13+
private String lastName;
14+
private String firstName;
15+
16+
public Long getId() {
17+
return id;
18+
}
19+
20+
public Person setId(Long id) {
21+
this.id = id;
22+
return this;
23+
}
24+
25+
public String getLastName() {
26+
return lastName;
27+
}
28+
29+
public Person setLastName(String lastName) {
30+
this.lastName = lastName;
31+
return this;
32+
}
33+
34+
public String getFirstName() {
35+
return firstName;
36+
}
37+
38+
public Person setFirstName(String firstName) {
39+
this.firstName = firstName;
40+
return this;
41+
}
42+
43+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package com.baeldung.param;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Somewhat contrived validation class to illustrate unit test
7+
* concepts.
8+
*
9+
* @author J Steven Perry
10+
*
11+
*/
12+
public class PersonValidator {
13+
14+
/**
15+
* Contrived checked exception to illustrate one possible
16+
* way to handle validation errors (via a checked exception).
17+
*
18+
* @author J Steven Perry
19+
*
20+
*/
21+
public static class ValidationException extends Exception {
22+
23+
/**
24+
*
25+
*/
26+
private static final long serialVersionUID = -134518049431883102L;
27+
28+
// Probably should implement some more constructors, but don't want
29+
/// to tarnish the lesson...
30+
31+
/**
32+
* The one and only way to create this checked exception.
33+
*
34+
* @param message
35+
* The message accompanying the exception. Should be meaningful.
36+
*/
37+
public ValidationException(String message) {
38+
super(message);
39+
40+
}
41+
42+
}
43+
44+
private static final String[] ILLEGAL_NAME_CHARACTERS = {
45+
",",
46+
"_",
47+
"{",
48+
"}",
49+
"!"
50+
};
51+
52+
/**
53+
* Validate the first name of the specified Person object.
54+
*
55+
* @param person
56+
* The Person object to validate.
57+
*
58+
* @return - returns true if the specified Person is valid
59+
*
60+
* @throws ValidationException
61+
* - this Exception is thrown if any kind of validation error occurs.
62+
*/
63+
public static boolean validateFirstName(Person person) throws ValidationException {
64+
boolean ret = true;
65+
// The validation rules go here.
66+
// Naive: use simple ifs
67+
if (person == null) {
68+
throw new ValidationException("Person is null (not allowed)!");
69+
}
70+
if (person.getFirstName() == null) {
71+
throw new ValidationException("Person FirstName is null (not allowed)!");
72+
}
73+
if (person.getFirstName().isEmpty()) {
74+
throw new ValidationException("Person FirstName is an empty String (not allowed)!");
75+
}
76+
if (!isStringValid(person.getFirstName(), ILLEGAL_NAME_CHARACTERS)) {
77+
throw new ValidationException(
78+
"Person FirstName (" + person.getFirstName() + ") may not contain any of the following characters: "
79+
+ Arrays.toString(ILLEGAL_NAME_CHARACTERS)
80+
+ "!");
81+
}
82+
return ret;
83+
}
84+
85+
/**
86+
* Validate the last name of the specified Person object. Looks the same as first
87+
* name? Look closer. Just kidding. It's the same. But real world code can (and will) diverge.
88+
*
89+
* @param person
90+
* The Person object to validate.
91+
*
92+
* @return - returns true if the specified Person is valid
93+
*
94+
* @throws ValidationException
95+
* - this Exception is thrown if any kind of validation error occurs.
96+
*/
97+
public static boolean validateLastName(Person person) throws ValidationException {
98+
boolean ret = true;
99+
// The validation rules go here.
100+
// Naive: use simple ifs
101+
if (person == null) {
102+
throw new ValidationException("Person is null (not allowed)!");
103+
}
104+
if (person.getFirstName() == null) {
105+
throw new ValidationException("Person FirstName is null (not allowed)!");
106+
}
107+
if (person.getFirstName().isEmpty()) {
108+
throw new ValidationException("Person FirstName is an empty String (not allowed)!");
109+
}
110+
if (!isStringValid(person.getFirstName(), ILLEGAL_NAME_CHARACTERS)) {
111+
throw new ValidationException(
112+
"Person LastName (" + person.getLastName() + ") may not contain any of the following characters: "
113+
+ Arrays.toString(ILLEGAL_NAME_CHARACTERS)
114+
+ "!");
115+
}
116+
return ret;
117+
}
118+
119+
/**
120+
* Validates the specified name. If it contains any of the illegalCharacters,
121+
* this method returns false (indicating the name is illegal). Otherwise it returns true.
122+
*
123+
* @param candidate
124+
* The candidate String to validate
125+
*
126+
* @param illegalCharacters
127+
* The characters the String is not allowed to have
128+
*
129+
* @return - boolean - true if the name is valid, false otherwise.
130+
*/
131+
private static boolean isStringValid(String candidate, String[] illegalCharacters) {
132+
boolean ret = true;
133+
for (String illegalChar : illegalCharacters) {
134+
if (candidate.contains(illegalChar)) {
135+
ret = false;
136+
break;
137+
}
138+
}
139+
return ret;
140+
}
141+
142+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.baeldung.param;
2+
3+
import static org.junit.jupiter.api.Assertions.assertThrows;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
import static org.junit.jupiter.api.Assertions.fail;
6+
7+
import org.junit.jupiter.api.DisplayName;
8+
import org.junit.jupiter.api.Nested;
9+
import org.junit.jupiter.api.RepeatedTest;
10+
import org.junit.jupiter.api.extension.ExtendWith;
11+
import org.junit.platform.runner.JUnitPlatform;
12+
import org.junit.runner.RunWith;
13+
14+
@RunWith(JUnitPlatform.class)
15+
@DisplayName("Testing PersonValidator")
16+
public class PersonValidatorTest {
17+
18+
/**
19+
* Nested class, uses ExtendWith
20+
* {@link com.baeldung.param.ValidPersonParameterResolver ValidPersonParameterResolver}
21+
* to feed Test methods with "good" data.
22+
*/
23+
@Nested
24+
@DisplayName("When using Valid data")
25+
@ExtendWith(ValidPersonParameterResolver.class)
26+
public class ValidData {
27+
28+
/**
29+
* Repeat the test ten times, that way we have a good shot at
30+
* running all of the data through at least once.
31+
*
32+
* @param person
33+
* A valid Person object to validate.
34+
*/
35+
@RepeatedTest(value = 10)
36+
@DisplayName("All first names are valid")
37+
public void validateFirstName(Person person) {
38+
try {
39+
assertTrue(PersonValidator.validateFirstName(person));
40+
} catch (PersonValidator.ValidationException e) {
41+
fail("Exception not expected: " + e.getLocalizedMessage());
42+
}
43+
}
44+
45+
/**
46+
* Repeat the test ten times, that way we have a good shot at
47+
* running all of the data through at least once.
48+
*
49+
* @param person
50+
* A valid Person object to validate.
51+
*/
52+
@RepeatedTest(value = 10)
53+
@DisplayName("All last names are valid")
54+
public void validateLastName(Person person) {
55+
try {
56+
assertTrue(PersonValidator.validateLastName(person));
57+
} catch (PersonValidator.ValidationException e) {
58+
fail("Exception not expected: " + e.getLocalizedMessage());
59+
}
60+
}
61+
62+
}
63+
64+
/**
65+
* Nested class, uses ExtendWith
66+
* {@link com.baeldung.param.InvalidPersonParameterResolver InvalidPersonParameterResolver}
67+
* to feed Test methods with "bad" data.
68+
*/
69+
@Nested
70+
@DisplayName("When using Invalid data")
71+
@ExtendWith(InvalidPersonParameterResolver.class)
72+
public class InvalidData {
73+
74+
/**
75+
* Repeat the test ten times, that way we have a good shot at
76+
* running all of the data through at least once.
77+
*
78+
* @param person
79+
* An invalid Person object to validate.
80+
*/
81+
@RepeatedTest(value = 10)
82+
@DisplayName("All first names are invalid")
83+
public void validateFirstName(Person person) {
84+
assertThrows(PersonValidator.ValidationException.class, () -> PersonValidator.validateFirstName(person));
85+
}
86+
87+
/**
88+
* Repeat the test ten times, that way we have a good shot at
89+
* running all of the data through at least once.
90+
*
91+
* @param person
92+
* An invalid Person object to validate.
93+
*/
94+
@RepeatedTest(value = 10)
95+
@DisplayName("All first names are invalid")
96+
public void validateLastName(Person person) {
97+
assertThrows(PersonValidator.ValidationException.class, () -> PersonValidator.validateLastName(person));
98+
}
99+
100+
}
101+
102+
}

0 commit comments

Comments
 (0)