001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)TestExchangeIdImpl.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.proxy.util;
030:
031: import com.sun.jbi.messaging.ExchangeIdGenerator;
032:
033: import java.util.Set;
034:
035: /**
036: * Tests for the ExchangeIdImpl class
037: *
038: * @author Sun Microsystems, Inc.
039: */
040: public class TestExchangeIdImpl extends junit.framework.TestCase {
041: /** Test instance message */
042: private ExchangeIdGenerator mGenerator;
043:
044: /**
045: * The constructor for this testcase, forwards the test name to
046: * the jUnit TestCase base class.
047: * @param aTestName String with the name of this test.
048: */
049: public TestExchangeIdImpl(String aTestName) {
050: super (aTestName);
051: }
052:
053: /**
054: * Setup for the test. This creates the ComponentRegistry instance
055: * and other objects needed for the tests.
056: * @throws Exception when set up fails for any reason.
057: */
058: public void setUp() throws Exception {
059: super .setUp();
060: }
061:
062: /**
063: * Cleanup for the test.
064: * @throws Exception when tearDown fails for any reason.
065: */
066: public void tearDown() throws Exception {
067: super .tearDown();
068: }
069:
070: // ============================= test methods ================================
071:
072: /**
073: * testNextId
074: * @throws Exception if an unexpected error occurs
075: */
076: public void testNextId() throws Exception {
077: ExchangeIdGenerator g;
078: ExchangeIdGenerator g2;
079: String id;
080: String id2;
081: String[] pieces;
082: String[] pieces2;
083:
084: g = (ExchangeIdGenerator) new ExchangeIdImpl();
085: g2 = (ExchangeIdGenerator) new ExchangeIdImpl();
086: id = g.nextId();
087: id2 = g2.nextId();
088:
089: pieces = id.split("-");
090: pieces2 = id2.split("-");
091:
092: //
093: // Test that the output has 3 pieces.
094: //
095: assertTrue(pieces.length == 3);
096: assertTrue(pieces2.length == 3);
097:
098: //
099: // Test that all 3 components are different. There is a slight chance
100: // of this not being true. This mostly effects the last one which only has
101: // 8 bits of randomness.
102: //
103: assertTrue(!pieces[0].equals(pieces2[0]));
104: assertTrue(!pieces[1].equals(pieces2[1]));
105: assertTrue(!pieces[2].equals(pieces2[2]));
106:
107: }
108:
109: /**
110: * testClockReset
111: * @throws Exception if an unexpected error occurs
112: */
113: public void testClockReset() throws Exception {
114: ExchangeIdImpl g;
115: String id;
116: String id2;
117: String[] pieces;
118: String[] pieces2;
119:
120: g = new ExchangeIdImpl();
121: id = g.nextId();
122: g.mLastTimestamp += 1000 * 60 * 60 * 24;
123: id2 = g.nextId();
124:
125: pieces = id.split("-");
126: pieces2 = id2.split("-");
127:
128: //
129: // After detecting a backwards clock, the node should stay the same but
130: // a new sequence number should be generated.
131: //
132: assertTrue(pieces[0].equals(pieces2[0]));
133: assertTrue(!pieces[1].equals(pieces2[1]));
134: assertTrue(!pieces[2].equals(pieces2[2]));
135: }
136:
137: /**
138: * testSequencing
139: * @throws Exception if an unexpected error occurs
140: */
141: public void testSequencing() throws Exception {
142: ExchangeIdImpl g;
143: String id;
144: String id2;
145: String[] pieces;
146: String[] pieces2;
147:
148: g = new ExchangeIdImpl();
149: id = g.nextId();
150: id2 = g.nextId();
151:
152: pieces = id.split("-");
153: pieces2 = id2.split("-");
154:
155: //
156: // Consecutive number should use the same node and sequence. The timestamp
157: // counter difference should be one except at clock tick boundaries.
158: //
159: assertTrue(pieces[0].equals(pieces2[0]));
160: assertTrue(pieces[1].equals(pieces2[1]));
161: assertTrue(!pieces[2].equals(pieces2[2]));
162: }
163:
164: }
|