1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.ldap;
21  
22  
23  import junit.framework.TestCase;
24  import org.apache.directory.server.core.DirectoryService;
25  import org.apache.directory.server.ldap.LdapService;
26  import org.apache.directory.server.ldap.handlers.AbandonHandler;
27  import org.apache.directory.server.ldap.handlers.AddHandler;
28  import org.apache.directory.server.ldap.handlers.BindHandler;
29  import org.apache.directory.server.ldap.handlers.CompareHandler;
30  import org.apache.directory.server.ldap.handlers.DeleteHandler;
31  import org.apache.directory.server.ldap.handlers.ModifyDnHandler;
32  import org.apache.directory.server.ldap.handlers.ModifyHandler;
33  import org.apache.directory.server.ldap.handlers.SearchHandler;
34  import org.apache.directory.server.ldap.handlers.UnbindHandler;
35  import org.apache.directory.shared.ldap.NotImplementedException;
36  import org.apache.directory.shared.ldap.exception.LdapNamingException;
37  import org.apache.directory.shared.ldap.message.AbandonRequest;
38  import org.apache.directory.shared.ldap.message.AddRequest;
39  import org.apache.directory.shared.ldap.message.BindRequest;
40  import org.apache.directory.shared.ldap.message.CompareRequest;
41  import org.apache.directory.shared.ldap.message.DeleteRequest;
42  import org.apache.directory.shared.ldap.message.ModifyDnRequest;
43  import org.apache.directory.shared.ldap.message.ModifyRequest;
44  import org.apache.directory.shared.ldap.message.SearchRequest;
45  import org.apache.directory.shared.ldap.message.UnbindRequest;
46  import org.apache.mina.common.IoSession;
47  
48  
49  /**
50   * This test is simply used to test that handlers can be set properly.
51   *
52   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
53   * @version $Rev: 692919 $
54   */
55  public class SettingAlternativeHandlersTest extends TestCase
56  {
57      LdapService ldapService;
58  
59  
60      public void setUp() throws Exception
61      {
62          ldapService = new LdapService();
63          
64          if ( getName().equals( "testAlternativeConfiguration" ) )
65          {
66              ldapService.setAbandonHandler( new BogusAbandonHandler() );
67              ldapService.setAddHandler( new BogusAddHandler() );
68              ldapService.setBindHandler( new BogusBindHandler() );
69              ldapService.setCompareHandler( new BogusCompareHandler() );
70              ldapService.setDeleteHandler( new BogusDeleteHandler() );
71              ldapService.setModifyDnHandler( new BogusModifyDnHandler() );
72              ldapService.setModifyHandler( new BogusModifyHandler() );
73              ldapService.setSearchHandler( new BogusSearchHandler() );
74              ldapService.setUnbindHandler( new BogusUnbindHandler() );
75          }
76      }
77  
78  
79      /**
80       * Tests to make sure all the default handlers are kicking in properly with
81       * the right request type.
82       *
83       * @throws LdapNamingException if there are problems initializing the
84       * provider
85       */
86      public void testDefaultOperation() throws LdapNamingException
87      {
88          assertEquals( ldapService.getName(), LdapService.SERVICE_NAME );
89      }
90  
91  
92      /**
93       * Tests to make sure handlers for alternative configurations are kicking
94       * in properly with the right request type.
95       *
96       * @throws LdapNamingException if there are problems initializing the
97       * provider
98       */
99      public void testAlternativeConfiguration() throws LdapNamingException
100     {
101         assertEquals( ldapService.getAbandonHandler().getClass(), BogusAbandonHandler.class  );
102         assertEquals( ldapService.getAddHandler().getClass(), BogusAddHandler.class  );
103         assertEquals( ldapService.getBindHandler().getClass(), BogusBindHandler.class  );
104         assertEquals( ldapService.getCompareHandler().getClass(), BogusCompareHandler.class  );
105         assertEquals( ldapService.getDeleteHandler().getClass(), BogusDeleteHandler.class  );
106         assertEquals( ldapService.getModifyDnHandler().getClass(), BogusModifyDnHandler.class  );
107         assertEquals( ldapService.getModifyHandler().getClass(), BogusModifyHandler.class  );
108         assertEquals( ldapService.getSearchHandler().getClass(), BogusSearchHandler.class  );
109         assertEquals( ldapService.getUnbindHandler().getClass(), BogusUnbindHandler.class  );
110         assertEquals( ldapService.getName(), LdapService.SERVICE_NAME );
111     }
112 
113     
114     public static class BogusAbandonHandler extends AbandonHandler
115     {
116         public void abandonMessageReceived( IoSession session, AbandonRequest request )
117         {
118             throw new NotImplementedException( "handler not implemented!" );
119         }
120     }
121 
122     
123     public static class BogusUnbindHandler extends UnbindHandler
124     {
125         public void unbindMessageReceived( IoSession session, UnbindRequest request )
126         {
127             throw new NotImplementedException( "handler not implemented!" );
128         }
129     }
130 
131     public static class BogusAddHandler extends AddHandler
132     {
133         public void addMessageReceived( IoSession session, AddRequest request )
134         {
135             throw new NotImplementedException( "handler not implemented!" );
136         }
137     }
138 
139     public static class BogusBindHandler extends BindHandler
140     {
141         public void setDirectoryService( DirectoryService directoryService )
142         {
143         }
144 
145 
146         public void bindMessageReceived( IoSession session, BindRequest request )
147         {
148             throw new NotImplementedException( "handler not implemented!" );
149         }
150     }
151 
152     public static class BogusCompareHandler extends CompareHandler
153     {
154         public void compareMessageReceived( IoSession session, CompareRequest request )
155         {
156             throw new NotImplementedException( "handler not implemented!" );
157         }
158     }
159 
160     public static class BogusDeleteHandler extends  DeleteHandler
161     {
162         public void deleteMessageReceived( IoSession session, DeleteRequest request )
163         {
164             throw new NotImplementedException( "handler not implemented!" );
165         }
166     }
167 
168     public static class BogusModifyDnHandler extends  ModifyDnHandler
169     {
170         public void modifyDnMessageReceived( IoSession session, ModifyDnRequest request )
171         {
172             throw new NotImplementedException( "handler not implemented!" );
173         }
174     }
175 
176     public static class BogusModifyHandler extends ModifyHandler
177     {
178         public void modifyMessageReceived( IoSession session, ModifyRequest request )
179         {
180             throw new NotImplementedException( "handler not implemented!" );
181         }
182     }
183 
184     public static class BogusSearchHandler extends SearchHandler
185     {
186         public void searchMessageReceived( IoSession session, SearchRequest request )
187         {
188             throw new NotImplementedException( "handler not implemented!" );
189         }
190     }
191 }