001: /**
002: * Copyright 2003-2007 Luck Consulting Pty Ltd
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package net.sf.ehcache.distribution;
016:
017: import junit.framework.TestCase;
018:
019: import java.util.Map;
020: import java.util.HashMap;
021: import java.lang.ref.SoftReference;
022: import java.io.IOException;
023: import java.io.ByteArrayOutputStream;
024: import java.io.ObjectOutputStream;
025: import java.io.ByteArrayInputStream;
026: import java.io.ObjectInputStream;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import net.sf.ehcache.Element;
031: import net.sf.ehcache.AbstractCacheTest;
032:
033: /**
034: * Tests Serialization and SoftReferences in EventMessage
035: *
036: * @author Greg Luck
037: * @version $Id: EventMessageTest.java 519 2007-07-27 07:11:45Z gregluck $
038: */
039: public class EventMessageTest extends TestCase {
040:
041: private static final Log LOG = LogFactory
042: .getLog(EventMessageTest.class.getName());
043:
044: /**
045: * SoftReference behaviour testing.
046: */
047: public void testSoftReferences() {
048: AbstractCacheTest.forceVMGrowth();
049: Map map = new HashMap();
050: for (int i = 0; i < 100; i++) {
051: map.put(new Integer(i),
052: new SoftReference(new byte[1000000]));
053: }
054:
055: int counter = 0;
056: for (int i = 0; i < 100; i++) {
057: SoftReference softReference = (SoftReference) map
058: .get(new Integer(i));
059: byte[] payload = (byte[]) softReference.get();
060: if (payload != null) {
061: LOG.info("Value found for " + i);
062: counter++;
063: }
064: }
065: //This one varies by operating system and architecture.
066: assertTrue("You should get more than " + counter
067: + " out of SoftReferences", counter >= 13);
068:
069: }
070:
071: /**
072: * test serialization and deserialization of EventMessage.
073: */
074: public void testSerialization() throws IOException,
075: ClassNotFoundException {
076:
077: EventMessage eventMessage = new EventMessage(EventMessage.PUT,
078: "key", new Element("key", "element"));
079:
080: ByteArrayOutputStream bout = new ByteArrayOutputStream();
081: ObjectOutputStream oos = new ObjectOutputStream(bout);
082: oos.writeObject(eventMessage);
083: byte[] serializedValue = bout.toByteArray();
084: oos.close();
085: EventMessage eventMessage2 = null;
086: ByteArrayInputStream bin = new ByteArrayInputStream(
087: serializedValue);
088: ObjectInputStream ois = new ObjectInputStream(bin);
089: eventMessage2 = (EventMessage) ois.readObject();
090: ois.close();
091:
092: //Check after Serialization
093: assertEquals("key", eventMessage2.getSerializableKey());
094: assertEquals("element", eventMessage2.getElement()
095: .getObjectValue());
096: assertEquals(EventMessage.PUT, eventMessage2.getEvent());
097: assertTrue(eventMessage2.isValid());
098:
099: }
100:
101: }
|