Skip to content

Commit 2671fff

Browse files
committed
Add a test to write/read BigDecimal
References #617 (cherry picked from commit 02284ea) Conflicts: src/test/java/com/rabbitmq/client/test/ClientTests.java
1 parent 693503a commit 2671fff

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

src/main/java/com/rabbitmq/client/impl/ValueReader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2007-Present Pivotal Software, Inc. All rights reserved.
1+
// Copyright (c) 2007-2019 Pivotal Software, Inc. All rights reserved.
22
//
33
// This software, the RabbitMQ Java client library, is triple-licensed under the
44
// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2
@@ -153,7 +153,8 @@ private static Map<String, Object> readTable(DataInputStream in)
153153
return table;
154154
}
155155

156-
private static Object readFieldValue(DataInputStream in)
156+
// package protected for testing
157+
static Object readFieldValue(DataInputStream in)
157158
throws IOException {
158159
Object value = null;
159160
switch(in.readUnsignedByte()) {

src/main/java/com/rabbitmq/client/impl/ValueWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2007-Present Pivotal Software, Inc. All rights reserved.
1+
// Copyright (c) 2007-2019 Pivotal Software, Inc. All rights reserved.
22
//
33
// This software, the RabbitMQ Java client library, is triple-licensed under the
44
// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2

src/test/java/com/rabbitmq/client/impl/ValueWriterTest.java

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1+
// Copyright (c) 2019 Pivotal Software, Inc. All rights reserved.
2+
//
3+
// This software, the RabbitMQ Java client library, is triple-licensed under the
4+
// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2
5+
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
6+
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
7+
// please see LICENSE-APACHE2.
8+
//
9+
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
10+
// either express or implied. See the LICENSE file for specific language governing
11+
// rights and limitations of this software.
12+
//
13+
// If you have any questions regarding licensing, please contact us at
14+
15+
116
package com.rabbitmq.client.impl;
217

318
import org.junit.Test;
419

5-
import java.io.DataOutputStream;
6-
import java.io.IOException;
7-
import java.io.OutputStream;
20+
import java.io.*;
821
import java.math.BigDecimal;
922
import java.math.BigInteger;
1023

24+
import static org.assertj.core.api.Assertions.assertThat;
25+
1126
public class ValueWriterTest {
12-
@Test(expected = IllegalArgumentException.class) public void writingOverlyLargeBigDecimalShouldFail()
13-
throws IOException {
27+
28+
@Test(expected = IllegalArgumentException.class)
29+
public void writingOverlyLargeBigDecimalShouldFail()
30+
throws IOException {
1431

1532
OutputStream outputStream = new OutputStream() {
1633
@Override
@@ -26,8 +43,9 @@ public void write(int b) {
2643

2744
}
2845

29-
@Test(expected = IllegalArgumentException.class) public void writingOverlyLargeScaleInBigDecimalShouldFail()
30-
throws IOException {
46+
@Test(expected = IllegalArgumentException.class)
47+
public void writingOverlyLargeScaleInBigDecimalShouldFail()
48+
throws IOException {
3149

3250
OutputStream outputStream = new OutputStream() {
3351
@Override
@@ -41,4 +59,16 @@ public void write(int b) {
4159

4260
valueWriter.writeFieldValue(new BigDecimal(BigInteger.ONE, 500));
4361
}
62+
63+
@Test
64+
public void bigDecimalWrittenAndReadMatches() throws IOException {
65+
BigDecimal value = new BigDecimal(BigInteger.valueOf(56), 3);
66+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
67+
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
68+
ValueWriter valueWriter = new ValueWriter(dataOutputStream);
69+
valueWriter.writeFieldValue(value);
70+
71+
BigDecimal read = (BigDecimal) ValueReader.readFieldValue(new DataInputStream(new ByteArrayInputStream(outputStream.toByteArray())));
72+
assertThat(read).isEqualTo(value);
73+
}
4474
}

src/test/java/com/rabbitmq/client/test/ClientTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.rabbitmq.client.JacksonJsonRpcTest;
2020
import com.rabbitmq.client.DefaultJsonRpcTest;
21+
import com.rabbitmq.client.impl.ValueWriterTest;
2122
import com.rabbitmq.utility.IntAllocatorTests;
2223
import org.junit.runner.RunWith;
2324
import org.junit.runners.Suite;
@@ -71,7 +72,8 @@
7172
RpcTopologyRecordingTest.class,
7273
ConnectionTest.class,
7374
TlsUtilsTest.class,
74-
ChannelNTest.class
75+
ChannelNTest.class,
76+
ValueWriterTest.class
7577
})
7678
public class ClientTests {
7779

0 commit comments

Comments
 (0)