View Javadoc

1   /*
2    * Copyright 2008 Brian Thomas Matthews
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.btmatthews.ldapunit;
18  
19  import java.util.HashSet;
20  import java.util.Set;
21  
22  import junit.framework.TestCase;
23  import netscape.ldap.LDAPConnection;
24  import netscape.ldap.LDAPEntry;
25  import netscape.ldap.LDAPException;
26  import netscape.ldap.LDAPSearchResults;
27  
28  /**
29   * @author <a href="brian@btmatthews.com">Brian Matthews</a>
30   * @version 1.0
31   */
32  public abstract class LDAPUnitTestCase
33      extends TestCase
34  {
35      /**
36       * The LDAP connection.
37       */
38      private LDAPConnection connection;
39  
40      /**
41       * The default constructor.
42       */
43      public LDAPUnitTestCase()
44      {
45      }
46  
47      public LDAPUnitTestCase(final String name)
48      {
49          super(name);
50      }
51  
52      protected void setUp()
53          throws Exception
54      {
55          connection = null;
56          super.setUp();
57      }
58  
59      protected void tearDown()
60          throws Exception
61      {
62          super.tearDown();
63          if (this.connection != null)
64          {
65              this.connection.disconnect();
66          }
67      }
68  
69      protected void assertContains(final String queryString,
70          final ILDAPDataSet dataSet)
71      {
72      }
73  
74      protected void assertMatches(final String queryString,
75          final ILDAPDataSet dataSet)
76      {
77          try
78          {
79              final String[] attributeNames = dataSet.getAttributeNames();
80              final LDAPSearchResults results = this.connection.search(
81                  queryString, 0, null, attributeNames, false);
82              final Set dataRecords = new HashSet();
83              while (results.hasMoreElements())
84              {
85                  final LDAPEntry entry = results.next();
86                  final Object[] dataRecord = new Object[attributeNames.length];
87                  for (int i = 0; i < attributeNames.length; ++i)
88                  {
89                      dataRecord[i] = entry.getAttribute(attributeNames[i]);
90                  }
91                  dataRecords.add(dataRecord);
92  
93              }
94          }
95          catch (LDAPException e)
96          {
97          }
98      }
99  
100     protected void assertExists(final LDAPConnection connection,
101         final String queryString)
102     {
103     }
104 
105     protected void assertExists(final LDAPConnection connection,
106         final String queryString, final int count)
107     {
108     }
109 
110     protected void assertNotExists(final LDAPConnection connection,
111         final String queryString)
112     {
113     }
114 }