Home | History | Annotate | Download | only in ssl
      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 package org.apache.harmony.xnet.tests.javax.net.ssl;
     19 
     20 import java.security.KeyStore;
     21 import java.security.KeyStoreException;
     22 import java.security.KeyStore.Builder;
     23 import java.util.ArrayList;
     24 import java.util.List;
     25 
     26 import javax.net.ssl.KeyStoreBuilderParameters;
     27 
     28 import junit.framework.TestCase;
     29 
     30 /**
     31  * Tests for <code>KeyStoreBuilderParameters</code> class constructors and
     32  * methods.
     33  */
     34 public class KeyStoreBuilderParametersTest extends TestCase {
     35 
     36     class EmptyBuilder extends KeyStore.Builder {
     37         @Override
     38         public KeyStore getKeyStore() throws KeyStoreException {
     39             return null;
     40         }
     41 
     42         @Override
     43         public KeyStore.ProtectionParameter getProtectionParameter(String alias)
     44                 throws KeyStoreException {
     45             return null;
     46         }
     47     }
     48 
     49     /*
     50      * Class under test for void KeyStoreBuilderParameters(KeyStore.Builder)
     51      */
     52     public final void testKeyStoreBuilderParametersBuilder() {
     53         try {
     54             new KeyStoreBuilderParameters((KeyStore.Builder) null);
     55         } catch (NullPointerException e) {
     56             // javadoc says this should throw NPE, but it doesn't
     57             fail("no NPE expected");
     58         }
     59     }
     60 
     61     /*
     62      * Class under test for void KeyStoreBuilderParameters(List)
     63      */
     64     public final void testKeyStoreBuilderParametersList() {
     65         try {
     66             new KeyStoreBuilderParameters((List<?>) null);
     67             fail("expected a NPE");
     68         } catch (NullPointerException e) {
     69         }
     70 
     71         try {
     72             new KeyStoreBuilderParameters(new ArrayList<Builder>());
     73             fail("expected a IAE");
     74         } catch (IllegalArgumentException e) {
     75         }
     76 
     77     }
     78 
     79     @SuppressWarnings("unchecked")
     80     public final void testGetParameters() {
     81         List<Builder> ksbuilders;
     82         KeyStore.Builder builder = new EmptyBuilder();
     83         List<Object> result;
     84         KeyStoreBuilderParameters param = new KeyStoreBuilderParameters(builder);
     85         result = param.getParameters();
     86         try {
     87             result.add(new EmptyBuilder());
     88             fail("The list is modifiable");
     89         } catch (UnsupportedOperationException e) {
     90         }
     91         assertEquals("incorrect size", 1, result.size());
     92         assertTrue("incorrect list", result.contains(builder));
     93 
     94         ksbuilders = new ArrayList<Builder>();
     95         ksbuilders.add(builder);
     96         ksbuilders.add(new EmptyBuilder());
     97         param = new KeyStoreBuilderParameters(ksbuilders);
     98         result = param.getParameters();
     99         try {
    100             result.add(new Object());
    101             fail("The list is modifiable");
    102         } catch (UnsupportedOperationException e) {
    103         }
    104         assertEquals("incorrect size", 2, result.size());
    105         assertTrue("incorrect list", result.containsAll(ksbuilders));
    106     }
    107 }
    108