001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: */
016: package org.apache.catalina.tribes.test.membership;
017:
018: import junit.framework.TestCase;
019: import org.apache.catalina.tribes.membership.MemberImpl;
020: import java.util.Arrays;
021:
022: /**
023: * <p>Title: </p>
024: *
025: * <p>Description: </p>
026: *
027: * <p>Company: </p>
028: *
029: * @author not attributable
030: * @version 1.0
031: */
032: public class MemberSerialization extends TestCase {
033: MemberImpl m1, m2, p1, p2;
034: byte[] payload = null;
035:
036: protected void setUp() throws Exception {
037: super .setUp();
038: payload = new byte[333];
039: Arrays.fill(payload, (byte) 1);
040: m1 = new MemberImpl("localhost", 3333, 1, payload);
041: m2 = new MemberImpl("localhost", 3333, 1);
042: payload = new byte[333];
043: Arrays.fill(payload, (byte) 2);
044: p1 = new MemberImpl("127.0.0.1", 3333, 1, payload);
045: p2 = new MemberImpl("localhost", 3331, 1, payload);
046: m1.setDomain(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
047: m2.setDomain(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
048: m1.setCommand(new byte[] { 1, 2, 4, 5, 6, 7, 8, 9 });
049: m2.setCommand(new byte[] { 1, 2, 4, 5, 6, 7, 8, 9 });
050: }
051:
052: public void testCompare() throws Exception {
053: assertTrue(m1.equals(m2));
054: assertTrue(m2.equals(m1));
055: assertTrue(p1.equals(m2));
056: assertFalse(m1.equals(p2));
057: assertFalse(m1.equals(p2));
058: assertFalse(m2.equals(p2));
059: assertFalse(p1.equals(p2));
060: }
061:
062: public void testSerializationOne() throws Exception {
063: MemberImpl m = m1;
064: byte[] md1 = m.getData(false, true);
065: byte[] mda1 = m.getData(false, false);
066: assertTrue(Arrays.equals(md1, mda1));
067: assertTrue(md1 == mda1);
068: mda1 = m.getData(true, true);
069: MemberImpl ma1 = MemberImpl.getMember(mda1);
070: assertTrue(compareMembers(m, ma1));
071: mda1 = p1.getData(false);
072: assertFalse(Arrays.equals(md1, mda1));
073: ma1 = MemberImpl.getMember(mda1);
074: assertTrue(compareMembers(p1, ma1));
075:
076: md1 = m.getData(true, true);
077: Thread.sleep(50);
078: mda1 = m.getData(true, true);
079: MemberImpl a1 = MemberImpl.getMember(md1);
080: MemberImpl a2 = MemberImpl.getMember(mda1);
081: assertTrue(a1.equals(a2));
082: assertFalse(Arrays.equals(md1, mda1));
083:
084: }
085:
086: public boolean compareMembers(MemberImpl impl1, MemberImpl impl2) {
087: boolean result = true;
088: result = result
089: && Arrays.equals(impl1.getHost(), impl2.getHost());
090: result = result
091: && Arrays
092: .equals(impl1.getPayload(), impl2.getPayload());
093: result = result
094: && Arrays.equals(impl1.getUniqueId(), impl2
095: .getUniqueId());
096: result = result && impl1.getPort() == impl2.getPort();
097: return result;
098: }
099:
100: protected void tearDown() throws Exception {
101: super.tearDown();
102: }
103:
104: }
|