Skip to content

Commit

Permalink
Added more negative testing for Converter.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdereg committed Mar 23, 2024
1 parent 1eab09e commit 2f07faf
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 184 deletions.
2 changes: 0 additions & 2 deletions src/main/java/com/cedarsoftware/util/ArrayUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ public final class ArrayUtilities
* Immutable common arrays.
*/
public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];

public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
public static final char[] EMPTY_CHAR_ARRAY = new char[0];
public static final Character[] EMPTY_CHARACTER_ARRAY = new Character[0];

public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/cedarsoftware/util/DateUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ public static Date parseDate(String dateStr) {
* passed in, null will be returned.
*/
public static ZonedDateTime parseDate(String dateStr, ZoneId defaultZoneId, boolean ensureDateTimeAlone) {
if (StringUtilities.isEmpty(dateStr)) {
dateStr = StringUtilities.trimToNull(dateStr);
if (dateStr == null) {
return null;
}
Convention.throwIfNull(defaultZoneId, "ZoneId cannot be null. Use ZoneId.of(\"America/New_York\"), ZoneId.systemDefault(), etc.");
dateStr = dateStr.trim();

if (allDigits.matcher(dateStr).matches()) {
return Instant.ofEpochMilli(Long.parseLong(dateStr)).atZone(defaultZoneId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ static byte[] toByteArray(Object from, Converter converter) {

static char[] toCharArray(Object from, Converter converter) {
char[] chars = (char[])from;
if (chars == null) {
return null;
}
return Arrays.copyOf(chars, chars.length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class CharBufferConversions {
private CharBufferConversions() {}

static CharBuffer toCharBuffer(Object from, Converter converter) {
// Create a readonly buffer so we aren't changing
// Create a readonly buffer, so we aren't changing
// the original buffers mark and position when
// working with this buffer. This could be inefficient
// if constantly fed with writeable buffers so should be documented
Expand All @@ -49,7 +49,7 @@ static String toString(Object from, Converter converter) {
static char[] toCharArray(Object from, Converter converter) {
CharBuffer buffer = toCharBuffer(from, converter);

if (buffer == null || !buffer.hasRemaining()) {
if (!buffer.hasRemaining()) {
return EMPTY_CHAR_ARRAY;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import com.cedarsoftware.util.StringUtilities;

/**
* @author Kenny Partlow ([email protected])
* <br>
Expand Down Expand Up @@ -118,10 +116,6 @@ static AtomicInteger toAtomicInteger(Object from, Converter converter) {
return new AtomicInteger(toInt(from, converter));
}

static BigDecimal toBigDecimal(Object from, Converter converter) {
return new BigDecimal(StringUtilities.trimToEmpty(from.toString()));
}

static AtomicBoolean toAtomicBoolean(Object from, Converter converter) {
return new AtomicBoolean(toLong(from, converter) != 0);
}
Expand Down Expand Up @@ -152,10 +146,6 @@ static boolean isBigDecimalNotZero(Object from, Converter converter) {
return ((BigDecimal)from).compareTo(BigDecimal.ZERO) != 0;
}

static BigInteger toBigInteger(Object from, Converter converter) {
return new BigInteger(StringUtilities.trimToEmpty(from.toString()));
}

/**
* @param from - object that is a number to be converted to char
* @param converter - instance of converter mappings to use.
Expand Down Expand Up @@ -212,9 +202,6 @@ static OffsetDateTime toOffsetDateTime(Object from, Converter converter) {
}

static Year toYear(Object from, Converter converter) {
if (from instanceof Byte) {
throw new IllegalArgumentException("Cannot convert Byte to Year, not enough precision.");
}
Number number = (Number) from;
return Year.of(number.shortValue());
}
Expand Down
Loading

0 comments on commit 2f07faf

Please sign in to comment.