Java tutorial
/* * HSM Proxy Project. * Copyright (C) 2013 FedICT. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License version * 3.0 as published by the Free Software Foundation. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, see * http://www.gnu.org/licenses/. */ package test.integ.be.fedict.hsm.model; import java.net.URL; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.container.test.api.RunAsClient; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.jboss.shrinkwrap.api.spec.WebArchive; import org.jboss.shrinkwrap.resolver.api.maven.Maven; import org.junit.Test; import org.junit.runner.RunWith; import test.integ.be.fedict.hsm.ws.WebServiceTest; import be.fedict.hsm.entity.CredentialEntity; import be.fedict.hsm.model.KeyStoreLoader; import be.fedict.hsm.model.KeyStoreLoaderBean; import be.fedict.hsm.model.KeyStoreSingletonBean; @RunWith(Arquillian.class) @RunAsClient public class ConcurrencyTest { private static final Log LOG = LogFactory.getLog(ConcurrencyTest.class); private static final int TEST_COUNT = 10; @ArquillianResource private URL baseURL; @Deployment public static WebArchive createTestArchive() { WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war") .addAsLibraries(Maven.resolver().resolve("commons-io:commons-io:2.4").withoutTransitivity() .as(JavaArchive.class)) .addPackage(CredentialEntity.class.getPackage()) .addClasses(KeyStoreSingletonBean.class, KeyStoreLoader.class, KeyStoreLoaderBean.class, KeyStoreTestBean.class, ConcurrencyTestController.class) .addAsResource(KeyStoreSingletonBeanTest.class.getResource("/test-persistence.xml"), "META-INF/persistence.xml") .addAsWebInfResource(WebServiceTest.class.getResource("/test-jax-rs-web.xml"), "/web.xml"); return war; } @Test public void testPKCS11Concurrency() throws Exception { LOG.debug("PKCS#11 concurrency test"); LOG.debug("base url: " + this.baseURL); HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(this.baseURL + "rest/test/init"); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = httpClient.execute(httpGet, responseHandler); LOG.debug("response: " + responseBody); Thread eTokenThread = new Thread(new ETokenTest()); Thread beIdThread = new Thread(new BeIDTest()); eTokenThread.start(); beIdThread.start(); eTokenThread.join(); beIdThread.join(); } private class ETokenTest implements Runnable { @Override public void run() { int count = TEST_COUNT; long t0 = System.currentTimeMillis(); while (count > 0) { try { HttpClient httpClient = new DefaultHttpClient(); httpClient.execute(new HttpGet(ConcurrencyTest.this.baseURL + "rest/test/etoken")); } catch (Exception e) { LOG.error("eToken client error: " + e.getMessage()); } count--; } long t1 = System.currentTimeMillis(); LOG.debug("etoken dt: " + (t1 - t0) / TEST_COUNT); } } private class BeIDTest implements Runnable { public void run() { int count = TEST_COUNT; long t0 = System.currentTimeMillis(); while (count > 0) { try { HttpClient httpClient = new DefaultHttpClient(); httpClient.execute(new HttpGet(ConcurrencyTest.this.baseURL + "rest/test/beid")); } catch (Exception e) { LOG.error("BeID client error: " + e.getMessage()); } count--; } long t1 = System.currentTimeMillis(); LOG.debug("BeID dt: " + (t1 - t0) / TEST_COUNT); } } }