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 Vera Y. Petrashkova
20  */
21 
22 package javax.net.ssl;
23 
24 import java.security.InvalidAlgorithmParameterException;
25 import java.security.KeyStore;
26 import java.security.KeyStoreException;
27 
28 import javax.net.ssl.TrustManagerFactorySpi;
29 
30 import junit.framework.TestCase;
31 
32 /**
33  * Tests for <code>TrustManagerFactorySpi</code> class constructors and
34  * methods.
35  */
36 
37 public class TrustManagerFactorySpiTests extends TestCase {
38     /**
39      * Constructor for TrustManegerFactorySpiTests.
40      *
41      * @param arg0
42      */
TrustManagerFactorySpiTests(String arg0)43     public TrustManagerFactorySpiTests(String arg0) {
44         super(arg0);
45     }
46 
47     /**
48      * Test for <code>TrustManagerFactorySpi</code> constructor
49      * Assertion: constructs TrustManagerFactorySpi
50      */
testTrustManagerFactorySpi01()51     public void testTrustManagerFactorySpi01() throws Exception {
52         TrustManagerFactorySpi kmfSpi = new MyTrustManagerFactorySpi();
53         assertNull("Not null results", kmfSpi.engineGetTrustManagers());
54         KeyStore kStore = null;
55         ManagerFactoryParameters mfp = null;
56 
57         try {
58             kmfSpi.engineInit(kStore);
59             fail("KeyStoreException must be thrown");
60         } catch (KeyStoreException e) {
61         }
62         try {
63             kmfSpi.engineInit(mfp);
64             fail("InvalidAlgorithmParameterException must be thrown");
65         } catch (InvalidAlgorithmParameterException e) {
66         }
67         assertNull("getTrustManagers() should return null object",
68                 kmfSpi.engineGetTrustManagers());
69 
70         try {
71             kStore = KeyStore.getInstance(KeyStore.getDefaultType());
72             kStore.load(null, null);
73         } catch (KeyStoreException e) {
74             fail("default keystore is not supported");
75             return;
76         }
77         kmfSpi.engineInit(kStore);
78         mfp = new MyTrustManagerFactorySpi.Parameters(null);
79         try {
80             kmfSpi.engineInit(mfp);
81             fail("RuntimeException must be thrown");
82         } catch (RuntimeException e) {
83             assertTrue("Incorrect exception", e.getCause() instanceof KeyStoreException);
84         }
85         mfp = new MyTrustManagerFactorySpi.Parameters(kStore);
86         kmfSpi.engineInit(mfp);
87     }
88 }
89