Skip to content

Commit 0ac46f1

Browse files
committed
#128 Refactoring of Expectation.shouldBeEqual() and TestException
1 parent 75afb49 commit 0ac46f1

File tree

2 files changed

+64
-45
lines changed

2 files changed

+64
-45
lines changed

javalite-common/src/main/java/org/javalite/test/jspec/Expectation.java

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
/*
2-
Copyright 2009-2014 Igor Polevoy
2+
Copyright 2009-2015 Igor Polevoy
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
1515
*/
16-
17-
1816
package org.javalite.test.jspec;
1917
import java.lang.reflect.Method;
18+
import java.util.Collection;
2019
import java.util.List;
2120
import java.util.Map;
2221

2322
import static org.javalite.common.Inflector.capitalize;
2423

24+
/**
25+
* @author Igor Polevoy
26+
* @author Eric Nielsen
27+
*/
2528
public class Expectation<T> {
2629

2730
private final T actual;
@@ -40,30 +43,42 @@ public void shouldEqual(T expected){
4043
}
4144

4245
/**
43-
* Tested value is equal expected.
46+
* Tested value is equal expected.
4447
*
4548
* @param expected expected value.
4649
*/
4750
public void shouldBeEqual(T expected) {
4851
checkNull();
49-
String expectedName = expected == null? "null":expected.getClass().getName();
50-
String actualName = actual == null? "null":actual.getClass().getName();
51-
52-
TestException te = new TestException("\nTest object: \n" +
53-
actualName + " == <" + actual + "> \n" +
54-
"and expected\n" +
55-
expectedName + " == <" + expected + "> \nare not equal, but they should be.");
56-
57-
58-
if(actual == null && expected != null || actual != null && expected == null)
59-
throw te;
52+
if (actual == null) {
53+
if (expected != null) { throw newShouldBeEqualException(expected); }
54+
} else {
55+
if (expected == null) { throw newShouldBeEqualException(expected); }
56+
//TODO: improve Number comparison, see http://stackoverflow.com/questions/2683202/comparing-the-values-of-two-generic-numbers
57+
if (actual instanceof Number && expected instanceof Number) {
58+
if (((Number) actual).doubleValue() != ((Number) expected).doubleValue()) {
59+
throw newShouldBeEqualException(expected);
60+
}
61+
} else if (!actual.equals(expected)) {
62+
throw newShouldBeEqualException(expected);
63+
}
64+
}
65+
}
6066

61-
if (actual instanceof Number && expected instanceof Number) {
62-
Double t1 = ((Number) actual).doubleValue();
63-
Double t2 = ((Number) expected).doubleValue();
64-
if (!t1.equals(t2))
65-
throw te;
66-
} else if (!actual.equals(expected)) throw te;
67+
private TestException newShouldBeEqualException(T expected) {
68+
StringBuilder sb = new StringBuilder().append("Test object:\n");
69+
if (actual == null) {
70+
sb.append("null");
71+
} else {
72+
sb.append(actual.getClass().getName()).append(" == <").append(actual).append(">\n");
73+
}
74+
sb.append("and expected\n");
75+
if (expected == null) {
76+
sb.append("null");
77+
} else {
78+
sb.append(expected.getClass().getName()).append(" == <").append(expected).append(">\n");
79+
}
80+
sb.append("are not equal, but they should be.");
81+
return new TestException(sb.toString());
6782
}
6883

6984
/**
Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/*
2-
Copyright 2009-2014 Igor Polevoy
2+
Copyright 2009-2015 Igor Polevoy
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
1515
*/
1616

1717

@@ -21,14 +21,18 @@
2121

2222
public class TestException extends RuntimeException {
2323

24-
private final String message;
24+
public TestException() {
25+
}
2526

2627
public TestException(String message) {
27-
this.message = message;
28+
super(message);
29+
}
30+
31+
public TestException(String message, Throwable cause) {
32+
super(message, cause);
2833
}
2934

30-
@Override
31-
public String getMessage() {
32-
return message;
35+
public TestException(Throwable cause) {
36+
super(cause);
3337
}
3438
}

0 commit comments

Comments
 (0)