|
| 1 | +package com.baeldung.assertions; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import org.springframework.util.Assert; |
| 10 | + |
| 11 | +public class Car { |
| 12 | + private String state = "stop"; |
| 13 | + |
| 14 | + public void drive(int speed) { |
| 15 | + Assert.isTrue(speed > 0, "speed must be positive"); |
| 16 | + this.state = "drive"; |
| 17 | + // ... |
| 18 | + if (!(speed > 0)) { |
| 19 | + throw new IllegalArgumentException("speed must be >0"); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public void stop() { |
| 24 | + this.state = "stop"; |
| 25 | + } |
| 26 | + |
| 27 | + public void fuel() { |
| 28 | + Assert.state(this.state.equals("stop"), "car must be stopped"); |
| 29 | + // ... |
| 30 | + } |
| 31 | + |
| 32 | + public void fuelwithSupplier() { |
| 33 | + Assert.state(this.state.equals("stop"), () -> "car must be stopped"); |
| 34 | + // ... |
| 35 | + } |
| 36 | + |
| 37 | + public void сhangeOil(String oil) { |
| 38 | + Assert.notNull(oil, "oil mustn't be null"); |
| 39 | + // ... |
| 40 | + } |
| 41 | + |
| 42 | + public void сhangeOilWithIsNull(String oil) { |
| 43 | + Assert.isNull(oil, "oil must be null"); |
| 44 | + // ... |
| 45 | + } |
| 46 | + |
| 47 | + public void сhangeEngine(Engine engine) { |
| 48 | + Assert.isInstanceOf(ToyotaEngine.class, engine); |
| 49 | + // ... |
| 50 | + } |
| 51 | + |
| 52 | + public void repairEngine(Engine engine) { |
| 53 | + Assert.isAssignable(Engine.class, ToyotaEngine.class); |
| 54 | + // ... |
| 55 | + } |
| 56 | + |
| 57 | + public void startWithHasLength(String key) { |
| 58 | + Assert.hasLength(key, "key must not be null and must not the empty"); |
| 59 | + // ... |
| 60 | + } |
| 61 | + |
| 62 | + public void startWithHasText(String key) { |
| 63 | + Assert.hasText(key, "key must not be null and must contain at least one non-whitespace character"); |
| 64 | + // ... |
| 65 | + } |
| 66 | + |
| 67 | + public void startWithNotContain(String key) { |
| 68 | + Assert.doesNotContain(key, "123", "key must not contain 123"); |
| 69 | + // ... |
| 70 | + } |
| 71 | + |
| 72 | + public void repair(Collection<String> repairParts) { |
| 73 | + Assert.notEmpty(repairParts, "collection of repairParts mustn't be empty"); |
| 74 | + // ... |
| 75 | + } |
| 76 | + |
| 77 | + public void repair(Map<String, String> repairParts) { |
| 78 | + Assert.notEmpty(repairParts, "map of repairParts mustn't be empty"); |
| 79 | + // ... |
| 80 | + } |
| 81 | + |
| 82 | + public void repair(String[] repairParts) { |
| 83 | + Assert.notEmpty(repairParts, "array of repairParts must not be empty"); |
| 84 | + // ... |
| 85 | + } |
| 86 | + |
| 87 | + public void repairWithNoNull(String[] repairParts) { |
| 88 | + Assert.noNullElements(repairParts, "array of repairParts must not contain null elements"); |
| 89 | + // ... |
| 90 | + } |
| 91 | + |
| 92 | + public static void main(String[] args) { |
| 93 | + Car car = new Car(); |
| 94 | + |
| 95 | + car.drive(50); |
| 96 | + |
| 97 | + car.stop(); |
| 98 | + |
| 99 | + car.fuel(); |
| 100 | + |
| 101 | + car.сhangeOil("oil"); |
| 102 | + car.сhangeOilWithIsNull(null); |
| 103 | + |
| 104 | + car.сhangeEngine(new ToyotaEngine()); |
| 105 | + |
| 106 | + car.startWithHasLength(" "); |
| 107 | + car.startWithHasText("t"); |
| 108 | + car.startWithNotContain("132"); |
| 109 | + |
| 110 | + List<String> repairPartsCollection = new ArrayList<String>(); |
| 111 | + repairPartsCollection.add("part"); |
| 112 | + car.repair(repairPartsCollection); |
| 113 | + |
| 114 | + Map<String, String> repairPartsMap = new HashMap<String, String>(); |
| 115 | + repairPartsMap.put("1", "part"); |
| 116 | + car.repair(repairPartsMap); |
| 117 | + |
| 118 | + String[] repairPartsArray = { "part" }; |
| 119 | + car.repair(repairPartsArray); |
| 120 | + |
| 121 | + } |
| 122 | +} |
0 commit comments