diff --git a/jee7/src/main/java/com/baeldung/arquillian/CapsConvertor.java b/jee7/src/main/java/com/baeldung/arquillian/CapsConvertor.java new file mode 100644 index 0000000000..8492068842 --- /dev/null +++ b/jee7/src/main/java/com/baeldung/arquillian/CapsConvertor.java @@ -0,0 +1,10 @@ +package com.baeldung.arquillian; + +import javax.ejb.Stateless; + +@Stateless +public class CapsConvertor { + public ConvertToLowerCase getLowerCase(){ + return new ConvertToLowerCase(); + } +} \ No newline at end of file diff --git a/jee7/src/main/java/com/baeldung/arquillian/CapsService.java b/jee7/src/main/java/com/baeldung/arquillian/CapsService.java new file mode 100644 index 0000000000..5abc28f525 --- /dev/null +++ b/jee7/src/main/java/com/baeldung/arquillian/CapsService.java @@ -0,0 +1,14 @@ +package com.baeldung.arquillian; + +import javax.ejb.Stateless; +import javax.inject.Inject; + +@Stateless +public class CapsService { + @Inject + private CapsConvertor capsConvertor; + + public String getConvertedCaps(final String word){ + return capsConvertor.getLowerCase().convert(word); + } +} \ No newline at end of file diff --git a/jee7/src/main/java/com/baeldung/arquillian/Car.java b/jee7/src/main/java/com/baeldung/arquillian/Car.java new file mode 100644 index 0000000000..ca630a037f --- /dev/null +++ b/jee7/src/main/java/com/baeldung/arquillian/Car.java @@ -0,0 +1,37 @@ +package com.baeldung.arquillian; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.validation.constraints.NotNull; + +@Entity +public class Car { + @Id + @GeneratedValue + private Long id; + + @NotNull + private String name; + + public Long getId() { + return id; + } + + public void setId(final Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + @Override + public String toString() { + return "Car [id=" + id + ", name=" + name + "]"; + } +} \ No newline at end of file diff --git a/jee7/src/main/java/com/baeldung/arquillian/CarEJB.java b/jee7/src/main/java/com/baeldung/arquillian/CarEJB.java new file mode 100644 index 0000000000..2281f62039 --- /dev/null +++ b/jee7/src/main/java/com/baeldung/arquillian/CarEJB.java @@ -0,0 +1,35 @@ +package com.baeldung.arquillian; + +import java.util.ArrayList; +import java.util.List; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +@Stateless +public class CarEJB { + @PersistenceContext(unitName = "defaultPersistenceUnit") + private EntityManager em; + + public Car saveCar(final Car car) { + em.persist(car); + return car; + } + + @SuppressWarnings("unchecked") + public List findAllCars() { + final Query query = em.createQuery("SELECT b FROM Car b ORDER BY b.name ASC"); + List entries = query.getResultList(); + if (entries == null) { + entries = new ArrayList(); + } + return entries; + } + + public void deleteCar(Car car) { + car = em.merge(car); + em.remove(car); + } +} \ No newline at end of file diff --git a/jee7/src/main/java/com/baeldung/arquillian/Component.java b/jee7/src/main/java/com/baeldung/arquillian/Component.java new file mode 100644 index 0000000000..27e8d10f34 --- /dev/null +++ b/jee7/src/main/java/com/baeldung/arquillian/Component.java @@ -0,0 +1,13 @@ +package com.baeldung.arquillian; + +import java.io.PrintStream; + +public class Component { + public void sendMessage(PrintStream to, String msg) { + to.println(message(msg)); + } + + public String message(String msg) { + return "Message, " + msg; + } +} \ No newline at end of file diff --git a/jee7/src/main/java/com/baeldung/arquillian/ConvertToLowerCase.java b/jee7/src/main/java/com/baeldung/arquillian/ConvertToLowerCase.java new file mode 100644 index 0000000000..fd9b961308 --- /dev/null +++ b/jee7/src/main/java/com/baeldung/arquillian/ConvertToLowerCase.java @@ -0,0 +1,7 @@ +package com.baeldung.arquillian; + +public class ConvertToLowerCase { + public String convert(String word){ + return word.toLowerCase(); + } +} diff --git a/jee7/src/main/resources/META-INF/persistence.xml b/jee7/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000000..8a2c3eafe1 --- /dev/null +++ b/jee7/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,13 @@ + + + com.baeldung.arquillian.Car + + + + + + + + + + \ No newline at end of file diff --git a/jee7/src/test/java/com/baeldung/arquillan/ArquillianTest.java b/jee7/src/test/java/com/baeldung/arquillan/ArquillianTest.java new file mode 100644 index 0000000000..5219f221ca --- /dev/null +++ b/jee7/src/test/java/com/baeldung/arquillan/ArquillianTest.java @@ -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 ArquillianTest { + + @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()); + } +} \ No newline at end of file diff --git a/jee7/src/test/resources/META-INF/persistence.xml b/jee7/src/test/resources/META-INF/persistence.xml new file mode 100644 index 0000000000..8a2c3eafe1 --- /dev/null +++ b/jee7/src/test/resources/META-INF/persistence.xml @@ -0,0 +1,13 @@ + + + com.baeldung.arquillian.Car + + + + + + + + + + \ No newline at end of file