/**
 * 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.
 */
package org.apache.commons.crypto.random;

import java.security.GeneralSecurityException;
import java.util.Properties;

import org.junit.Assert;
import org.junit.Test;

public class CryptoRandomFactoryTest {

    @Test(expected=NullPointerException.class)
    public void testNull() throws Exception {
        CryptoRandomFactory.getCryptoRandom(null);
    }

    @Test
    public void testEmpty() throws Exception {
        final Properties props = new Properties();
        props.setProperty(CryptoRandomFactory.CLASSES_KEY, "");
        CryptoRandomFactory.getCryptoRandom(props);
    }


    @Test
    public void testDefaultRandom() throws GeneralSecurityException {
        Properties props = new Properties();
        CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props);
        Assert.assertEquals(OpenSslCryptoRandom.class.getName(),
            random.getClass().getName());
    }

    @Test
    public void testGetOSRandom() throws GeneralSecurityException {
        Properties props = new Properties();
        props.setProperty(
            CryptoRandomFactory.CLASSES_KEY,
            CryptoRandomFactory.RandomProvider.OS.getClassName());
        CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props);

        Assert.assertEquals(OsCryptoRandom.class.getName(), random.getClass()
            .getName());
    }

    @Test
    public void testFullClassName() throws GeneralSecurityException {
        Properties props = new Properties();
        props.setProperty(
            CryptoRandomFactory.CLASSES_KEY,
            OsCryptoRandom.class.getName());
        CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props);

        Assert.assertEquals(OsCryptoRandom.class.getName(), random.getClass()
            .getName());
    }

    @Test(expected = GeneralSecurityException.class)
    public void testInvalidRandom() throws GeneralSecurityException {
        Properties properties = new Properties();
        properties.setProperty(
            CryptoRandomFactory.CLASSES_KEY,
            "InvalidCipherName");
        CryptoRandomFactory.getCryptoRandom(properties);
    }

    @Test
    public void testAbstractRandom() {
        Properties props = new Properties();
        props.setProperty(CryptoRandomFactory.CLASSES_KEY, AbstractRandom.class.getName());
        try {
            CryptoRandomFactory.getCryptoRandom(props);
            Assert.fail("Expected GeneralSecurityException");
        } catch (GeneralSecurityException e) {
            final String message = e.getMessage();
            Assert.assertTrue(message, message.contains("InstantiationException"));
        }

    }

    @Test
    public void testDummmyRandom() throws GeneralSecurityException {
        Properties props = new Properties();
        props.setProperty(CryptoRandomFactory.CLASSES_KEY, DummyRandom.class.getName());
        try {
            CryptoRandomFactory.getCryptoRandom(props);
            Assert.fail("Expected GeneralSecurityException");
        } catch (GeneralSecurityException e) {
            final String message = e.getMessage();
            Assert.assertTrue(message, message.contains("NoSuchMethodException"));
        }
    }

    @Test(expected=IllegalArgumentException.class)
    public void testNoClasses() throws Exception {
        final Properties props = new Properties();
        // An empty string currently means use the default
        // However the splitter drops empty fields
        props.setProperty(CryptoRandomFactory.CLASSES_KEY, ",");
        CryptoRandomFactory.getCryptoRandom(props);
    }

}

@@ -1583,32 +1583,150 @@
 oRandom(props);%0A
+        final String name = random.getClass().getName();%0A        if (OpenSslCryptoRandom.isNativeCodeEnabled()) %7B%0A    
         Assert.a
@@ -1773,16 +1773,23 @@
 tName(),
+ name);
 %0A       
@@ -1793,29 +1793,71 @@
     
-    r
+%7D else %7B%0A            Assert.assertEquals(JavaCryptoR
 andom.
-getC
+c
 lass
-()
 .get
@@ -1854,34 +1854,50 @@
 .class.getName()
-);
+, name);%0A        %7D
 %0A    %7D%0A%0A    @Tes

Allow test to work if JNI does not load