KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > directory > RunningTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.directory;
19
20 import junit.framework.TestCase;
21 import org.apache.geronimo.system.serverinfo.BasicServerInfo;
22 import org.apache.geronimo.system.serverinfo.ServerInfo;
23
24 import javax.naming.Context JavaDoc;
25 import javax.naming.NameClassPair JavaDoc;
26 import javax.naming.NamingEnumeration JavaDoc;
27 import javax.naming.directory.DirContext JavaDoc;
28 import javax.naming.directory.InitialDirContext JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Hashtable JavaDoc;
32
33 public class RunningTest extends TestCase {
34
35     private static final String JavaDoc PRINCIPAL = "uid=admin,ou=system";
36     private static final String JavaDoc CREDENTIALS = "secret";
37     private ClassLoader JavaDoc cl = this.getClass().getClassLoader();
38
39     private DirectoryGBean directory;
40
41     public void testRunning() throws Exception JavaDoc {
42
43         Hashtable JavaDoc env = new Hashtable JavaDoc();
44         env.put(Context.PROVIDER_URL, "ldap://localhost:9389");
45         String JavaDoc ldapContextFactory = System.getProperty("initial.context.factory");
46         if (ldapContextFactory == null) ldapContextFactory = "com.sun.jndi.ldap.LdapCtxFactory";
47         env.put(Context.INITIAL_CONTEXT_FACTORY,
48                 ldapContextFactory);
49         //env.put( Context.SECURITY_AUTHENTICATION, "simple");
50
env.put( Context.SECURITY_PRINCIPAL, PRINCIPAL);
51         env.put( Context.SECURITY_CREDENTIALS, CREDENTIALS);
52         DirContext JavaDoc ctx = new InitialDirContext JavaDoc(env);
53
54         // Perform search using URL
55
// NamingEnumeration answer = ctx.search(
56
// "ldap://localhost:389/ou=system", "(uid=admin)", null);
57
HashSet JavaDoc set = new HashSet JavaDoc();
58
59         NamingEnumeration JavaDoc list = ctx.list("ou=system");
60
61         while (list.hasMore()) {
62             NameClassPair JavaDoc ncp = (NameClassPair JavaDoc) list.next();
63             set.add(ncp.getName());
64         }
65
66         assertTrue(set.contains("uid=admin"));
67         assertTrue( set.contains( "ou=users" ) );
68         assertTrue( set.contains( "ou=groups" ) );
69         assertTrue( set.contains( "ou=configuration" ) );
70         assertTrue( set.contains( "prefNodeName=sysPrefRoot" ) );
71
72     }
73
74     protected void setUp() throws Exception JavaDoc {
75
76         URL JavaDoc configURL = cl.getResource("directory.xml");
77         if (configURL == null) {
78             throw new Exception JavaDoc("Can't find config file on classpath");
79         }
80         String JavaDoc path = configURL.getPath();
81         path = path.substring(0, path.lastIndexOf("/"));
82         ServerInfo serverInfo = new BasicServerInfo(path);
83         directory = new DirectoryGBean(cl, null, true, "directory.xml", serverInfo);
84         directory.setEnableNetworking(true);
85         directory.setPort(9389);
86         directory.setProviderURL("ou=system");
87         directory.setSecurityAuthentication("simple");
88         directory.setSecurityCredentials(CREDENTIALS);
89         directory.setSecurityPrincipal(PRINCIPAL);
90         directory.doStart();
91
92
93     }
94
95     protected void tearDown() throws Exception JavaDoc {
96         directory.doStop();
97     }
98
99 }
100
Popular Tags