|
| 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