directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r636959 - in /directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src: main/java/org/apache/directory/server/core/avltree/ test/java/org/apache/directory/server/core/avltree/
Date Fri, 14 Mar 2008 02:30:55 GMT
Author: akarasulu
Date: Thu Mar 13 19:30:52 2008
New Revision: 636959

URL: http://svn.apache.org/viewvc?rev=636959&view=rev
Log:
adding default marshaller which uses standard java serialization for any kind of object

Added:
    directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/DefaultMarshaller.java
    directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/DefaultMarshallerTest.java
Modified:
    directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/AvlTreeMarshaller.java
    directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreeMarshallerTest.java

Modified: directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/AvlTreeMarshaller.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/AvlTreeMarshaller.java?rev=636959&r1=636958&r2=636959&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/AvlTreeMarshaller.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/AvlTreeMarshaller.java
Thu Mar 13 19:30:52 2008
@@ -50,18 +50,32 @@
     
 
     /**
-     * 
-     * Creates a new instance of AvlTreeMarshaller.
+     * Creates a new instance of AvlTreeMarshaller with a custom key
+     * Marshaller.
      *
      * @param comparator Comparator to be used for key comparision
      * @param keyMarshaller marshaller for keys
      */
-    public AvlTreeMarshaller(Comparator<E> comparator, Marshaller<E> keyMarshaller)
+    public AvlTreeMarshaller( Comparator<E> comparator, Marshaller<E> keyMarshaller
)
     {
         this.comparator = comparator;
         this.keyMarshaller = keyMarshaller;
     }
-    
+
+
+    /**
+     * Creates a new instance of AvlTreeMarshaller with the default key
+     * Marshaller which uses Java Serialization.
+     *
+     * @param comparator Comparator to be used for key comparision
+     */
+    public AvlTreeMarshaller( Comparator<E> comparator )
+    {
+        this.comparator = comparator;
+        this.keyMarshaller = ( Marshaller<E> ) DefaultMarshaller.INSTANCE;
+    }
+
+
     /**
      * Marshals the given tree to bytes
      * @param tree the tree to be marshalled

Added: directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/DefaultMarshaller.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/DefaultMarshaller.java?rev=636959&view=auto
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/DefaultMarshaller.java
(added)
+++ directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/DefaultMarshaller.java
Thu Mar 13 19:30:52 2008
@@ -0,0 +1,69 @@
+/*
+ *  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.server.core.avltree;
+
+
+import java.io.*;
+
+
+/**
+ * A marshaller which uses default Java Serialization.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class DefaultMarshaller implements Marshaller
+{
+    public static final DefaultMarshaller INSTANCE = new DefaultMarshaller();
+
+
+    public byte[] serialize( Object object ) throws IOException
+    {
+        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( byteStream );
+        byte[] data;
+
+        out.writeObject( object );
+        out.flush();
+        data = byteStream.toByteArray();
+        out.close();
+
+        return data;
+    }
+
+
+    public Object deserialize( byte[] bytes ) throws IOException
+    {
+        Object object;
+        ByteArrayInputStream byteStream = new ByteArrayInputStream( bytes );
+        ObjectInputStream in = new ObjectInputStream( byteStream );
+
+        try
+        {
+            object = in.readObject();
+        }
+        catch ( ClassNotFoundException e )
+        {
+            throw new IOException( "Could not find class", e );
+        }
+
+        return object;
+    }
+}

Modified: directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreeMarshallerTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreeMarshallerTest.java?rev=636959&r1=636958&r2=636959&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreeMarshallerTest.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreeMarshallerTest.java
Thu Mar 13 19:30:52 2008
@@ -25,11 +25,7 @@
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertEquals;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
+import java.io.*;
 import java.util.Comparator;
 
 import org.junit.Before;
@@ -136,6 +132,64 @@
             assertEquals( ii, ( int ) cursor.get() );
             cursor.next();
         }
+    }
+
+
+    @Test
+    public void testRoundTripManyEntriesDefaultSerialization() throws Exception
+    {
+        Comparator<Bar> barComparator = new Comparator<Bar>() {
+            public int compare( Bar o1, Bar o2 )
+            {
+                return o1.intValue.compareTo( o2.intValue );
+            }
+        };
+
+        AvlTree<Bar> original = new AvlTree<Bar>( barComparator );
+
+        for ( int ii = 0; ii < 100; ii++ )
+        {
+            original.insert( new Bar( ii ) );
+        }
+
+        AvlTreeMarshaller<Bar> marshaller = new AvlTreeMarshaller<Bar>( barComparator
);
+        byte[] bites = marshaller.serialize( original );
+        AvlTree<Bar> deserialized = marshaller.deserialize( bites );
+        assertFalse( deserialized.isEmpty() );
+        assertEquals( 100, deserialized.getSize() );
+
+        AvlTreeCursor<Bar> cursor = new AvlTreeCursor<Bar>( deserialized );
+        cursor.first();
+        for ( int ii = 0; ii < 100; ii++ )
+        {
+            assertEquals( ii, ( int ) cursor.get().intValue );
+            cursor.next();
+        }
+    }
+
+
+    static class Bar implements Serializable
+    {
+        Integer intValue = 37;
+        String stringValue = "bar";
+        long longValue = 32L;
+        Foo fooValue = new Foo();
+
+
+        public Bar( int ii )
+        {
+            intValue = ii;
+        }
+    }
+
+
+    static class Foo implements Serializable
+    {
+        float floatValue = 3;
+        String stringValue = "foo";
+        double doubleValue = 1.2;
+        byte byteValue = 3;
+        char charValue = 'a';
     }
 
 

Added: directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/DefaultMarshallerTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/DefaultMarshallerTest.java?rev=636959&view=auto
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/DefaultMarshallerTest.java
(added)
+++ directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/DefaultMarshallerTest.java
Thu Mar 13 19:30:52 2008
@@ -0,0 +1,78 @@
+/*
+ *  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.server.core.avltree;
+
+
+import org.junit.Test;
+import org.apache.directory.shared.ldap.util.ArrayUtils;
+import static junit.framework.Assert.*;
+
+import java.io.Serializable;
+
+
+/**
+ * Test case for the default Marshaller implementation.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $$Rev$$
+ */
+public class DefaultMarshallerTest
+{
+    DefaultMarshaller marshaller = DefaultMarshaller.INSTANCE;
+
+
+    @Test
+    public void testRoundTrip() throws Exception
+    {
+        byte[] serialized = marshaller.serialize( "test" );
+        Object deserialized = marshaller.deserialize( serialized );
+        assertEquals( "test", deserialized );
+        assertTrue( ArrayUtils.isEquals( serialized, marshaller.serialize( deserialized )
) );
+    }
+
+
+    @Test
+    public void testRoundTripComplex() throws Exception
+    {
+        byte[] serialized = marshaller.serialize( new Bar() );
+        Object deserialized = marshaller.deserialize( serialized );
+        assertNotNull( deserialized );
+        assertTrue( ArrayUtils.isEquals( serialized, marshaller.serialize( deserialized )
) );
+    }
+
+
+    static class Bar implements Serializable
+    {
+        int intValue = 37;
+        String stringValue = "bar";
+        long longValue = 32L;
+        Foo fooValue = new Foo();
+    }
+
+
+    static class Foo implements Serializable
+    {
+        float floatValue = 3;
+        String stringValue = "foo";
+        double doubleValue = 1.2;
+        byte byteValue = 3;
+        char charValue = 'a';
+    }
+}



Mime
View raw message