You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2012/04/30 01:48:41 UTC
svn commit: r1332027 -
/directory/apacheds/branches/index-work/server-integ/src/test/java/org/apache/directory/server/operations/search/ReferralSearchIT.java
Author: elecharny
Date: Sun Apr 29 23:48:41 2012
New Revision: 1332027
URL: http://svn.apache.org/viewvc?rev=1332027&view=rev
Log:
o Closed the conext after usage
o Closed the NamingEnumeration after usage
o Reformating the class
Modified:
directory/apacheds/branches/index-work/server-integ/src/test/java/org/apache/directory/server/operations/search/ReferralSearchIT.java
Modified: directory/apacheds/branches/index-work/server-integ/src/test/java/org/apache/directory/server/operations/search/ReferralSearchIT.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/index-work/server-integ/src/test/java/org/apache/directory/server/operations/search/ReferralSearchIT.java?rev=1332027&r1=1332026&r2=1332027&view=diff
==============================================================================
--- directory/apacheds/branches/index-work/server-integ/src/test/java/org/apache/directory/server/operations/search/ReferralSearchIT.java (original)
+++ directory/apacheds/branches/index-work/server-integ/src/test/java/org/apache/directory/server/operations/search/ReferralSearchIT.java Sun Apr 29 23:48:41 2012
@@ -124,306 +124,334 @@ import org.junit.runner.RunWith;
})
public class ReferralSearchIT extends AbstractLdapTestUnit
{
-
-@Before
-public void setupReferrals() throws Exception
-{
- String ldif =
- "dn: c=europ,ou=Countries,ou=system\n" +
- "objectClass: top\n" +
- "objectClass: referral\n" +
- "objectClass: extensibleObject\n" +
- "c: europ\n" +
- "ref: ldap://localhost:" + getLdapServer().getPort() + "/c=france,ou=system\n\n" +
-
- "dn: c=america,ou=Countries,ou=system\n" +
- "objectClass: top\n" +
- "objectClass: referral\n" +
- "objectClass: extensibleObject\n" +
- "c: america\n" +
- "ref: ldap://localhost:" + getLdapServer().getPort() + "/c=usa,ou=system\n\n";
-
- LdifReader reader = new LdifReader( new StringReader( ldif ) );
-
- while ( reader.hasNext() )
- {
- LdifEntry entry = reader.next();
- getLdapServer().getDirectoryService().getAdminSession().add(
- new DefaultEntry( getLdapServer().getDirectoryService().getSchemaManager(), entry.getEntry() ) );
- }
-}
-
-
-@Test
-public void testSearchBaseIsReferral() throws Exception
-{
- DirContext ctx = getWiredContextThrowOnRefferal( getLdapServer() );
- SearchControls controls = new SearchControls();
- controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
-
- try
- {
- ctx.search( "ou=RemoteUsers,ou=system", "(objectClass=*)", controls );
- fail( "should never get here" );
- }
- catch ( ReferralException e )
- {
- assertEquals( "ldap://fermi:10389/ou=users,ou=system??sub", e.getReferralInfo() );
- assertTrue( e.skipReferral() );
- assertEquals( "ldap://hertz:10389/ou=users,dc=example,dc=com??sub", e.getReferralInfo() );
- assertTrue( e.skipReferral() );
- assertEquals( "ldap://maxwell:10389/ou=users,ou=system??sub", e.getReferralInfo() );
- assertFalse( e.skipReferral() );
- }
-}
-
-
-@Test
-public void testSearchBaseParentIsReferral() throws Exception
-{
- DirContext ctx = getWiredContextThrowOnRefferal( getLdapServer() );
- SearchControls controls = new SearchControls();
- controls.setSearchScope( SearchControls.OBJECT_SCOPE );
-
- try
+ @Before
+ public void setupReferrals() throws Exception
{
- ctx.search( "cn=alex karasulu,ou=RemoteUsers,ou=system", "(objectClass=*)", controls );
+ String ldif =
+ "dn: c=europ,ou=Countries,ou=system\n" +
+ "objectClass: top\n" +
+ "objectClass: referral\n" +
+ "objectClass: extensibleObject\n" +
+ "c: europ\n" +
+ "ref: ldap://localhost:" + getLdapServer().getPort() + "/c=france,ou=system\n\n" +
+
+ "dn: c=america,ou=Countries,ou=system\n" +
+ "objectClass: top\n" +
+ "objectClass: referral\n" +
+ "objectClass: extensibleObject\n" +
+ "c: america\n" +
+ "ref: ldap://localhost:" + getLdapServer().getPort() + "/c=usa,ou=system\n\n";
+
+ LdifReader reader = new LdifReader( new StringReader( ldif ) );
+
+ while ( reader.hasNext() )
+ {
+ LdifEntry entry = reader.next();
+ getLdapServer().getDirectoryService().getAdminSession().add(
+ new DefaultEntry( getLdapServer().getDirectoryService().getSchemaManager(), entry.getEntry() ) );
+ }
}
- catch ( ReferralException e )
+
+
+ @Test
+ public void testSearchBaseIsReferral() throws Exception
{
- assertEquals( "ldap://fermi:10389/cn=alex%20karasulu,ou=users,ou=system??base", e.getReferralInfo() );
- assertTrue( e.skipReferral() );
- assertEquals( "ldap://hertz:10389/cn=alex%20karasulu,ou=users,dc=example,dc=com??base", e.getReferralInfo() );
- assertTrue( e.skipReferral() );
- assertEquals( "ldap://maxwell:10389/cn=alex%20karasulu,ou=users,ou=system??base", e.getReferralInfo() );
- assertFalse( e.skipReferral() );
- }
-}
-
-
-@Test
-public void testSearchBaseAncestorIsReferral() throws Exception
-{
- DirContext ctx = getWiredContextThrowOnRefferal( getLdapServer() );
- SearchControls controls = new SearchControls();
- controls.setSearchScope( SearchControls.OBJECT_SCOPE );
+ DirContext ctx = getWiredContextThrowOnRefferal( getLdapServer() );
+ SearchControls controls = new SearchControls();
+ controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
- try
- {
- ctx.search( "cn=alex karasulu,ou=apache,ou=RemoteUsers,ou=system", "(objectClass=*)", controls );
+ try
+ {
+ ctx.search( "ou=RemoteUsers,ou=system", "(objectClass=*)", controls );
+ fail( "should never get here" );
+ }
+ catch ( ReferralException e )
+ {
+ assertEquals( "ldap://fermi:10389/ou=users,ou=system??sub", e.getReferralInfo() );
+ assertTrue( e.skipReferral() );
+ assertEquals( "ldap://hertz:10389/ou=users,dc=example,dc=com??sub", e.getReferralInfo() );
+ assertTrue( e.skipReferral() );
+ assertEquals( "ldap://maxwell:10389/ou=users,ou=system??sub", e.getReferralInfo() );
+ assertFalse( e.skipReferral() );
+ }
+ finally
+ {
+ ctx.close();
+ }
}
- catch ( ReferralException e )
+
+
+ @Test
+ public void testSearchBaseParentIsReferral() throws Exception
{
- assertEquals( "ldap://fermi:10389/cn=alex%20karasulu,ou=apache,ou=users,ou=system??base", e.getReferralInfo() );
- assertTrue( e.skipReferral() );
- assertEquals( "ldap://hertz:10389/cn=alex%20karasulu,ou=apache,ou=users,dc=example,dc=com??base", e
- .getReferralInfo() );
- assertTrue( e.skipReferral() );
- assertEquals( "ldap://maxwell:10389/cn=alex%20karasulu,ou=apache,ou=users,ou=system??base", e
- .getReferralInfo() );
- assertFalse( e.skipReferral() );
+ DirContext ctx = getWiredContextThrowOnRefferal( getLdapServer() );
+ SearchControls controls = new SearchControls();
+ controls.setSearchScope( SearchControls.OBJECT_SCOPE );
+
+ try
+ {
+ ctx.search( "cn=alex karasulu,ou=RemoteUsers,ou=system", "(objectClass=*)", controls );
+ }
+ catch ( ReferralException e )
+ {
+ assertEquals( "ldap://fermi:10389/cn=alex%20karasulu,ou=users,ou=system??base", e.getReferralInfo() );
+ assertTrue( e.skipReferral() );
+ assertEquals( "ldap://hertz:10389/cn=alex%20karasulu,ou=users,dc=example,dc=com??base", e.getReferralInfo() );
+ assertTrue( e.skipReferral() );
+ assertEquals( "ldap://maxwell:10389/cn=alex%20karasulu,ou=users,ou=system??base", e.getReferralInfo() );
+ assertFalse( e.skipReferral() );
+ }
+ finally
+ {
+ ctx.close();
+ }
}
-}
-
-
-@Test
-public void testSearchContinuations() throws Exception
-{
- DirContext ctx = getWiredContext( getLdapServer() );
-
- SearchControls controls = new SearchControls();
- controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
- NamingEnumeration<SearchResult> list = ctx.search( "ou=system", "(objectClass=*)", controls );
- Map<String, SearchResult> results = new HashMap<String, SearchResult>();
- while ( list.hasMore() )
+
+
+ @Test
+ public void testSearchBaseAncestorIsReferral() throws Exception
{
- SearchResult result = list.next();
- results.put( result.getName(), result );
+ DirContext ctx = getWiredContextThrowOnRefferal( getLdapServer() );
+ SearchControls controls = new SearchControls();
+ controls.setSearchScope( SearchControls.OBJECT_SCOPE );
+
+ try
+ {
+ ctx.search( "cn=alex karasulu,ou=apache,ou=RemoteUsers,ou=system", "(objectClass=*)", controls );
+ }
+ catch ( ReferralException e )
+ {
+ assertEquals( "ldap://fermi:10389/cn=alex%20karasulu,ou=apache,ou=users,ou=system??base", e.getReferralInfo() );
+ assertTrue( e.skipReferral() );
+ assertEquals( "ldap://hertz:10389/cn=alex%20karasulu,ou=apache,ou=users,dc=example,dc=com??base", e
+ .getReferralInfo() );
+ assertTrue( e.skipReferral() );
+ assertEquals( "ldap://maxwell:10389/cn=alex%20karasulu,ou=apache,ou=users,ou=system??base", e
+ .getReferralInfo() );
+ assertFalse( e.skipReferral() );
+ }
+ finally
+ {
+ ctx.close();
+ }
}
-
- assertNotNull( results.get( "ou=users" ) );
-
- // -------------------------------------------------------------------
- // Now we will throw exceptions when searching for referrals
- // -------------------------------------------------------------------
-
- ctx.addToEnvironment( Context.REFERRAL, "throw" );
- list = ctx.search( "ou=system", "(objectClass=*)", controls );
- results = new HashMap<String, SearchResult>();
-
- try
+
+
+ @Test
+ public void testSearchContinuations() throws Exception
{
+ DirContext ctx = getWiredContext( getLdapServer() );
+
+ SearchControls controls = new SearchControls();
+ controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
+ NamingEnumeration<SearchResult> list = ctx.search( "ou=system", "(objectClass=*)", controls );
+ Map<String, SearchResult> results = new HashMap<String, SearchResult>();
while ( list.hasMore() )
{
SearchResult result = list.next();
results.put( result.getName(), result );
}
- }
- catch ( ReferralException e )
- {
- // As we use the uuidIndex the order of search continuations returned by
- // the server is not deterministic. So we collect all referrals first into
- // an hashset and check afterwards if the expected URLs are included.
- Set<Object> s = new HashSet<Object>();
- s.add( e.getReferralInfo() );
- while ( e.skipReferral() )
+
+ assertNotNull( results.get( "ou=users" ) );
+
+ // -------------------------------------------------------------------
+ // Now we will throw exceptions when searching for referrals
+ // -------------------------------------------------------------------
+
+ ctx.addToEnvironment( Context.REFERRAL, "throw" );
+ list = ctx.search( "ou=system", "(objectClass=*)", controls );
+ results = new HashMap<String, SearchResult>();
+
+ try
{
- try
+ while ( list.hasMore() )
{
- Context ctx2 = e.getReferralContext();
- ctx2.list( "" );
+ SearchResult result = list.next();
+ results.put( result.getName(), result );
}
- catch ( NamingException ne )
+ }
+ catch ( ReferralException e )
+ {
+ // As we use the uuidIndex the order of search continuations returned by
+ // the server is not deterministic. So we collect all referrals first into
+ // an hashset and check afterwards if the expected URLs are included.
+ Set<Object> s = new HashSet<Object>();
+ s.add( e.getReferralInfo() );
+
+ while ( e.skipReferral() )
{
- if ( ne instanceof ReferralException )
+ try
{
- e = ( ReferralException ) ne;
- s.add( e.getReferralInfo() );
+ Context ctx2 = e.getReferralContext();
+ ctx2.list( "" );
}
- else
+ catch ( NamingException ne )
{
- break;
+ if ( ne instanceof ReferralException )
+ {
+ e = ( ReferralException ) ne;
+ s.add( e.getReferralInfo() );
+ }
+ else
+ {
+ break;
+ }
}
}
+
+ assertEquals( 5, s.size() );
+ assertTrue( s.contains( "ldap://fermi:10389/ou=users,ou=system??sub" ) );
+ assertTrue( s.contains( "ldap://hertz:10389/ou=users,dc=example,dc=com??sub" ) );
+ assertTrue( s.contains( "ldap://maxwell:10389/ou=users,ou=system??sub" ) );
}
-
- assertEquals( 5, s.size() );
- assertTrue( s.contains( "ldap://fermi:10389/ou=users,ou=system??sub" ) );
- assertTrue( s.contains( "ldap://hertz:10389/ou=users,dc=example,dc=com??sub" ) );
- assertTrue( s.contains( "ldap://maxwell:10389/ou=users,ou=system??sub" ) );
- }
-
- assertNull( results.get( "ou=remoteusers" ) );
-
- // try again but this time with single level scope
-
- controls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
- list = ctx.search( "ou=system", "(objectClass=*)", controls );
- results = new HashMap<String, SearchResult>();
-
- try
- {
- while ( list.hasMore() )
+
+ assertNull( results.get( "ou=remoteusers" ) );
+ list.close();
+
+ // try again but this time with single level scope
+
+ controls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
+ list = ctx.search( "ou=system", "(objectClass=*)", controls );
+ results = new HashMap<String, SearchResult>();
+
+ try
{
- SearchResult result = list.next();
- results.put( result.getName(), result );
+ while ( list.hasMore() )
+ {
+ SearchResult result = list.next();
+ results.put( result.getName(), result );
+ }
+ }
+ catch ( ReferralException e )
+ {
+ assertEquals( "ldap://fermi:10389/ou=users,ou=system??base", e.getReferralInfo() );
+ assertTrue( e.skipReferral() );
+ assertEquals( "ldap://hertz:10389/ou=users,dc=example,dc=com??base", e.getReferralInfo() );
+ assertTrue( e.skipReferral() );
+ assertEquals( "ldap://maxwell:10389/ou=users,ou=system??base", e.getReferralInfo() );
+ }
+
+ list.close();
+ ctx.close();
+
+ assertNull( results.get( "ou=remoteusers" ) );
+ }
+
+
+ /**
+ * Test of an search operation with a referral
+ *
+ * search for "cn=alex karasulu" on "c=america, ou=system"
+ * we should get a referral URL thrown, which point to
+ * "c=usa, ou=system", and ask for a subtree search
+ */
+ @Test
+ public void testSearchWithReferralThrow() throws Exception
+ {
+ DirContext ctx = getWiredContextThrowOnRefferal( getLdapServer() );
+
+ try
+ {
+ SearchControls controls = new SearchControls();
+ controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
+ ctx.search( "c=america,ou=Countries,ou=system", "(cn=alex karasulu)", controls );
+ fail( "Should fail here throwing a ReferralException" );
+ }
+ catch ( ReferralException re )
+ {
+ String referral = ( String ) re.getReferralInfo();
+ assertEquals( "ldap://localhost:" + getLdapServer().getPort() + "/c=usa,ou=system??sub", referral );
+ }
+ finally
+ {
+ ctx.close();
}
}
- catch ( ReferralException e )
- {
- assertEquals( "ldap://fermi:10389/ou=users,ou=system??base", e.getReferralInfo() );
- assertTrue( e.skipReferral() );
- assertEquals( "ldap://hertz:10389/ou=users,dc=example,dc=com??base", e.getReferralInfo() );
- assertTrue( e.skipReferral() );
- assertEquals( "ldap://maxwell:10389/ou=users,ou=system??base", e.getReferralInfo() );
- }
-
- assertNull( results.get( "ou=remoteusers" ) );
-}
-
-
-/**
- * Test of an search operation with a referral
- *
- * search for "cn=alex karasulu" on "c=america, ou=system"
- * we should get a referral URL thrown, which point to
- * "c=usa, ou=system", and ask for a subtree search
- */
-@Test
-public void testSearchWithReferralThrow() throws Exception
-{
- DirContext ctx = getWiredContextThrowOnRefferal( getLdapServer() );
-
- try
+
+
+ /**
+ * Test of an search operation with a referral
+ *
+ * search for "cn=alex karasulu" on "c=america, ou=system"
+ * we should get a referral URL thrown, which point to
+ * "c=usa, ou=system", and ask for a subtree search
+ */
+ @Test
+ public void testSearchBaseWithReferralThrow() throws Exception
{
+ DirContext ctx = getWiredContextThrowOnRefferal( getLdapServer() );
+
SearchControls controls = new SearchControls();
- controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
- ctx.search( "c=america,ou=Countries,ou=system", "(cn=alex karasulu)", controls );
- fail( "Should fail here throwing a ReferralException" );
- }
- catch ( ReferralException re )
- {
- String referral = ( String ) re.getReferralInfo();
- assertEquals( "ldap://localhost:" + getLdapServer().getPort() + "/c=usa,ou=system??sub", referral );
- }
-}
-
-
-/**
- * Test of an search operation with a referral
- *
- * search for "cn=alex karasulu" on "c=america, ou=system"
- * we should get a referral URL thrown, which point to
- * "c=usa, ou=system", and ask for a subtree search
- */
-@Test
-public void testSearchBaseWithReferralThrow() throws Exception
-{
- DirContext ctx = getWiredContextThrowOnRefferal( getLdapServer() );
-
- SearchControls controls = new SearchControls();
- controls.setSearchScope( SearchControls.OBJECT_SCOPE );
-
- try
- {
- ctx.search( "c=america,ou=Countries,ou=system", "(cn=alex karasulu)", controls );
- fail( "Should fail here throwing a ReferralException" );
- }
- catch ( ReferralException re )
- {
- String referral = ( String ) re.getReferralInfo();
- assertEquals( "ldap://localhost:" + getLdapServer().getPort() + "/c=usa,ou=system??base", referral );
- }
-}
-
-
-/**
- * Test of an search operation with a referral after the entry
- * has been renamed.
- *
- * search for "cn=alex karasulu" on "c=usa, ou=system"
- * we should get a referral URL thrown, which point to
- * "c=usa, ou=system", and ask for a subtree search
- */
-@Test
-public void testSearchBaseWithReferralThrowAfterRename() throws Exception
-{
- DirContext ctx = getWiredContextThrowOnRefferal( getLdapServer() );
-
- SearchControls controls = new SearchControls();
- controls.setSearchScope( SearchControls.OBJECT_SCOPE );
-
- try
- {
- ctx.search( "c=america,ou=Countries,ou=system", "(cn=alex karasulu)", controls );
- fail( "Should fail here throwing a ReferralException" );
- }
- catch ( ReferralException re )
- {
- String referral = ( String ) re.getReferralInfo();
- assertEquals( "ldap://localhost:" + getLdapServer().getPort() + "/c=usa,ou=system??base", referral );
- }
-
- ( ( LdapContext ) ctx ).setRequestControls( new javax.naming.ldap.Control[]
- { new ManageReferralControl() } );
-
- // Now let's move the entry
- ctx.rename( "c=america,ou=Countries,ou=system", "c=USA,ou=Countries,ou=system" );
-
- controls.setSearchScope( SearchControls.OBJECT_SCOPE );
-
- ( ( LdapContext ) ctx ).setRequestControls( new javax.naming.ldap.Control[]
- {} );
-
- try
- {
- ctx.search( "c=usa,ou=Countries,ou=system", "(cn=alex karasulu)", controls );
- fail( "Should fail here throwing a ReferralException" );
+ controls.setSearchScope( SearchControls.OBJECT_SCOPE );
+
+ try
+ {
+ ctx.search( "c=america,ou=Countries,ou=system", "(cn=alex karasulu)", controls );
+ fail( "Should fail here throwing a ReferralException" );
+ }
+ catch ( ReferralException re )
+ {
+ String referral = ( String ) re.getReferralInfo();
+ assertEquals( "ldap://localhost:" + getLdapServer().getPort() + "/c=usa,ou=system??base", referral );
+ }
+ finally
+ {
+ ctx.close();
+ }
}
- catch ( ReferralException re )
+
+
+ /**
+ * Test of an search operation with a referral after the entry
+ * has been renamed.
+ *
+ * search for "cn=alex karasulu" on "c=usa, ou=system"
+ * we should get a referral URL thrown, which point to
+ * "c=usa, ou=system", and ask for a subtree search
+ */
+ @Test
+ public void testSearchBaseWithReferralThrowAfterRename() throws Exception
{
- String referral = ( String ) re.getReferralInfo();
- assertEquals( "ldap://localhost:" + getLdapServer().getPort() + "/c=usa,ou=system??base", referral );
+ DirContext ctx = getWiredContextThrowOnRefferal( getLdapServer() );
+
+ SearchControls controls = new SearchControls();
+ controls.setSearchScope( SearchControls.OBJECT_SCOPE );
+
+ try
+ {
+ ctx.search( "c=america,ou=Countries,ou=system", "(cn=alex karasulu)", controls );
+ fail( "Should fail here throwing a ReferralException" );
+ }
+ catch ( ReferralException re )
+ {
+ String referral = ( String ) re.getReferralInfo();
+ assertEquals( "ldap://localhost:" + getLdapServer().getPort() + "/c=usa,ou=system??base", referral );
+ }
+
+ ( ( LdapContext ) ctx ).setRequestControls( new javax.naming.ldap.Control[]
+ { new ManageReferralControl() } );
+
+ // Now let's move the entry
+ ctx.rename( "c=america,ou=Countries,ou=system", "c=USA,ou=Countries,ou=system" );
+
+ controls.setSearchScope( SearchControls.OBJECT_SCOPE );
+
+ ( ( LdapContext ) ctx ).setRequestControls( new javax.naming.ldap.Control[]
+ {} );
+
+ try
+ {
+ ctx.search( "c=usa,ou=Countries,ou=system", "(cn=alex karasulu)", controls );
+ fail( "Should fail here throwing a ReferralException" );
+ }
+ catch ( ReferralException re )
+ {
+ String referral = ( String ) re.getReferralInfo();
+ assertEquals( "ldap://localhost:" + getLdapServer().getPort() + "/c=usa,ou=system??base", referral );
+ }
+ finally
+ {
+ ctx.close();
+ }
}
-}
}
\ No newline at end of file