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

Do not encode booleans with XMLEncoder #306

Closed
wants to merge 5 commits into from
Closed
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
25 changes: 23 additions & 2 deletions src/main/java/nl/nn/testtool/MessageEncoderImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021-2023 WeAreFrank!
Copyright 2021-2024 WeAreFrank!

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -26,6 +26,7 @@
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.lang.invoke.MethodHandles;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
Expand All @@ -35,6 +36,8 @@
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;

import lombok.SneakyThrows;
Expand All @@ -48,6 +51,7 @@
* @author Jaco de Groot
*/
public class MessageEncoderImpl implements MessageEncoder {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dit is zonde, je weet immers de classname al...

public static final String XML_ENCODER = "XMLEncoder";
public static final String UTF8_ENCODER = "UTF-8";
public static final String CHARSET_ENCODER_PREFIX = "CHARSET-";
Expand All @@ -69,7 +73,10 @@ public ToStringResult toString(Object message, String charset) {
} else if (message instanceof String) {
toStringResult = new ToStringResult((String)message, null);
} else {
if (message instanceof byte[]) {
if (message instanceof Boolean) {
toStringResult = new ToStringResult(Boolean.toString((Boolean) message), null, Boolean.class.getName());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gewoon direct returnen toch?

}
else if (message instanceof byte[]) {
String encoding;
if (charset == null) {
charset = "UTF-8";
Expand Down Expand Up @@ -147,6 +154,20 @@ public <T> T toObject(Checkpoint originalCheckpoint, T messageToStub) {
} else {
String message = originalCheckpoint.getMessage();
String encoding = originalCheckpoint.getEncoding();
String messageClassName = originalCheckpoint.getMessageClassName();
if ( (messageClassName != null) && (messageClassName.equals(Boolean.class.getName())) ) {
try {
Object rawResult = Boolean.valueOf(message);
return (T) rawResult;
Comment on lines +160 to +161
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Direct returnen.

} catch(ClassCastException e) {
if (messageToStub == null) {
log.error("Could not parse message [{}] to generic type T", message);
} else {
log.error("Could not parse message [{}] to [{}]", message, messageToStub.getClass().getName());
}
return null;
}
}
if (encoding == null) {
if (originalCheckpoint.getStreaming() == null) {
return (T)message;
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/nl/nn/testtool/test/junit/TestMessageEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.ParserConfigurationException;

import nl.nn.testtool.MessageEncoder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.rules.TestName;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
Expand All @@ -46,6 +50,40 @@ public class TestMessageEncoder {
@Rule
public TestName name = new TestName();

@Test
public void encode_and_decode_boolean_true() {
MessageEncoder instance = new MessageEncoderImpl();
MessageEncoder.ToStringResult encoded = instance.toString(true, null);
Assertions.assertEquals("true", encoded.getString());
Assertions.assertEquals("java.lang.Boolean", encoded.getMessageClassName());
Checkpoint checkpoint = new Checkpoint();
checkpoint.setMessage(encoded.getString());
checkpoint.setEncoding(encoded.getEncoding());
checkpoint.setMessageClassName(encoded.getMessageClassName());
Object back = instance.toObject(checkpoint);
if(! (back instanceof Boolean)) {
fail("Expected to get back Boolean");
}
Assertions.assertTrue((Boolean) back);
}

@Test
public void encode_decode_boolean_false() {
MessageEncoder instance = new MessageEncoderImpl();
MessageEncoder.ToStringResult encoded = instance.toString(false, null);
Assertions.assertEquals("false", encoded.getString());
Assertions.assertEquals("java.lang.Boolean", encoded.getMessageClassName());
Checkpoint checkpoint = new Checkpoint();
checkpoint.setMessage(encoded.getString());
checkpoint.setEncoding(encoded.getEncoding());
checkpoint.setMessageClassName(encoded.getMessageClassName());
Object back = instance.toObject(checkpoint);
if(! (back instanceof Boolean)) {
fail("Expected to get back Boolean");
}
assertFalse((Boolean) back);
}

@Test
public void testToString() throws SAXException, IOException, ParserConfigurationException, StorageException {
TestTool testTool = new TestTool();
Expand Down
Loading