Skip to content

Commit

Permalink
log message id format: use static final + add test
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer authored and schrader committed Oct 12, 2020
1 parent 1be5c22 commit 2b6a2a5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class LogMessageIdFormat {

private final Object PLACEHOLDER = new Object();
private static final Object PLACEHOLDER = new Object();


private final List<Object> tokens;
Expand Down Expand Up @@ -34,12 +34,13 @@ private List<Object> prepareFormatter(String messageFormat) {

String formatMessage(Object[] args) {
StringBuilder sb = new StringBuilder();
int argsLength = args == null ? 0 : args.length;
int size = tokens.size();
int argsUse = 0;
for (int i = 0; i < size; i++) {
Object f = tokens.get(i);
if (f == PLACEHOLDER) {
Object argument = args[argsUse];
Object argument = argsUse < argsLength ? args[argsUse] : "";
sb.append(argument);
argsUse++;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.openhtmltopdf.util;

import org.junit.Assert;
import org.junit.Test;

public class LogMessageIdFormatTest {

@Test
public void testFormats() {
Assert.assertEquals("", new LogMessageIdFormat("").formatMessage(new Integer[]{1,2,3}));
Assert.assertEquals("abcd", new LogMessageIdFormat("abcd").formatMessage(null));
Assert.assertEquals("1a2b3", new LogMessageIdFormat("1{}2{}3").formatMessage(new String[]{"a", "b"}));
Assert.assertEquals("123", new LogMessageIdFormat("{}{}{}").formatMessage(new Integer[]{1,2,3}));
Assert.assertEquals("A1a2b3B", new LogMessageIdFormat("{}1{}2{}3{}").formatMessage(new String[]{"A", "a", "b", "B"}));

Assert.assertEquals("A123", new LogMessageIdFormat("{}1{}2{}3{}").formatMessage(new String[]{"A"}));

Assert.assertEquals("", new LogMessageIdFormat("{}{}{}").formatMessage(new Integer[]{}));
Assert.assertEquals("", new LogMessageIdFormat("{}{}{}").formatMessage(null));
}
}

0 comments on commit 2b6a2a5

Please sign in to comment.