1   /**
2    *  Copyright 2003-2007 Greg Luck
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  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   * Tests Serialization and SoftReferences in EventMessage
37   *
38   * @author Greg Luck
39   * @version $Id: EventMessageTest.java 512 2007-07-10 09:18:45Z gregluck $
40   */
41  public class EventMessageTest extends TestCase {
42  
43      private static final Log LOG = LogFactory.getLog(EventMessageTest.class.getName());
44  
45  
46      /**
47       * SoftReference behaviour testing.
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          //This one varies by operating system and architecture. 
66          assertTrue("You should get more than " + counter + " out of SoftReferences", counter >= 13);
67  
68      }
69  
70      /**
71       * test serialization and deserialization of EventMessage.
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          //Check after Serialization
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  }