<commit_before>---
layout: guide
title: Getting Started Faster with Forge
authors: [<NAME>]
guide_summary: Learn how to use JBoss Forge to get started faster with Arquillian and work more efficiently as you develop tests.
guide_group: 1
---
<div class="header">
h2. #{page.title}

div(authors). *Authors:* #{page.authors.join(', ')}

This guide gets you acquainted with the Arquillian plugin for JBoss Forge. After reading this guide, you'll be able to:

* Install the Arquillian plugin into your Forge installation
* Use the plugin to add the Arquillian infrastructure to a Maven-based Java Project
* Generate a component and cooresponding Arquillian test
* Execute the Arquillian test on multiple containers without manual configuration

You'll be performing many of the same steps described in the "Getting Started":../getting_started guide, only you'll be letting Forge handle the dirty work. We've designed this guide to be a fast read to get you started even quicker than ever before.
</div>

h3. Assumptions

This guide assumes you have "JBoss Forge":http://jboss.org/forge setup. Installing Forge is a relatively short process. Refer to the "Getting Started guide" or the "official Forge documentation":https://docs.jboss.org/author/display/FORGE/Home for download and installation instructions. You'll also need "JDK(Java Development Kit) 1.6":http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u29-download-513648.html or better installed on your machine.

h3. Create a Project

Before we can get started with Arquillian we need a project. If you did not create one yet, use the following Forge commands to create a simple Java EE 6 project that includes JPA.

bc(command).. $ new-project --named arquillian-demo --topLevelPackage demo

p. The new project is created. Next, we need to have some code to test. Let's use Forge to create a new CDI bean.

bc(command).. $ beans new-bean --type demo.MySimpleBean --scoped DEPENDENT

p. Now configure JPA, an Entity and a simple Data Access Object.

bc(command). $ persistence setup --provider HIBERNATE --container JBOSS_AS7

bc(command). $ entity --named Language --package demo.entities

bc(command). $ field string --named name

bc(command). $ beans new-bean --type demo.dao.LanguageDao --scoped REQUEST

p. Your project is set up now. It's time for some testing!

h3. Getting Started 

Setting up Arquillian is easy, as you might have seen in the "general getting started guide":../getting_started. It is a lot of copy/paste work however to configure a new container for Arquillian in the pom.xml. Forge can do that for you with a single command!

First you will need to install the Arquillian plugin.

bc(command).. $ forge install-plugin arquillian

p. Now we need to set up Arquillian and a container configuration. Let's first try JBoss AS7

bc(command).. $ arquillian setup --container JBOSS_AS_7_MANAGED

p. Forge will prompt you which version of JUnit, Arquillian and JBoss AS7 to use. Use the latest final versions for each. Forge can even download JBoss AS7 automatically if you haven't done so yet.

That's it! Arquillian is configured. You can take a look at your pom.xml to see the dependencies and profile that is added. Forge also created the arquillian.xml file in src/test/resources.

h3. Writing a Test

Now let's write a test. Forge can help you get started again.

bc(command).. $ arquillian create-test --class demo.MySimpleBean.java

p. A new test class with a deployment and test method is added to the project. It injects the class under test using CDI, so this will immediately prove if the test really runs in a container. If your IDE can't find imports, make sure you enable the Maven profile that was created earlier. You can run the test from your IDE, directly from Maven or from Forge.

bc(command).. $ build --profile arq-jbossas-7-managed

p. *Congratulations!* You've earned your first *(greenbar)green bar* with Arquillian and Forge!

Now let's try to test some JPA code.

h3. Test Persistence

First add some JPA code to your project to test. Add a class @demo.dao.LanguageDao@ in your IDE and populate it with the content shown below:

div(filename). src/main/java/demo/dao/LanguageDao.java

bc(prettify).. package demo.dao;

import demo.entities.Language;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Singleton
@Startup
public class LanguageDao {
    @PersistenceContext
    EntityManager em;

    public List<Language> listLanguages() {
        return em.createQuery("select l from Language l").getResultList();
    }

