Skip to content

Commit cfdd951

Browse files
committed
Merge pull request #48 from marschall/missing-serializable
BuildableCurrencyUnit made Serializable.
2 parents 78511f7 + dbd79f2 commit cfdd951

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

src/main/java/org/javamoney/moneta/BuildableCurrencyUnit.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
package org.javamoney.moneta;
1717

1818
import javax.money.*;
19-
import javax.money.CurrencyContext;
2019

20+
import java.io.Serializable;
2121
import java.util.Objects;
2222

2323
/**
@@ -26,9 +26,13 @@
2626
* singleton, which publishes the instances, so they are visible from the {@link javax.money.MonetaryCurrencies}
2727
* singleton.
2828
*/
29-
final class BuildableCurrencyUnit implements CurrencyUnit, Comparable<CurrencyUnit>{
29+
final class BuildableCurrencyUnit implements CurrencyUnit, Comparable<CurrencyUnit>, Serializable {
3030

3131
/**
32+
* serialVersionUID.
33+
*/
34+
private static final long serialVersionUID = -2389580389919492220L;
35+
/**
3236
* The unique currency code.
3337
*/
3438
private String currencyCode;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.javamoney.moneta;
17+
18+
import static org.testng.Assert.assertEquals;
19+
20+
import java.io.ByteArrayInputStream;
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.io.ObjectInputStream;
25+
import java.io.ObjectOutputStream;
26+
27+
import javax.money.CurrencyUnit;
28+
29+
import org.testng.annotations.Test;
30+
31+
/**
32+
*
33+
* @author Philippe Marschall
34+
*/
35+
public class BuildableCurrencyUnitTest {
36+
37+
/**
38+
* Tests that currencies built by {@link CurrencyUnitBuilder} are serializable.
39+
*/
40+
@Test
41+
public void testSerialization() throws ClassNotFoundException, IOException {
42+
CurrencyUnit currencyUnit = CurrencyUnitBuilder.of("SDR", "serialization-test")
43+
.setDefaultFractionDigits(3).build(false);
44+
45+
CurrencyUnit copy = (CurrencyUnit) deserailize(serailize(currencyUnit));
46+
assertEquals(currencyUnit, copy);
47+
}
48+
49+
private byte[] serailize(Object obj) throws IOException {
50+
ByteArrayOutputStream out = new ByteArrayOutputStream();
51+
try (ObjectOutputStream objectStream = new ObjectOutputStream(out)) {
52+
objectStream.writeObject(obj);
53+
}
54+
return out.toByteArray();
55+
}
56+
57+
private Object deserailize(byte[] buf) throws IOException, ClassNotFoundException {
58+
try (InputStream in = new ByteArrayInputStream(buf);
59+
ObjectInputStream objectStream = new ObjectInputStream(in)) {
60+
return objectStream.readObject();
61+
}
62+
}
63+
64+
}

0 commit comments

Comments
 (0)