[Major] change DBC to add bad values for better debugging

This commit is contained in:
Robert von Burg 2015-07-07 17:53:08 +02:00
parent 9e449e56ee
commit 2e58db83fd
2 changed files with 41 additions and 27 deletions

View File

@ -17,6 +17,7 @@ package ch.eitchnet.utils.dbc;
import java.io.File;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Collection;
import ch.eitchnet.utils.helper.StringHelper;
@ -38,8 +39,8 @@ public enum DBC {
if (value2 != null && value2.equals(value1))
return;
String ex = "Values are not equal: {0}"; //$NON-NLS-1$
ex = MessageFormat.format(ex, msg);
String ex = "{0}: {1} != {2}"; //$NON-NLS-1$
ex = MessageFormat.format(ex, msg, value1, value2);
throw new DbcException(ex);
}
@ -50,8 +51,8 @@ public enum DBC {
if (value2 != null && !value2.equals(value1))
return;
String ex = "Values are equal: {0}"; //$NON-NLS-1$
ex = MessageFormat.format(ex, msg);
String ex = "{0}: {1} == {2}"; //$NON-NLS-1$
ex = MessageFormat.format(ex, msg, value1, value2);
throw new DbcException(ex);
}
@ -73,8 +74,8 @@ public enum DBC {
public void assertEmpty(String msg, String value) {
if (!StringHelper.isEmpty(value)) {
String ex = "Illegal non-empty value: {0}"; //$NON-NLS-1$
ex = MessageFormat.format(ex, msg);
String ex = "{0}: Illegal non-empty value: {1}"; //$NON-NLS-1$
ex = MessageFormat.format(ex, msg, value);
throw new DbcException(ex);
}
}
@ -82,8 +83,8 @@ public enum DBC {
public void assertEmpty(String msg, Object[] array) {
assertNotNull(msg, array);
if (array.length != 0) {
String ex = "Illegal non-empty value: {0}"; //$NON-NLS-1$
ex = MessageFormat.format(ex, msg);
String ex = "{0}: Illegal non-empty value: {1}"; //$NON-NLS-1$
ex = MessageFormat.format(ex, msg, Arrays.toString(array));
throw new DbcException(ex);
}
}
@ -91,15 +92,15 @@ public enum DBC {
public void assertEmpty(String msg, Collection<?> collection) {
assertNotNull(msg, collection);
if (!collection.isEmpty()) {
String ex = "Illegal non-empty value: {0}"; //$NON-NLS-1$
ex = MessageFormat.format(ex, msg);
String ex = "{0}: Illegal non-empty value: {1}"; //$NON-NLS-1$
ex = MessageFormat.format(ex, msg, collection.toString());
throw new DbcException(ex);
}
}
public void assertNotEmpty(String msg, String value) {
if (StringHelper.isEmpty(value)) {
String ex = "Illegal empty value: {0}"; //$NON-NLS-1$
String ex = "{0}: Illegal empty value"; //$NON-NLS-1$
ex = MessageFormat.format(ex, msg);
throw new DbcException(ex);
}
@ -108,7 +109,7 @@ public enum DBC {
public void assertNotEmpty(String msg, Object[] array) {
assertNotNull(msg, array);
if (array.length == 0) {
String ex = "Illegal empty value: {0}"; //$NON-NLS-1$
String ex = "{0}: Illegal empty value"; //$NON-NLS-1$
ex = MessageFormat.format(ex, msg);
throw new DbcException(ex);
}
@ -117,7 +118,7 @@ public enum DBC {
public void assertNotEmpty(String msg, Collection<?> collection) {
assertNotNull(msg, collection);
if (collection.isEmpty()) {
String ex = "Illegal empty value: {0}"; //$NON-NLS-1$
String ex = "{0}: Illegal empty value"; //$NON-NLS-1$
ex = MessageFormat.format(ex, msg);
throw new DbcException(ex);
}
@ -125,7 +126,7 @@ public enum DBC {
public void assertNotNull(String msg, Object value) {
if (value == null) {
String ex = "Illegal null value: {0}"; //$NON-NLS-1$
String ex = "{0}: Illegal null value"; //$NON-NLS-1$
ex = MessageFormat.format(ex, msg);
throw new DbcException(ex);
}
@ -133,8 +134,8 @@ public enum DBC {
public void assertNull(String msg, Object value) {
if (value != null) {
String ex = "Illegal situation as value is not null: {0}"; //$NON-NLS-1$
ex = MessageFormat.format(ex, msg);
String ex = "{0}: {1} != null"; //$NON-NLS-1$
ex = MessageFormat.format(ex, msg, value);
throw new DbcException(ex);
}
}

View File

@ -16,6 +16,7 @@
package ch.eitchnet.utils.dbc;
import java.io.File;
import java.text.MessageFormat;
import org.junit.Rule;
import org.junit.Test;
@ -64,10 +65,10 @@ public class DBCTest {
@Test
public void testAssertEquals_2() throws Exception {
this.exception.expect(DbcException.class);
this.exception.expectMessage("Values are not equal:");
String msg = "";
Object value1 = new Object();
Object value2 = new Object();
String msg = MessageFormat.format("{0}: {1} != {2}", "", value1, value2);
this.exception.expectMessage(msg);
DBC.PRE.assertEquals(msg, value1, value2);
@ -87,12 +88,13 @@ public class DBCTest {
@Test
public void testAssertEquals_3() throws Exception {
this.exception.expect(DbcException.class);
this.exception.expectMessage("Values are not equal:");
String msg = "";
Object value1 = null;
Object value2 = new Object();
String msg = MessageFormat.format("{0}: {1} != {2}", "", value1, value2);
this.exception.expectMessage(msg);
DBC.PRE.assertEquals(msg, value1, value2);
// add additional test code here
@ -108,12 +110,13 @@ public class DBCTest {
@Test
public void testAssertEquals_4() throws Exception {
this.exception.expect(DbcException.class);
this.exception.expectMessage("Values are not equal:");
String msg = "";
Object value1 = new Object();
Object value2 = null;
String msg = MessageFormat.format("{0}: {1} != {2}", "", value1, value2);
this.exception.expectMessage(msg);
DBC.PRE.assertEquals(msg, value1, value2);
// add additional test code here
@ -150,12 +153,15 @@ public class DBCTest {
@Test
public void testAssertNotEquals_1() throws Exception {
this.exception.expect(DbcException.class);
this.exception.expectMessage("Values are equal:");
String msg = "";
Object value1 = null;
Object value2 = null;
String ex = "{0}: {1} == {2}";
ex = MessageFormat.format(ex, msg, value1, value2);
this.exception.expectMessage(ex);
DBC.PRE.assertNotEquals(msg, value1, value2);
}
@ -217,12 +223,15 @@ public class DBCTest {
@Test
public void testAssertNotEquals_5() throws Exception {
this.exception.expect(DbcException.class);
this.exception.expectMessage("Values are equal:");
String msg = "";
Object value1 = "bla";
Object value2 = "bla";
String ex = "{0}: {1} == {2}";
ex = MessageFormat.format(ex, msg, value1, value2);
this.exception.expectMessage(ex);
DBC.PRE.assertNotEquals(msg, value1, value2);
}
@ -379,11 +388,14 @@ public class DBCTest {
@Test
public void testAssertNotNull_1() throws Exception {
this.exception.expect(DbcException.class);
this.exception.expectMessage("Illegal null value:");
String msg = "";
Object value = null;
String ex = "{0}: Illegal null value";
ex = MessageFormat.format(ex, msg, value);
this.exception.expectMessage(ex);
DBC.PRE.assertNotNull(msg, value);
}
@ -414,11 +426,12 @@ public class DBCTest {
@Test
public void testAssertNull_1() throws Exception {
this.exception.expect(DbcException.class);
this.exception.expectMessage("Illegal situation as value is not null:");
String msg = "";
Object value = new Object();
String msg = MessageFormat.format("{0}: {1} != null", "", value);
this.exception.expectMessage(msg);
DBC.PRE.assertNull(msg, value);
}