directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r776096 - in /directory/shared/trunk/ldap/src: main/java/org/apache/directory/shared/ldap/csn/ main/java/org/apache/directory/shared/ldap/schema/comparators/ test/java/org/apache/directory/shared/ldap/csn/ test/java/org/apache/directory/sha...
Date Mon, 18 May 2009 21:04:39 GMT
Author: elecharny
Date: Mon May 18 21:04:39 2009
New Revision: 776096

URL: http://svn.apache.org/viewvc?rev=776096&view=rev
Log:
Added classes for CSN and UUID comparator. Fixed the CSN calss and factory to reflect the
OpenLDAP implementation. Added some test

Added:
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/CSNComparator.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/UUIDComparator.java
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/comparators/CSNComparatorTest.java
Modified:
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/csn/CSN.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/csn/CSNFactory.java
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/csn/CSNFactoryTest.java
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/csn/CSNTest.java

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/csn/CSN.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/csn/CSN.java?rev=776096&r1=776095&r2=776096&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/csn/CSN.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/csn/CSN.java
Mon May 18 21:04:39 2009
@@ -41,10 +41,10 @@
  * The CSN syntax is :
  * <pre>
  * <CSN>            ::= <timestamp> # <changeCount> # <replicaId>
# <modifierNumber>
- * <timestamp>      ::= A GMT based time, YYYYMMDDhh:mm:ssz
- * <changeCount>    ::= [0-9a-zA-Z]+
- * <replicaId>      ::= IA5String
- * <modifierNumber> ::= [0-9a-zA-Z]+
+ * <timestamp>      ::= A GMT based time, YYYYmmddHHMMSS.uuuuuuZ
+ * <changeCount>    ::= [000000-ffffff] 
+ * <replicaId>      ::= [000-fff]
+ * <modifierNumber> ::= [000000-ffffff]
  * </pre>
  *  
  * It distinguishes a change made on an object on a server,
@@ -71,7 +71,7 @@
     private final long timestamp;
 
     /** The server identification */
-    private final String replicaId;
+    private final int replicaId;
 
     /** The operation number in a modification operation */
     private final int operationNumber;
@@ -86,7 +86,11 @@
     private transient byte[] bytes;
 
     /** The Timestamp syntax. The last 'z' is _not_ the Time Zone */
-    private static final SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHH:mm:ss'z'"
);
+    private static final SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmss" );
+    
+    /** Padding used to format number with a fixed size */
+    private static final String[] PADDING_6 = new String[] { "00000", "0000", "000", "00",
"0", "" };
+    private static final String[] PADDING_3 = new String[] { "00", "0", "" };
 
 
     /**
@@ -98,7 +102,7 @@
      * @param replicaId Replica ID where modification occurred (<tt>[-_A-Za-z0-9]{1,16}</tt>)
      * @param operationNumber Operation number in a modification operation
      */
-    public CSN( long timestamp, int changeCount, String replicaId, int operationNumber )
+    public CSN( long timestamp, int changeCount, int replicaId, int operationNumber )
     {
         this.timestamp = timestamp;
         this.replicaId = replicaId;
@@ -123,6 +127,13 @@
             LOG.error( message );
             throw new InvalidCSNException( message );
         }
+        
+        if ( value.length() != 40 )
+        {
+            String message = "The CSN's length is incorrect, it should be 40 chars long";
+            LOG.error( message );
+            throw new InvalidCSNException( message );
+        }
 
         // Get the Timestamp
         int sepTS = value.indexOf( '#' );
@@ -136,11 +147,23 @@
         
         String timestampStr = value.substring( 0, sepTS ).trim();
         
+        if ( timestampStr.length() != 22 )
+        {
+            String message = "The timestamp is not long enough";
+            LOG.error( message );
+            throw new InvalidCSNException( message );
+        }
+        
+        // Let's transform the Timestamp by removing the mulliseconds and microseconds
+        String realTimestamp = timestampStr.substring( 0, 14 );
+        
+        long tempTimestamp = 0L;
+        
         synchronized ( sdf )
         {
             try
             {
-                timestamp = sdf.parse( timestampStr ).getTime();
+                tempTimestamp = sdf.parse( realTimestamp ).getTime();
             }
             catch ( ParseException pe )
             {
@@ -149,6 +172,12 @@
                 throw new InvalidCSNException( message );
             }
         }
