Skip to content

Commit

Permalink
Fixed the BusinessIdentity.toString() to handle Arrays properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
oshoukry committed Oct 23, 2015
1 parent 08eebf4 commit f4c7428
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.openpojo.reflection.utils;

import java.util.Arrays;
import java.util.List;

import com.openpojo.reflection.PojoClass;
Expand All @@ -42,7 +43,10 @@ public final class ToStringHelper {
* String formatted, human readable name/value pair.
*/
public static String nameValuePair(final Object name, final Object value) {
return String.format(NAME_VALUE_TOKEN_FORMAT, name, value);
String valueString = "" + value;
if (value != null && value.getClass().isArray())
valueString = Arrays.deepToString((Object[]) value);
return String.format(NAME_VALUE_TOKEN_FORMAT, name, valueString);
}

/**
Expand Down
30 changes: 26 additions & 4 deletions src/test/java/com/openpojo/business/BusinessIdentityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.openpojo.business.annotation.BusinessKey;
import com.openpojo.business.exception.BusinessException;
import com.openpojo.business.sampleclasses.JavaClassWithArray;
import com.openpojo.utils.dummypackage.Person;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -95,6 +96,7 @@ public void testIncompleteObject() {
}

try {
//noinspection RedundantStringConstructorCall
BusinessIdentity.areEqual(new String("First"), new String("First"));
Assert.fail("Expected Exception due to no BusinessKeys defined");
} catch (final BusinessException be) {
Expand Down Expand Up @@ -134,6 +136,29 @@ public void testToString() {
Assert.assertTrue(String.format("BusinessIdentity.toString() failed!! recieved[%s]", toString), toString.startsWith("com.openpojo.business.BusinessIdentityTest$ToStringTestData [@") && toString.endsWith(": instance_name=Instance Name, static_name=Static Name, STATIC_FINAL_NAME=Static Final Name]"));
}

@Test
public void shouldToStringOnArrays() {
String [][] data = {{"One"}, {"One", "Two"}, {"One", "Two", "Three"} };
JavaClassWithArray javaClassWithArray = new JavaClassWithArray(data);
String toStringOutput = javaClassWithArray.toString();
String expected = JavaClassWithArray.class.getName()+ " [@";
expected += Integer.toHexString(System.identityHashCode(javaClassWithArray));
expected += ": data=[[One], [One, Two], [One, Two, Three]]";
expected += "]";
Assert.assertEquals(expected, toStringOutput);
}

@Test
public void shouldToStringOnNullArray() {
JavaClassWithArray javaClassWithArray = new JavaClassWithArray(null);
String toStringOutput = javaClassWithArray.toString();
String expected = JavaClassWithArray.class.getName()+ " [@";
expected += Integer.toHexString(System.identityHashCode(javaClassWithArray));
expected += ": data=null";
expected += "]";
Assert.assertEquals(expected, toStringOutput);
}

@Test
public void whenNullObject_Then_toString_returnNull() {
Assert.assertEquals("null", BusinessIdentity.toString(null));
Expand Down Expand Up @@ -168,10 +193,7 @@ private static class HashCodeTestData {
private final String nonBusinessKey;

private int expectedHashCode = 1;
/**
* @param id
* @param name
*/

public HashCodeTestData(final String id, final String name, final String optionalPart1, final String optionalPart2, final String nonBusinessKey) {
this.id = id;
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2010-2015 Osman Shoukry
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.openpojo.business.sampleclasses;

import com.openpojo.business.BusinessIdentity;

/**
* @author oshoukry
*/
public class JavaClassWithArray {

@SuppressWarnings({ "unused", "FieldCanBeLocal" })
private String[][] data;

public JavaClassWithArray(String[][] data) {
this.data = data;
}

@Override
public String toString() {
return BusinessIdentity.toString(this);
}
}

0 comments on commit f4c7428

Please sign in to comment.