Skip to content

Commit

Permalink
#2641 - toString() - addCollection() firstElement change
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Apr 8, 2022
1 parent 183b601 commit be24660
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ebean-api/src/main/java/io/ebean/bean/ToStringBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,12 @@ public void addCollection(Collection<?> c) {
sb.append("[]");
return;
}
int collectionPos = 0;
boolean firstElement = true;
sb.append("[");
for (Object o : c) {
if (collectionPos++ > 0) {
if (firstElement) {
firstElement = false;
} else {
sb.append(", ");
}
value(o);
Expand Down
28 changes: 28 additions & 0 deletions ebean-api/src/test/java/io/ebean/bean/ToStringBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ void fields() {
assertThat(builder.toString()).isEqualTo("Object@0(a:1, b:B)");
}

@Test
void add_referenceBeanCollection_expect_nothingAdded() {
ToStringBuilder builder = new ToStringBuilder();
builder.add("ref", new BeanList<>(null));
assertThat(builder.toString()).isEqualTo("");
}

@Test
void addCollection_some() {
ToStringBuilder builder = new ToStringBuilder();
builder.addCollection( new BeanList<>(List.of("A","B")));
assertThat(builder.toString()).isEqualTo("[A, B]");
}

@Test
void addCollection_null() {
ToStringBuilder builder = new ToStringBuilder();
builder.addCollection(null);
assertThat(builder.toString()).isEqualTo("[]");
}

@Test
void addCollection_empty() {
ToStringBuilder builder = new ToStringBuilder();
builder.addCollection(Collections.emptyList());
assertThat(builder.toString()).isEqualTo("[]");
}

@Test
void flatBean() {
Recurse instance0 = new Recurse(42, "java");
Expand Down

0 comments on commit be24660

Please sign in to comment.