/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
* @author Boris V. Kuznetsov
* @version $Revision$
*/

package java.security;

import java.util.Map;
import java.util.Properties;
import java.util.Set;

import junit.framework.TestCase;


/**
 * Tests for <code>Security</code> constructor and methods
 * 
 */
public class Security_ImplTest extends TestCase {

    public final void testGetAlgorithmProperty() {
        assertNull("Incorrect result on null parameter", Security
                .getAlgorithmProperty(null, "MyService"));
        assertNull("Incorrect result on null parameter", Security
                .getAlgorithmProperty("MyAlgorithm", null));
        assertNull("Incorrect result (provider not added)", Security
                .getAlgorithmProperty("MyAlgorithm", "MyService"));

        Provider p = new MyProvider();
        Security.addProvider(p);
        try {
            assertEquals("Incorrect result (provider added)",
                    "SomeClassName", Security.getAlgorithmProperty("MyAlGoriThm", "MySerVicE"));    
        } finally { //clean up
            Security.removeProvider(p.getName());
        }
    }

    /*
     * Class under test for Provider[] getProviders()
     */
    public final void testGetProviders() {
        Provider[] providers;

        providers = Security.getProviders();
        for (int i = 0; i < providers.length; i++) {
            // position is 1-based
            assertEquals("Incorrect provider number", i + 1, providers[i]
                    .getProviderNumber());
        }
    }

    /*
     * Class under test for Provider[] getProviders(String)
     */
    public final void testGetProvidersString() {
        try {
            Security.getProviders("AAA.BBB CCC");
            fail("AAA.BBB CCC: No expected InvalidParameterException");
        } catch (InvalidParameterException e) {
        }
    }

    /*
     * Class under test for Provider[] getProviders(Map)
     */
    public final void testGetProvidersMap() {
        Map m = new Properties();
        m.put("AAA.BBB CCC", "");
        m.put("AAA.BBB", "");
        try {
            Security.getProviders(m);
            fail("attribute value is empty string: No expected InvalidParameterException");
        } catch (InvalidParameterException  e) {    
        }
    }

    public final void testGetAlgorithms() {
        Set alg1;
        Set alg2;

        alg1 = Security.getAlgorithms("AAAAAAAAAAAAAAA");
        assertTrue("failed for non-existent service", alg1 != null);
        assertEquals("failed for non-existent service", 0, alg1.size());

        alg1 = Security.getAlgorithms("SecureRandom");
        alg2 = Security.getAlgorithms("seCuReranDom");
        assertEquals("different size", alg1.size(), alg2.size());
        assertTrue("different content", alg2.containsAll(alg1));

        Provider p = new MyProvider();

        try {
            Security.addProvider(p);
            alg1 = Security.getAlgorithms("MyService");
            assertEquals("failed for MyService", 1, alg1.size());
            assertTrue("failed for MyService", alg1.contains("MyAlgorithm"));
        } finally { //clean up
            Security.removeProvider(p.getName());
        }
    }

    class MyProvider extends Provider {
        MyProvider() {
            super("MyProvider", 1.0, "Provider for testing");
            put("MessageDigest.SHA-1", "SomeClassName");
            put("MyService.MyAlgorithm", "SomeClassName");
            put("MyService.MyAlgorithm KeySize", "1024");
        }
    }

}

@@ -2321,32 +2321,33 @@
    %7D%0A%0A    /*
+*
 %0A     * 
 Class under 
@@ -2338,40 +2338,31 @@
   * 
-Class under test for Provider%5B%5D 
+java.security.Security#
 getP
@@ -2404,33 +2404,34 @@
  final void test
-G
+_g
 etProvidersStrin
@@ -2429,18 +2429,89 @@
 ders
-String() %7B
+Ljava_lang_String() %7B%0A        // Regression for Harmony-3154 (non-bug difference)
 %0A   
@@ -2710,32 +2710,33 @@
    %7D%0A%0A    /*
+*
 %0A     * 
 Class under 
@@ -2727,40 +2727,31 @@
   * 
-Class under test for Provider%5B%5D 
+java.security.Security#
 getP
@@ -2790,33 +2790,34 @@
  final void test
-G
+_g
 etProvidersMap()
@@ -2815,15 +2815,86 @@
 ders
-Map() %7B
+Ljava_util_Map() %7B%0A        // Regression for Harmony-3154 (non-bug difference)
 %0A   

Add comments as HARMONY-3186 was closed as non-bug difference.