BUG-7464: Split TrieMap into read-only and read-write
[yangtools.git] / third-party / triemap / src / test / java / org / opendaylight / yangtools / triemap / TestSerialization.java
1 /*
2  * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.opendaylight.yangtools.triemap;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.ObjectInputStream;
22 import java.io.ObjectOutputStream;
23 import org.junit.Assert;
24 import org.junit.Test;
25
26 public class TestSerialization {
27     @Test
28     public void testSerialization() throws IOException, ClassNotFoundException {
29         TrieMap<String, String> map = TrieMap.create();
30
31         map.put("dude-0", "tom");
32         map.put("dude-1", "john");
33         map.put("dude-3", "ravi");
34         map.put("dude-4", "alex");
35
36         TrieMap<String, String> expected = map.immutableSnapshot();
37
38         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
39         final ObjectOutputStream oos = new ObjectOutputStream(bos);
40         oos.writeObject(expected);
41         oos.close();
42
43         final byte[] bytes = bos.toByteArray();
44         final ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
45         final ObjectInputStream ois = new ObjectInputStream(bis);
46
47         @SuppressWarnings("unchecked")
48         final TrieMap<String, String> actual = (TrieMap<String, String>) ois.readObject();
49         ois.close();
50
51         Assert.assertEquals(expected, actual);
52     }
53 }