Skip to content

Commit 910ce13

Browse files
committed
refactoring asserts
1 parent c51e790 commit 910ce13

7 files changed

Lines changed: 33 additions & 24 deletions

File tree

src/main/java/com/aventstack/extentreports/ExtentTest.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.aventstack.extentreports.model.ScreenCapture;
1717
import com.aventstack.extentreports.model.Test;
1818
import com.aventstack.extentreports.model.service.ExceptionInfoService;
19+
import com.aventstack.extentreports.util.Assert;
1920

2021
import lombok.Getter;
2122

@@ -92,8 +93,7 @@ public class ExtentTest implements RunResult, Serializable {
9293
* Test description
9394
*/
9495
ExtentTest(ExtentReports extent, Class<? extends IGherkinFormatterModel> type, String name, String description) {
95-
if (name == null || name.isEmpty())
96-
throw new IllegalArgumentException("Test name cannot be null or empty");
96+
Assert.notEmpty(name, "Test name must not be null or empty");
9797
model = Test.builder()
9898
.bddType(type)
9999
.name(name)
@@ -357,8 +357,7 @@ public ExtentTest generateLog(Status status, Markup markup) {
357357
* @return An {@link ExtentTest} object
358358
*/
359359
public ExtentTest log(Status status, String details, Throwable t, Media media) {
360-
if (status == null)
361-
throw new IllegalArgumentException("Status must not be null");
360+
Assert.notNull(status, "Status must not be null");
362361
Log log = Log.builder()
363362
.status(status)
364363
.details(details == null ? "" : details)
@@ -1068,8 +1067,7 @@ public Status getStatus() {
10681067
}
10691068

10701069
public ExtentTest addScreenCaptureFromPath(String path, String title) {
1071-
if (path == null || path.isEmpty())
1072-
throw new IllegalArgumentException("ScreenCapture path cannot be null or empty");
1070+
Assert.notEmpty(path, "ScreenCapture path must not be null or empty");
10731071
Media m = ScreenCapture.builder().path(path).title(title).build();
10741072
model.addMedia(m);
10751073
extent.onMediaAdded(m, model);
@@ -1081,8 +1079,7 @@ public ExtentTest addScreenCaptureFromPath(String path) {
10811079
}
10821080

10831081
public ExtentTest addScreenCaptureFromBase64String(String base64, String title) {
1084-
if (base64 == null || base64.isEmpty())
1085-
throw new IllegalArgumentException("Base64 string cannot be null or empty");
1082+
Assert.notEmpty(base64, "ScreenCapture's base64 string must not be null or empty");
10861083
if (!base64.startsWith("data:"))
10871084
base64 = "data:image/png;base64," + base64;
10881085
Media m = ScreenCapture.builder().base64(base64).title(title).build();

src/main/java/com/aventstack/extentreports/MediaEntityBuilder.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.aventstack.extentreports.model.Media;
44
import com.aventstack.extentreports.model.ScreenCapture;
5+
import com.aventstack.extentreports.util.Assert;
56

67
/**
78
* Media builder for base64 and binary screenshots
@@ -30,8 +31,7 @@ public Media build() {
3031
}
3132

3233
public static MediaEntityBuilder createScreenCaptureFromPath(String path, String title) {
33-
if (path == null || path.isEmpty())
34-
throw new IllegalArgumentException("ScreenCapture path cannot be null or empty");
34+
Assert.notEmpty(path, "ScreenCapture path must not be null or empty");
3535
media.set(ScreenCapture.builder().path(path).title(title).build());
3636
return getInstance();
3737
}
@@ -41,8 +41,7 @@ public static MediaEntityBuilder createScreenCaptureFromPath(String path) {
4141
}
4242

4343
public static MediaEntityBuilder createScreenCaptureFromBase64String(String base64, String title) {
44-
if (base64 == null || base64.trim().equals(""))
45-
throw new IllegalArgumentException("Base64 string cannot be null or empty");
44+
Assert.notEmpty(base64, "ScreenCapture's base64 string must not be null or empty");
4645
if (!base64.startsWith("data:"))
4746
base64 = BASE64_ENCODED + base64;
4847
media.set(ScreenCapture.builder().base64(base64).title(title).build());

src/main/java/com/aventstack/extentreports/config/external/JsonConfigLoader.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.lang.reflect.Type;
77
import java.nio.file.Files;
88

9+
import com.aventstack.extentreports.util.Assert;
910
import com.google.gson.Gson;
1011
import com.google.gson.GsonBuilder;
1112
import com.google.gson.InstanceCreator;
@@ -17,17 +18,15 @@ public class JsonConfigLoader<T> implements ConfigLoadable<T> {
1718
private InstanceCreator<T> creator;
1819

1920
public JsonConfigLoader(T instance, File f) throws FileNotFoundException {
20-
if (f == null)
21-
throw new IllegalArgumentException("File cannot be null");
21+
Assert.notNull(f, "File must not be null");
2222
if (!f.exists())
2323
throw new FileNotFoundException("File " + f.getAbsolutePath() + " could not be found");
2424
init(instance);
2525
this.f = f;
2626
}
2727

2828
public JsonConfigLoader(T instance, String json) {
29-
if (json == null || json.isEmpty())
30-
throw new IllegalArgumentException("Json input cannot be null or empty");
29+
Assert.notEmpty(json, "Json input must not be null or empty");
3130
init(instance);
3231
this.json = json;
3332
}

src/main/java/com/aventstack/extentreports/model/Test.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.aventstack.extentreports.gherkin.model.IGherkinFormatterModel;
2020
import com.aventstack.extentreports.gherkin.model.Scenario;
2121
import com.aventstack.extentreports.gherkin.model.ScenarioOutline;
22+
import com.aventstack.extentreports.util.Assert;
2223

2324
import lombok.AllArgsConstructor;
2425
import lombok.Builder;
@@ -64,8 +65,7 @@ public final class Test implements RunResult, Serializable, BaseEntity, MetaData
6465
private final List<Log> generatedLog = Collections.synchronizedList(new ArrayList<>());
6566

6667
public final void addChild(Test child) {
67-
if (child == null)
68-
throw new IllegalArgumentException("Node must not be null");
68+
Assert.notNull(child, "Node must not be null");
6969
child.setLevel(level + 1);
7070
child.setParent(this);
7171
child.setLeaf(true);
@@ -100,8 +100,7 @@ public final void addGeneratedLog(Log log) {
100100
}
101101

102102
private final void addLog(Log log, List<Log> list) {
103-
if (log == null)
104-
throw new IllegalArgumentException("Log must not be null");
103+
Assert.notNull(log, "Log must not be null");
105104
log.setSeq(list.size());
106105
list.add(log);
107106
end(log.getStatus());

src/main/java/com/aventstack/extentreports/model/context/NamedAttributeContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.aventstack.extentreports.Status;
1111
import com.aventstack.extentreports.model.NamedAttribute;
1212
import com.aventstack.extentreports.model.Test;
13+
import com.aventstack.extentreports.util.Assert;
1314

1415
import lombok.Getter;
1516
import lombok.ToString;
@@ -29,8 +30,7 @@ public NamedAttributeContext(T attribute, Test test) {
2930
}
3031

3132
public void addTest(Test test) {
32-
if (test == null)
33-
throw new IllegalArgumentException("Test cannot be null");
33+
Assert.notNull(test, "Test must not be null");
3434
testList.add(test);
3535
refresh(test);
3636
}

src/main/java/com/aventstack/extentreports/model/service/TestService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.aventstack.extentreports.gherkin.model.IGherkinFormatterModel;
77
import com.aventstack.extentreports.model.Log;
88
import com.aventstack.extentreports.model.Test;
9+
import com.aventstack.extentreports.util.Assert;
910

1011
public class TestService {
1112
public static Boolean testHasScreenCapture(Test test, Boolean deep) {
@@ -20,8 +21,7 @@ public static Boolean testHasScreenCapture(Test test, Boolean deep) {
2021
}
2122

2223
public static Test createTest(Class<? extends IGherkinFormatterModel> type, String name, String description) {
23-
if (name == null || name.isEmpty())
24-
throw new IllegalArgumentException("Test name cannot be null or empty");
24+
Assert.notEmpty(name, "Test name must not be null or empty");
2525
return Test.builder()
2626
.bddType(type)
2727
.name(name)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.aventstack.extentreports.util;
2+
3+
public class Assert {
4+
public static void notNull(Object o, String message) {
5+
if (o == null) {
6+
throw new IllegalArgumentException(message);
7+
}
8+
}
9+
10+
public static void notEmpty(String s, String message) {
11+
if (s == null || s.isEmpty()) {
12+
throw new IllegalArgumentException(message);
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)