1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.mitosis.common;
21  
22  
23  import junit.framework.TestCase;
24  
25  
26  /**
27   * 
28   * Test for the SimpleCSN class
29   *
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   */
32  public class DefaultCSNTest extends TestCase
33  {
34  
35      public void testCSN()
36      {
37          long ts = System.currentTimeMillis();
38  
39          CSN csn = new DefaultCSN( Long.toString( ts, 16 ) + ":abcdefghi0123:" + 1 );
40  
41          assertEquals( ts, csn.getTimestamp() );
42          assertEquals( 1, csn.getOperationSequence() );
43          assertEquals( "abcdefghi0123", csn.getReplicaId().toString() );
44      }
45  
46  
47      public void testCSNEmpty()
48      {
49          try
50          {
51              new DefaultCSN( "" );
52              fail();
53          }
54          catch ( AssertionError ae )
55          {
56              assertTrue( true );
57          }
58          catch ( InvalidCSNException ice )
59          {
60              assertTrue( true );
61          }
62      }
63  
64  
65      public void testCSNTSOnly()
66      {
67          try
68          {
69              new DefaultCSN( "123" );
70              fail();
71          }
72          catch ( AssertionError ae )
73          {
74              assertTrue( true );
75          }
76          catch ( InvalidCSNException ice )
77          {
78              assertTrue( true );
79          }
80      }
81  
82  
83      public void testCSNInvalidTS()
84      {
85          try
86          {
87              new DefaultCSN( "zzz:abc:1" );
88              fail();
89          }
90          catch ( AssertionError ae )
91          {
92              assertTrue( true );
93          }
94          catch ( InvalidCSNException ice )
95          {
96              assertTrue( true );
97          }
98      }
99  
100 
101     public void testCSNNoTS()
102     {
103         try
104         {
105             new DefaultCSN( ":abc:1" );
106             fail();
107         }
108         catch ( AssertionError ae )
109         {
110             assertTrue( true );
111         }
112         catch ( InvalidCSNException ice )
113         {
114             assertTrue( true );
115         }
116     }
117 
118 
119     public void testCSNInavlidReplica()
120     {
121         try
122         {
123             new DefaultCSN( "123:*:1" );
124             fail();
125         }
126         catch ( AssertionError ae )
127         {
128             assertTrue( true );
129         }
130         catch ( InvalidCSNException ice )
131         {
132             assertTrue( true );
133         }
134     }
135 
136 
137     public void testCSNNoReplica()
138     {
139         try
140         {
141             new DefaultCSN( "123::1" );
142             fail();
143         }
144         catch ( AssertionError ae )
145         {
146             assertTrue( true );
147         }
148         catch ( InvalidCSNException ice )
149         {
150             assertTrue( true );
151         }
152     }
153 
154 
155     public void testCSNInavlidOpSeq()
156     {
157         try
158         {
159             new DefaultCSN( "123:abc:zzz" );
160             fail();
161         }
162         catch ( AssertionError ae )
163         {
164             assertTrue( true );
165         }
166         catch ( InvalidCSNException ice )
167         {
168             assertTrue( true );
169         }
170     }
171 
172 
173     public void testCSNEmptyOpSeq()
174     {
175         try
176         {
177             new DefaultCSN( "123:abc:" );
178             fail();
179         }
180         catch ( AssertionError ae )
181         {
182             assertTrue( true );
183         }
184         catch ( InvalidCSNException ice )
185         {
186             assertTrue( true );
187         }
188     }
189 
190 
191     public void testCSNNoOpSeq()
192     {
193         try
194         {
195             new DefaultCSN( "123:abc" );
196             fail();
197         }
198         catch ( AssertionError ae )
199         {
200             assertTrue( true );
201         }
202         catch ( InvalidCSNException ice )
203         {
204             assertTrue( true );
205         }
206     }
207 
208 
209     public void testCSNToBytes()
210     {
211         CSN csn = new DefaultCSN( "0123456789abcdef:test:5678cdef" );
212 
213         byte[] bytes = csn.toBytes();
214 
215         assertEquals( 0x01, bytes[0] );
216         assertEquals( 0x23, bytes[1] );
217         assertEquals( 0x45, bytes[2] );
218         assertEquals( 0x67, bytes[3] );
219         assertEquals( ( byte ) 0x89, bytes[4] );
220         assertEquals( ( byte ) 0xAB, bytes[5] );
221         assertEquals( ( byte ) 0xCD, bytes[6] );
222         assertEquals( ( byte ) 0xEF, bytes[7] );
223         assertEquals( 0x56, bytes[8] );
224         assertEquals( 0x78, bytes[9] );
225         assertEquals( ( byte ) 0xCD, bytes[10] );
226         assertEquals( ( byte ) 0xEF, bytes[11] );
227 
228         assertEquals( "test", new String( bytes, 12, bytes.length - 12 ) );
229 
230         CSN deserializedCSN = new DefaultCSN( bytes );
231         assertEquals( csn, deserializedCSN );
232     }
233 }