View Javadoc

1   package com.atlassian.vcache.internal.core.cas;
2   
3   import org.junit.Test;
4   
5   import java.io.ByteArrayInputStream;
6   import java.io.ByteArrayOutputStream;
7   import java.io.IOException;
8   import java.io.ObjectInputStream;
9   import java.io.ObjectOutputStream;
10  
11  import static org.hamcrest.Matchers.is;
12  import static org.hamcrest.Matchers.notNullValue;
13  import static org.junit.Assert.assertThat;
14  
15  public class IdentifiedDataBytesTest {
16      @Test
17      public void the_basics() throws Exception {
18          final IdentifiedDataBytes isFirst = new IdentifiedDataBytes(new byte[]{1, 2});
19  
20          assertThat(isFirst, notNullValue());
21          assertThat(isFirst.getBytes(), is(new byte[]{1, 2}));
22          assertThat(isFirst.equals(isFirst), is(true));
23  
24          final IdentifiedDataBytes ibSecond = new IdentifiedDataBytes(new byte[]{1, 2});
25  
26          assertThat(ibSecond, notNullValue());
27          assertThat(ibSecond.getBytes(), is(new byte[]{1, 2}));
28          assertThat(isFirst.getBytes(), is(ibSecond.getBytes()));
29          assertThat(ibSecond.equals(ibSecond), is(true));
30          assertThat(isFirst.equals(ibSecond), is(false));
31          assertThat(ibSecond.equals(isFirst), is(false));
32      }
33  
34      @Test
35      public void serialize_works() throws IOException, ClassNotFoundException {
36          final IdentifiedDataBytes before = new IdentifiedDataBytes(new byte[]{1, 7});
37  
38          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
39          final ObjectOutputStream oos = new ObjectOutputStream(baos);
40  
41          oos.writeObject(before);
42  
43          oos.flush();
44          oos.close();
45  
46          final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
47          final ObjectInputStream ois = new ObjectInputStream(bais);
48  
49          final IdentifiedDataBytes after = (IdentifiedDataBytes) ois.readObject();
50  
51          ois.close();
52  
53          assertThat(before.equals(after), is(true));
54          assertThat(after.equals(before), is(true));
55      }
56  }