[JAVA-13854] Half list moved (#12598)
* [JAVA-13854] added parent module * [JAVA-13854] moved apache-tapestry(submodule) to web-modules(parent) * [JAVA-13854] moved bootique(submodule) to web-modules(parent) * [JAVA-13854] moved dropwizard(submodule) to web-modules(parent) * [JAVA-13854] moved blade(submodule) to web-modules(parent) * [JAVA-13854] moved java-lite(submodule) to web-modules(parent) * [JAVA-13854] moved jooby(submodule) to web-modules(parent) * [JAVA-13854] moved linkrest(submodule) to web-modules(parent) * [JAVA-13854] moved ninja(submodule) to web-modules(parent) * [JAVA-13854] moved ratpack(submodule) to web-modules(parent) * [JAVA-13854] moved resteasy(submodule) to web-modules(parent) * [JAVA-13854] moved restx(submodule) to web-modules(parent) * [JAVA-13854] moved spark-java(submodule) to web-modules(parent) * [JAVA-13854] moved vraptor(submodule) to web-modules(parent) * [JAVA-13854] delete modules that were moved * [JAVA-13854] * [JAVA-13854] * [JAVA-13854] delete ninja submodule + moved raml(submodule) to web-modules(parent) * [JAVA-13854] moved gwt(submodule) to web-modules(parent) * [JAVA-13854] moved jakarta-ee(submodule) to web-modules(parent) * [JAVA-13854] moved javax-servlets(submodule) to web-modules(parent) * [JAVA-13854] moved javax-servlets-2(submodule) to web-modules(parent) * [JAVA-13854] moved jee-7(submodule) to web-modules(parent) * [JAVA-13854] moved play-framework(not a module) to web-modules * [JAVA-13854] fix failing test * [JAVA-13854] moved struts-2(submodule) to web-modules(parent) * [JAVA-13854] moved wicket(submodule) to web-modules(parent) * [JAVA-13854] deleted modules that were moved to web-modules * JAVA-13854 Removed moved modules from the main pom.xml Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com> Co-authored-by: Dhawal Kapil <dhawalkapil@gmail.com>
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
package com.baeldug.json;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.hasItems;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonObject;
|
||||
import javax.json.JsonReader;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.json.Person;
|
||||
import com.baeldung.json.PersonBuilder;
|
||||
import com.baeldung.json.PersonWriter;
|
||||
|
||||
public class JsonUnitTest {
|
||||
private Person person;
|
||||
|
||||
private SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
|
||||
private String personJson;
|
||||
private String petshopJson;
|
||||
|
||||
@Test
|
||||
public void whenPersonIsConvertedToString_thenAValidJsonStringIsReturned() throws IOException {
|
||||
String generatedJsonString = new PersonWriter(person).write();
|
||||
|
||||
assertEquals(
|
||||
"Generated String has the expected format and content",
|
||||
personJson,
|
||||
generatedJsonString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenJsonStringIsConvertedToPerson_thenAValidObjectIsReturned(
|
||||
) throws IOException, ParseException {
|
||||
Person person = new PersonBuilder(personJson).build();
|
||||
|
||||
assertEquals("First name has expected value", "Michael", person.getFirstName());
|
||||
assertEquals("Last name has expected value", "Scott", person.getLastName());
|
||||
assertEquals(
|
||||
"Birthdate has expected value",
|
||||
dateFormat.parse("06/15/1978"),
|
||||
person.getBirthdate());
|
||||
assertThat(
|
||||
"Email list has two items",
|
||||
person.getEmails(),
|
||||
hasItems("michael.scott@dd.com", "michael.scarn@gmail.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingObjectModelToQueryForSpecificProperty_thenExpectedValueIsReturned(
|
||||
) throws IOException, ParseException {
|
||||
JsonReader reader = Json.createReader(new StringReader(petshopJson));
|
||||
|
||||
JsonObject jsonObject = reader.readObject();
|
||||
|
||||
assertEquals(
|
||||
"The query should return the 'name' property of the third pet from the list",
|
||||
"Jake",
|
||||
jsonObject.getJsonArray("pets").getJsonObject(2).getString("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingStreamingApiToQueryForSpecificProperty_thenExpectedValueIsReturned(
|
||||
) throws IOException, ParseException {
|
||||
JsonParser jsonParser = Json.createParser(new StringReader(petshopJson));
|
||||
|
||||
int count = 0;
|
||||
String result = null;
|
||||
|
||||
while(jsonParser.hasNext()) {
|
||||
Event e = jsonParser.next();
|
||||
|
||||
if (e == Event.KEY_NAME) {
|
||||
if(jsonParser.getString().equals("name")) {
|
||||
jsonParser.next();
|
||||
|
||||
if(++count == 3) {
|
||||
result = jsonParser.getString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals(
|
||||
"The query should return the 'name' property of the third pet from the list",
|
||||
"Jake",
|
||||
result);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() throws ParseException {
|
||||
// Creates a Person object
|
||||
person = new Person();
|
||||
|
||||
person.setFirstName("Michael");
|
||||
person.setLastName("Scott");
|
||||
person.setBirthdate(dateFormat.parse("06/15/1978"));
|
||||
person.setEmails(Arrays.asList("michael.scott@dd.com", "michael.scarn@gmail.com"));
|
||||
|
||||
// Initializes the Person Json
|
||||
personJson = "\n" +
|
||||
"{\n" +
|
||||
" \"firstName\":\"Michael\",\n" +
|
||||
" \"lastName\":\"Scott\",\n" +
|
||||
" \"birthdate\":\"06/15/1978\",\n" +
|
||||
" \"emails\":[\n" +
|
||||
" \"michael.scott@dd.com\",\n" +
|
||||
" \"michael.scarn@gmail.com\"\n" +
|
||||
" ]\n" +
|
||||
"}";
|
||||
|
||||
// Initializes the Pet Shop Json
|
||||
petshopJson = "\n" +
|
||||
"{\n" +
|
||||
" \"ownerId\":\"1\", \n" +
|
||||
" \"pets\":[ \n" +
|
||||
" {\"name\": \"Kitty\", \"type\": \"cat\"}, \n" +
|
||||
" {\"name\": \"Rex\", \"type\": \"dog\"}, \n" +
|
||||
" {\"name\": \"Jake\", \"type\": \"dog\"} \n" +
|
||||
" ]\n" +
|
||||
"}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.arquillan;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
|
||||
import org.jboss.shrinkwrap.api.spec.JavaArchive;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.baeldung.arquillian.CapsConvertor;
|
||||
import com.baeldung.arquillian.CapsService;
|
||||
import com.baeldung.arquillian.Car;
|
||||
import com.baeldung.arquillian.CarEJB;
|
||||
import com.baeldung.arquillian.Component;
|
||||
import com.baeldung.arquillian.ConvertToLowerCase;
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class ArquillianLiveTest {
|
||||
|
||||
@Deployment
|
||||
public static JavaArchive createDeployment() {
|
||||
return ShrinkWrap.create(JavaArchive.class).addClasses(Component.class, CapsService.class, CapsConvertor.class, ConvertToLowerCase.class, Car.class, CarEJB.class).addAsResource("META-INF/persistence.xml").addAsManifestResource(EmptyAsset.INSTANCE,
|
||||
"beans.xml");
|
||||
}
|
||||
|
||||
@Inject
|
||||
Component component;
|
||||
|
||||
@Test
|
||||
public void givenMessage_WhenComponentSendMessage_ThenMessageReceived() {
|
||||
Assert.assertEquals("Message, MESSAGE", component.message(("MESSAGE")));
|
||||
component.sendMessage(System.out, "MESSAGE");
|
||||
}
|
||||
|
||||
@Inject
|
||||
private CapsService capsService;
|
||||
|
||||
@Test
|
||||
public void givenWord_WhenUppercase_ThenLowercase() {
|
||||
assertTrue("capitalize".equals(capsService.getConvertedCaps("CAPITALIZE")));
|
||||
assertEquals("capitalize", capsService.getConvertedCaps("CAPITALIZE"));
|
||||
}
|
||||
|
||||
@Inject
|
||||
private CarEJB carEJB;
|
||||
|
||||
@Test
|
||||
public void testCars() {
|
||||
assertTrue(carEJB.findAllCars().isEmpty());
|
||||
Car c1 = new Car();
|
||||
c1.setName("Impala");
|
||||
Car c2 = new Car();
|
||||
c2.setName("Maverick");
|
||||
Car c3 = new Car();
|
||||
c3.setName("Aspen");
|
||||
Car c4 = new Car();
|
||||
c4.setName("Lincoln");
|
||||
carEJB.saveCar(c1);
|
||||
carEJB.saveCar(c2);
|
||||
carEJB.saveCar(c3);
|
||||
carEJB.saveCar(c4);
|
||||
assertEquals(4, carEJB.findAllCars().size());
|
||||
carEJB.deleteCar(c4);
|
||||
assertEquals(3, carEJB.findAllCars().size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.baeldung.batch.understanding;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.batch.runtime.BatchRuntime;
|
||||
import javax.batch.runtime.BatchStatus;
|
||||
import javax.batch.runtime.JobExecution;
|
||||
import javax.batch.runtime.Metric;
|
||||
import javax.batch.runtime.StepExecution;
|
||||
|
||||
public class BatchTestHelper {
|
||||
private static final int MAX_TRIES = 40;
|
||||
private static final int THREAD_SLEEP = 1000;
|
||||
|
||||
private BatchTestHelper() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public static JobExecution keepTestAlive(JobExecution jobExecution) throws InterruptedException {
|
||||
int maxTries = 0;
|
||||
while (!jobExecution.getBatchStatus()
|
||||
.equals(BatchStatus.COMPLETED)) {
|
||||
if (maxTries < MAX_TRIES) {
|
||||
maxTries++;
|
||||
Thread.sleep(THREAD_SLEEP);
|
||||
jobExecution = BatchRuntime.getJobOperator()
|
||||
.getJobExecution(jobExecution.getExecutionId());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Thread.sleep(THREAD_SLEEP);
|
||||
return jobExecution;
|
||||
}
|
||||
|
||||
public static JobExecution keepTestFailed(JobExecution jobExecution) throws InterruptedException {
|
||||
int maxTries = 0;
|
||||
while (!jobExecution.getBatchStatus()
|
||||
.equals(BatchStatus.FAILED)) {
|
||||
if (maxTries < MAX_TRIES) {
|
||||
maxTries++;
|
||||
Thread.sleep(THREAD_SLEEP);
|
||||
jobExecution = BatchRuntime.getJobOperator()
|
||||
.getJobExecution(jobExecution.getExecutionId());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Thread.sleep(THREAD_SLEEP);
|
||||
|
||||
return jobExecution;
|
||||
}
|
||||
|
||||
public static JobExecution keepTestStopped(JobExecution jobExecution) throws InterruptedException {
|
||||
int maxTries = 0;
|
||||
while (!jobExecution.getBatchStatus()
|
||||
.equals(BatchStatus.STOPPED)) {
|
||||
if (maxTries < MAX_TRIES) {
|
||||
maxTries++;
|
||||
Thread.sleep(THREAD_SLEEP);
|
||||
jobExecution = BatchRuntime.getJobOperator()
|
||||
.getJobExecution(jobExecution.getExecutionId());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Thread.sleep(THREAD_SLEEP);
|
||||
return jobExecution;
|
||||
}
|
||||
|
||||
public static long getCommitCount(StepExecution stepExecution) {
|
||||
Map<Metric.MetricType, Long> metricsMap = getMetricsMap(stepExecution.getMetrics());
|
||||
return metricsMap.get(Metric.MetricType.COMMIT_COUNT);
|
||||
}
|
||||
|
||||
public static long getProcessSkipCount(StepExecution stepExecution) {
|
||||
Map<Metric.MetricType, Long> metricsMap = getMetricsMap(stepExecution.getMetrics());
|
||||
return metricsMap.get(Metric.MetricType.PROCESS_SKIP_COUNT);
|
||||
}
|
||||
|
||||
public static Map<Metric.MetricType, Long> getMetricsMap(Metric[] metrics) {
|
||||
Map<Metric.MetricType, Long> metricsMap = new HashMap<>();
|
||||
for (Metric metric : metrics) {
|
||||
metricsMap.put(metric.getType(), metric.getValue());
|
||||
}
|
||||
return metricsMap;
|
||||
}
|
||||
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.batch.understanding;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.batch.operations.JobOperator;
|
||||
import javax.batch.runtime.BatchRuntime;
|
||||
import javax.batch.runtime.BatchStatus;
|
||||
import javax.batch.runtime.JobExecution;
|
||||
import javax.batch.runtime.StepExecution;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CustomCheckPointIntegrationTest {
|
||||
@Test
|
||||
public void givenChunk_whenCustomCheckPoint_thenCommitCountIsThree() throws Exception {
|
||||
JobOperator jobOperator = BatchRuntime.getJobOperator();
|
||||
Long executionId = jobOperator.start("customCheckPoint", new Properties());
|
||||
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
|
||||
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
|
||||
for (StepExecution stepExecution : jobOperator.getStepExecutions(executionId)) {
|
||||
if (stepExecution.getStepName()
|
||||
.equals("firstChunkStep")) {
|
||||
jobOperator.getStepExecutions(executionId)
|
||||
.stream()
|
||||
.map(BatchTestHelper::getCommitCount)
|
||||
.forEach(count -> assertEquals(3L, count.longValue()));
|
||||
}
|
||||
}
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
|
||||
}
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
package com.baeldung.batch.understanding;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.batch.operations.JobOperator;
|
||||
import javax.batch.runtime.BatchRuntime;
|
||||
import javax.batch.runtime.BatchStatus;
|
||||
import javax.batch.runtime.JobExecution;
|
||||
import javax.batch.runtime.StepExecution;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class JobSequenceIntegrationTest {
|
||||
@Test
|
||||
public void givenTwoSteps_thenBatch_CompleteWithSuccess() throws Exception {
|
||||
JobOperator jobOperator = BatchRuntime.getJobOperator();
|
||||
Long executionId = jobOperator.start("simpleJobSequence", new Properties());
|
||||
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
|
||||
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
|
||||
assertEquals(2 , jobOperator.getStepExecutions(executionId).size());
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFlow_thenBatch_CompleteWithSuccess() throws Exception {
|
||||
JobOperator jobOperator = BatchRuntime.getJobOperator();
|
||||
Long executionId = jobOperator.start("flowJobSequence", new Properties());
|
||||
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
|
||||
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
|
||||
assertEquals(3 , jobOperator.getStepExecutions(executionId).size());
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDecider_thenBatch_CompleteWithSuccess() throws Exception {
|
||||
JobOperator jobOperator = BatchRuntime.getJobOperator();
|
||||
Long executionId = jobOperator.start("decideJobSequence", new Properties());
|
||||
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
|
||||
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
|
||||
List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
|
||||
List<String> executedSteps = new ArrayList<>();
|
||||
for (StepExecution stepExecution : stepExecutions) {
|
||||
executedSteps.add(stepExecution.getStepName());
|
||||
}
|
||||
assertEquals(2, jobOperator.getStepExecutions(executionId).size());
|
||||
assertArrayEquals(new String[] { "firstBatchStepStep1", "firstBatchStepStep3" }, executedSteps.toArray());
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSplit_thenBatch_CompletesWithSuccess() throws Exception {
|
||||
JobOperator jobOperator = BatchRuntime.getJobOperator();
|
||||
Long executionId = jobOperator.start("splitJobSequence", new Properties());
|
||||
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
|
||||
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
|
||||
List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
|
||||
List<String> executedSteps = new ArrayList<>();
|
||||
for (StepExecution stepExecution : stepExecutions) {
|
||||
executedSteps.add(stepExecution.getStepName());
|
||||
}
|
||||
assertEquals(3, stepExecutions.size());
|
||||
assertTrue(executedSteps.contains("splitJobSequenceStep1"));
|
||||
assertTrue(executedSteps.contains("splitJobSequenceStep2"));
|
||||
assertTrue(executedSteps.contains("splitJobSequenceStep3"));
|
||||
assertTrue(executedSteps.get(0).equals("splitJobSequenceStep1") || executedSteps.get(0).equals("splitJobSequenceStep2"));
|
||||
assertTrue(executedSteps.get(1).equals("splitJobSequenceStep1") || executedSteps.get(1).equals("splitJobSequenceStep2"));
|
||||
assertTrue(executedSteps.get(2).equals("splitJobSequenceStep3"));
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.batch.understanding;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.batch.operations.JobOperator;
|
||||
import javax.batch.runtime.BatchRuntime;
|
||||
import javax.batch.runtime.BatchStatus;
|
||||
import javax.batch.runtime.JobExecution;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SimpleBatchLetIntegrationTest {
|
||||
@Test
|
||||
public void givenBatchLet_thenBatch_CompleteWithSuccess() throws Exception {
|
||||
JobOperator jobOperator = BatchRuntime.getJobOperator();
|
||||
Long executionId = jobOperator.start("simpleBatchLet", new Properties());
|
||||
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
|
||||
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBatchLetProperty_thenBatch_CompleteWithSuccess() throws Exception {
|
||||
JobOperator jobOperator = BatchRuntime.getJobOperator();
|
||||
Long executionId = jobOperator.start("injectionSimpleBatchLet", new Properties());
|
||||
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
|
||||
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBatchLetPartition_thenBatch_CompleteWithSuccess() throws Exception {
|
||||
JobOperator jobOperator = BatchRuntime.getJobOperator();
|
||||
Long executionId = jobOperator.start("partitionSimpleBatchLet", new Properties());
|
||||
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
|
||||
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBatchLetStarted_whenStopped_thenBatchStopped() throws Exception {
|
||||
JobOperator jobOperator = BatchRuntime.getJobOperator();
|
||||
Long executionId = jobOperator.start("simpleBatchLet", new Properties());
|
||||
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
|
||||
jobOperator.stop(executionId);
|
||||
jobExecution = BatchTestHelper.keepTestStopped(jobExecution);
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.STOPPED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBatchLetStopped_whenRestarted_thenBatchCompletesSuccess() throws Exception {
|
||||
JobOperator jobOperator = BatchRuntime.getJobOperator();
|
||||
Long executionId = jobOperator.start("simpleBatchLet", new Properties());
|
||||
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
|
||||
jobOperator.stop(executionId);
|
||||
jobExecution = BatchTestHelper.keepTestStopped(jobExecution);
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.STOPPED);
|
||||
executionId = jobOperator.restart(jobExecution.getExecutionId(), new Properties());
|
||||
jobExecution = BatchTestHelper.keepTestAlive(jobOperator.getJobExecution(executionId));
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.batch.understanding;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.batch.operations.JobOperator;
|
||||
import javax.batch.runtime.BatchRuntime;
|
||||
import javax.batch.runtime.BatchStatus;
|
||||
import javax.batch.runtime.JobExecution;
|
||||
import javax.batch.runtime.Metric;
|
||||
import javax.batch.runtime.StepExecution;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SimpleChunkIntegrationTest {
|
||||
@Test
|
||||
public void givenChunk_thenBatch_CompletesWithSucess() throws Exception {
|
||||
JobOperator jobOperator = BatchRuntime.getJobOperator();
|
||||
Long executionId = jobOperator.start("simpleChunk", new Properties());
|
||||
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
|
||||
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
|
||||
List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
|
||||
for (StepExecution stepExecution : stepExecutions) {
|
||||
if (stepExecution.getStepName()
|
||||
.equals("firstChunkStep")) {
|
||||
Map<Metric.MetricType, Long> metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics());
|
||||
assertEquals(10L, metricsMap.get(Metric.MetricType.READ_COUNT)
|
||||
.longValue());
|
||||
assertEquals(10L / 2L, metricsMap.get(Metric.MetricType.WRITE_COUNT)
|
||||
.longValue());
|
||||
assertEquals(10L / 3 + (10L % 3 > 0 ? 1 : 0), metricsMap.get(Metric.MetricType.COMMIT_COUNT)
|
||||
.longValue());
|
||||
}
|
||||
}
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenChunk__thenBatch_fetchInformation() throws Exception {
|
||||
JobOperator jobOperator = BatchRuntime.getJobOperator();
|
||||
Long executionId = jobOperator.start("simpleChunk", new Properties());
|
||||
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
|
||||
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
|
||||
// job name contains simpleBatchLet which is the name of the file
|
||||
assertTrue(jobOperator.getJobNames().contains("simpleChunk"));
|
||||
// job parameters are empty
|
||||
assertTrue(jobOperator.getParameters(executionId).isEmpty());
|
||||
// step execution information
|
||||
List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
|
||||
assertEquals("firstChunkStep", stepExecutions.get(0).getStepName());
|
||||
// finding out batch status
|
||||
assertEquals(BatchStatus.COMPLETED, stepExecutions.get(0).getBatchStatus());
|
||||
Map<Metric.MetricType, Long> metricTest = BatchTestHelper.getMetricsMap(stepExecutions.get(0).getMetrics());
|
||||
assertEquals(10L, metricTest.get(Metric.MetricType.READ_COUNT).longValue());
|
||||
assertEquals(5L, metricTest.get(Metric.MetricType.FILTER_COUNT).longValue());
|
||||
assertEquals(4L, metricTest.get(Metric.MetricType.COMMIT_COUNT).longValue());
|
||||
assertEquals(5L, metricTest.get(Metric.MetricType.WRITE_COUNT).longValue());
|
||||
assertEquals(0L, metricTest.get(Metric.MetricType.READ_SKIP_COUNT).longValue());
|
||||
assertEquals(0L, metricTest.get(Metric.MetricType.WRITE_SKIP_COUNT).longValue());
|
||||
assertEquals(0L, metricTest.get(Metric.MetricType.PROCESS_SKIP_COUNT).longValue());
|
||||
assertEquals(0L, metricTest.get(Metric.MetricType.ROLLBACK_COUNT).longValue());
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.batch.understanding;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.batch.operations.JobOperator;
|
||||
import javax.batch.runtime.BatchRuntime;
|
||||
import javax.batch.runtime.BatchStatus;
|
||||
import javax.batch.runtime.JobExecution;
|
||||
import javax.batch.runtime.StepExecution;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SimpleErrorChunkIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenChunkError_thenBatch_CompletesWithFailed() throws Exception {
|
||||
JobOperator jobOperator = BatchRuntime.getJobOperator();
|
||||
Long executionId = jobOperator.start("simpleErrorChunk", new Properties());
|
||||
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
|
||||
jobExecution = BatchTestHelper.keepTestFailed(jobExecution);
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.FAILED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenChunkError_thenErrorSkipped_CompletesWithSuccess() throws Exception {
|
||||
JobOperator jobOperator = BatchRuntime.getJobOperator();
|
||||
Long executionId = jobOperator.start("simpleErrorSkipChunk", new Properties());
|
||||
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
|
||||
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
|
||||
List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
|
||||
for (StepExecution stepExecution : stepExecutions) {
|
||||
if (stepExecution.getStepName()
|
||||
.equals("errorStep")) {
|
||||
jobOperator.getStepExecutions(executionId)
|
||||
.stream()
|
||||
.map(BatchTestHelper::getProcessSkipCount)
|
||||
.forEach(skipCount -> assertEquals(1L, skipCount.longValue()));
|
||||
}
|
||||
}
|
||||
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.baeldung.convListVal;
|
||||
|
||||
import static org.jboss.arquillian.graphene.Graphene.guardHttp;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.container.test.api.RunAsClient;
|
||||
import org.jboss.arquillian.drone.api.annotation.Drone;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.arquillian.test.api.ArquillianResource;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
|
||||
import org.jboss.shrinkwrap.api.spec.WebArchive;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.support.FindBy;
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class ConvListValLiveTest {
|
||||
|
||||
@ArquillianResource
|
||||
private URL deploymentUrl;
|
||||
|
||||
private static final String WEBAPP_SRC = "src/main/webapp";
|
||||
|
||||
@Deployment(testable = false)
|
||||
public static WebArchive createDeployment() {
|
||||
return ( ShrinkWrap.create(
|
||||
WebArchive.class, "jee7.war").
|
||||
addClasses(ConvListVal.class, MyListener.class)).
|
||||
addAsWebResource(new File(WEBAPP_SRC, "ConvListVal.xhtml")).
|
||||
addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
|
||||
}
|
||||
|
||||
@Drone
|
||||
WebDriver browser;
|
||||
|
||||
@ArquillianResource
|
||||
URL contextPath;
|
||||
|
||||
@FindBy(id="myForm:age")
|
||||
private WebElement ageInput;
|
||||
|
||||
@FindBy(id="myForm:average")
|
||||
private WebElement averageInput;
|
||||
|
||||
@FindBy(id="myForm:send")
|
||||
private WebElement sendButton;
|
||||
|
||||
@Test
|
||||
@RunAsClient
|
||||
public void givenAge_whenAgeInvalid_thenErrorMessage() throws Exception {
|
||||
browser.get(deploymentUrl.toExternalForm() + "ConvListVal.jsf");
|
||||
ageInput.sendKeys("stringage");
|
||||
guardHttp(sendButton).click();
|
||||
Assert.assertTrue("Show Age error message", browser.findElements(By.id("myForm:ageError")).size() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@RunAsClient
|
||||
public void givenAverage_whenAverageInvalid_thenErrorMessage() throws Exception {
|
||||
browser.get(deploymentUrl.toExternalForm() + "ConvListVal.jsf");
|
||||
averageInput.sendKeys("stringaverage");
|
||||
guardHttp(sendButton).click();
|
||||
Assert.assertTrue("Show Average error message", browser.findElements(By.id("myForm:averageError")).size() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@RunAsClient
|
||||
public void givenDate_whenDateInvalid_thenErrorMessage() throws Exception {
|
||||
browser.get(deploymentUrl.toExternalForm() + "ConvListVal.jsf");
|
||||
averageInput.sendKeys("123");
|
||||
guardHttp(sendButton).click();
|
||||
Assert.assertTrue("Show Date error message", browser.findElements(By.id("myForm:myDateError")).size() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@RunAsClient
|
||||
public void givenSurname_whenSurnameMinLenghtInvalid_thenErrorMessage() throws Exception {
|
||||
browser.get(deploymentUrl.toExternalForm() + "ConvListVal.jsf");
|
||||
averageInput.sendKeys("aaa");
|
||||
guardHttp(sendButton).click();
|
||||
Assert.assertTrue("Show Surname error message", browser.findElements(By.id("myForm:surnameError")).size() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@RunAsClient
|
||||
public void givenSurname_whenSurnameMaxLenghtInvalid_thenErrorMessage() throws Exception {
|
||||
browser.get(deploymentUrl.toExternalForm() + "ConvListVal.jsf");
|
||||
averageInput.sendKeys("aaaaabbbbbc");
|
||||
guardHttp(sendButton).click();
|
||||
Assert.assertTrue("Show Surname error message", browser.findElements(By.id("myForm:surnameError")).size() > 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package com.baeldung.jaxws;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.arquillian.test.api.ArquillianResource;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
|
||||
import org.jboss.shrinkwrap.api.spec.WebArchive;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.baeldung.jaxws.client.Employee;
|
||||
import com.baeldung.jaxws.client.EmployeeAlreadyExists_Exception;
|
||||
import com.baeldung.jaxws.client.EmployeeNotFound_Exception;
|
||||
import com.baeldung.jaxws.client.EmployeeService;
|
||||
import com.baeldung.jaxws.client.EmployeeService_Service;
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class EmployeeServiceLiveTest {
|
||||
|
||||
private static final String APP_NAME = "jee7";
|
||||
private static final String WSDL_PATH = "EmployeeService?wsdl";
|
||||
private static QName SERVICE_NAME = new QName("http://bottomup.server.jaxws.baeldung.com/", "EmployeeService");
|
||||
private static URL wsdlUrl;
|
||||
|
||||
@ArquillianResource
|
||||
private URL deploymentUrl;
|
||||
|
||||
private EmployeeService employeeServiceProxy;
|
||||
|
||||
@Deployment(testable = false)
|
||||
public static WebArchive createDeployment() {
|
||||
return ShrinkWrap.create(WebArchive.class, APP_NAME + ".war")
|
||||
.addPackage(com.baeldung.jaxws.server.bottomup.EmployeeService.class.getPackage())
|
||||
.addPackage(com.baeldung.jaxws.server.bottomup.model.Employee.class.getPackage())
|
||||
.addPackage(com.baeldung.jaxws.server.bottomup.exception.EmployeeNotFound.class.getPackage())
|
||||
.addPackage(com.baeldung.jaxws.server.repository.EmployeeRepository.class.getPackage())
|
||||
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
try {
|
||||
wsdlUrl = new URL(deploymentUrl, WSDL_PATH);
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
EmployeeService_Service employeeService_Service = new EmployeeService_Service(wsdlUrl);
|
||||
employeeServiceProxy = employeeService_Service.getEmployeeServiceImplPort();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployees_whenGetCount_thenCorrectNumberOfEmployeesReturned() {
|
||||
int employeeCount = employeeServiceProxy.countEmployees();
|
||||
List<Employee> employeeList = employeeServiceProxy.getAllEmployees();
|
||||
assertEquals(employeeList.size(), employeeCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployees_whenGetAvailableEmployee_thenCorrectEmployeeReturned() throws EmployeeNotFound_Exception {
|
||||
Employee employee = employeeServiceProxy.getEmployee(2);
|
||||
assertEquals(employee.getFirstName(), "Jack");
|
||||
}
|
||||
|
||||
@Test(expected = EmployeeNotFound_Exception.class)
|
||||
public void givenEmployees_whenGetNonAvailableEmployee_thenEmployeeNotFoundException() throws EmployeeNotFound_Exception {
|
||||
employeeServiceProxy.getEmployee(20);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployees_whenAddNewEmployee_thenEmployeeCountIncreased() throws EmployeeAlreadyExists_Exception {
|
||||
int employeeCount = employeeServiceProxy.countEmployees();
|
||||
employeeServiceProxy.addEmployee(4, "Anna");
|
||||
assertEquals(employeeServiceProxy.countEmployees(), employeeCount + 1);
|
||||
}
|
||||
|
||||
@Test(expected = EmployeeAlreadyExists_Exception.class)
|
||||
public void givenEmployees_whenAddAlreadyExistingEmployee_thenEmployeeAlreadyExistsException() throws EmployeeAlreadyExists_Exception {
|
||||
employeeServiceProxy.addEmployee(1, "Anna");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployees_whenUpdateExistingEmployee_thenUpdatedEmployeeReturned() throws EmployeeNotFound_Exception {
|
||||
Employee updated = employeeServiceProxy.updateEmployee(1, "Joan");
|
||||
assertEquals(updated.getFirstName(), "Joan");
|
||||
}
|
||||
|
||||
@Test(expected = EmployeeNotFound_Exception.class)
|
||||
public void givenEmployees_whenUpdateNonExistingEmployee_thenEmployeeNotFoundException() throws EmployeeNotFound_Exception {
|
||||
employeeServiceProxy.updateEmployee(20, "Joan");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployees_whenDeleteExistingEmployee_thenSuccessReturned() throws EmployeeNotFound_Exception {
|
||||
boolean deleteEmployee = employeeServiceProxy.deleteEmployee(3);
|
||||
assertEquals(deleteEmployee, true);
|
||||
}
|
||||
|
||||
@Test(expected = EmployeeNotFound_Exception.class)
|
||||
public void givenEmployee_whenDeleteNonExistingEmployee_thenEmployeeNotFoundException() throws EmployeeNotFound_Exception {
|
||||
employeeServiceProxy.deleteEmployee(20);
|
||||
}
|
||||
|
||||
}
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
package com.baeldung.singleton;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
import javax.enterprise.context.spi.CreationalContext;
|
||||
import javax.enterprise.inject.spi.Bean;
|
||||
import javax.enterprise.inject.spi.BeanManager;
|
||||
import javax.enterprise.inject.spi.CDI;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
|
||||
import org.jboss.shrinkwrap.api.spec.JavaArchive;
|
||||
import org.junit.Before;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class CarServiceIntegrationTest {
|
||||
|
||||
public static final Logger LOG = LoggerFactory.getLogger(CarServiceIntegrationTest.class);
|
||||
|
||||
@Deployment
|
||||
public static JavaArchive createDeployment() {
|
||||
return ShrinkWrap.create(JavaArchive.class)
|
||||
.addClasses(CarServiceBean.class, CarServiceSingleton.class, CarServiceEjbSingleton.class, Car.class)
|
||||
.addAsResource("META-INF/persistence.xml")
|
||||
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
|
||||
}
|
||||
|
||||
@Inject
|
||||
private CarServiceBean carServiceBean;
|
||||
|
||||
@Inject
|
||||
private CarServiceSingleton carServiceSingleton;
|
||||
|
||||
@EJB
|
||||
private CarServiceEjbSingleton carServiceEjbSingleton;
|
||||
|
||||
@Test
|
||||
public void givenASingleton_whenGetBeanIsCalledTwice_thenTheSameInstanceIsReturned() {
|
||||
CarServiceSingleton one = getBean(CarServiceSingleton.class);
|
||||
CarServiceSingleton two = getBean(CarServiceSingleton.class);
|
||||
assertTrue(one == two);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAPojo_whenGetBeanIsCalledTwice_thenDifferentInstancesAreReturned() {
|
||||
CarServiceBean one = getBean(CarServiceBean.class);
|
||||
CarServiceBean two = getBean(CarServiceBean.class);
|
||||
assertTrue(one != two);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T getBean(Class<T> beanClass) {
|
||||
BeanManager bm = CDI.current().getBeanManager();
|
||||
Bean<T> bean = (Bean<T>) bm.getBeans(beanClass).iterator().next();
|
||||
CreationalContext<T> ctx = bm.createCreationalContext(bean);
|
||||
return (T) bm.getReference(bean, beanClass, ctx);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCDI_whenConcurrentAccess_thenLockingIsNotProvided() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String model = Double.toString(Math.round(Math.random() * 100));
|
||||
Car car = new Car("Speedster", model);
|
||||
int serviceQueue = carServiceSingleton.service(car);
|
||||
assertTrue(serviceQueue < 10);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEJB_whenConcurrentAccess_thenLockingIsProvided() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String model = Double.toString(Math.round(Math.random() * 100));
|
||||
Car car = new Car("Speedster", model);
|
||||
int serviceQueue = carServiceEjbSingleton.service(car);
|
||||
assertEquals(0, serviceQueue);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.soap.ws.client;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.soap.ws.client.generated.CountryService;
|
||||
import com.baeldung.soap.ws.client.generated.CountryServiceImplService;
|
||||
import com.baeldung.soap.ws.client.generated.Currency;
|
||||
|
||||
//Ensure that com.baeldung.soap.ws.server.CountryServicePublisher is running before executing this test
|
||||
public class CountryClientLiveTest {
|
||||
|
||||
private static CountryService countryService;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
CountryServiceImplService service = new CountryServiceImplService();
|
||||
countryService = service.getCountryServiceImplPort();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCountryService_whenCountryIndia_thenCapitalIsNewDelhi() {
|
||||
assertEquals("New Delhi", countryService.findByName("India").getCapital());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCountryService_whenCountryFrance_thenPopulationCorrect() {
|
||||
assertEquals(66710000, countryService.findByName("France").getPopulation());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCountryService_whenCountryUSA_thenCurrencyUSD() {
|
||||
assertEquals(Currency.USD, countryService.findByName("USA").getCurrency());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.timer;
|
||||
|
||||
import com.jayway.awaitility.Awaitility;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
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 javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.jayway.awaitility.Awaitility.await;
|
||||
import static com.jayway.awaitility.Awaitility.to;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class AutomaticTimerBeanLiveTest {
|
||||
|
||||
//the @AutomaticTimerBean has a method called every 10 seconds
|
||||
//testing the difference ==> 100000
|
||||
final static long TIMEOUT = 10000l;
|
||||
|
||||
//the tolerance accepted , so if between two consecutive calls there has to be at least 9 or max 11 seconds.
|
||||
//because the timer service is not intended for real-time applications so it will not be exactly 10 seconds
|
||||
final static long TOLERANCE = 1000l;
|
||||
|
||||
@Inject
|
||||
TimerEventListener timerEventListener;
|
||||
|
||||
@Deployment
|
||||
public static WebArchive deploy() {
|
||||
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
|
||||
.resolve("com.jayway.awaitility:awaitility")
|
||||
.withTransitivity().asFile();
|
||||
|
||||
//only @AutomaticTimerBean is deployed not the other timers
|
||||
return ShrinkWrap.create(WebArchive.class)
|
||||
.addAsLibraries(jars)
|
||||
.addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, AutomaticTimerBean.class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void should_receive_two_pings() {
|
||||
Awaitility.setDefaultTimeout(30, TimeUnit.SECONDS);
|
||||
//the test will wait here until two events are triggered
|
||||
await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(2));
|
||||
|
||||
TimerEvent firstEvent = timerEventListener.getEvents().get(0);
|
||||
TimerEvent secondEvent = timerEventListener.getEvents().get(1);
|
||||
|
||||
long delay = secondEvent.getTime() - firstEvent.getTime();
|
||||
System.out.println("Actual timeout = " + delay);
|
||||
|
||||
//ensure that the delay between the events is more or less 10 seconds (no real time precision)
|
||||
assertThat(delay, Matchers.is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.timer;
|
||||
|
||||
import com.jayway.awaitility.Awaitility;
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
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 javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.jayway.awaitility.Awaitility.await;
|
||||
import static com.jayway.awaitility.Awaitility.to;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class ProgrammaticAtFixedRateTimerBeanLiveTest {
|
||||
|
||||
final static long TIMEOUT = 1000;
|
||||
final static long TOLERANCE = 500l;
|
||||
|
||||
@Inject
|
||||
TimerEventListener timerEventListener;
|
||||
|
||||
@Deployment
|
||||
public static WebArchive deploy() {
|
||||
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
|
||||
.resolve("com.jayway.awaitility:awaitility")
|
||||
.withTransitivity().asFile();
|
||||
|
||||
return ShrinkWrap.create(WebArchive.class)
|
||||
.addAsLibraries(jars)
|
||||
.addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, ProgrammaticAtFixedRateTimerBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_receive_ten_pings() {
|
||||
|
||||
Awaitility.setDefaultTimeout(30, TimeUnit.SECONDS);
|
||||
|
||||
await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(10));
|
||||
TimerEvent firstEvent = timerEventListener.getEvents().get(0);
|
||||
TimerEvent secondEvent = timerEventListener.getEvents().get(1);
|
||||
|
||||
long delay = secondEvent.getTime() - firstEvent.getTime();
|
||||
System.out.println("Actual timeout = " + delay);
|
||||
assertThat(delay, is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.timer;
|
||||
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
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 javax.inject.Inject;
|
||||
import java.io.File;
|
||||
|
||||
import static com.jayway.awaitility.Awaitility.await;
|
||||
import static com.jayway.awaitility.Awaitility.to;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class ProgrammaticTimerBeanLiveTest {
|
||||
|
||||
final static long TIMEOUT = 5000l;
|
||||
final static long TOLERANCE = 1000l;
|
||||
|
||||
@Inject
|
||||
TimerEventListener timerEventListener;
|
||||
|
||||
@Deployment
|
||||
public static WebArchive deploy() {
|
||||
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
|
||||
.resolve("com.jayway.awaitility:awaitility")
|
||||
.withTransitivity().asFile();
|
||||
|
||||
return ShrinkWrap.create(WebArchive.class)
|
||||
.addAsLibraries(jars)
|
||||
.addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, ProgrammaticTimerBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_receive_two_pings() {
|
||||
|
||||
await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(2));
|
||||
|
||||
TimerEvent firstEvent = timerEventListener.getEvents().get(0);
|
||||
TimerEvent secondEvent = timerEventListener.getEvents().get(1);
|
||||
|
||||
long delay = secondEvent.getTime() - firstEvent.getTime();
|
||||
System.out.println("Actual timeout = " + delay);
|
||||
assertThat(delay, is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.timer;
|
||||
|
||||
import com.jayway.awaitility.Awaitility;
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
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 javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.jayway.awaitility.Awaitility.await;
|
||||
import static com.jayway.awaitility.Awaitility.to;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class ProgrammaticWithFixedDelayTimerBeanLiveTest {
|
||||
|
||||
final static long TIMEOUT = 15000l;
|
||||
final static long TOLERANCE = 1000l;
|
||||
|
||||
@Inject
|
||||
TimerEventListener timerEventListener;
|
||||
|
||||
@Deployment
|
||||
public static WebArchive deploy() {
|
||||
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
|
||||
.resolve("com.jayway.awaitility:awaitility")
|
||||
.withTransitivity().asFile();
|
||||
|
||||
return ShrinkWrap.create(WebArchive.class)
|
||||
.addAsLibraries(jars)
|
||||
.addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, ProgrammaticWithInitialFixedDelayTimerBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_receive_two_pings() {
|
||||
|
||||
Awaitility.setDefaultTimeout(30, TimeUnit.SECONDS);
|
||||
|
||||
// 10 seconds pause so we get the startTime and it will trigger first event
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(2));
|
||||
TimerEvent firstEvent = timerEventListener.getEvents().get(0);
|
||||
TimerEvent secondEvent = timerEventListener.getEvents().get(1);
|
||||
|
||||
long delay = secondEvent.getTime() - startTime;
|
||||
System.out.println("Actual timeout = " + delay);
|
||||
|
||||
//apx 15 seconds = 10 delay + 2 timers (first after a pause of 10 seconds and the next others every 5 seconds)
|
||||
assertThat(delay, is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.timer;
|
||||
|
||||
import com.jayway.awaitility.Awaitility;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
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 javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.jayway.awaitility.Awaitility.await;
|
||||
import static com.jayway.awaitility.Awaitility.to;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class ScheduleTimerBeanLiveTest {
|
||||
|
||||
private final static long TIMEOUT = 5000l;
|
||||
private final static long TOLERANCE = 1000l;
|
||||
|
||||
@Inject TimerEventListener timerEventListener;
|
||||
|
||||
@Deployment
|
||||
public static WebArchive deploy() {
|
||||
File[] jars = Maven
|
||||
.resolver()
|
||||
.loadPomFromFile("pom.xml")
|
||||
.resolve("com.jayway.awaitility:awaitility")
|
||||
.withTransitivity()
|
||||
.asFile();
|
||||
|
||||
return ShrinkWrap
|
||||
.create(WebArchive.class)
|
||||
.addAsLibraries(jars)
|
||||
.addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, ScheduleTimerBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_receive_three_pings() {
|
||||
|
||||
Awaitility.setDefaultTimeout(30, TimeUnit.SECONDS);
|
||||
await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(3));
|
||||
|
||||
TimerEvent firstEvent = timerEventListener
|
||||
.getEvents()
|
||||
.get(0);
|
||||
TimerEvent secondEvent = timerEventListener
|
||||
.getEvents()
|
||||
.get(1);
|
||||
TimerEvent thirdEvent = timerEventListener
|
||||
.getEvents()
|
||||
.get(2);
|
||||
|
||||
long delay = secondEvent.getTime() - firstEvent.getTime();
|
||||
assertThat(delay, Matchers.is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||
delay = thirdEvent.getTime() - secondEvent.getTime();
|
||||
assertThat(delay, Matchers.is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.timer;
|
||||
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
import org.hamcrest.Matcher;
|
||||
|
||||
class WithinWindowMatcher extends BaseMatcher<Long> {
|
||||
private final long timeout;
|
||||
private final long tolerance;
|
||||
|
||||
public WithinWindowMatcher(long timeout, long tolerance) {
|
||||
this.timeout = timeout;
|
||||
this.tolerance = tolerance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Object item) {
|
||||
final Long actual = (Long) item;
|
||||
return Math.abs(actual - timeout) < tolerance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
//To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
public static Matcher<Long> withinWindow(final long timeout, final long tolerance) {
|
||||
return new WithinWindowMatcher(timeout, tolerance);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user