    @PostConstruct
    public void insertTestData() {
        Language java = new Language();
        java.setName("Java");
        em.persist(java);

        Language ruby = new Language();
        ruby.setName("Ruby");
        em.persist(ruby);

        Language groovy = new Language();
        groovy.setName("Groovy");
        em.persist(groovy);
    }
}

p. Now create a test class for this bean using Forge.

bc(command).. $ arquillian create-test --class demo.dao.LanguageDao.java --enableJPA

p. This again creates a new test, but also adds your persistence.xml to the deployment. Open the test in your IDE and add the @Language@ entity to the deployment.

Now write a test assertion:

div(filename). src/test/java/demo/dao/LanguageDaoTest.java

bc(prettify).. // clip
@Test
public void testListLanguages() {
    Assert.assertEquals(3, languagedao.listLanguages().size());
}
// clip
 
p. *Congratulations!* Another *(greenbar)green bar* with Arquillian and Forge!

h3. Add More Containers

Adding more containers is as easy as executing the setup command again. For example, add Glassfish.
<commit_msg>Added forge output<commit_after>---
layout: guide
title: Getting Started Faster with Forge
authors: [<NAME>]
guide_summary: Learn how to use JBoss Forge to get started faster with Arquillian and work more efficiently as you develop tests.
guide_group: 1
---
<div class="header">
h2. #{page.title}

div(authors). *Authors:* #{page.authors.join(', ')}

This guide gets you acquainted with the Arquillian plugin for JBoss Forge. After reading this guide, you'll be able to:

* Install the Arquillian plugin into your Forge installation
* Use the plugin to add the Arquillian infrastructure to a Maven-based Java Project
* Generate a component and cooresponding Arquillian test
* Execute the Arquillian test on multiple containers without manual configuration

You'll be performing many of the same steps described in the "Getting Started":../getting_started guide, only you'll be letting Forge handle the dirty work. We've designed this guide to be a fast read to get you started even quicker than ever before.
</div>

h3. Assumptions

This guide assumes you have "JBoss Forge":http://jboss.org/forge setup. Installing Forge is a relatively short process. Refer to the "Getting Started guide" or the "official Forge documentation":https://docs.jboss.org/author/display/FORGE/Home for download and installation instructions. You'll also need "JDK(Java Development Kit) 1.6":http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u29-download-513648.html or better installed on your machine.

h3. Create a Project

Before we can get started with Arquillian we need a project. If you did not create one yet, use the following Forge commands to create a simple Java EE 6 project that includes JPA.

bc(command).. $ new-project --named arquillian-demo --topLevelPackage demo
 ? Use [/Users/paul/arquillian-demo] as project directory? [Y/n]  [true] 
***SUCCESS*** Created project [arquillian-demo] in new working directory [/Users/paul/arquillian-demo]
Wrote /Users/paul/arquillian-demo
Wrote /Users/paul/arquillian-demo/pom.xml
Wrote /Users/paul/arquillian-demo/src/main/java
Wrote /Users/paul/arquillian-demo/src/test/java
Wrote /Users/paul/arquillian-demo/src/main/resources
Wrote /Users/paul/arquillian-demo/src/test/resources
Wrote /Users/paul/arquillian-demo/src/main/resources/META-INF/forge.xml

p. The new project is created. Next, we need to have some code to test. Let's use Forge to create a new CDI bean.

bc(command).. $ beans new-bean --type demo.MySimpleBean --scoped DEPENDENT
Picked up type <JavaResource>: demo.MySimpleBean
Wrote /Users/paul/arquillian-demo/src/main/java/demo/MySimpleBean.java

p. Your project is set up now. It's time for testing!

h3. Getting started 

Setting up Arquillian is easy, as you might have seen in the "general getting started guide":../getting_started. It is a lot of copy/paste work however to configure a new container for Arquillian in the pom.xml. Forge can do that for you with a single command!

First you will need to install the Arquillian plugin.

bc(command).. $ forge install-plugin arquillian

p. Now we need to set up Arquillian and a container configuration. Let's first try JBoss AS7

bc(command).. $ arquillian setup --container JBOSS_AS_7_MANAGED

