/**
 * 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.lang.Thread.State;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

public abstract class AbstractRandomTest {

    public abstract CryptoRandom getCryptoRandom()
            throws GeneralSecurityException;

    @Test(timeout = 120000)
    public void testRandomBytes() throws Exception {
        CryptoRandom random = getCryptoRandom();
        // len = 16
        checkRandomBytes(random, 16);
        // len = 32
        checkRandomBytes(random, 32);
        // len = 128
        checkRandomBytes(random, 128);
        // len = 256
        checkRandomBytes(random, 256);
        random.close();
    }

    @Test(timeout = 120000)
    public void testRandomBytesMultiThreaded() throws Exception {
        final int threadCount = 100;
        final CryptoRandom random = getCryptoRandom();
        final List<Thread> threads = new ArrayList<>(threadCount);

        for(int i=0; i< threadCount; i++) {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    checkRandomBytes(random, 10);
                    checkRandomBytes(random, 1000);
                    checkRandomBytes(random, 100000);
                }
            });
            t.start();
            threads.add(t);
        }

        for(Thread t: threads) {
            if(!t.getState().equals(State.NEW)) {
                t.join();
            }
        }

        random.close();
    }

    /**
     * Test will timeout if secure random implementation always returns a
     * constant value.
     */
    private void checkRandomBytes(CryptoRandom random, int len) {
        byte[] bytes = new byte[len];
        byte[] bytes1 = new byte[len];
        random.nextBytes(bytes);
        random.nextBytes(bytes1);

        while (Arrays.equals(bytes1, new byte[len]) || Arrays.equals(bytes, bytes1)) {
            random.nextBytes(bytes1);
        }
    }
}

@@ -1252,16 +1252,21 @@
         
+try (
 CryptoRa
@@ -1288,34 +1288,40 @@
 etCryptoRandom()
-;%0A
+) %7B%0A    
         // len =
@@ -1324,32 +1324,36 @@
 en = 16%0A        
+  
+  
 checkRandomBytes
@@ -1366,32 +1366,36 @@
 m, 16);%0A        
+    
 // len = 32%0A    
@@ -1390,32 +1390,36 @@
 en = 32%0A        
+ 
+   
 checkRandomBytes
@@ -1432,16 +1432,20 @@
 m, 32);%0A
+    
         
@@ -1457,32 +1457,36 @@
 n = 128%0A        
+    
 checkRandomBytes
@@ -1500,32 +1500,36 @@
 , 128);%0A        
+   
+ 
 // len = 256%0A   
@@ -1525,16 +1525,20 @@
 n = 256%0A
+    
         
@@ -1572,39 +1572,25 @@
 6);%0A        
-random.close();
+%7D
 %0A    %7D%0A%0A    

Use try-with-resources.