Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log message id format: use static final + add test #554

Merged
merged 1 commit into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}