001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.l2.msg;
006:
007: import com.tc.l2.ha.WeightGeneratorFactory;
008: import com.tc.l2.state.Enrollment;
009: import com.tc.l2.state.EnrollmentFactory;
010: import com.tc.net.groups.NodeIDImpl;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.ByteArrayOutputStream;
014: import java.io.ObjectInput;
015: import java.io.ObjectInputStream;
016: import java.io.ObjectOutput;
017: import java.io.ObjectOutputStream;
018:
019: import junit.framework.TestCase;
020:
021: public class L2StateMessageTest extends TestCase {
022:
023: private Enrollment enrollment;
024: private L2StateMessage l2StateMessage;
025:
026: public void setUp() {
027: enrollment = EnrollmentFactory.createEnrollment(new NodeIDImpl(
028: "30001", new byte[] { 54, -125, 34, -4 }), true,
029: WeightGeneratorFactory.createDefaultFactory());
030: l2StateMessage = new L2StateMessage();
031: }
032:
033: public void tearDown() {
034: enrollment = null;
035: l2StateMessage = null;
036: }
037:
038: private void validate(L2StateMessage l2sm, L2StateMessage l2sm1) {
039: assertEquals(l2sm.getType(), l2sm1.getType());
040: assertEquals(l2sm.getMessageID(), l2sm1.getMessageID());
041: assertEquals(l2sm.inResponseTo(), l2sm1.inResponseTo());
042: assertEquals(l2sm.messageFrom(), l2sm1.messageFrom());
043:
044: assertEquals(l2sm.getEnrollment(), l2sm1.getEnrollment());
045: assertEquals(l2sm.toString(), l2sm1.toString());
046: }
047:
048: private L2StateMessage writeAndRead(L2StateMessage l2sm)
049: throws Exception {
050: ByteArrayOutputStream bo = new ByteArrayOutputStream();
051: ObjectOutput oo = new ObjectOutputStream(bo);
052: oo.writeObject(l2sm);
053: System.err.println("Written : " + l2sm);
054: ByteArrayInputStream bi = new ByteArrayInputStream(bo
055: .toByteArray());
056: ObjectInput oi = new ObjectInputStream(bi);
057: L2StateMessage l2sm1 = (L2StateMessage) oi.readObject();
058: System.err.println("Read : " + l2sm1);
059: return l2sm1;
060: }
061:
062: public void testBasicSerialization() throws Exception {
063: L2StateMessage l2sm = (L2StateMessage) L2StateMessageFactory
064: .createElectionStartedMessage(enrollment);
065: L2StateMessage l2sm1 = writeAndRead(l2sm);
066: validate(l2sm, l2sm1);
067:
068: l2sm = (L2StateMessage) L2StateMessageFactory
069: .createElectionResultMessage(enrollment);
070: l2sm1 = writeAndRead(l2sm);
071: validate(l2sm, l2sm1);
072:
073: l2sm = (L2StateMessage) L2StateMessageFactory
074: .createElectionWonMessage(enrollment);
075: l2sm1 = writeAndRead(l2sm);
076: validate(l2sm, l2sm1);
077:
078: l2sm = (L2StateMessage) L2StateMessageFactory
079: .createMoveToPassiveStandbyMessage(enrollment);
080: l2sm1 = writeAndRead(l2sm);
081: validate(l2sm, l2sm1);
082:
083: l2sm = (L2StateMessage) L2StateMessageFactory
084: .createAbortElectionMessage(l2StateMessage, enrollment);
085: l2sm1 = writeAndRead(l2sm);
086: validate(l2sm, l2sm1);
087:
088: l2sm = (L2StateMessage) L2StateMessageFactory
089: .createElectionStartedMessage(l2StateMessage,
090: enrollment);
091: l2sm1 = writeAndRead(l2sm);
092: validate(l2sm, l2sm1);
093:
094: l2sm = (L2StateMessage) L2StateMessageFactory
095: .createResultConflictMessage(l2StateMessage, enrollment);
096: l2sm1 = writeAndRead(l2sm);
097: validate(l2sm, l2sm1);
098:
099: l2sm = (L2StateMessage) L2StateMessageFactory
100: .createResultAgreedMessage(l2StateMessage, enrollment);
101: l2sm1 = writeAndRead(l2sm);
102: validate(l2sm, l2sm1);
103: }
104:
105: }
|