1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ehcache.distribution;
18
19 import junit.framework.TestCase;
20
21 import java.util.Map;
22 import java.util.HashMap;
23 import java.lang.ref.SoftReference;
24 import java.io.IOException;
25 import java.io.ByteArrayOutputStream;
26 import java.io.ObjectOutputStream;
27 import java.io.ByteArrayInputStream;
28 import java.io.ObjectInputStream;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import net.sf.ehcache.Element;
33 import net.sf.ehcache.AbstractCacheTest;
34
35
36
37
38
39
40
41 public class EventMessageTest extends TestCase {
42
43 private static final Log LOG = LogFactory.getLog(EventMessageTest.class.getName());
44
45
46
47
48
49 public void testSoftReferences() {
50 AbstractCacheTest.forceVMGrowth();
51 Map map = new HashMap();
52 for (int i = 0; i < 100; i++) {
53 map.put(new Integer(i), new SoftReference(new byte[1000000]));
54 }
55
56 int counter = 0;
57 for (int i = 0; i < 100; i++) {
58 SoftReference softReference = (SoftReference) map.get(new Integer(i));
59 byte[] payload = (byte[]) softReference.get();
60 if (payload != null) {
61 LOG.info("Value found for " + i);
62 counter++;
63 }
64 }
65
66 assertTrue("You should get more than " + counter + " out of SoftReferences", counter >= 13);
67
68 }
69
70
71
72
73 public void testSerialization() throws IOException, ClassNotFoundException {
74
75 EventMessage eventMessage = new EventMessage(EventMessage.PUT, "key", new Element("key", "element"));
76
77 ByteArrayOutputStream bout = new ByteArrayOutputStream();
78 ObjectOutputStream oos = new ObjectOutputStream(bout);
79 oos.writeObject(eventMessage);
80 byte[] serializedValue = bout.toByteArray();
81 oos.close();
82 EventMessage eventMessage2 = null;
83 ByteArrayInputStream bin = new ByteArrayInputStream(serializedValue);
84 ObjectInputStream ois = new ObjectInputStream(bin);
85 eventMessage2 = (EventMessage) ois.readObject();
86 ois.close();
87
88
89 assertEquals("key", eventMessage2.getSerializableKey());
90 assertEquals("element", eventMessage2.getElement().getObjectValue());
91 assertEquals(EventMessage.PUT, eventMessage2.getEvent());
92 assertTrue(eventMessage2.isValid());
93
94 }
95
96
97 }