+        
+        // And add the milliseconds and microseconds now
+        int millis = Integer.valueOf( timestampStr.substring( 15, 21 ) );
+        
+        tempTimestamp += (millis/1000);
+        timestamp = tempTimestamp;
 
         // Get the changeCount. It should be an hex number prefixed with '0x'
         int sepCC = value.indexOf( '#', sepTS + 1 );
@@ -162,16 +191,9 @@
 
         String changeCountStr = value.substring( sepTS + 1, sepCC ).trim();
         
-        if ( !changeCountStr.startsWith( "0x" ) )
-        {
-            String message = "The changeCount '" + changeCountStr + "' is not a valid number";
-            LOG.error( message );
-            throw new InvalidCSNException( message );
-        }
-        
         try
         {
-            changeCount = Integer.parseInt( changeCountStr.substring( 2 ), 16 ); 
+            changeCount = Integer.parseInt( changeCountStr, 16 ); 
         }
         catch ( NumberFormatException nfe )
         {
@@ -190,18 +212,22 @@
             throw new InvalidCSNException( message );
         }
 
-        replicaId = value.substring( sepCC + 1, sepRI).trim();
+        String replicaIdStr = value.substring( sepCC + 1, sepRI).trim();
         
-        if ( StringTools.isEmpty( replicaId ) )
+        if ( StringTools.isEmpty( replicaIdStr ) )
         {
             String message = "The replicaID must not be null or empty";
             LOG.error( message );
             throw new InvalidCSNException( message );
         }
         
-        if ( !StringTools.isIA5String( replicaId ) )
+        try
+        {
+            replicaId = Integer.parseInt( replicaIdStr, 16 ); 
+        }
+        catch ( NumberFormatException nfe )
         {
-            String message = "The replicaID must contains only alphanumeric characters";
+            String message = "The replicaId '" + replicaIdStr + "' is not a valid number";
             LOG.error( message );
             throw new InvalidCSNException( message );
         }
@@ -216,16 +242,9 @@
         
         String operationNumberStr = value.substring( sepRI + 1 ).trim();
         
-        if ( !operationNumberStr.startsWith( "0x" ) )
-        {
-            String message = "The operationNumber '" + operationNumberStr + "' is not a valid
number";
-            LOG.error( message );
-            throw new InvalidCSNException( message );
-        }
-        
         try
         {
-            operationNumber = Integer.parseInt( operationNumberStr.substring( 2 ), 16 );

+            operationNumber = Integer.parseInt( operationNumberStr, 16 ); 
         }
         catch ( NumberFormatException nfe )
         {
@@ -296,7 +315,7 @@
     /**
      * @return The replicaId
      */
-    public String getReplicaId()
+    public int getReplicaId()
     {
         return replicaId;
     }
@@ -325,12 +344,26 @@
                 buf.append( sdf.format( new Date( timestamp ) ) );
             }
             
+            // Add the milliseconds part
+            long millis = (timestamp % 1000 ) * 1000;
+            String millisStr = Long.toString( millis );
+            
+            buf.append( '.' ).append( PADDING_3[ millisStr.length() ] ).append( millisStr
).append( "000Z#" );
+            
+            String countStr = Integer.toHexString( changeCount );
+            
+            buf.append( PADDING_6[countStr.length()] ).append( countStr );
             buf.append( '#' );
-            buf.append( "0x" ).append( Integer.toHexString( changeCount ) );
-            buf.append( '#' );
-            buf.append( replicaId );
+
+            String replicaIdStr = Integer.toHexString( replicaId );
+            
+            buf.append( PADDING_3[replicaIdStr.length()] ).append( replicaIdStr );
             buf.append( '#' );
