1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
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
17 package net.sf.ehcache.distribution;
18
19
20 import net.sf.ehcache.AbstractCacheTest;
21 import net.sf.ehcache.Element;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import org.junit.Test;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.ObjectInputStream;
32 import java.io.ObjectOutputStream;
33 import java.lang.ref.SoftReference;
34 import java.util.HashMap;
35 import java.util.Map;
36
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /***
41 * Tests Serialization and SoftReferences in EventMessage
42 *
43 * @author Greg Luck
44 * @version $Id: EventMessageTest.html 13146 2011-08-01 17:12:39Z oletizi $
45 */
46 public class EventMessageTest {
47
48 private static final Logger LOG = LoggerFactory.getLogger(EventMessageTest.class.getName());
49
50
51 /***
52 * SoftReference behaviour testing.
53 */
54 @Test
55 public void testSoftReferences() {
56 AbstractCacheTest.forceVMGrowth();
57 Map map = new HashMap();
58 for (int i = 0; i < 100; i++) {
59 map.put(Integer.valueOf(i), new SoftReference(new byte[1000000]));
60 }
61
62 int counter = 0;
63 for (int i = 0; i < 100; i++) {
64 SoftReference softReference = (SoftReference) map.get(Integer.valueOf(i));
65 byte[] payload = (byte[]) softReference.get();
66 if (payload != null) {
67 LOG.info("Value found for " + i);
68 counter++;
69 }
70 }
71
72 assertTrue("You should get more than " + counter + " out of SoftReferences", counter >= 13);
73
74 }
75
76 /***
77 * test serialization and deserialization of EventMessage.
78 */
79 @Test
80 public void testSerialization() throws IOException, ClassNotFoundException {
81
82 EventMessage eventMessage = new EventMessage(EventMessage.PUT, "key", new Element("key", "element"));
83
84 ByteArrayOutputStream bout = new ByteArrayOutputStream();
85 ObjectOutputStream oos = new ObjectOutputStream(bout);
86 oos.writeObject(eventMessage);
87 byte[] serializedValue = bout.toByteArray();
88 oos.close();
89 EventMessage eventMessage2 = null;
90 ByteArrayInputStream bin = new ByteArrayInputStream(serializedValue);
91 ObjectInputStream ois = new ObjectInputStream(bin);
92 eventMessage2 = (EventMessage) ois.readObject();
93 ois.close();
94
95
96 assertEquals("key", eventMessage2.getSerializableKey());
97 assertEquals("element", eventMessage2.getElement().getObjectValue());
98 assertEquals(EventMessage.PUT, eventMessage2.getEvent());
99 assertTrue(eventMessage2.isValid());
100
101 }
102
103
104 }