p. Forge will prompt you which version of JUnit, Arquillian and JBoss AS7 to use. Use the latest final versions for each. Forge can even download JBoss AS7 automatically if you haven't done so yet.

That's it! Arquillian is configured. You can take a look at your pom.xml to see the dependencies and profile that is added. Forge also created the arquillian.xml file in src/test/resources.

h3. Writing a Test

Now let's write a test. Forge can help you get started again.

bc(command).. $ arquillian create-test --class demo.MySimpleBean.java
Picked up type <JavaResource>: demo.MySimpleBeanTest
Wrote /Users/paul/arquillian-demo/src/test/java/demo/MySimpleBeanTest.java

p. A new test class with a deployment and test method is added to the project. It injects the class under test using CDI, so this will immediately prove if the test really runs in a container. If your IDE can't find imports, make sure you enable the Maven profile that was created earlier. You can run the test from your IDE, directly from Maven or from Forge.

bc(command).. $ build --profile arq-jbossas-7-managed

p. *Congratulations!* You've earned your first *(greenbar)green bar* with Arquillian and Forge!

Now let's try to test some JPA code.

h3. Test Persistence

Before we can start wrting tests we need to configure JPA, an Entity and a simple Data Access Object.

bc(command).. $ persistence setup --provider HIBERNATE --container JBOSS_AS7
***SUCCESS*** Installed [forge.spec.jpa] successfully.
***INFO*** Setting transaction-type="JTA"
***INFO*** Using example data source [java:jboss/datasources/ExampleDS]
Warning:  The encoding 'UTF-8' is not supported by the Java runtime.
 ? The JPA provider [HIBERNATE], also supplies extended APIs. Install these as well? [y/N]  [false] 
Wrote /Users/paul/arquillian-demo/pom.xml
Wrote /Users/paul/arquillian-demo/src/main/resources/META-INF/persistence.xml

$ entity --named Language --package demo.entities
Created @Entity [demo.entities.Language]
Picked up type <JavaResource>: demo.entities.Language
Wrote /Users/paul/arquillian-demo/src/main/java/demo/entities/Language.java

$ field string --named name
Added field to demo.entities.Language: @Column private String name;
Wrote /Users/paul/arquillian-demo/src/main/java/demo/entities/Language.java

div(filename). src/main/java/demo/dao/LanguageDao.java

bc(prettify).. package demo.dao;

import demo.entities.Language;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Singleton
@Startup
public class LanguageDao {
    @PersistenceContext
    EntityManager em;

    public List<Language> listLanguages() {
        return em.createQuery("select l from Language l").getResultList();
    }

    @PostConstruct
    public void insertTestData() {
        Language java = new Language();
        java.setName("Java");
        em.persist(java);

        Language ruby = new Language();
        ruby.setName("Ruby");
        em.persist(ruby);

        Language groovy = new Language();
        groovy.setName("Groovy");
        em.persist(groovy);
    }
}

p. Now create a test.

bc(command).. $ arquillian create-test --class demo.dao.LanguageDao.java --enableJPA
Picked up type <JavaResource>: demo.dao.LanguageDemoTest
Wrote /Users/paul/arquillian-demo/src/test/java/demo/dao/LanguageDemoTest.java

p. This again creates a new test, but also adds your persistence.xml to the deployment. Open the test in your IDE and add the @Language@ entity to the deployment.

Now write a test assertion:

div(filename). src/test/java/demo/dao/LanguageDaoTest.java

bc(prettify).. @Test
public void testListLanguages() {
	Assert.assertEquals(3, languagedao.listLanguages().size());
}
 
p. *Congratulations!* Another *(greenbar)green bar* with Arquillian!

h3. Add More Containers

Adding more containers is as easy as executing the setup command again. For example, add Glassfish.

{'fraction_non_alphanumeric': 0.08579995387099254, 'fraction_numerical': 0.00422849235027293, 'mean_word_length': 4.10718492343934, 'pattern_counts': {'":': 10, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1078367', 'n_tokens_mistral': 3940, 'n_tokens_neox': 3772, 'n_words': 1573}