-            buf.append( "0x" ).append( Integer.toHexString( operationNumber ) );
+            
+            String operationNumberStr = Integer.toHexString( operationNumber );
+            
+            buf.append( PADDING_6[operationNumberStr.length()] ).append( operationNumberStr
);
+            
             csnStr = buf.toString();
         }
         
@@ -349,7 +382,7 @@
         
         h = h*17 + (int)(timestamp ^ (timestamp >>> 32));
         h = h*17 + changeCount;
-        h = h*17 + replicaId.hashCode();
+        h = h*17 + replicaId;
         h = h*17 + operationNumber;
         
         return h;
@@ -380,7 +413,7 @@
         return 
             ( timestamp == that.timestamp ) &&
             ( changeCount == that.changeCount ) &&
-            ( replicaId.equals( that.replicaId ) ) &&
+            ( replicaId == that.replicaId ) &&
             ( operationNumber == that.operationNumber );
     }
 
@@ -422,7 +455,11 @@
         }
 
         // Then the replicaId
-        int replicaIdCompareResult = this.replicaId.compareTo( csn.replicaId );
+        int replicaIdCompareResult= 
+            ( this.replicaId < csn.replicaId ? 
+              -1 : 
+               ( this.replicaId > csn.replicaId ?
+                   1 : 0 ) );
 
         if ( replicaIdCompareResult != 0 )
         {

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/csn/CSNFactory.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/csn/CSNFactory.java?rev=776096&r1=776095&r2=776096&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/csn/CSNFactory.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/csn/CSNFactory.java
Mon May 18 21:04:39 2009
@@ -45,10 +45,10 @@
      * Generated CSN can be duplicate if user generates CSNs more than 2G 
      * times a milliseconds.
      * 
-     * @param replicaId Replica ID.  ReplicaID must be 1-8 digit alphanumeric
-     *        string.
+     * @param replicaId Replica ID.  ReplicaID must be 1-3 digit alphanumeric
+     *        value (from 000 to fff).
      */
-    public CSN newInstance( String replicaId )
+    public CSN newInstance( int replicaId )
     {
         long newTimestamp = System.currentTimeMillis();
         
@@ -73,11 +73,10 @@
      * This method is <b>not</b> to be used except for test purposes.
      * 
      * @param timestamp The timestamp to use
-     * @param replicaId Replica ID.  ReplicaID must be 1-8 digit alphanumeric
-     * string.
+     * @param replicaId Replica ID.  ReplicaID must be 1-3 digit value
      * @param changeCount The change count to use
      */
-    public CSN newInstance( long timestamp, String replicaId, int changeCount )
+    public CSN newInstance( long timestamp, int replicaId, int changeCount )
     {
         return new CSN( timestamp, changeCount, replicaId, 0 );
     }
@@ -91,6 +90,6 @@
      */
     public CSN newInstance( long expirationDate )
     {
-        return new CSN( expirationDate, Integer.MAX_VALUE, "ZZZZZZZZZZZZZZZZ", Integer.MAX_VALUE
);
+        return new CSN( expirationDate, Integer.MAX_VALUE, -1, Integer.MAX_VALUE );
     }
 }

Added: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/CSNComparator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/CSNComparator.java?rev=776096&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/CSNComparator.java
(added)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/CSNComparator.java
Mon May 18 21:04:39 2009
@@ -0,0 +1,87 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.shared.ldap.schema.comparators;
+
+
+import java.util.Comparator;
+
+import org.apache.directory.shared.ldap.csn.CSN;
+
+
+/**
+ * A comparator for CSN.
+ *
+ * The CSN are ordered depending on an evaluation of its component, in this order :
+ * - time, 
+ * - changeCount,
+ * - sid
+ * - modifierNumber
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class CSNComparator implements Comparator<CSN>
+{
+    /** A static instance of this comparator */
+    public static final Comparator<CSN> INSTANCE = new CSNComparator();
+    
+    
+    /**
+     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
+     */
+    public int compare( CSN csn1, CSN csn2 )
+    {
+        // -------------------------------------------------------------------
+        // Handle some basis cases
+        // -------------------------------------------------------------------
+
+        if ( csn1 == null )
+        {
+            return ( csn2 == null ) ? 0 : -1;
+        }
+        
+        if ( csn2 == null )
+        {
+            return 1;
+        }
+        
+        if ( csn1.getTimestamp() != csn2.getTimestamp() )
+        {
+            return ( csn1.getTimestamp() < csn2.getTimestamp() ? -1 : 1 );
+        }
+        
+        if ( csn1.getChangeCount() != csn2.getChangeCount() )
+        {
+            return ( csn1.getChangeCount() < csn2.getChangeCount() ? -1 : 1 );
+        }
+        
+        if ( csn1.getReplicaId() != csn2.getReplicaId() )
+        {
+            return ( csn1.getReplicaId() < csn2.getReplicaId() ? -1 : 1 );
+        }
+        
+        if ( csn1.getOperationNumber() != csn2.getOperationNumber() )
+        {
+            return ( csn1.getOperationNumber() < csn2.getOperationNumber() ? -1 : 1 );
+        }
+        
+        return 0;
+    }
+}

