forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#3967 from myluckagain/BAEL-1645
BAEL-1645
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
spring-5/src/main/java/com/baeldung/assertions/Car.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.baeldung.assertions; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.springframework.util.Assert; | ||
|
||
public class Car { | ||
private String state = "stop"; | ||
|
||
public void drive(int speed) { | ||
Assert.isTrue(speed > 0, "speed must be positive"); | ||
this.state = "drive"; | ||
// ... | ||
if (!(speed > 0)) { | ||
throw new IllegalArgumentException("speed must be >0"); | ||
} | ||
} | ||
|
||
public void stop() { | ||
this.state = "stop"; | ||
} | ||
|
||
public void fuel() { | ||
Assert.state(this.state.equals("stop"), "car must be stopped"); | ||
// ... | ||
} | ||
|
||
public void fuelwithSupplier() { | ||
Assert.state(this.state.equals("stop"), () -> "car must be stopped"); | ||
// ... | ||
} | ||
|
||
public void сhangeOil(String oil) { | ||
Assert.notNull(oil, "oil mustn't be null"); | ||
// ... | ||
} | ||
|
||
public void сhangeOilWithIsNull(String oil) { | ||
Assert.isNull(oil, "oil must be null"); | ||
// ... | ||
} | ||
|
||
public void сhangeEngine(Engine engine) { | ||
Assert.isInstanceOf(ToyotaEngine.class, engine); | ||
// ... | ||
} | ||
|
||
public void repairEngine(Engine engine) { | ||
Assert.isAssignable(Engine.class, ToyotaEngine.class); | ||
// ... | ||
} | ||
|
||
public void startWithHasLength(String key) { | ||
Assert.hasLength(key, "key must not be null and must not the empty"); | ||
// ... | ||
} | ||
|
||
public void startWithHasText(String key) { | ||
Assert.hasText(key, "key must not be null and must contain at least one non-whitespace character"); | ||
// ... | ||
} | ||
|
||
public void startWithNotContain(String key) { | ||
Assert.doesNotContain(key, "123", "key must not contain 123"); | ||
// ... | ||
} | ||
|
||
public void repair(Collection<String> repairParts) { | ||
Assert.notEmpty(repairParts, "collection of repairParts mustn't be empty"); | ||
// ... | ||
} | ||
|
||
public void repair(Map<String, String> repairParts) { | ||
Assert.notEmpty(repairParts, "map of repairParts mustn't be empty"); | ||
// ... | ||
} | ||
|
||
public void repair(String[] repairParts) { | ||
Assert.notEmpty(repairParts, "array of repairParts must not be empty"); | ||
// ... | ||
} | ||
|
||
public void repairWithNoNull(String[] repairParts) { | ||
Assert.noNullElements(repairParts, "array of repairParts must not contain null elements"); | ||
// ... | ||
} | ||
|
||
public static void main(String[] args) { | ||
Car car = new Car(); | ||
|
||
car.drive(50); | ||
|
||
car.stop(); | ||
|
||
car.fuel(); | ||
|
||
car.сhangeOil("oil"); | ||
car.сhangeOilWithIsNull(null); | ||
|
||
car.сhangeEngine(new ToyotaEngine()); | ||
|
||
car.startWithHasLength(" "); | ||
car.startWithHasText("t"); | ||
car.startWithNotContain("132"); | ||
|
||
List<String> repairPartsCollection = new ArrayList<String>(); | ||
repairPartsCollection.add("part"); | ||
car.repair(repairPartsCollection); | ||
|
||
Map<String, String> repairPartsMap = new HashMap<String, String>(); | ||
repairPartsMap.put("1", "part"); | ||
car.repair(repairPartsMap); | ||
|
||
String[] repairPartsArray = { "part" }; | ||
car.repair(repairPartsArray); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.baeldung.assertions; | ||
|
||
public class Engine { | ||
} |
5 changes: 5 additions & 0 deletions
5
spring-5/src/main/java/com/baeldung/assertions/ToyotaEngine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.baeldung.assertions; | ||
|
||
public class ToyotaEngine extends Engine { | ||
|
||
} |