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 /**
19 * @author Vladimir N. Molotkov
20 * @version $Revision$
21 */
22 
23 package tests.security.cert;
24 
25 import junit.framework.TestCase;
26 
27 import java.security.cert.CertStoreParameters;
28 import java.security.cert.LDAPCertStoreParameters;
29 
30 /**
31  * Tests for <code>java.security.cert.LDAPCertStoreParameters</code>
32  * fields and methods
33  *
34  */
35 public class LDAPCertStoreParametersTest extends TestCase {
36 
37     //
38     // Tests
39     //
40 
41     /**
42      * Test #1 for <code>LDAPCertStoreParameters()</code> constructor<br>
43      * Assertion: Creates an instance of <code>LDAPCertStoreParameters</code>
44      * with the default parameter values (server name "localhost", port 389)
45      */
testLDAPCertStoreParameters01()46     public final void testLDAPCertStoreParameters01() {
47         CertStoreParameters cp = new LDAPCertStoreParameters();
48         assertTrue("isLDAPCertStoreParameters",
49                 cp instanceof LDAPCertStoreParameters);
50     }
51 
52     /**
53      * Test #2 for <code>LDAPCertStoreParameters()</code> constructor<br>
54      * Assertion: Creates an instance of <code>LDAPCertStoreParameters</code>
55      * with the default parameter values (server name "localhost", port 389)
56      */
testLDAPCertStoreParameters02()57     public final void testLDAPCertStoreParameters02() {
58         LDAPCertStoreParameters cp = new LDAPCertStoreParameters();
59         assertEquals("host", "localhost", cp.getServerName());
60         assertEquals("port", 389, cp.getPort());
61     }
62 
63     /**
64      * Test #1 for <code>LDAPCertStoreParameters(String)</code> constructor<br>
65      * Assertion: Creates an instance of <code>LDAPCertStoreParameters</code>
66      * with the specified server name and a default port of 389
67      */
testLDAPCertStoreParametersString01()68     public final void testLDAPCertStoreParametersString01() {
69         CertStoreParameters cp = new LDAPCertStoreParameters("myhost");
70         assertTrue("isLDAPCertStoreParameters",
71                 cp instanceof LDAPCertStoreParameters);
72     }
73 
74     /**
75      * Test #2 for <code>LDAPCertStoreParameters(String)</code> constructor<br>
76      * Assertion: Creates an instance of <code>LDAPCertStoreParameters</code>
77      * with the specified server name and a default port of 389
78      */
testLDAPCertStoreParametersString02()79     public final void testLDAPCertStoreParametersString02() {
80         String serverName = "myhost";
81         LDAPCertStoreParameters cp = new LDAPCertStoreParameters(serverName);
82         assertTrue("host", serverName.equals(cp.getServerName()));
83         assertEquals("port", 389, cp.getPort());
84     }
85 
86     /**
87      * Test #3 for <code>LDAPCertStoreParameters(String)</code> constructor<br>
88      * Assertion: throws <code>NullPointerException</code> -
89      * if <code>serverName</code> is <code>null</code>
90      */
testLDAPCertStoreParametersString03()91     public final void testLDAPCertStoreParametersString03() {
92         try {
93             new LDAPCertStoreParameters(null);
94             fail("NPE expected");
95         } catch (NullPointerException e) {
96         }
97     }
98 
99     /**
100      * Test #1 for <code>LDAPCertStoreParameters(String, int)</code> constructor<br>
101      * Assertion: Creates an instance of <code>LDAPCertStoreParameters</code>
102      * with the specified parameter values
103      */
testLDAPCertStoreParametersStringint01()104     public final void testLDAPCertStoreParametersStringint01() {
105         CertStoreParameters cp = new LDAPCertStoreParameters("myhost", 1098);
106         assertTrue("isLDAPCertStoreParameters",
107                 cp instanceof LDAPCertStoreParameters);
108     }
109 
110     /**
111      * Test #2 for <code>LDAPCertStoreParameters(String, int)</code> constructor<br>
112      * Assertion: Creates an instance of <code>LDAPCertStoreParameters</code>
113      * with the specified parameter values
114      */
testLDAPCertStoreParametersStringint02()115     public final void testLDAPCertStoreParametersStringint02() {
116         String serverName = "myhost";
117         int portNumber = 1099;
118         LDAPCertStoreParameters cp =
119             new LDAPCertStoreParameters(serverName, portNumber);
120         assertTrue("host", serverName.equals(cp.getServerName()));
121         assertTrue("port", cp.getPort() == portNumber);
122     }
123 
124     /**
125      * Test #3 for <code>LDAPCertStoreParameters(String, int)</code> constructor<br>
126      * Assertion: throws <code>NullPointerException</code> -
127      * if <code>serverName</code> is <code>null</code>
128      */
testLDAPCertStoreParametersStringint03()129     public final void testLDAPCertStoreParametersStringint03() {
130         try {
131             new LDAPCertStoreParameters(null, 0);
132             fail("NPE expected");
133         } catch (NullPointerException e) {
134         }
135 
136         String serverName = "myhost";
137         int[] portNumber = {-1, -100, Integer.MIN_VALUE, Integer.MAX_VALUE};
138         for (int i = 0; i < portNumber.length; i++) {
139             try {
140                 new LDAPCertStoreParameters(serverName, portNumber[i]);
141             } catch (Exception e) {
142                 fail("Unexpected exception for incorrect integer parametr");
143             }
144         }
145     }
146 
147     /**
148      * Test for <code>clone()</code> method<br>
149      * Assertion: Returns a copy of this object
150      */
testClone()151     public final void testClone() {
152         LDAPCertStoreParameters cp1 =
153             new LDAPCertStoreParameters("myhost", 1100);
154         LDAPCertStoreParameters cp2 = (LDAPCertStoreParameters)cp1.clone();
155         // check that that we have new object
156         assertTrue("newObject", cp1 != cp2);
157         assertTrue("hostsTheSame",
158                 cp1.getServerName().equals(cp2.getServerName()));
159         assertTrue("portsTheSame", cp1.getPort() == cp2.getPort());
160     }
161 
162     /**
163      * Test for <code>toString()</code> method<br>
164      * Assertion: returns the formatted string describing parameters
165      */
testToString()166     public final void testToString() {
167         LDAPCertStoreParameters cp1 =
168             new LDAPCertStoreParameters("myhost", 1101);
169 
170         assertNotNull(cp1.toString());
171     }
172 
173     /**
174      * Test for <code>toString()</code> method<br>
175      * Assertion: returns the port number
176      */
testGetPort()177     public final void testGetPort() {
178         int portNumber = -1099;
179         LDAPCertStoreParameters cp =
180             new LDAPCertStoreParameters("serverName", portNumber);
181         assertTrue(cp.getPort() == portNumber);
182     }
183 
184     /**
185      * Test for <code>toString()</code> method<br>
186      * Assertion: returns the server name (never <code>null</code>)
187      */
testGetServerName()188     public final void testGetServerName() {
189         LDAPCertStoreParameters cp =
190             new LDAPCertStoreParameters("serverName");
191         assertNotNull(cp.getServerName());
192     }
193 
194 }
195