001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Boris V. Kuznetsov
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.java.security;
022:
023: import java.security.Provider;
024: import java.security.NoSuchAlgorithmException;
025: import java.util.HashMap;
026:
027: import org.apache.harmony.security.tests.support.RandomImpl;
028:
029: import junit.framework.TestCase;
030:
031: /**
032: * Tests for <code>Provider.Service</code> constructor and methods
033: *
034: */
035: public class ProviderServiceTest extends TestCase {
036:
037: public void testService() {
038: Provider p = new MyProvider();
039: try {
040: new Provider.Service(null, "type", "algorithm",
041: "className", null, null);
042: fail("provider is null: No expected NullPointerException");
043: } catch (NullPointerException e) {
044: }
045: try {
046: new Provider.Service(p, null, "algorithm", "className",
047: null, null);
048: fail("type is null: No expected NullPointerException");
049: } catch (NullPointerException e) {
050: }
051: try {
052: new Provider.Service(p, "type", null, "className", null,
053: null);
054: fail("algorithm is null: No expected NullPointerException");
055: } catch (NullPointerException e) {
056: }
057: try {
058: new Provider.Service(p, "type", "algorithm", null, null,
059: null);
060: fail("className is null: No expected NullPointerException");
061: } catch (NullPointerException e) {
062: }
063:
064: Provider.Service s = new Provider.Service(p, "type",
065: "algorithm", "className", null, null);
066:
067: if (!s.getType().equals("type")) {
068: fail("getType() failed");
069: }
070: if (!s.getAlgorithm().equals("algorithm")) {
071: fail("getAlgorithm() failed");
072: }
073: if (s.getProvider() != p) {
074: fail("getProvider() failed");
075: }
076: if (!s.getClassName().equals("className")) {
077: fail("getClassName() failed");
078: }
079: if (!s.supportsParameter(new Object())) {
080: fail("supportsParameter() failed");
081: }
082: }
083:
084: public void testGetAttribute() {
085: Provider p = new MyProvider();
086: Provider.Service s = new Provider.Service(p, "type",
087: "algorithm", "className", null, null);
088: try {
089: s.getAttribute(null);
090: fail("No expected NullPointerException");
091: } catch (NullPointerException e) {
092: }
093:
094: if (s.getAttribute("aaa") != null) {
095: fail("getAttribute(aaa) failed");
096: }
097:
098: HashMap hm = new HashMap();
099: hm.put("attribute", "value");
100: hm.put("KeySize", "1024");
101: hm.put("AAA", "BBB");
102:
103: s = new Provider.Service(p, "type", "algorithm", "className",
104: null, hm);
105: if (s.getAttribute("bbb") != null) {
106: fail("getAttribute(bbb) failed");
107: }
108: if (!s.getAttribute("attribute").equals("value")) {
109: fail("getAttribute(attribute) failed");
110: }
111: if (!s.getAttribute("KeySize").equals("1024")) {
112: fail("getAttribute(KeySize) failed");
113: }
114: }
115:
116: public void testNewInstance() {
117: Provider p = new MyProvider();
118: Provider.Service s = new Provider.Service(p, "SecureRandom",
119: "algorithm",
120: "org.apache.harmony.security.tests.support.RandomImpl",
121: null, null);
122: Object o = null;
123: try {
124: o = s.newInstance(null);
125: } catch (NoSuchAlgorithmException e) {
126: e.printStackTrace();
127: fail("newInstance() failed");
128: }
129: if (!(o instanceof RandomImpl)) {
130: fail("incorrect instance");
131: }
132:
133: try {
134: o = s.newInstance(new Object());
135: fail("No expected NoSuchAlgorithmException");
136: } catch (NoSuchAlgorithmException e) {
137: }
138: }
139:
140: class MyProvider extends Provider {
141: MyProvider() {
142: super ("MyProvider", 1.0, "Provider for testing");
143: put("MessageDigest.SHA-1", "SomeClassName");
144: }
145: }
146: }
|