/*
 * Copyright 2014-2021 Web Firm Framework
 *
 * Licensed 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 com.webfirmframework.wffweb.tag.html.attribute.core;

import static org.junit.Assert.fail;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.Test;

public class AttributeIdGeneratorTest {

    @Test
    public void testNextId() {
        
        Set<Object> ids = Collections.newSetFromMap(new ConcurrentHashMap<>());
        ExecutorService threadPool = Executors.newCachedThreadPool();
        
        for (int i = 0; i < 1000; i++) {
            threadPool.execute(() -> {
                final String id = AttributeIdGenerator.nextId();
                if (ids.contains(id)) {
                    fail("Not yet implemented");
                }
                ids.add(id);
            });
        }
        
    }

}

@@ -700,16 +700,44 @@
 .fail;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -758,16 +758,39 @@
 ctions;%0A
+import java.util.List;%0A
 import j
@@ -833,31 +833,126 @@
 rrent.Co
-ncurrentHashMap
+mpletableFuture;%0Aimport java.util.concurrent.ConcurrentHashMap;%0Aimport java.util.concurrent.ExecutionException
 ;%0Aimport
@@ -1028,16 +1028,66 @@
 ecutors;
+%0Aimport java.util.concurrent.atomic.AtomicBoolean;
 %0A%0Aimport
@@ -1183,16 +1183,64 @@
 NextId()
+ throws InterruptedException, ExecutionException
  %7B%0A     
@@ -1414,233 +1414,456 @@
     
-for (int i = 0; i %3C 1000; i++) %7B%0A            threadPool.execute(() -%3E %7B%0A                final String id = AttributeIdGenerator.nextId();%0A                if (ids.contains(id)) %7B%0A                    fail(%22Not yet implemented%22);
+List%3CCompletableFuture%3CVoid%3E%3E tasks = new ArrayList%3C%3E(1000);%0A        %0A        AtomicBoolean alreadyExists = new AtomicBoolean(false);%0A        %0A        for (int i = 0; i %3C 1000; i++) %7B%0A            CompletableFuture%3CVoid%3E task = CompletableFuture.runAsync(() -%3E %7B%0A                final String id = AttributeIdGenerator.nextId();%0A                %0A                if (!alreadyExists.get()) %7B%0A                    alreadyExists.set(ids.contains(id));    
 %0A   
@@ -1869,32 +1869,49 @@
               %7D%0A
+                %0A
                 
@@ -1936,16 +1936,279 @@
        %7D
+, threadPool);%0A            %0A            tasks.add(task);%0A        %7D%0A        for (CompletableFuture%3CVoid%3E task : tasks) %7B%0A            task.get();%0A        %7D%0A        if (alreadyExists.get()) %7B%0A            fail(%22AttributeIdGenerator.nextId generated a duplicate value%22
 );%0A     

Test case improvements