package br.net.mirante.singular.form.wicket.mapper.selection;

import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.BeforeClass;

import br.net.mirante.singular.form.mform.MDicionario;
import br.net.mirante.singular.form.mform.MIComposto;
import br.net.mirante.singular.form.mform.MTipo;
import br.net.mirante.singular.form.mform.MTipoComposto;
import br.net.mirante.singular.form.mform.PacoteBuilder;
import br.net.mirante.singular.form.wicket.test.base.TestApp;
import br.net.mirante.singular.form.wicket.test.base.TestPage;

import static br.net.mirante.singular.form.wicket.hepers.TestFinders.findId;

public abstract class SelectionFieldBaseTest {
    protected static MDicionario dicionario;
    protected PacoteBuilder localPackage;
    protected WicketTester driver;
    protected TestPage page;
    protected FormTester form;
    
    @BeforeClass
    public static void createDicionario() {
        dicionario = MDicionario.create();
    }

    protected void setupPage() {
        driver = new WicketTester(new TestApp());
        page = new TestPage();
        page.setDicionario(dicionario);
        localPackage = dicionario.criarNovoPacote("test"+(int)(Math.random()*1000));
        MTipoComposto<? extends MIComposto> group = localPackage.createTipoComposto("group");
        createSelectionType(group);
        
        page.setNewInstanceOfType(group.getNome());
    }
    
    @SuppressWarnings("rawtypes")
    abstract MTipo createSelectionType(MTipoComposto group); 
    
    protected void buildPage() {
        page.build();
        driver.startPage(page);
        
        form = driver.newFormTester("test-form", false);
    }

    protected String formField(FormTester form, String leafName) {
        return "test-form:" + findId(form.getForm(), leafName).get();
    }
}

@@ -585,16 +585,67 @@
 tPage;%0A%0A
+import java.util.concurrent.atomic.AtomicInteger;%0A%0A
 import s
@@ -713,16 +713,16 @@
 findId;%0A
-
 %0Apublic 
@@ -943,16 +943,85 @@
 r form;%0A
+    private static final AtomicInteger index = new AtomicInteger(0);%0A
     %0A   
@@ -1346,29 +1346,29 @@
 +(in
-t)(Math.random()*1000
+dex.getAndIncrement()
 ));%0A

fix concurrent test