Added: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/UUIDComparator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/UUIDComparator.java?rev=776096&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/UUIDComparator.java
(added)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/UUIDComparator.java
Mon May 18 21:04:39 2009
@@ -0,0 +1,59 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.shared.ldap.schema.comparators;
+
+
+import java.util.Comparator;
+import java.util.UUID;
+
+
+/**
+ * A comparator for UUID. We simply use the UUID compareTo method.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class UUIDComparator implements Comparator<UUID>
+{
+    /** A static instance of this comparator */
+    public static final Comparator<UUID> INSTANCE = new UUIDComparator();
+    
+    
+    /**
+     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
+     */
+    public int compare( UUID uuid1, UUID uuid2 )
+    {
+        // -------------------------------------------------------------------
+        // Handle some basis cases
+        // -------------------------------------------------------------------
+        if ( uuid1 == null )
+        {
+            return ( uuid2 == null ) ? 0 : -1;
+        }
+        
+        if ( uuid2 == null )
+        {
+            return 1;
+        }
+        
+        return uuid1.compareTo( uuid2 );
+    }
+}

Modified: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/csn/CSNFactoryTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/csn/CSNFactoryTest.java?rev=776096&r1=776095&r2=776096&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/csn/CSNFactoryTest.java
(original)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/csn/CSNFactoryTest.java
Mon May 18 21:04:39 2009
@@ -43,7 +43,7 @@
     @Test
     public void testUnique()
     {
-        String replicaID = "test";
+        int replicaID = 001;
         CSNFactory defaultCSNFactory = new CSNFactory();
 
         CSN[] csns = new CSN[NUM_GENERATES];

Modified: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/csn/CSNTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/csn/CSNTest.java?rev=776096&r1=776095&r2=776096&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/csn/CSNTest.java
(original)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/csn/CSNTest.java
Mon May 18 21:04:39 2009
@@ -39,19 +39,21 @@
  */
 public class CSNTest
 {
-    private SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHH:mm:ss'z'" );
+    private SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmss.123456'Z'" );
 
     @Test
     public void testCSN()
     {
         long ts = System.currentTimeMillis();
 
-        CSN csn = new CSN( sdf.format( new Date( ts ) ) + "#0x0000#abcdefghi0123#0x0001"
);
+        CSN csn = new CSN( sdf.format( new Date( ts ) ) + "#123456#abc#654321" );
 
         assertEquals( ts/1000, csn.getTimestamp()/1000 );
-        assertEquals( 0, csn.getChangeCount() );
-        assertEquals( 1, csn.getOperationNumber() );
-        assertEquals( "abcdefghi0123", csn.getReplicaId() );
+        
+        // ALl the value are converted from hex to int
+        assertEquals( 1193046, csn.getChangeCount() );
+        assertEquals( 6636321, csn.getOperationNumber() );
+        assertEquals( 2748, csn.getReplicaId() );
     }
 
 
@@ -105,8 +107,8 @@
     {
         try
         {
-            // A missing 'z'
-            new CSN( "2000010100:00:00#0x1#abc#0x1" );
+            // A missing 'Z'
+            new CSN( "20010101000000.000000#000001#abc#000001" );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -116,8 +118,8 @@
         
         try
         {
-            // Missing seconds
-            new CSN( "2000010100:00:z#0x1#abc#0x1" );
+            // Missing milliseconds
+            new CSN( "20000101000000.Z#000001#abc#000001" );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -127,8 +129,8 @@
 
         try
         {
-            // Missing minutes
-            new CSN( "2000010100::00z#0x1#abc#0x1" );
+            // Missing dot
+            new CSN( "20010101000000000000Z#0x1#abc#0x1" );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -138,8 +140,8 @@
 
         try
         {
-            // Missing hours
-            new CSN( "2000010100::00z#0x1#abc#0x1" );
+            // Missing dot and millis
+            new CSN( "20010101000000Z#000001#abc#000001" );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -150,7 +152,7 @@
         try
         {
             // Invalid date
-            new CSN( "200A010100::00z#0x1#abc#0x1" );
+            new CSN( "200A01010000Z#000001#abc#000001" );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -165,7 +167,7 @@
     {
         try
         {
-            new CSN( "#0x1#abc#0x1" );
+            new CSN( "#000001#abc#000001" );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -180,7 +182,7 @@
     {
         try
         {
-            new CSN( "2000010100:00:00z##a#0x1" );
+            new CSN( "20010101000000.000000Z##abc#000001" );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -195,7 +197,7 @@
     {
         try
         {
-            new CSN( "2000010100:00:00z#00#a#0x1" );
+            new CSN( "20010101000000.000000Z#00#abc#000001" );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -205,17 +207,7 @@
 
         try
         {
-            new CSN( "2000010100:00:00z#0x#a#0x1" );
-            fail();
-        }
-        catch ( InvalidCSNException ice )
-        {
-            assertTrue( true );
-        }
-        
-        try
-        {
-            new CSN( "2000010100:00:00z#0x0G#a#0x1" );
+            new CSN( "20010101000000.000000Z#00000G#abc#000001" );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -230,7 +222,7 @@
     {
         try
         {
-            new CSN( "2000010100:00:00z#0x1##0x1" );
+            new CSN( "20010101000000.000000Z#000001##000001" );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -245,7 +237,7 @@
     {
         try
         {
-            new CSN( "2000010100:00:00z#0x1#a12-b3é#0x1" );
+            new CSN( "20010101000000.000000Z#000001#a12-b3é#000001" );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -260,7 +252,7 @@
     {
         try
         {
-            new CSN( "2001010100:00:00z#0x0#abc" );
+            new CSN( "20010101000000.000000Z#000000#abc" );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -270,7 +262,7 @@
         
         try
         {
-            new CSN( "2001010100:00:00z#0x0#abc#  " );
+            new CSN( "20010101000000.000000Z#000000#abc#  " );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -285,7 +277,7 @@
     {
         try
         {
-            new CSN( "2001010100:00:00z#0x0#abc#zzz" );
+            new CSN( "20010101000000.000000Z#000000#abc#000zzz" );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -295,7 +287,7 @@
 
         try
         {
-            new CSN( "2001010100:00:00z#0x0#abc#0x" );
+            new CSN( "20010101000000.000000Z#000000#abc#00000" );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -305,7 +297,7 @@
 
         try
         {
-            new CSN( "2001010100:00:00z#0x0#abc#000" );
+            new CSN( "20010101000000.000000Z#000000#abc#" );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -315,7 +307,7 @@
 
         try
         {
-            new CSN( "2001010100:00:00z#0x0#abc#0x0G" );
+            new CSN( "20010101000000.000000Z#000000#abc#00000G" );
             fail();
         }
         catch ( InvalidCSNException ice )
@@ -328,16 +320,17 @@
     @Test
     public void testCSNToBytes()
     {
-        CSN csn = new CSN( "2001010100:00:00z#0x0#abc#0x1" );
+        CSN csn = new CSN( "20010101000000.000000Z#000000#abc#000001" );
 
         byte[] bytes = csn.toBytes();
 
         byte[] expected = new byte[]
             { 
                 '2', '0', '0', '1', '0', '1', '0', '1', 
-                '0', '0', ':', '0', '0', ':', '0', '0', 
-                'z', '#', '0', 'x', '0', '#', 'a', 'b', 
-                'c', '#', '0', 'x', '1' 
+                '0', '0', '0', '0', '0', '0', '.', '0',
+                '0', '0', '0', '0', '0', 'Z', '#', '0', 
+                '0', '0', '0', '0', '0', '#', 'a', 'b', 
+                'c', '#', '0', '0', '0', '0', '0', '1' 
             };
         
         assertTrue( Arrays.equals( expected, bytes ) );

Added: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/comparators/CSNComparatorTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/comparators/CSNComparatorTest.java?rev=776096&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/comparators/CSNComparatorTest.java
(added)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/comparators/CSNComparatorTest.java
Mon May 18 21:04:39 2009
@@ -0,0 +1,119 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.shared.ldap.schema.comparators;
+
+import org.apache.directory.shared.ldap.csn.CSN;
+import org.junit.Before;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+
+
+/**
+ * Test the CSN comparator
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class CSNComparatorTest
+{
+    private CSNComparator comparator;
+    
+    @Before
+    public void init()
+    {
+        comparator = new CSNComparator();
+    }
+    
+    
+    @Test
+    public void testNullCSNs()
+    {
+        CSN csn1 = null;
+        CSN csn2 = null;
+        
+        assertEquals( 0, comparator.compare( csn1, csn2 ) );
+        
+        csn2 = new CSN( System.currentTimeMillis(), 1, 1, 1 );
+        assertEquals( -1, comparator.compare( csn1, csn2 ) );
+
+        CSN csn3 = null;
+        assertEquals( 1, comparator.compare( csn2, csn3 ) );
+    }
+
+
+    @Test
+    public void testEqualsCSNs()
+    {
+        long t0 = System.currentTimeMillis();
+        CSN csn1 = new CSN( t0, 0, 0, 0 );
+        CSN csn2 = new CSN( t0, 0, 0, 0 );
+        
+        assertEquals( 0, comparator.compare( csn1, csn2 ) );
+    }
+    
+    
+    @Test
+    public void testDifferentTimeStampCSNs()
+    {
+        long t0 = System.currentTimeMillis();
+        long t1 = System.currentTimeMillis() + 1000;
+        CSN csn1 = new CSN( t0, 0, 0, 0 );
+        CSN csn2 = new CSN( t1, 0, 0, 0 );
+        
+        assertEquals( -1, comparator.compare( csn1, csn2 ) );
+        assertEquals( 1, comparator.compare( csn2, csn1 ) );
+    }
+    
+    
+    @Test
+    public void testDifferentChangeCountCSNs()
+    {
+        long t0 = System.currentTimeMillis();
+        CSN csn1 = new CSN( t0, 0, 0, 0 );
+        CSN csn2 = new CSN( t0, 1, 0, 0 );
+        
+        assertEquals( -1, comparator.compare( csn1, csn2 ) );
+        assertEquals( 1, comparator.compare( csn2, csn1 ) );
+    }
+    
+    
+    @Test
+    public void testDifferentReplicaIdCSNs()
+    {
+        long t0 = System.currentTimeMillis();
+        CSN csn1 = new CSN( t0, 0, 0, 0 );
+        CSN csn2 = new CSN( t0, 0, 1, 0 );
+        
+        assertEquals( -1, comparator.compare( csn1, csn2 ) );
+        assertEquals( 1, comparator.compare( csn2, csn1 ) );
+    }
+    
+    
+    @Test
+    public void testDifferentOperationNumberCSNs()
+    {
+        long t0 = System.currentTimeMillis();
+        CSN csn1 = new CSN( t0, 0, 0, 0 );
+        CSN csn2 = new CSN( t0, 0, 0, 1 );
+        
+        assertEquals( -1, comparator.compare( csn1, csn2 ) );
+        assertEquals( 1, comparator.compare( csn2, csn1 ) );
+    }
+}



Mime
View raw message