Skip to content

Commit d1096af

Browse files
author
Krzysztof Suszyński
committed
Adding JDK assert test and updates to README.md
1 parent 3306668 commit d1096af

File tree

2 files changed

+87
-24
lines changed

2 files changed

+87
-24
lines changed

README.md

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,33 @@ This small library holds a set of Exceptions that implements idea of fast, reusa
66

77
## Idea
88

9-
The idea is to have a set of simple runtime exceptions. They should always take the field Exception ID (Eid) in the making. This field will then be reported when displaying or logging that exception. It can also be viewed on the professional fatal error window of the application as a bug reference. This approach simplifies the management of exceptions in the application and allows developers to focus on functionalities rather than coming up with the correct statement for the exception.
9+
The idea is to use a set of simple runtime exceptions. They should always take the field Exception ID (Eid) in the making. This field will then be reported when displaying or logging that exception. It can also be viewed on the professional fatal error window of the application as a bug reference. EidRuntimeExceptions contains also additional unique ID to identify each single exception. This approach simplifies the management of exceptions in the application and allows developers to focus on functionalities rather than coming up with the correct statement for the exception.
1010

1111
This approach is best to use with tools and plugins like:
1212

1313
* [EidGenerator for Netbeans IDE](http://plugins.netbeans.org/plugin/53137/exception-id-eid-generator)
1414
* [Generating Exception Id number in Intellij IDEA with Live Templates](https://github.com/wavesoftware/java-eid-exceptions/wiki/Generating%20Exception%20Id%20number%20in%20Intellij%20IDEA%20with%20Live%20Templates)
1515

16+
Example:
17+
18+
```java
19+
throw new EidIllegalStateException("20150721:100554", cause);
20+
```
21+
22+
Example log:
23+
24+
```
25+
pl.wavesoftware.eid.exceptions.EidIllegalStateException: [20150721:100554]<g0qrwx> => Zipfile in invalid format
26+
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
27+
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
28+
29+
Caused by: java.util.zip.DataFormatException: Zipfile in invalid format
30+
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
31+
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
32+
... 62 more
33+
```
34+
35+
1636
## Caution
1737

1838
This classes shouldn't be used in any public API or library. It is designed to be used for in-house development of end user applications which will report bugs in standardized error pages or post them to issue tracker.
@@ -35,39 +55,54 @@ Static convenience methods that help a method or constructor check whether it wa
3555

3656
Each method accepts a EID string or Eid object, which is designed to ease of use and provide strict ID for given exception usage. This approach speed up development of large application and helps support teams by giving both static and random ID for each possible unpredicted bug.
3757

38-
Example:
58+
Each example uses static import:
3959

4060
```java
41-
/**
42-
* Returns the positive square root of the given value.
43-
*
44-
* @param value value to be square rooted
45-
* @return the square root of input value
46-
* @throws EidIllegalArgumentException if the value is negative
47-
*/
48-
public static double sqrt(double value) {
49-
EidPreconditions.checkArgument(value >= 0.0, "20150718:012333");
50-
// calculate the square root
51-
}
61+
import static pl.wavesoftware.eid.utils.EidPreconditions.*;
62+
```
63+
64+
#### `checkArgument` method
65+
66+
`checkArgument` method should be used to check argument of the method, and validate it in technical terms (not business terms).
67+
68+
Example:
5269

53-
void exampleBadCaller() {
54-
// will throw EidIllegalArgumentException with "20150718:012333" as ID
55-
double d = sqrt(-1.0);
70+
```java
71+
// [..]
72+
public static double sqrt(double value);
73+
checkArgument(value >= 0.0, "20150718:012333");
74+
// if ok, calculate the square root
5675
}
5776
```
5877

59-
In this example, `checkArgument` throws an `EidIllegalArgumentException` to indicate that `exampleBadCaller` made an error in its call to sqrt. Exception, when it will be printed will contain user given EID and also randomly generated ID. Those fields can be displayed to end user on error page on posted directly to issue tracker.
78+
In this example, `checkArgument` throws an `EidIllegalArgumentException` to indicate that developer made an error in its call to `sqrt`.
79+
80+
#### `checkState` method
81+
82+
`checkState` method should be used to check state of the class in given moment, and validate it in technical terms (not business terms).
6083

6184
Example:
6285

6386
```java
64-
// Main application class for ex.: http servlet
65-
try {
66-
performRequest(request, response);
67-
} catch (EidRuntimeException ex) {
68-
issuesTracker.putIssue(ex);
69-
throw ex;
70-
}
87+
checkState(a >= 3.14 && b < 0., "20150721:115016");
88+
```
89+
90+
#### `checkNotNull` method
91+
92+
`checkNotNull` method should be used to check if given non null argument is actually `null`
93+
94+
Example:
95+
96+
```java
97+
String nonNullUserName = checkNotNull(userName, "20150721:115515");
98+
```
99+
100+
#### `checkElementIndex` method
101+
102+
`checkElementIndex` method can be used to test parameters of an array, before being used
103+
104+
```java
105+
checkElementIndex(index, list.size(), "20150721:115749");
71106
```
72107

73108
#### Functional try to execute blocks
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package pl.wavesoftware.eid.exceptions;
2+
3+
import org.junit.Rule;
4+
import org.junit.Test;
5+
import org.junit.rules.ExpectedException;
6+
7+
/**
8+
* @author Krzysztof Suszyński <[email protected]>
9+
* @since 21.07.15
10+
*/
11+
public class AssertionsTest {
12+
13+
@Rule
14+
public final ExpectedException thrown = ExpectedException.none();
15+
16+
@Test
17+
public void testJdkAssertions() {
18+
// then
19+
thrown.expect(AssertionError.class);
20+
21+
// when
22+
assert getTestNumber() < 3 : new Eid("20150721:101958");
23+
}
24+
25+
private int getTestNumber() {
26+
return 3 * 5;
27+
}
28+
}

0 commit comments

Comments
 (0)