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 Boris V. Kuznetsov 20*/ 21 22package org.apache.harmony.security.tests.java.security; 23 24import java.security.Provider; 25import java.security.NoSuchAlgorithmException; 26import java.util.HashMap; 27 28import org.apache.harmony.security.tests.support.RandomImpl; 29 30import junit.framework.TestCase; 31 32 33/** 34 * Tests for <code>Provider.Service</code> constructor and methods 35 * 36 */ 37public class ProviderServiceTest extends TestCase { 38 39 public void testService() { 40 Provider p = new MyProvider(); 41 try { 42 new Provider.Service(null, "type", "algorithm", 43 "className", null, null); 44 fail("provider is null: No expected NullPointerException"); 45 } catch (NullPointerException e) { 46 } 47 try { 48 new Provider.Service(p, null, "algorithm", 49 "className", null, null); 50 fail("type is null: No expected NullPointerException"); 51 } catch (NullPointerException e) { 52 } 53 try { 54 new Provider.Service(p, "type", null, 55 "className", null, null); 56 fail("algorithm is null: No expected NullPointerException"); 57 } catch (NullPointerException e) { 58 } 59 try { 60 new Provider.Service(p, "type", "algorithm", 61 null, null, null); 62 fail("className is null: No expected NullPointerException"); 63 } catch (NullPointerException e) { 64 } 65 66 Provider.Service s = new Provider.Service(p, 67 "type", "algorithm", "className", null, null); 68 69 assertEquals("getType() failed", "type", s.getType()); 70 assertEquals("getAlgorithm() failed", "algorithm", s.getAlgorithm()); 71 assertSame("getProvider() failed", p, s.getProvider()); 72 assertEquals("getClassName() failed", "className", s.getClassName()); 73 assertTrue("supportsParameter() failed", s 74 .supportsParameter(new Object())); 75 } 76 77 public void testGetAttribute() { 78 Provider p = new MyProvider(); 79 Provider.Service s = new Provider.Service(p, 80 "type", "algorithm", "className", null, null); 81 try { 82 s.getAttribute(null); 83 fail("No expected NullPointerException"); 84 } catch (NullPointerException e) { 85 } 86 87 assertNull("getAttribute(aaa) failed", s.getAttribute("aaa")); 88 89 HashMap hm = new HashMap(); 90 hm.put("attribute", "value"); 91 hm.put("KeySize", "1024"); 92 hm.put("AAA", "BBB"); 93 94 s = new Provider.Service(p, "type", "algorithm", "className", 95 null, hm); 96 assertNull("getAttribute(bbb) failed", s.getAttribute("bbb")); 97 assertEquals("getAttribute(attribute) failed", "value", s 98 .getAttribute("attribute")); 99 assertEquals("getAttribute(KeySize) failed", "1024", s 100 .getAttribute("KeySize")); 101 } 102 103 public void testNewInstance() throws NoSuchAlgorithmException { 104 Provider p = new MyProvider(); 105 Provider.Service s = new Provider.Service(p, "SecureRandom", 106 "algorithm", 107 "org.apache.harmony.security.tests.support.RandomImpl", 108 null, null); 109 110 Object o = s.newInstance(null); 111 assertTrue("incorrect instance", o instanceof RandomImpl); 112 113 try { 114 o = s.newInstance(new Object()); 115 fail("No expected NoSuchAlgorithmException"); 116 } catch (NoSuchAlgorithmException e) { 117 } 118 } 119 120 class MyProvider extends Provider { 121 MyProvider() { 122 super("MyProvider", 1.0, "Provider for testing"); 123 put("MessageDigest.SHA-1", "SomeClassName"); 124 } 125 } 126} 127