You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by se...@apache.org on 2010/08/28 22:04:26 UTC
svn commit: r990428 [2/3] - in
/directory/sandbox/studio-persistence-tooling: ./ persistence-core/
persistence-core/src/ persistence-core/src/main/
persistence-core/src/main/java/ persistence-core/src/main/java/org/
persistence-core/src/main/java/org/a...
Added: directory/sandbox/studio-persistence-tooling/persistence-core/src/main/resources/genericLdapDao_template.vm
URL: http://svn.apache.org/viewvc/directory/sandbox/studio-persistence-tooling/persistence-core/src/main/resources/genericLdapDao_template.vm?rev=990428&view=auto
==============================================================================
--- directory/sandbox/studio-persistence-tooling/persistence-core/src/main/resources/genericLdapDao_template.vm (added)
+++ directory/sandbox/studio-persistence-tooling/persistence-core/src/main/resources/genericLdapDao_template.vm Sat Aug 28 20:04:24 2010
@@ -0,0 +1,539 @@
+#*
+ * 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.
+ *
+ *#
+#set( $this = "Velocity" )
+#set( $dao = ".dao" )
+
+package $package$dao;
+
+
+import java.io.IOException;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.naming.InvalidNameException;
+import javax.naming.NamingException;
+
+import org.apache.directory.ldap.client.api.LdapConnection;
+import org.apache.directory.ldap.client.api.exception.LdapException;
+import org.apache.directory.ldap.client.api.message.ModifyRequest;
+import org.apache.directory.ldap.client.api.message.SearchResponse;
+import org.apache.directory.ldap.client.api.message.SearchResultEntry;
+import org.apache.directory.shared.ldap.cursor.Cursor;
+import org.apache.directory.shared.ldap.entry.Entry;
+import org.apache.directory.shared.ldap.entry.EntryAttribute;
+import org.apache.directory.shared.ldap.entry.ModificationOperation;
+import org.apache.directory.shared.ldap.entry.Value;
+import org.apache.directory.shared.ldap.entry.client.ClientModification;
+import org.apache.directory.shared.ldap.entry.client.DefaultClientAttribute;
+import org.apache.directory.shared.ldap.entry.client.DefaultClientEntry;
+import org.apache.directory.shared.ldap.filter.SearchScope;
+import org.apache.directory.shared.ldap.name.DN;
+import org.apache.directory.shared.ldap.util.GeneralizedTime;
+
+
+public abstract class GenericLdapDao<T>
+{
+
+ protected String server;
+ protected int port;
+ protected String name;
+ protected String credentials;
+ protected String rdnAttribute;
+ protected String parentDn;
+ protected String[] attributes;
+ protected String[] objectClasses;
+
+
+ public T getById( String id )
+ {
+ LdapConnection connection = null;
+ try
+ {
+ connection = getLdapConnection();
+ DN dn = new DN( rdnAttribute, id, parentDn );
+ Entry entry = lookupEntry( connection, dn.getName() );
+ if ( entry != null )
+ {
+ T object = mapFromEntry( entry );
+ return object;
+ }
+ else
+ {
+ return null;
+ }
+ }
+ catch ( Exception e )
+ {
+ throw new RuntimeException( e );
+ }
+ finally
+ {
+ returnLdapConnection( connection );
+ }
+ }
+
+
+ public Collection<T> getAll()
+ {
+ Collection<T> all = new ArrayList<T>();
+
+ LdapConnection connection = null;
+ try
+ {
+ connection = getLdapConnection();
+
+ String filter = "(&";
+ for ( String objectClass : objectClasses )
+ {
+ filter += "(objectClass=" + objectClass + ")";
+ }
+ filter += ")";
+
+ Cursor<SearchResponse> cursor = connection.search( parentDn, filter, SearchScope.ONELEVEL, attributes );
+ while ( cursor.next() )
+ {
+ SearchResponse response = cursor.get();
+ if ( response instanceof SearchResultEntry )
+ {
+ SearchResultEntry sre = ( SearchResultEntry ) response;
+ Entry entry = sre.getEntry();
+ T object = mapFromEntry( entry );
+ all.add( object );
+ }
+ // TODO: handle other response types
+ }
+ }
+ catch ( Exception e )
+ {
+ throw new RuntimeException( e );
+ }
+ finally
+ {
+ returnLdapConnection( connection );
+ }
+
+ return all;
+ }
+
+
+ public void create( T object )
+ {
+ LdapConnection connection = null;
+ try
+ {
+ connection = getLdapConnection();
+ Entry entry = getEntry( object );
+ connection.add( entry );
+ }
+ catch ( Exception e )
+ {
+ throw new RuntimeException( e );
+ }
+ finally
+ {
+ returnLdapConnection( connection );
+ }
+ }
+
+
+ public void update( T object )
+ {
+ LdapConnection connection = null;
+ try
+ {
+ connection = getLdapConnection();
+
+ // get old and new entry
+ Entry newEntry = getEntry( object );
+ Entry oldEntry = lookupEntry( connection, newEntry.getDn().getName() );
+
+ // generate diff
+ ModifyRequest modRequest = new ModifyRequest( newEntry.getDn() );
+ for ( EntryAttribute newAttribute : newEntry )
+ {
+ if ( oldEntry.contains( newAttribute ) )
+ {
+ EntryAttribute oldAttribute = oldEntry.get( newAttribute.getId() );
+ if ( !oldAttribute.equals( newAttribute ) )
+ {
+ modRequest.addModification( newAttribute, ModificationOperation.REPLACE_ATTRIBUTE );
+ }
+ }
+ else
+ {
+ modRequest.addModification( newAttribute, ModificationOperation.REPLACE_ATTRIBUTE );
+ }
+ }
+ for ( EntryAttribute oldAttribute : oldEntry )
+ {
+ if ( !newEntry.contains( oldAttribute ) )
+ {
+ modRequest.addModification( new ClientModification( ModificationOperation.REPLACE_ATTRIBUTE,
+ new DefaultClientAttribute( oldAttribute.getId() ) ) );
+ }
+ }
+
+ connection.modify( modRequest );
+ }
+ catch ( Exception e )
+ {
+ throw new RuntimeException( e );
+ }
+ finally
+ {
+ returnLdapConnection( connection );
+ }
+ }
+
+
+ public void delete( T object )
+ {
+ LdapConnection connection = null;
+ try
+ {
+ connection = getLdapConnection();
+ Entry entry = getEntry( object );
+ connection.delete( entry.getDn() );
+ }
+ catch ( Exception e )
+ {
+ throw new RuntimeException( e );
+ }
+ finally
+ {
+ returnLdapConnection( connection );
+ }
+ }
+
+
+ private Entry getEntry( T object ) throws InvalidNameException, NamingException
+ {
+ Entry entry = new DefaultClientEntry();
+
+ // map object to entry first, because this fills the RDN attribute
+ mapToEntry( object, entry );
+
+ DN dn = new DN( rdnAttribute, getString( entry, rdnAttribute ), parentDn );
+ entry.setDn( dn );
+
+ entry.add( "objectClass", objectClasses );
+
+ return entry;
+ }
+
+
+ private Entry lookupEntry( LdapConnection connection, String dn ) throws LdapException
+ {
+ SearchResponse response = connection.lookup( dn, attributes );
+ if ( response instanceof SearchResultEntry )
+ {
+ SearchResultEntry sre = ( SearchResultEntry ) response;
+ Entry entry = sre.getEntry();
+ return entry;
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+
+ protected abstract T mapFromEntry( Entry entry ) throws ParseException;
+
+
+ protected abstract void mapToEntry( T object, Entry entry ) throws NamingException;
+
+
+ protected byte[] getBytes( Entry entry, String attribute )
+ {
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ return entryAttribute.get().getBytes();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+
+ protected void setBytes( Entry entry, String attribute, byte[] value ) throws NamingException
+ {
+ if ( value != null )
+ {
+ entry.add( attribute, value );
+ }
+ }
+
+
+ protected Integer getInt( Entry entry, String attribute )
+ {
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ return Integer.parseInt( entryAttribute.get().getString() );
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+
+ protected void setInt( Entry entry, String attribute, Integer value ) throws NamingException
+ {
+ if ( value != null )
+ {
+ entry.add( attribute, value.toString() );
+ }
+ }
+
+
+ protected void setCalendar( Entry entry, String attribute, Calendar value ) throws NamingException
+ {
+ if ( value != null )
+ {
+ entry.add( attribute, new GeneralizedTime( value ).toGeneralizedTime() );
+ }
+ }
+
+
+ protected Calendar getCalendar( Entry entry, String attribute ) throws ParseException
+ {
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ return new GeneralizedTime( attribute ).getCalendar();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+
+ protected Long getLong( Entry entry, String attribute )
+ {
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ return Long.parseLong( entryAttribute.get().getString() );
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+
+ protected void setLong( Entry entry, String attribute, Long value ) throws NamingException
+ {
+ if ( value != null )
+ {
+ entry.add( attribute, value.toString() );
+ }
+ }
+
+
+ protected String getString( Entry entry, String attribute )
+ {
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ return entryAttribute.get().getString();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+
+ protected void setString( Entry entry, String attribute, String value ) throws NamingException
+ {
+ if ( value != null )
+ {
+ entry.add( attribute, value.toString() );
+ }
+ }
+
+
+ protected void setBoolean( Entry entry, String attribute, Boolean value ) throws NamingException
+ {
+ if ( value != null )
+ {
+ entry.add( attribute, "TRUE" );
+ }
+ else
+ {
+ entry.add( attribute, "FALSE" );
+ }
+ }
+
+
+ protected Boolean getBoolean( Entry entry, String attribute )
+ {
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ return Boolean.parseBoolean( attribute );
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+
+ protected Set<String> getStringSet( Entry entry, String attribute )
+ {
+ Set<String> values = new HashSet<String>();
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ for ( Value<?> value : entryAttribute )
+ {
+ values.add( value.getString() );
+ }
+ }
+ return values;
+ }
+
+
+ protected Set<Integer> getIntegerSet( Entry entry, String attribute )
+ {
+ Set<Integer> values = new HashSet<Integer>();
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ for ( Value<?> value : entryAttribute )
+ {
+ values.add( Integer.parseInt( value.getString() ) );
+ }
+ }
+ return values;
+ }
+
+
+ protected Set<Long> getLongSet( Entry entry, String attribute )
+ {
+ Set<Long> values = new HashSet<Long>();
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ for ( Value<?> value : entryAttribute )
+ {
+ values.add( Long.parseLong( value.getString() ) );
+ }
+ }
+ return values;
+ }
+
+
+ protected Set<Boolean> getBooleanSet( Entry entry, String attribute )
+ {
+ Set<Boolean> values = new HashSet<Boolean>();
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ for ( Value<?> value : entryAttribute )
+ {
+ values.add( Boolean.parseBoolean( value.getString() ) );
+ }
+ }
+ return values;
+ }
+
+
+ protected Set<Calendar> getCalendarSet( Entry entry, String attribute ) throws ParseException
+ {
+ Set<Calendar> values = new HashSet<Calendar>();
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ for ( Value<?> value : entryAttribute )
+ {
+ values.add( new GeneralizedTime( value.getString() ).getCalendar() );
+ }
+ }
+ return values;
+ }
+
+
+ protected Set<byte[]> getByteArraySet( Entry entry, String attribute )
+ {
+ Set<byte[]> values = new HashSet<byte[]>();
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ for ( Value<?> value : entryAttribute )
+ {
+ values.add( value.getBytes() );
+ }
+ }
+ return values;
+ }
+
+
+ protected LdapConnection getLdapConnection()
+ {
+ try
+ {
+ LdapConnection connection = new LdapConnection( server, port );
+ connection.bind( name, credentials );
+ return connection;
+ }
+ catch ( LdapException e )
+ {
+ throw new RuntimeException( e );
+ }
+ catch ( IOException e )
+ {
+ throw new RuntimeException( e );
+ }
+ }
+
+
+ protected void returnLdapConnection( LdapConnection connection )
+ {
+ if ( connection != null )
+ {
+ try
+ {
+ connection.unBind();
+ connection.close();
+ }
+ catch ( IOException e )
+ {
+ throw new RuntimeException( e );
+ }
+ catch ( Exception e )
+ {
+ throw new RuntimeException( e );
+ }
+ }
+ }
+}
Added: directory/sandbox/studio-persistence-tooling/persistence-core/src/main/resources/java_template.vm
URL: http://svn.apache.org/viewvc/directory/sandbox/studio-persistence-tooling/persistence-core/src/main/resources/java_template.vm?rev=990428&view=auto
==============================================================================
--- directory/sandbox/studio-persistence-tooling/persistence-core/src/main/resources/java_template.vm (added)
+++ directory/sandbox/studio-persistence-tooling/persistence-core/src/main/resources/java_template.vm Sat Aug 28 20:04:24 2010
@@ -0,0 +1,62 @@
+#*
+ * 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.
+ *
+ *#
+
+#set( $this = "Velocity")
+#set( $boolean = "boolean")
+
+package $package;
+
+#foreach( $import in $imports )
+import $import;
+#end
+
+public class $utility.firstToUpperCase($class)
+{
+
+#foreach( $entryPro in $entryProps )
+ private $entryPro.WrittenJavaType $entryPro.JavaMemberName;
+#end
+
+ public $utility.firstToUpperCase($class)()
+ {
+ }
+
+#foreach( $entryPro in $entryProps )
+#if($entryPro.WrittenJavaType == $boolean)
+ public $entryPro.WrittenJavaType is$utility.firstToUpperCase($entryPro.JavaMemberName)()
+ {
+ return $entryPro.JavaMemberName;
+ }
+
+#else
+ public $entryPro.WrittenJavaType get$utility.firstToUpperCase($entryPro.JavaMemberName)()
+ {
+ return $entryPro.JavaMemberName;
+ }
+
+#end
+ public void set$utility.firstToUpperCase($entryPro.JavaMemberName)($entryPro.WrittenJavaType $entryPro.JavaMemberName)
+ {
+ this.$entryPro.JavaMemberName = $entryPro.JavaMemberName;
+ }
+
+#end
+}
Added: directory/sandbox/studio-persistence-tooling/persistence-core/src/main/resources/velocity.properties
URL: http://svn.apache.org/viewvc/directory/sandbox/studio-persistence-tooling/persistence-core/src/main/resources/velocity.properties?rev=990428&view=auto
==============================================================================
--- directory/sandbox/studio-persistence-tooling/persistence-core/src/main/resources/velocity.properties (added)
+++ directory/sandbox/studio-persistence-tooling/persistence-core/src/main/resources/velocity.properties Sat Aug 28 20:04:24 2010
@@ -0,0 +1,22 @@
+# 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.
+runtime.log = velocity_example.log
+
+resource.loader = class
+class.resource.loader.description = Velocity Classpath Resource Loader
+class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
+
Added: directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/CodeGenIntegrationTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/CodeGenIntegrationTest.java?rev=990428&view=auto
==============================================================================
--- directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/CodeGenIntegrationTest.java (added)
+++ directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/CodeGenIntegrationTest.java Sat Aug 28 20:04:24 2010
@@ -0,0 +1,130 @@
+/*
+ * 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.studio.persistence.core;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.BufferedReader;
+import java.io.DataInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+
+
+public class CodeGenIntegrationTest
+{
+ private CodeGenerator codeGenerator;
+ private CodeGeneratorContext generatorContext;
+
+ private String packageName;
+ private String projectFolderPath;
+ private String[] objectClasses;
+ private List<EntryProperties> entryProperties;
+
+ private EntryProperties entryProperties_0;
+ private EntryProperties entryProperties_1;
+
+
+ @Before
+ public void setUp()
+ {
+ packageName = "com.test";
+ projectFolderPath = "IntegrationTest/";
+ objectClasses = new String[]
+ { "inetOrgPerson", "organizationalPerson", "person", "top" };
+
+ entryProperties_0 = new EntryProperties();
+ entryProperties_0.setCardinality( Cardinality.ONE );
+ entryProperties_0.setGen( true );
+ entryProperties_0.setJavaMemberName( "member_0" );
+ entryProperties_0.setJavaType( JavaType.STRING );
+ entryProperties_0.setLdapAttributeName( "attrib_0" );
+
+ entryProperties_1 = new EntryProperties();
+ entryProperties_1.setCardinality( Cardinality.ONE_OR_MANY_SET );
+ entryProperties_1.setGen( true );
+ entryProperties_1.setJavaMemberName( "member_1" );
+ entryProperties_1.setJavaType( JavaType.STRING );
+ entryProperties_1.setLdapAttributeName( "attrib_1" );
+
+ //adding EntryProperties object to List<EntryProperties>
+ entryProperties = new ArrayList<EntryProperties>();
+ entryProperties.add( entryProperties_0 );
+ entryProperties.add( entryProperties_1 );
+
+ //setting up CodeGeneratorContext object
+ generatorContext = new CodeGeneratorContext();
+ generatorContext.setClassName( "TesterClass" );
+ generatorContext.setPackageName( packageName );
+ generatorContext.setProjectFolderPath( projectFolderPath );
+ generatorContext.setServer( "localhost" );
+ generatorContext.setPort( 123 );
+ generatorContext.setBindDn( "uid=admin,ou=system" );
+ generatorContext.setBindPassword( "mypass" );
+ generatorContext.setRdnAttribute( "uid" );
+ generatorContext.setParentDn( "ou=users,dc=example,dc=com" );
+ generatorContext.setObjectClasses( objectClasses );
+
+ generatorContext.setEntryProperties( entryProperties );
+
+ codeGenerator = new CodeGenerator();
+
+ }
+
+
+ @Test
+ public void integrationTest() throws IOException
+ {
+ codeGenerator.generateCode( generatorContext, "java_template.vm" );
+ codeGenerator.generateDaoCode( generatorContext, "dao_template.vm" );
+ codeGenerator.generateGenericDaoCode( generatorContext, "genericLdapDao_template.vm" );
+ String daoFileName = "IntegrationTest/com/test/dao/TesterClassDao.java";
+ String domainFileName = "IntegrationTest/com/test/TesterClass.java";
+ assertTrue( new File( daoFileName ).exists() );
+ assertTrue( new File( domainFileName ).exists() );
+ assertCode( daoFileName, "public class TesterClassDao extends GenericLdapDao<TesterClass>", 15 );
+ assertCode( daoFileName, " TesterClass attribute = new TesterClass();", 34 );
+ assertCode( daoFileName, " for ( String val : attribute.getMember_1() )", 43 );
+ assertCode( domainFileName, " private Set<String> member_1;", 11 );
+ }
+
+
+ public void assertCode( String fileName, String expectedCodeLine, int lineNumber ) throws IOException
+ {
+ FileInputStream fstream = new FileInputStream( fileName );
+ DataInputStream in = new DataInputStream( fstream );
+ BufferedReader br = new BufferedReader( new InputStreamReader( in ) );
+ for ( int i = 0; i < lineNumber; ++i )
+ br.readLine();
+ String actualCodeLine = br.readLine();
+ assertEquals( expectedCodeLine, actualCodeLine );
+ in.close();
+ }
+}
Added: directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/CodeGeneratorTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/CodeGeneratorTest.java?rev=990428&view=auto
==============================================================================
--- directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/CodeGeneratorTest.java (added)
+++ directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/CodeGeneratorTest.java Sat Aug 28 20:04:24 2010
@@ -0,0 +1,297 @@
+/*
+ * 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.studio.persistence.core;
+
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.directory.studio.persistence.core.Cardinality;
+import org.apache.directory.studio.persistence.core.CodeGenerator;
+import org.apache.directory.studio.persistence.core.CodeGeneratorContext;
+import org.apache.directory.studio.persistence.core.EntryProperties;
+import org.apache.directory.studio.persistence.core.JavaType;
+
+import org.junit.Before;
+import org.junit.Test;
+
+
+public class CodeGeneratorTest
+{
+ private CodeGenerator codeGenerator;
+ private CodeGeneratorContext generatorContext;
+
+ private String className;
+ private String packageName;
+ private List<EntryProperties> entryProperties;
+
+ private EntryProperties entryProperties_0;
+ private EntryProperties entryProperties_1;
+ private EntryProperties entryProperties_2;
+ private EntryProperties entryProperties_3;
+ private EntryProperties entryProperties_4;
+ private EntryProperties entryProperties_5;
+ private EntryProperties entryProperties_6;
+ private EntryProperties entryProperties_7;
+ private EntryProperties entryProperties_8;
+ private EntryProperties entryProperties_9;
+ private EntryProperties entryProperties_10;
+ private EntryProperties entryProperties_11;
+
+
+ @Before
+ public void setUp()
+ {
+ className = "CodeGenTest";
+ packageName = "com.apache.directory.studio.persistence.core";
+
+ //EntryProperties object with String JavaType and Cardinality ONE
+ entryProperties_0 = new EntryProperties();
+ entryProperties_0.setCardinality( Cardinality.ONE );
+ entryProperties_0.setGen( true );
+ entryProperties_0.setJavaMemberName( "member_0" );
+ entryProperties_0.setJavaType( JavaType.STRING );
+ entryProperties_0.setLdapAttributeName( "attrib_0" );
+
+ //EntryProperties object with CALENDAR JavaType and Cardinality ZERO_OR_MANY_ARRAY
+ entryProperties_1 = new EntryProperties();
+ entryProperties_1.setCardinality( Cardinality.ZERO_OR_MANY_ARRAY );
+ entryProperties_1.setGen( true );
+ entryProperties_1.setJavaMemberName( "member_0" );
+ entryProperties_1.setJavaType( JavaType.CALENDAR );
+ entryProperties_1.setLdapAttributeName( "attrib_0" );
+
+ //EntryProperties object with CALENDAR JavaType and Cardinality ZERO_OR_MANY_SET
+ entryProperties_2 = new EntryProperties();
+ entryProperties_2.setCardinality( Cardinality.ZERO_OR_MANY_SET );
+ entryProperties_2.setGen( true );
+ entryProperties_2.setJavaMemberName( "member_0" );
+ entryProperties_2.setJavaType( JavaType.CALENDAR );
+ entryProperties_2.setLdapAttributeName( "attrib_0" );
+
+ //EntryProperties object with STRING JavaType and Cardinality ZERO_OR_MANY_LIST
+ entryProperties_3 = new EntryProperties();
+ entryProperties_3.setCardinality( Cardinality.ZERO_OR_MANY_LIST );
+ entryProperties_3.setGen( true );
+ entryProperties_3.setJavaMemberName( "member_0" );
+ entryProperties_3.setJavaType( JavaType.STRING );
+ entryProperties_3.setLdapAttributeName( "attrib_0" );
+
+ //EntryProperties object with INT JavaType and Cardinality ZERO_OR_MANY_SET
+ entryProperties_4 = new EntryProperties();
+ entryProperties_4.setCardinality( Cardinality.ZERO_OR_MANY_SET );
+ entryProperties_4.setGen( true );
+ entryProperties_4.setJavaMemberName( "member_0" );
+ entryProperties_4.setJavaType( JavaType.INT );
+ entryProperties_4.setLdapAttributeName( "attrib_0" );
+
+ //EntryProperties object with LONG JavaType and Cardinality ZERO_OR_MANY_SET
+ entryProperties_5 = new EntryProperties();
+ entryProperties_5.setCardinality( Cardinality.ZERO_OR_MANY_SET );
+ entryProperties_5.setGen( true );
+ entryProperties_5.setJavaMemberName( "member_0" );
+ entryProperties_5.setJavaType( JavaType.LONG );
+ entryProperties_5.setLdapAttributeName( "attrib_0" );
+
+ //EntryProperties object with BOOLEAN JavaType and Cardinality ZERO_OR_MANY_SET
+ entryProperties_6 = new EntryProperties();
+ entryProperties_6.setCardinality( Cardinality.ZERO_OR_MANY_SET );
+ entryProperties_6.setGen( true );
+ entryProperties_6.setJavaMemberName( "member_0" );
+ entryProperties_6.setJavaType( JavaType.BOOLEAN );
+ entryProperties_6.setLdapAttributeName( "attrib_0" );
+
+ //EntryProperties object with BYTE_ARRAY JavaType and Cardinality ZERO_OR_MANY_SET
+ entryProperties_7 = new EntryProperties();
+ entryProperties_7.setCardinality( Cardinality.ZERO_OR_MANY_SET );
+ entryProperties_7.setGen( true );
+ entryProperties_7.setJavaMemberName( "member_0" );
+ entryProperties_7.setJavaType( JavaType.BYTE_ARRAY );
+ entryProperties_7.setLdapAttributeName( "attrib_0" );
+
+ //EntryProperties object with LONG JavaType and Cardinality ZERO_OR_MANY_LIST
+ entryProperties_8 = new EntryProperties();
+ entryProperties_8.setCardinality( Cardinality.ZERO_OR_MANY_LIST );
+ entryProperties_8.setGen( true );
+ entryProperties_8.setJavaMemberName( "member_0" );
+ entryProperties_8.setJavaType( JavaType.LONG );
+ entryProperties_8.setLdapAttributeName( "attrib_0" );
+
+ //EntryProperties object with BOOLEAN JavaType and Cardinality ZERO_OR_MANY_LIST
+ entryProperties_9 = new EntryProperties();
+ entryProperties_9.setCardinality( Cardinality.ZERO_OR_MANY_LIST );
+ entryProperties_9.setGen( true );
+ entryProperties_9.setJavaMemberName( "member_0" );
+ entryProperties_9.setJavaType( JavaType.BOOLEAN );
+ entryProperties_9.setLdapAttributeName( "attrib_0" );
+
+ //EntryProperties object with BYTE_ARRAY JavaType and Cardinality ZERO_OR_MANY_LIST
+ entryProperties_10 = new EntryProperties();
+ entryProperties_10.setCardinality( Cardinality.ZERO_OR_MANY_LIST );
+ entryProperties_10.setGen( true );
+ entryProperties_10.setJavaMemberName( "member_0" );
+ entryProperties_10.setJavaType( JavaType.BYTE_ARRAY );
+ entryProperties_10.setLdapAttributeName( "attrib_0" );
+
+ //EntryProperties object with INT JavaType and Cardinality ZERO_OR_MANY_LIST
+ entryProperties_11 = new EntryProperties();
+ entryProperties_11.setCardinality( Cardinality.ZERO_OR_MANY_LIST );
+ entryProperties_11.setGen( true );
+ entryProperties_11.setJavaMemberName( "member_0" );
+ entryProperties_11.setJavaType( JavaType.INT );
+ entryProperties_11.setLdapAttributeName( "attrib_0" );
+
+ //adding EntryProperties objects to List<EntryProperties>
+ entryProperties = new ArrayList<EntryProperties>();
+ entryProperties.add( entryProperties_0 );
+ entryProperties.add( entryProperties_1 );
+ entryProperties.add( entryProperties_2 );
+ entryProperties.add( entryProperties_3 );
+ entryProperties.add( entryProperties_4 );
+ entryProperties.add( entryProperties_5 );
+ entryProperties.add( entryProperties_6 );
+ entryProperties.add( entryProperties_7 );
+ entryProperties.add( entryProperties_8 );
+ entryProperties.add( entryProperties_9 );
+ entryProperties.add( entryProperties_10 );
+ entryProperties.add( entryProperties_11 );
+
+ //setting up CodeGeneratorContext object
+ generatorContext = new CodeGeneratorContext();
+ generatorContext.setClassName( className );
+ generatorContext.setPackageName( packageName );
+ generatorContext.setEntryProperties( entryProperties );
+
+ codeGenerator = new CodeGenerator();
+
+ }
+
+
+ @Test
+ public void testGenerateClassTemplate_0()
+ {
+ List<EntryProperties> properties = codeGenerator.generateClassTemplate( generatorContext );
+ String actualWrittenJavaType = properties.get( 0 ).getWrittenJavaType();
+ assertEquals( "String", actualWrittenJavaType );
+ }
+
+
+ @Test
+ public void testGenerateClassTemplate_1()
+ {
+ List<EntryProperties> properties = codeGenerator.generateClassTemplate( generatorContext );
+ String actualWrittenJavaType = properties.get( 1 ).getWrittenJavaType();
+ assertEquals( "java.util.Calendar[]", actualWrittenJavaType );
+ }
+
+
+ @Test
+ public void testGenerateClassTemplate_2()
+ {
+ List<EntryProperties> properties = codeGenerator.generateClassTemplate( generatorContext );
+ String actualWrittenJavaType = properties.get( 2 ).getWrittenJavaType();
+ assertEquals( "Set<java.util.Calendar>", actualWrittenJavaType );
+ }
+
+
+ @Test
+ public void testGenerateClassTemplate_3()
+ {
+ List<EntryProperties> properties = codeGenerator.generateClassTemplate( generatorContext );
+ String actualWrittenJavaType = properties.get( 3 ).getWrittenJavaType();
+ assertEquals( "List<String>", actualWrittenJavaType );
+ }
+
+
+ @Test
+ public void testGenerateClassTemplate_4()
+ {
+ List<EntryProperties> properties = codeGenerator.generateClassTemplate( generatorContext );
+ String actualWrittenJavaType = properties.get( 4 ).getWrittenJavaType();
+ assertEquals( "Set<Integer>", actualWrittenJavaType );
+ }
+
+
+ @Test
+ public void testGenerateClassTemplate_5()
+ {
+ List<EntryProperties> properties = codeGenerator.generateClassTemplate( generatorContext );
+ String actualWrittenJavaType = properties.get( 5 ).getWrittenJavaType();
+ assertEquals( "Set<Long>", actualWrittenJavaType );
+ }
+
+
+ @Test
+ public void testGenerateClassTemplate_6()
+ {
+ List<EntryProperties> properties = codeGenerator.generateClassTemplate( generatorContext );
+ String actualWrittenJavaType = properties.get( 6 ).getWrittenJavaType();
+ assertEquals( "Set<Boolean>", actualWrittenJavaType );
+ }
+
+
+ @Test
+ public void testGenerateClassTemplate_7()
+ {
+ List<EntryProperties> properties = codeGenerator.generateClassTemplate( generatorContext );
+ String actualWrittenJavaType = properties.get( 7 ).getWrittenJavaType();
+ assertEquals( "Set<byte[]>", actualWrittenJavaType );
+ }
+
+
+ @Test
+ public void testGenerateClassTemplate_8()
+ {
+ List<EntryProperties> properties = codeGenerator.generateClassTemplate( generatorContext );
+ String actualWrittenJavaType = properties.get( 8 ).getWrittenJavaType();
+ assertEquals( "List<Long>", actualWrittenJavaType );
+ }
+
+
+ @Test
+ public void testGenerateClassTemplate_9()
+ {
+ List<EntryProperties> properties = codeGenerator.generateClassTemplate( generatorContext );
+ String actualWrittenJavaType = properties.get( 9 ).getWrittenJavaType();
+ assertEquals( "List<Boolean>", actualWrittenJavaType );
+ }
+
+
+ @Test
+ public void testGenerateClassTemplate_10()
+ {
+ List<EntryProperties> properties = codeGenerator.generateClassTemplate( generatorContext );
+ String actualWrittenJavaType = properties.get( 10 ).getWrittenJavaType();
+ assertEquals( "List<Byte[]>", actualWrittenJavaType );
+ }
+
+
+ @Test
+ public void testGenerateClassTemplate_11()
+ {
+ List<EntryProperties> properties = codeGenerator.generateClassTemplate( generatorContext );
+ String actualWrittenJavaType = properties.get( 11 ).getWrittenJavaType();
+ assertEquals( "List<Integer>", actualWrittenJavaType );
+ }
+}
\ No newline at end of file
Added: directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/JavaUtilsTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/JavaUtilsTest.java?rev=990428&view=auto
==============================================================================
--- directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/JavaUtilsTest.java (added)
+++ directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/JavaUtilsTest.java Sat Aug 28 20:04:24 2010
@@ -0,0 +1,99 @@
+package org.apache.directory.studio.persistence.core;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+
+
+public class JavaUtilsTest
+{
+ private List<String> sampleJavaIdentifiers;
+
+
+ @Before
+ public void setUp()
+ {
+ sampleJavaIdentifiers = new ArrayList<String>();
+ // add some String values to the list
+ sampleJavaIdentifiers.add( "speed" );
+ sampleJavaIdentifiers.add( "cost" );
+ sampleJavaIdentifiers.add( "mass" );
+ sampleJavaIdentifiers.add( "volume" );
+ sampleJavaIdentifiers.add( "weight" );
+ sampleJavaIdentifiers.add( "balnce" );
+ }
+
+
+ @Test
+ public void testgetValidValue_0()
+ {
+ String actualValidValue = JavaUtils.getValidValue( "" );
+ assertEquals( "", actualValidValue );
+ }
+
+
+ @Test
+ public void testgetValidValue_1()
+ {
+ String actualValidValue = JavaUtils.getValidValue( "*" );
+ assertEquals( "", actualValidValue );
+ }
+
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testgetValidValue_2()
+ {
+ JavaUtils.getValidValue( null );
+ }
+
+
+ @Test
+ public void testgetValidValue_3()
+ {
+ String actualValidValue = JavaUtils.getValidValue( "~test" );
+ assertEquals( "test", actualValidValue );
+ }
+
+
+ @Test
+ public void testgetValidValue_4()
+ {
+ String actualValidValue = JavaUtils.getValidValue( "~!#te@#%st^&+" );
+ assertEquals( "test", actualValidValue );
+ }
+
+
+ @Test
+ public void testIsKeyword_0()
+ {
+ assertTrue( JavaUtils.isKeyword( "int" ) );
+ }
+
+
+ @Test
+ public void testIsKeyword_1()
+ {
+ assertFalse( JavaUtils.isKeyword( "String" ) );
+ }
+
+
+ @Test
+ public void testIsCollide_0()
+ {
+ assertTrue( JavaUtils.isCollide( sampleJavaIdentifiers, "speed" ) );
+ }
+
+
+ @Test
+ public void testIsCollide_1()
+ {
+ assertFalse( JavaUtils.isCollide( sampleJavaIdentifiers, "temperature" ) );
+ }
+}
Added: directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/LdapEntryAnalyzerTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/LdapEntryAnalyzerTest.java?rev=990428&view=auto
==============================================================================
--- directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/LdapEntryAnalyzerTest.java (added)
+++ directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/LdapEntryAnalyzerTest.java Sat Aug 28 20:04:24 2010
@@ -0,0 +1,308 @@
+/*
+ * 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.studio.persistence.core;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.powermock.api.mockito.PowerMockito.mock;
+import static org.powermock.api.mockito.PowerMockito.mockStatic;
+import static org.powermock.api.mockito.PowerMockito.when;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.naming.InvalidNameException;
+
+import org.apache.directory.shared.ldap.constants.SchemaConstants;
+import org.apache.directory.shared.ldap.name.LdapDN;
+import org.apache.directory.shared.ldap.name.Rdn;
+import org.apache.directory.shared.ldap.schema.parsers.AttributeTypeDescription;
+import org.apache.directory.shared.ldap.schema.parsers.ObjectClassDescription;
+import org.apache.directory.studio.connection.core.Connection;
+import org.apache.directory.studio.ldapbrowser.core.BrowserCorePlugin;
+import org.apache.directory.studio.ldapbrowser.core.BrowserCorePreferences;
+import org.apache.directory.studio.ldapbrowser.core.model.IBrowserConnection;
+import org.apache.directory.studio.ldapbrowser.core.model.IEntry;
+import org.apache.directory.studio.ldapbrowser.core.model.schema.Schema;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+
+/**
+ * TODO LdapEntryAnalyzerTest.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(BrowserCorePlugin.class)
+public class LdapEntryAnalyzerTest
+{
+
+ @Before
+ public void setUp()
+ {
+ // setup BrowserCorePreferences mock
+ mockStatic( BrowserCorePlugin.class );
+ BrowserCorePlugin mockPlugin = mock( BrowserCorePlugin.class );
+ when( BrowserCorePlugin.getDefault() ).thenReturn( mockPlugin );
+ BrowserCorePreferences mockPreferences = mock( BrowserCorePreferences.class );
+ when( mockPlugin.getCorePreferences() ).thenReturn( mockPreferences );
+ Set<String> binarySyntaxes = new HashSet<String>();
+ binarySyntaxes.add( SchemaConstants.BINARY_SYNTAX );
+ binarySyntaxes.add( SchemaConstants.JPEG_SYNTAX );
+ binarySyntaxes.add( SchemaConstants.OCTET_STRING_SYNTAX );
+ Set<String> binaryAttributes = new HashSet<String>();
+ binaryAttributes.add( SchemaConstants.USER_PASSWORD_AT );
+ binaryAttributes.add( SchemaConstants.USER_PASSWORD_AT_OID );
+ when( mockPreferences.getUpperCasedBinarySyntaxOids() ).thenReturn( binarySyntaxes );
+ when( mockPreferences.getUpperCasedBinaryAttributeOidsAndNames() ).thenReturn( binaryAttributes );
+ }
+
+
+ @Test
+ public void testFirstToUpperCase()
+ {
+ String actualTestValue = LdapEntryAnalyzer.firstToUpperCase( "testValue" );
+ assertEquals( "TestValue", actualTestValue );
+ }
+
+
+ @Test
+ public void testGetClassName_1()
+ {
+ ObjectClassDescription ocd = Schema.DEFAULT_SCHEMA.getObjectClassDescription( "inetOrgPerson" );
+ String actualTestValue = LdapEntryAnalyzer.getClassName( ocd );
+ assertEquals( "InetOrgPerson", actualTestValue );
+ }
+
+
+ @Test
+ public void testGetClassName_3()
+ {
+ ObjectClassDescription ocd = Schema.DEFAULT_SCHEMA.getObjectClassDescription( "RFC822localPart" );
+ String actualTestValue = LdapEntryAnalyzer.getClassName( ocd );
+ assertEquals( "RFC822localPart", actualTestValue );
+ }
+
+
+ @Test
+ public void testGetClassName_2()
+ {
+ // expect non java identifiers (e.g.e the "dash") is removed and the first character is capitalized
+ ObjectClassDescription ocd = Schema.DEFAULT_SCHEMA.getObjectClassDescription( "certificationAuthority-V2" );
+ String actualTestValue = LdapEntryAnalyzer.getClassName( ocd );
+ assertEquals( "CertificationAuthorityV2", actualTestValue );
+ }
+
+
+ @Test
+ public void testComputePackageName_0()
+ {
+ String actualPackageName = LdapEntryAnalyzer.computePackageName( LdapDN.EMPTY_LDAPDN );
+ assertEquals( "", actualPackageName );
+ }
+
+
+ @Test
+ public void testComputePackageName_1() throws InvalidNameException
+ {
+ LdapDN dn = new LdapDN( "ou=system" );
+ String actualPackageName = LdapEntryAnalyzer.computePackageName( dn );
+ assertEquals( "", actualPackageName );
+ }
+
+
+ @Test
+ public void testComputePackageName_2() throws InvalidNameException
+ {
+ LdapDN dn = new LdapDN( "uid=admin,ou=system" );
+ String actualPackageName = LdapEntryAnalyzer.computePackageName( dn );
+ assertEquals( "system", actualPackageName );
+ }
+
+
+ @Test
+ public void testComputePackageName_3() throws InvalidNameException
+ {
+ LdapDN dn = new LdapDN( "cn=Foo Bar , dc=example , dc=com" );
+ String actualPackageName = LdapEntryAnalyzer.computePackageName( dn );
+ assertEquals( "com.example", actualPackageName );
+ }
+
+
+ @Test
+ public void testComputePackageName_4() throws InvalidNameException
+ {
+ // expect that non java identifiers are removed and the package name is lower cased
+ LdapDN dn = new LdapDN( " cn = Foo Bar , ou = External Users , dc = example , dc = com " );
+ String actualPackageName = LdapEntryAnalyzer.computePackageName( dn );
+ assertEquals( "com.example.externalusers", actualPackageName );
+ }
+
+
+ @Test
+ public void testAnalyze_1() throws InvalidNameException
+ {
+ // setup an entry
+ Connection connection = mock( Connection.class );
+ when( connection.getPort() ).thenReturn( 389 );
+ when( connection.getHost() ).thenReturn( "ldap.example.com" );
+ when( connection.getBindPrincipal() ).thenReturn( "uid=admin,dc=example,dc=com" );
+ when( connection.getBindPassword() ).thenReturn( "test1234" );
+
+ IBrowserConnection browserConnection = mock( IBrowserConnection.class );
+ when( browserConnection.getConnection() ).thenReturn( connection );
+ when( browserConnection.getSchema() ).thenReturn( Schema.DEFAULT_SCHEMA );
+
+ IEntry entry = mock( IEntry.class );
+ when( entry.getBrowserConnection() ).thenReturn( browserConnection );
+ when( entry.getDn() ).thenReturn( new LdapDN( "uid=foobar,ou=users,dc=example,dc=com" ) );
+ when( entry.getRdn() ).thenReturn( new Rdn( "uid=foobar" ) );
+
+ Collection<ObjectClassDescription> ocds = new HashSet<ObjectClassDescription>();
+ ocds.add( Schema.DEFAULT_SCHEMA.getObjectClassDescription( "top" ) );
+ ocds.add( Schema.DEFAULT_SCHEMA.getObjectClassDescription( "person" ) );
+ ocds.add( Schema.DEFAULT_SCHEMA.getObjectClassDescription( "organizationalPerson" ) );
+ ocds.add( Schema.DEFAULT_SCHEMA.getObjectClassDescription( "inetOrgPerson" ) );
+ when( entry.getObjectClassDescriptions() ).thenReturn( ocds );
+
+ // analyze the entry
+ LdapEntryAnalyzer analyzer = new LdapEntryAnalyzer();
+ CodeGeneratorContext ctx = analyzer.analyze( entry );
+
+ // check the result
+ assertEquals( "com.example.users", ctx.getPackageName() );
+ assertEquals( "InetOrgPerson", ctx.getClassName() );
+ assertEquals( 4, ctx.getObjectClasses().length );
+ assertEquals( "uid", ctx.getRdnAttribute() );
+
+ assertEquals( "uid=admin,dc=example,dc=com", ctx.getBindDn() );
+ assertEquals( "test1234", ctx.getBindPassword() );
+ assertEquals( "ldap.example.com", ctx.getServer() );
+ assertEquals( 389, ctx.getPort() );
+
+ List<EntryProperties> properties = ctx.getEntryProperties();
+ assertNotNull( properties );
+ assertEquals( 50, properties.size() ); // inetOrgPerson object class chain defines 51 attributes
+ for ( EntryProperties property : properties )
+ {
+ // TODO: assert properties
+ //assertTrue(property.isGen());
+ //property.getJavaType();
+ //assertTrue(property.getLdapAttributeName().equals( property.getJavaMemberName() ));
+ }
+ }
+
+
+ @Test
+ public void testGetJavaType_1()
+ {
+ AttributeTypeDescription atd = Schema.DEFAULT_SCHEMA.getAttributeTypeDescription( "supportedLDAPVersion" );
+ JavaType javaType = LdapEntryAnalyzer.getJavaType( atd, Schema.DEFAULT_SCHEMA );
+ assertEquals( JavaType.INT, javaType );
+ }
+
+
+ @Test
+ public void testGetJavaType_2()
+ {
+ AttributeTypeDescription atd = Schema.DEFAULT_SCHEMA.getAttributeTypeDescription( "hasSubordinates" );
+ JavaType javaType = LdapEntryAnalyzer.getJavaType( atd, Schema.DEFAULT_SCHEMA );
+ assertEquals( JavaType.BOOLEAN, javaType );
+ }
+
+
+ @Test
+ public void testGetJavaType_3()
+ {
+ AttributeTypeDescription atd = Schema.DEFAULT_SCHEMA.getAttributeTypeDescription( "modifyTimestamp" );
+ JavaType javaType = LdapEntryAnalyzer.getJavaType( atd, Schema.DEFAULT_SCHEMA );
+ assertEquals( JavaType.CALENDAR, javaType );
+ }
+
+
+ @Test
+ public void testGetJavaType_4()
+ {
+ AttributeTypeDescription atd = Schema.DEFAULT_SCHEMA.getAttributeTypeDescription( "userPassword" );
+ JavaType javaType = LdapEntryAnalyzer.getJavaType( atd, Schema.DEFAULT_SCHEMA );
+ assertEquals( JavaType.BYTE_ARRAY, javaType );
+ }
+
+
+ @Test
+ public void testGetJavaType_5()
+ {
+ AttributeTypeDescription atd = Schema.DEFAULT_SCHEMA.getAttributeTypeDescription( "cn" );
+ JavaType javaType = LdapEntryAnalyzer.getJavaType( atd, Schema.DEFAULT_SCHEMA );
+ assertEquals( JavaType.STRING, javaType );
+ }
+
+
+ @Test
+ public void testGetCardinality()
+ {
+ AttributeTypeDescription atd = Schema.DEFAULT_SCHEMA.getAttributeTypeDescription( "cn" );
+ Collection<ObjectClassDescription> ocds = new HashSet<ObjectClassDescription>();
+ ocds.add( Schema.DEFAULT_SCHEMA.getObjectClassDescription( "inetOrgPerson" ) );
+ Cardinality cardinality = LdapEntryAnalyzer.getCardinality( atd, ocds, Schema.DEFAULT_SCHEMA );
+ assertEquals( Cardinality.ONE_OR_MANY_SET, cardinality );
+ }
+
+
+ @Test
+ public void testGetCardinality_2()
+ {
+ AttributeTypeDescription atd = Schema.DEFAULT_SCHEMA.getAttributeTypeDescription( "givenName" );
+ Collection<ObjectClassDescription> ocds = new HashSet<ObjectClassDescription>();
+ ocds.add( Schema.DEFAULT_SCHEMA.getObjectClassDescription( "inetOrgPerson" ) );
+ Cardinality cardinality = LdapEntryAnalyzer.getCardinality( atd, ocds, Schema.DEFAULT_SCHEMA );
+ assertEquals( Cardinality.ZERO_OR_MANY_SET, cardinality );
+ }
+
+
+ @Test
+ public void testGetCardinality_3()
+ {
+ AttributeTypeDescription atd = Schema.DEFAULT_SCHEMA.getAttributeTypeDescription( "aliasedObjectName" );
+ Collection<ObjectClassDescription> ocds = new HashSet<ObjectClassDescription>();
+ ocds.add( Schema.DEFAULT_SCHEMA.getObjectClassDescription( "alias" ) );
+ Cardinality cardinality = LdapEntryAnalyzer.getCardinality( atd, ocds, Schema.DEFAULT_SCHEMA );
+ assertEquals( Cardinality.ONE, cardinality );
+ }
+
+
+ @Test
+ public void testGetCardinality_4()
+ {
+ AttributeTypeDescription atd = Schema.DEFAULT_SCHEMA.getAttributeTypeDescription( "displayName" );
+ Collection<ObjectClassDescription> ocds = new HashSet<ObjectClassDescription>();
+ ocds.add( Schema.DEFAULT_SCHEMA.getObjectClassDescription( "inetOrgPerson" ) );
+ Cardinality cardinality = LdapEntryAnalyzer.getCardinality( atd, ocds, Schema.DEFAULT_SCHEMA );
+ assertEquals( Cardinality.ZERO_OR_ONE, cardinality );
+ }
+}
Added: directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/SchemaUtilsTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/SchemaUtilsTest.java?rev=990428&view=auto
==============================================================================
--- directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/SchemaUtilsTest.java (added)
+++ directory/sandbox/studio-persistence-tooling/persistence-core/src/test/java/org/apache/directory/studio/persistence/core/SchemaUtilsTest.java Sat Aug 28 20:04:24 2010
@@ -0,0 +1,368 @@
+/*
+ * 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.studio.persistence.core;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.directory.shared.ldap.schema.parsers.AttributeTypeDescription;
+import org.apache.directory.shared.ldap.schema.parsers.ObjectClassDescription;
+import org.apache.directory.studio.ldapbrowser.core.model.schema.Schema;
+import org.junit.Before;
+import org.junit.Test;
+
+
+/**
+ * TODO SchemaUtilsTest.
+ *
+ * From RFC 4512, Section 2.4.2 (http://tools.ietf.org/html/rfc4512#section-2.4.2):
+ * An object or alias entry is characterized by precisely one
+ * structural object class superclass chain which has a single
+ * structural object class as the most subordinate object class.
+ * This structural object class is referred to as the structural
+ * object class of the entry.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class SchemaUtilsTest
+{
+ /**
+ * Contains multiple structural object classes of different superclass chains.
+ * That is invalid because an entry must contain exactly one structural
+ * object class superclass chain.
+ */
+ private List<ObjectClassDescription> ocdList1;
+
+ /**
+ * Contains exactly one structural object class (nisObject)
+ * and the special auxiliary class extensibleObject.
+ */
+ private List<ObjectClassDescription> ocdList2;
+
+ /**
+ * Contains exactly one structural object class (applicationEntity)
+ * and an auxiliary class (ieee802Device).
+ */
+ private List<ObjectClassDescription> ocdList3;
+
+ /**
+ * Contains exactly one structural object class: inetOrgPerson.
+ * Note: the list doesn't contain the classes contained in the superclass chain.
+ */
+ private List<ObjectClassDescription> ocdList4;
+
+ /**
+ * Contains exactly one structural object class chain including all
+ * object classes of the superclass chain and an additional auxiliary object class:
+ * inetOrgPerson, organizationalPerson, person, top, posixAccount
+ */
+ private List<ObjectClassDescription> ocdList5;
+
+ /**
+ * Contains no structural object class.
+ * That is invalid because an entry must contain an structural object class.
+ */
+ private List<ObjectClassDescription> ocdList6;
+
+ /** structural, superiors: organizationalPerson, person, top */
+ private ObjectClassDescription inetOrgPerson;
+
+ /** structural, superiors: person, top */
+ private ObjectClassDescription organizationalPerson;
+
+ /** structural, superiors: person, top */
+ private ObjectClassDescription person;
+
+ /** structural, superiors: top */
+ private ObjectClassDescription nisMap;
+
+ /** structural, superiors: top */
+ private ObjectClassDescription nisNetgroup;
+
+ /** auxiliary, superiors: top */
+ private ObjectClassDescription ieee802Device;
+
+ /** structural, superiors: top */
+ private ObjectClassDescription nisObject;
+
+ /** abstract, superiors: - */
+ private ObjectClassDescription top;
+
+ /** auxiliary, superiors: top */
+ private ObjectClassDescription extensibleObject;
+
+ /** structural, superiors: top */
+ private ObjectClassDescription openLDAProotDSE;
+
+ /** structural, superiors: top */
+ private ObjectClassDescription applicationEntity;
+
+ /** auxiliary, superiors: top */
+ private ObjectClassDescription posixAccount;
+
+
+ @Before
+ public void setUp()
+ {
+ ocdList1 = new ArrayList<ObjectClassDescription>();
+ ocdList2 = new ArrayList<ObjectClassDescription>();
+ ocdList3 = new ArrayList<ObjectClassDescription>();
+ ocdList4 = new ArrayList<ObjectClassDescription>();
+ ocdList5 = new ArrayList<ObjectClassDescription>();
+ ocdList6 = new ArrayList<ObjectClassDescription>();
+
+ inetOrgPerson = Schema.DEFAULT_SCHEMA.getObjectClassDescription( "inetOrgPerson" );
+ organizationalPerson = Schema.DEFAULT_SCHEMA.getObjectClassDescription( "organizationalPerson" );
+ person = Schema.DEFAULT_SCHEMA.getObjectClassDescription( "person" );
+ nisMap = Schema.DEFAULT_SCHEMA.getObjectClassDescription( "nisMap" );
+ nisNetgroup = Schema.DEFAULT_SCHEMA.getObjectClassDescription( "nisNetgroup" );
+ ieee802Device = Schema.DEFAULT_SCHEMA.getObjectClassDescription( "ieee802Device" );
+ nisObject = Schema.DEFAULT_SCHEMA.getObjectClassDescription( "nisObject" );
+ top = Schema.DEFAULT_SCHEMA.getObjectClassDescription( "top" );
+ extensibleObject = Schema.DEFAULT_SCHEMA.getObjectClassDescription( "extensibleObject" );
+ openLDAProotDSE = Schema.DEFAULT_SCHEMA.getObjectClassDescription( "OpenLDAProotDSE" );
+ applicationEntity = Schema.DEFAULT_SCHEMA.getObjectClassDescription( "applicationEntity" );
+ posixAccount = Schema.DEFAULT_SCHEMA.getObjectClassDescription( "posixAccount" );
+
+ //add some values to list1
+ ocdList1.add( inetOrgPerson );
+ ocdList1.add( nisMap );
+ ocdList1.add( nisNetgroup );
+
+ //add some values to list2
+ ocdList2.add( nisObject );
+ ocdList2.add( top );
+ ocdList2.add( extensibleObject );
+
+ //add some values to list3
+ ocdList3.add( ieee802Device );
+ ocdList3.add( top );
+ ocdList3.add( applicationEntity );
+
+ //add some values to list4
+ ocdList4.add( inetOrgPerson );
+
+ //add some values to list5
+ ocdList5.add( posixAccount );
+ ocdList5.add( organizationalPerson );
+ ocdList5.add( person );
+ ocdList5.add( inetOrgPerson );
+ ocdList5.add( top );
+
+ //add some values to list6
+ ocdList6.add( posixAccount );
+ ocdList6.add( ieee802Device );
+ }
+
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetStructuralObjectClass_list1()
+ {
+ SchemaUtils2.getStructuralObjectClass( ocdList1, Schema.DEFAULT_SCHEMA );
+ }
+
+
+ @Test
+ public void testGetStructuralObjectClass_list2()
+ {
+ ObjectClassDescription ocd = SchemaUtils2.getStructuralObjectClass( ocdList2, Schema.DEFAULT_SCHEMA );
+ assertEquals( nisObject, ocd );
+ assertEquals( "1.3.6.1.1.1.2.10", ocd.getNumericOid() );
+ }
+
+
+ @Test
+ public void testGetStructuralObjectClass_list3()
+ {
+ ObjectClassDescription ocd = SchemaUtils2.getStructuralObjectClass( ocdList3, Schema.DEFAULT_SCHEMA );
+ assertEquals( applicationEntity, ocd );
+ assertEquals( "2.5.6.12", ocd.getNumericOid() );
+ }
+
+
+ @Test
+ public void testGetSpecificStructuralObjectClass_list4()
+ {
+ ObjectClassDescription ocd = SchemaUtils2.getStructuralObjectClass( ocdList4, Schema.DEFAULT_SCHEMA );
+ assertEquals( inetOrgPerson, ocd );
+ assertEquals( "2.16.840.1.113730.3.2.2", ocd.getNumericOid() );
+ }
+
+
+ @Test
+ public void testGetStructuralObjectClass_list5()
+ {
+ ObjectClassDescription ocd = SchemaUtils2.getStructuralObjectClass( ocdList5, Schema.DEFAULT_SCHEMA );
+ assertEquals( inetOrgPerson, ocd );
+ assertEquals( "2.16.840.1.113730.3.2.2", ocd.getNumericOid() );
+ }
+
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetSpecificStructuralObjectClass_list6()
+ {
+ SchemaUtils2.getStructuralObjectClass( ocdList6, Schema.DEFAULT_SCHEMA );
+ }
+
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetSpecificStructuralObjectClass_nullList()
+ {
+ SchemaUtils2.getStructuralObjectClass( null, Schema.DEFAULT_SCHEMA );
+ }
+
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetSpecificStructuralObjectClass_emptyList()
+ {
+ SchemaUtils2.getStructuralObjectClass( Collections.EMPTY_LIST, Schema.DEFAULT_SCHEMA );
+ }
+
+
+ @Test
+ public void testGetAllAttributes_list2()
+ {
+ Collection<String> attributes = SchemaUtils2.getAllAttributes( ocdList2, Schema.DEFAULT_SCHEMA );
+
+ // extensibleObject object class contains all attributes
+ assertTrue( attributes.size() > 100 );
+
+ // from 'top'
+ assertTrue( attributes.contains( "objectClass" ) );
+
+ // from 'nisObject'
+ assertTrue( attributes.contains( "cn" ) );
+ assertTrue( attributes.contains( "description" ) );
+ assertTrue( attributes.contains( "nisMapEntry" ) );
+ assertTrue( attributes.contains( "nisMapName" ) );
+
+ // from 'extensibleObject'
+ assertTrue( attributes.contains( "photo" ) );
+ }
+
+
+ @Test
+ public void testGetAllAttributes_list4()
+ {
+ Collection<String> attributes = SchemaUtils2.getAllAttributes( ocdList4, Schema.DEFAULT_SCHEMA );
+
+ // inetOrgPerson object class chain defines 51 attributes
+ assertEquals( 51, attributes.size() );
+
+ // from 'top'
+ assertTrue( attributes.contains( "objectClass" ) );
+
+ // from 'person'
+ assertTrue( attributes.contains( "cn" ) );
+ assertTrue( attributes.contains( "description" ) );
+
+ // from 'organizationalPerson'
+ assertTrue( attributes.contains( "title" ) );
+ assertTrue( attributes.contains( "x121Address" ) );
+ assertTrue( attributes.contains( "registeredAddress" ) );
+
+ // from 'inetOrgPerson'
+ assertTrue( attributes.contains( "mail" ) );
+ assertTrue( attributes.contains( "userCertificate" ) );
+ }
+
+
+ @Test
+ public void testGetAllAttributes_list5()
+ {
+ Collection<String> attributes = SchemaUtils2.getAllAttributes( ocdList5, Schema.DEFAULT_SCHEMA );
+
+ // inetOrgPerson object class chain defines 51 attributes
+ // posixAccount defines 10 attributes, but 5 of them are already defined in inetOrgPerson object class chain
+ assertEquals( 56, attributes.size() );
+
+ // from 'top'
+ assertTrue( attributes.contains( "objectClass" ) );
+
+ // from 'person'
+ assertTrue( attributes.contains( "cn" ) );
+ assertTrue( attributes.contains( "description" ) );
+
+ // from 'organizationalPerson'
+ assertTrue( attributes.contains( "title" ) );
+ assertTrue( attributes.contains( "x121Address" ) );
+ assertTrue( attributes.contains( "registeredAddress" ) );
+
+ // from 'inetOrgPerson'
+ assertTrue( attributes.contains( "mail" ) );
+ assertTrue( attributes.contains( "userCertificate" ) );
+
+ // from 'posixAccount'
+ assertTrue( attributes.contains( "gidNumber" ) );
+ assertTrue( attributes.contains( "homeDirectory" ) );
+ assertTrue( attributes.contains( "uidNumber" ) );
+ assertTrue( attributes.contains( "gecos" ) );
+ assertTrue( attributes.contains( "loginShell" ) );
+ }
+
+
+ @Test
+ public void testIsMustAttribute_1()
+ {
+ AttributeTypeDescription atd = Schema.DEFAULT_SCHEMA.getAttributeTypeDescription( "objectClass" );
+ assertTrue( SchemaUtils2.isMustAttribute( atd, ocdList4, Schema.DEFAULT_SCHEMA ) );
+ }
+
+
+ @Test
+ public void testIsMustAttribute_2()
+ {
+ AttributeTypeDescription atd = Schema.DEFAULT_SCHEMA.getAttributeTypeDescription( "cn" );
+ assertTrue( SchemaUtils2.isMustAttribute( atd, ocdList4, Schema.DEFAULT_SCHEMA ) );
+ }
+
+
+ @Test
+ public void testIsMustAttribute_3()
+ {
+ AttributeTypeDescription atd = Schema.DEFAULT_SCHEMA.getAttributeTypeDescription( "givenName" );
+ assertFalse( SchemaUtils2.isMustAttribute( atd, ocdList4, Schema.DEFAULT_SCHEMA ) );
+ }
+
+
+ @Test
+ public void testIsMustAttribute_4()
+ {
+ AttributeTypeDescription atd = Schema.DEFAULT_SCHEMA.getAttributeTypeDescription( "xyz" );
+ assertFalse( SchemaUtils2.isMustAttribute( atd, ocdList4, Schema.DEFAULT_SCHEMA ) );
+ }
+
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testIsMustAttribute_5()
+ {
+ AttributeTypeDescription atd = null;
+ assertFalse( SchemaUtils2.isMustAttribute( atd, ocdList4, Schema.DEFAULT_SCHEMA ) );
+ }
+
+}
Added: directory/sandbox/studio-persistence-tooling/persistence-example/pom.xml
URL: http://svn.apache.org/viewvc/directory/sandbox/studio-persistence-tooling/persistence-example/pom.xml?rev=990428&view=auto
==============================================================================
--- directory/sandbox/studio-persistence-tooling/persistence-example/pom.xml (added)
+++ directory/sandbox/studio-persistence-tooling/persistence-example/pom.xml Sat Aug 28 20:04:24 2010
@@ -0,0 +1,110 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- 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. -->
+<!-- @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.directory.studio</groupId>
+ <artifactId>persistence.example</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <name>Apache Directory Studio LDAP Persistence Tooling Example</name>
+ <description />
+
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-eclipse-plugin</artifactId>
+ <version>2.8</version>
+ <configuration>
+ <downloadSources>true</downloadSources>
+ <downloadJavadocs>true</downloadJavadocs>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.1</version>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ <optimize>true</optimize>
+ <showDeprecations>true</showDeprecations>
+ <encoding>ISO-8859-1</encoding>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.directory.client.ldap</groupId>
+ <artifactId>ldap-client-api</artifactId>
+ <version>0.1</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-core-annotations</artifactId>
+ <version>1.5.6</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-core-api</artifactId>
+ <version>1.5.6</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-protocol-ldap</artifactId>
+ <version>1.5.6</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-server-annotations</artifactId>
+ <version>1.5.6</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.directory.server</groupId>
+ <artifactId>apacheds-test-framework</artifactId>
+ <version>1.5.6</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>1.2.16</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <version>1.5.10</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
Added: directory/sandbox/studio-persistence-tooling/persistence-example/src/main/java/com/example/dao/GenericLdapDao.java
URL: http://svn.apache.org/viewvc/directory/sandbox/studio-persistence-tooling/persistence-example/src/main/java/com/example/dao/GenericLdapDao.java?rev=990428&view=auto
==============================================================================
--- directory/sandbox/studio-persistence-tooling/persistence-example/src/main/java/com/example/dao/GenericLdapDao.java (added)
+++ directory/sandbox/studio-persistence-tooling/persistence-example/src/main/java/com/example/dao/GenericLdapDao.java Sat Aug 28 20:04:24 2010
@@ -0,0 +1,516 @@
+package com.example.dao;
+
+
+import java.io.IOException;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.naming.InvalidNameException;
+import javax.naming.NamingException;
+
+import org.apache.directory.ldap.client.api.LdapConnection;
+import org.apache.directory.ldap.client.api.exception.LdapException;
+import org.apache.directory.ldap.client.api.message.ModifyRequest;
+import org.apache.directory.ldap.client.api.message.SearchResponse;
+import org.apache.directory.ldap.client.api.message.SearchResultEntry;
+import org.apache.directory.shared.ldap.cursor.Cursor;
+import org.apache.directory.shared.ldap.entry.Entry;
+import org.apache.directory.shared.ldap.entry.EntryAttribute;
+import org.apache.directory.shared.ldap.entry.ModificationOperation;
+import org.apache.directory.shared.ldap.entry.Value;
+import org.apache.directory.shared.ldap.entry.client.ClientModification;
+import org.apache.directory.shared.ldap.entry.client.DefaultClientAttribute;
+import org.apache.directory.shared.ldap.entry.client.DefaultClientEntry;
+import org.apache.directory.shared.ldap.filter.SearchScope;
+import org.apache.directory.shared.ldap.name.DN;
+import org.apache.directory.shared.ldap.util.GeneralizedTime;
+
+
+public abstract class GenericLdapDao<T>
+{
+
+ protected String server;
+ protected int port;
+ protected String name;
+ protected String credentials;
+ protected String rdnAttribute;
+ protected String parentDn;
+ protected String[] attributes;
+ protected String[] objectClasses;
+
+
+ public T getById( String id )
+ {
+ LdapConnection connection = null;
+ try
+ {
+ connection = getLdapConnection();
+ DN dn = new DN( rdnAttribute, id, parentDn );
+ Entry entry = lookupEntry( connection, dn.getName() );
+ if ( entry != null )
+ {
+ T object = mapFromEntry( entry );
+ return object;
+ }
+ else
+ {
+ return null;
+ }
+ }
+ catch ( Exception e )
+ {
+ throw new RuntimeException( e );
+ }
+ finally
+ {
+ returnLdapConnection( connection );
+ }
+ }
+
+
+ public Collection<T> getAll()
+ {
+ Collection<T> all = new ArrayList<T>();
+
+ LdapConnection connection = null;
+ try
+ {
+ connection = getLdapConnection();
+
+ String filter = "(&";
+ for ( String objectClass : objectClasses )
+ {
+ filter += "(objectClass=" + objectClass + ")";
+ }
+ filter += ")";
+
+ Cursor<SearchResponse> cursor = connection.search( parentDn, filter, SearchScope.ONELEVEL, attributes );
+ while ( cursor.next() )
+ {
+ SearchResponse response = cursor.get();
+ if ( response instanceof SearchResultEntry )
+ {
+ SearchResultEntry sre = ( SearchResultEntry ) response;
+ Entry entry = sre.getEntry();
+ T object = mapFromEntry( entry );
+ all.add( object );
+ }
+ // TODO: handle other response types
+ }
+ }
+ catch ( Exception e )
+ {
+ throw new RuntimeException( e );
+ }
+ finally
+ {
+ returnLdapConnection( connection );
+ }
+
+ return all;
+ }
+
+
+ public void create( T object )
+ {
+ LdapConnection connection = null;
+ try
+ {
+ connection = getLdapConnection();
+ Entry entry = getEntry( object );
+ connection.add( entry );
+ }
+ catch ( Exception e )
+ {
+ throw new RuntimeException( e );
+ }
+ finally
+ {
+ returnLdapConnection( connection );
+ }
+ }
+
+
+ public void update( T object )
+ {
+ LdapConnection connection = null;
+ try
+ {
+ connection = getLdapConnection();
+
+ // get old and new entry
+ Entry newEntry = getEntry( object );
+ Entry oldEntry = lookupEntry( connection, newEntry.getDn().getName() );
+
+ // generate diff
+ ModifyRequest modRequest = new ModifyRequest( newEntry.getDn() );
+ for ( EntryAttribute newAttribute : newEntry )
+ {
+ if ( oldEntry.contains( newAttribute ) )
+ {
+ EntryAttribute oldAttribute = oldEntry.get( newAttribute.getId() );
+ if ( !oldAttribute.equals( newAttribute ) )
+ {
+ modRequest.addModification( newAttribute, ModificationOperation.REPLACE_ATTRIBUTE );
+ }
+ }
+ else
+ {
+ modRequest.addModification( newAttribute, ModificationOperation.REPLACE_ATTRIBUTE );
+ }
+ }
+ for ( EntryAttribute oldAttribute : oldEntry )
+ {
+ if ( !newEntry.contains( oldAttribute ) )
+ {
+ modRequest.addModification( new ClientModification( ModificationOperation.REPLACE_ATTRIBUTE,
+ new DefaultClientAttribute( oldAttribute.getId() ) ) );
+ }
+ }
+
+ connection.modify( modRequest );
+ }
+ catch ( Exception e )
+ {
+ throw new RuntimeException( e );
+ }
+ finally
+ {
+ returnLdapConnection( connection );
+ }
+ }
+
+
+ public void delete( T object )
+ {
+ LdapConnection connection = null;
+ try
+ {
+ connection = getLdapConnection();
+ Entry entry = getEntry( object );
+ connection.delete( entry.getDn() );
+ }
+ catch ( Exception e )
+ {
+ throw new RuntimeException( e );
+ }
+ finally
+ {
+ returnLdapConnection( connection );
+ }
+ }
+
+
+ private Entry getEntry( T object ) throws InvalidNameException, NamingException
+ {
+ Entry entry = new DefaultClientEntry();
+
+ // map object to entry first, because this fills the RDN attribute
+ mapToEntry( object, entry );
+
+ DN dn = new DN( rdnAttribute, getString( entry, rdnAttribute ), parentDn );
+ entry.setDn( dn );
+
+ entry.add( "objectClass", objectClasses );
+
+ return entry;
+ }
+
+
+ private Entry lookupEntry( LdapConnection connection, String dn ) throws LdapException
+ {
+ SearchResponse response = connection.lookup( dn, attributes );
+ if ( response instanceof SearchResultEntry )
+ {
+ SearchResultEntry sre = ( SearchResultEntry ) response;
+ Entry entry = sre.getEntry();
+ return entry;
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+
+ protected abstract T mapFromEntry( Entry entry );
+
+
+ protected abstract void mapToEntry( T object, Entry entry ) throws NamingException;
+
+
+ protected byte[] getBytes( Entry entry, String attribute )
+ {
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ return entryAttribute.get().getBytes();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+
+ protected void setBytes( Entry entry, String attribute, byte[] value ) throws NamingException
+ {
+ if ( value != null )
+ {
+ entry.add( attribute, value );
+ }
+ }
+
+
+ protected Integer getInt( Entry entry, String attribute )
+ {
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ return Integer.parseInt( entryAttribute.get().getString() );
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+
+ protected void setInt( Entry entry, String attribute, Integer value ) throws NamingException
+ {
+ if ( value != null )
+ {
+ entry.add( attribute, value.toString() );
+ }
+ }
+
+
+ protected void setCalendar( Entry entry, String attribute, Calendar value ) throws NamingException
+ {
+ if ( value != null )
+ {
+ entry.add( attribute, new GeneralizedTime( value ).toGeneralizedTime() );
+ }
+ }
+
+
+ protected Calendar getCalendar( Entry entry, String attribute ) throws ParseException
+ {
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ return new GeneralizedTime( attribute ).getCalendar();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+
+ protected Long getLong( Entry entry, String attribute )
+ {
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ return Long.parseLong( entryAttribute.get().getString() );
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+
+ protected void setLong( Entry entry, String attribute, Long value ) throws NamingException
+ {
+ if ( value != null )
+ {
+ entry.add( attribute, value.toString() );
+ }
+ }
+
+
+ protected String getString( Entry entry, String attribute )
+ {
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ return entryAttribute.get().getString();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+
+ protected void setString( Entry entry, String attribute, String value ) throws NamingException
+ {
+ if ( value != null )
+ {
+ entry.add( attribute, value.toString() );
+ }
+ }
+
+
+ protected void setBoolean( Entry entry, String attribute, Boolean value ) throws NamingException
+ {
+ if ( value != null )
+ {
+ entry.add( attribute, "TRUE" );
+ }
+ else
+ {
+ entry.add( attribute, "FALSE" );
+ }
+ }
+
+
+ protected Boolean getBoolean( Entry entry, String attribute )
+ {
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ return Boolean.parseBoolean( attribute );
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+
+ protected Set<String> getStringSet( Entry entry, String attribute )
+ {
+ Set<String> values = new HashSet<String>();
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ for ( Value<?> value : entryAttribute )
+ {
+ values.add( value.getString() );
+ }
+ }
+ return values;
+ }
+
+
+ protected Set<Integer> getIntegerSet( Entry entry, String attribute )
+ {
+ Set<Integer> values = new HashSet<Integer>();
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ for ( Value<?> value : entryAttribute )
+ {
+ values.add( Integer.parseInt( value.getString() ) );
+ }
+ }
+ return values;
+ }
+
+
+ protected Set<Long> getLongSet( Entry entry, String attribute )
+ {
+ Set<Long> values = new HashSet<Long>();
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ for ( Value<?> value : entryAttribute )
+ {
+ values.add( Long.parseLong( value.getString() ) );
+ }
+ }
+ return values;
+ }
+
+
+ protected Set<Boolean> getBooleanSet( Entry entry, String attribute )
+ {
+ Set<Boolean> values = new HashSet<Boolean>();
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ for ( Value<?> value : entryAttribute )
+ {
+ values.add( Boolean.parseBoolean( value.getString() ) );
+ }
+ }
+ return values;
+ }
+
+
+ protected Set<Calendar> getCalendarSet( Entry entry, String attribute ) throws ParseException
+ {
+ Set<Calendar> values = new HashSet<Calendar>();
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ for ( Value<?> value : entryAttribute )
+ {
+ values.add( new GeneralizedTime( value.getString() ).getCalendar() );
+ }
+ }
+ return values;
+ }
+
+
+ protected Set<byte[]> getByteArraySet( Entry entry, String attribute )
+ {
+ Set<byte[]> values = new HashSet<byte[]>();
+ EntryAttribute entryAttribute = entry.get( attribute );
+ if ( entryAttribute != null )
+ {
+ for ( Value<?> value : entryAttribute )
+ {
+ values.add( value.getBytes() );
+ }
+ }
+ return values;
+ }
+
+
+ protected LdapConnection getLdapConnection()
+ {
+ try
+ {
+ LdapConnection connection = new LdapConnection( server, port );
+ connection.bind( name, credentials );
+ return connection;
+ }
+ catch ( LdapException e )
+ {
+ throw new RuntimeException( e );
+ }
+ catch ( IOException e )
+ {
+ throw new RuntimeException( e );
+ }
+ }
+
+
+ protected void returnLdapConnection( LdapConnection connection )
+ {
+ if ( connection != null )
+ {
+ try
+ {
+ connection.unBind();
+ connection.close();
+ }
+ catch ( IOException e )
+ {
+ throw new RuntimeException( e );
+ }
+ catch ( Exception e )
+ {
+ throw new RuntimeException( e );
+ }
+ }
+ }
+}