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 dalvik.annotation.TestTargets; 26 import dalvik.annotation.TestLevel; 27 import dalvik.annotation.TestTargetNew; 28 import dalvik.annotation.TestTargetClass; 29 30 import junit.framework.TestCase; 31 32 import java.security.cert.CertStoreParameters; 33 import java.security.cert.LDAPCertStoreParameters; 34 35 /** 36 * Tests for <code>java.security.cert.LDAPCertStoreParameters</code> 37 * fields and methods 38 * 39 */ 40 @TestTargetClass(LDAPCertStoreParameters.class) 41 public class LDAPCertStoreParametersTest extends TestCase { 42 43 // 44 // Tests 45 // 46 47 /** 48 * Test #1 for <code>LDAPCertStoreParameters()</code> constructor<br> 49 * Assertion: Creates an instance of <code>LDAPCertStoreParameters</code> 50 * with the default parameter values (server name "localhost", port 389) 51 */ 52 @TestTargetNew( 53 level = TestLevel.PARTIAL_COMPLETE, 54 notes = "", 55 method = "LDAPCertStoreParameters", 56 args = {} 57 ) 58 public final void testLDAPCertStoreParameters01() { 59 CertStoreParameters cp = new LDAPCertStoreParameters(); 60 assertTrue("isLDAPCertStoreParameters", 61 cp instanceof LDAPCertStoreParameters); 62 } 63 64 /** 65 * Test #2 for <code>LDAPCertStoreParameters()</code> constructor<br> 66 * Assertion: Creates an instance of <code>LDAPCertStoreParameters</code> 67 * with the default parameter values (server name "localhost", port 389) 68 */ 69 @TestTargetNew( 70 level = TestLevel.PARTIAL_COMPLETE, 71 notes = "", 72 method = "LDAPCertStoreParameters", 73 args = {} 74 ) 75 public final void testLDAPCertStoreParameters02() { 76 LDAPCertStoreParameters cp = new LDAPCertStoreParameters(); 77 assertEquals("host", "localhost", cp.getServerName()); 78 assertEquals("port", 389, cp.getPort()); 79 } 80 81 /** 82 * Test #1 for <code>LDAPCertStoreParameters(String)</code> constructor<br> 83 * Assertion: Creates an instance of <code>LDAPCertStoreParameters</code> 84 * with the specified server name and a default port of 389 85 */ 86 @TestTargetNew( 87 level = TestLevel.PARTIAL_COMPLETE, 88 notes = "", 89 method = "LDAPCertStoreParameters", 90 args = {java.lang.String.class} 91 ) 92 public final void testLDAPCertStoreParametersString01() { 93 CertStoreParameters cp = new LDAPCertStoreParameters("myhost"); 94 assertTrue("isLDAPCertStoreParameters", 95 cp instanceof LDAPCertStoreParameters); 96 } 97 98 /** 99 * Test #2 for <code>LDAPCertStoreParameters(String)</code> constructor<br> 100 * Assertion: Creates an instance of <code>LDAPCertStoreParameters</code> 101 * with the specified server name and a default port of 389 102 */ 103 @TestTargetNew( 104 level = TestLevel.PARTIAL_COMPLETE, 105 notes = "", 106 method = "LDAPCertStoreParameters", 107 args = {java.lang.String.class} 108 ) 109 public final void testLDAPCertStoreParametersString02() { 110 String serverName = "myhost"; 111 LDAPCertStoreParameters cp = new LDAPCertStoreParameters(serverName); 112 assertTrue("host", serverName.equals(cp.getServerName())); 113 assertEquals("port", 389, cp.getPort()); 114 } 115 116 /** 117 * Test #3 for <code>LDAPCertStoreParameters(String)</code> constructor<br> 118 * Assertion: throws <code>NullPointerException</code> - 119 * if <code>serverName</code> is <code>null</code> 120 */ 121 @TestTargetNew( 122 level = TestLevel.PARTIAL_COMPLETE, 123 notes = "Verifies NullPointerException.", 124 method = "LDAPCertStoreParameters", 125 args = {java.lang.String.class} 126 ) 127 public final void testLDAPCertStoreParametersString03() { 128 try { 129 new LDAPCertStoreParameters(null); 130 fail("NPE expected"); 131 } catch (NullPointerException e) { 132 } 133 } 134 135 /** 136 * Test #1 for <code>LDAPCertStoreParameters(String, int)</code> constructor<br> 137 * Assertion: Creates an instance of <code>LDAPCertStoreParameters</code> 138 * with the specified parameter values 139 */ 140 @TestTargetNew( 141 level = TestLevel.PARTIAL_COMPLETE, 142 notes = "", 143 method = "LDAPCertStoreParameters", 144 args = {java.lang.String.class, int.class} 145 ) 146 public final void testLDAPCertStoreParametersStringint01() { 147 CertStoreParameters cp = new LDAPCertStoreParameters("myhost", 1098); 148 assertTrue("isLDAPCertStoreParameters", 149 cp instanceof LDAPCertStoreParameters); 150 } 151 152 /** 153 * Test #2 for <code>LDAPCertStoreParameters(String, int)</code> constructor<br> 154 * Assertion: Creates an instance of <code>LDAPCertStoreParameters</code> 155 * with the specified parameter values 156 */ 157 @TestTargetNew( 158 level = TestLevel.PARTIAL_COMPLETE, 159 notes = "", 160 method = "LDAPCertStoreParameters", 161 args = {java.lang.String.class, int.class} 162 ) 163 public final void testLDAPCertStoreParametersStringint02() { 164 String serverName = "myhost"; 165 int portNumber = 1099; 166 LDAPCertStoreParameters cp = 167 new LDAPCertStoreParameters(serverName, portNumber); 168 assertTrue("host", serverName.equals(cp.getServerName())); 169 assertTrue("port", cp.getPort() == portNumber); 170 } 171 172 /** 173 * Test #3 for <code>LDAPCertStoreParameters(String, int)</code> constructor<br> 174 * Assertion: throws <code>NullPointerException</code> - 175 * if <code>serverName</code> is <code>null</code> 176 */ 177 @TestTargetNew( 178 level = TestLevel.PARTIAL_COMPLETE, 179 notes = "Verifies NullPointerException.", 180 method = "LDAPCertStoreParameters", 181 args = {java.lang.String.class, int.class} 182 ) 183 public final void testLDAPCertStoreParametersStringint03() { 184 try { 185 new LDAPCertStoreParameters(null, 0); 186 fail("NPE expected"); 187 } catch (NullPointerException e) { 188 } 189 190 String serverName = "myhost"; 191 int[] portNumber = {-1, -100, Integer.MIN_VALUE, Integer.MAX_VALUE}; 192 for (int i = 0; i < portNumber.length; i++) { 193 try { 194 new LDAPCertStoreParameters(serverName, portNumber[i]); 195 } catch (Exception e) { 196 fail("Unexpected exception for incorrect integer parametr"); 197 } 198 } 199 } 200 201 /** 202 * Test for <code>clone()</code> method<br> 203 * Assertion: Returns a copy of this object 204 */ 205 @TestTargetNew( 206 level = TestLevel.COMPLETE, 207 notes = "", 208 method = "clone", 209 args = {} 210 ) 211 public final void testClone() { 212 LDAPCertStoreParameters cp1 = 213 new LDAPCertStoreParameters("myhost", 1100); 214 LDAPCertStoreParameters cp2 = (LDAPCertStoreParameters)cp1.clone(); 215 // check that that we have new object 216 assertTrue("newObject", cp1 != cp2); 217 assertTrue("hostsTheSame", 218 cp1.getServerName().equals(cp2.getServerName())); 219 assertTrue("portsTheSame", cp1.getPort() == cp2.getPort()); 220 } 221 222 /** 223 * Test for <code>toString()</code> method<br> 224 * Assertion: returns the formatted string describing parameters 225 */ 226 @TestTargetNew( 227 level = TestLevel.COMPLETE, 228 notes = "", 229 method = "toString", 230 args = {} 231 ) 232 public final void testToString() { 233 LDAPCertStoreParameters cp1 = 234 new LDAPCertStoreParameters("myhost", 1101); 235 236 assertNotNull(cp1.toString()); 237 } 238 239 /** 240 * Test for <code>toString()</code> method<br> 241 * Assertion: returns the port number 242 */ 243 @TestTargetNew( 244 level = TestLevel.COMPLETE, 245 notes = "", 246 method = "getPort", 247 args = {} 248 ) 249 public final void testGetPort() { 250 int portNumber = -1099; 251 LDAPCertStoreParameters cp = 252 new LDAPCertStoreParameters("serverName", portNumber); 253 assertTrue(cp.getPort() == portNumber); 254 } 255 256 /** 257 * Test for <code>toString()</code> method<br> 258 * Assertion: returns the server name (never <code>null</code>) 259 */ 260 @TestTargetNew( 261 level = TestLevel.COMPLETE, 262 notes = "", 263 method = "getServerName", 264 args = {} 265 ) 266 public final void testGetServerName() { 267 LDAPCertStoreParameters cp = 268 new LDAPCertStoreParameters("serverName"); 269 assertNotNull(cp.getServerName()); 270 } 271 272 } 273