|
1 | 1 | /**
|
2 |
| - * Copyright 2009-2015 the original author or authors. |
| 2 | + * Copyright 2009-2017 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 | package org.apache.ibatis.cache;
|
17 | 17 |
|
18 | 18 | import static org.junit.Assert.*;
|
| 19 | + |
| 20 | +import org.junit.Assert; |
19 | 21 | import org.junit.Test;
|
20 | 22 |
|
| 23 | +import java.io.FileInputStream; |
| 24 | +import java.io.FileOutputStream; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.NotSerializableException; |
| 27 | +import java.io.ObjectInputStream; |
| 28 | +import java.io.ObjectOutputStream; |
21 | 29 | import java.util.Date;
|
22 | 30 |
|
23 | 31 | public class CacheKeyTest {
|
@@ -80,4 +88,31 @@ public void shouldTestCacheKeysWithBinaryArrays() throws Exception {
|
80 | 88 | assertTrue(key1.equals(key2));
|
81 | 89 | }
|
82 | 90 |
|
| 91 | + @Test (expected = NotSerializableException.class) |
| 92 | + public void serializationExceptionTest() throws ClassNotFoundException, IOException { |
| 93 | + CacheKey cacheKey = new CacheKey(); |
| 94 | + cacheKey.update(new Object()); |
| 95 | + canSerialize(cacheKey); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void serializationTest() throws ClassNotFoundException, IOException { |
| 100 | + CacheKey cacheKey = new CacheKey(); |
| 101 | + cacheKey.update("serializable"); |
| 102 | + canSerialize(cacheKey); |
| 103 | + } |
| 104 | + |
| 105 | + private void canSerialize(final CacheKey object) throws ClassNotFoundException, IOException { |
| 106 | + FileOutputStream fout = new FileOutputStream("target/address.ser"); |
| 107 | + ObjectOutputStream output = new ObjectOutputStream(fout); |
| 108 | + output.writeObject(object); |
| 109 | + output.close(); |
| 110 | + |
| 111 | + FileInputStream fin = new FileInputStream("target/address.ser"); |
| 112 | + ObjectInputStream input = new ObjectInputStream(fin); |
| 113 | + CacheKey cacheKey = (CacheKey) input.readObject(); |
| 114 | + input.close(); |
| 115 | + |
| 116 | + Assert.assertEquals(1, cacheKey.getUpdateCount()); |
| 117 | + } |
83 | 118 | }
|
0 commit comments