Commit 7ba792b4 authored by Administrator's avatar Administrator

Replaces JSF annotations by CDI annotations

The JSF 2.2 specification recommends using CDI annotations (e.g. @Named,
@RequestScoped, etc.) instead of specific JSF annotations (e.g.
@ManagedBean, @RequestScoped, etc.). This commit replaces these
annotations following the recommendations. This change has some side
effects, as now the container does not automatically recognizes the
/faces route and JSF must be configured in the web.xml file. The new
configuration now processes every XHTML page as a JSF and the /faces
paths are no longer required or recognized.
parent c4d17fa9
......@@ -2,14 +2,14 @@ package es.uvigo.esei.xcs.jsf;
import java.security.Principal;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@ManagedBean(name = "login")
@Named("login")
@RequestScoped
public class LoginManagedBean {
@Inject
......
......@@ -4,25 +4,25 @@ import static java.util.stream.Collectors.joining;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import es.uvigo.esei.xcs.domain.entities.Owner;
import es.uvigo.esei.xcs.domain.entities.Pet;
import es.uvigo.esei.xcs.service.OwnerService;
@ManagedBean(name = "owner")
@SessionScoped
public class OwnerManagerdBean {
@Named("owner")
@RequestScoped
public class OwnerManagedBean {
@Inject
private OwnerService service;
private String login;
private String password;
private Owner currentOwner;
private boolean editing;
private String errorMessage;
......@@ -51,7 +51,11 @@ public class OwnerManagerdBean {
}
public boolean isEditing() {
return this.currentOwner != null;
return this.editing;
}
public void setEditing(boolean editing) {
this.editing = editing;
}
public List<Owner> getOwners() {
......@@ -64,9 +68,9 @@ public class OwnerManagerdBean {
.collect(joining(", "));
}
public String edit(Owner owner) {
this.currentOwner = owner;
this.login = this.currentOwner.getLogin();
public String edit(String login) {
this.editing = true;
this.login = login;
return this.getViewId();
}
......@@ -86,9 +90,10 @@ public class OwnerManagerdBean {
public String store() {
try {
if (this.isEditing()) {
this.currentOwner.changePassword(this.password);
final Owner owner = this.service.get(this.login);
owner.changePassword(this.password);
this.service.update(this.currentOwner);
this.service.update(owner);
} else {
this.service.create(new Owner(login, password));
}
......@@ -107,7 +112,7 @@ public class OwnerManagerdBean {
this.login = null;
this.password = null;
this.errorMessage = null;
this.currentOwner = null;
this.editing = false;
}
private String redirectTo(String url) {
......
......@@ -4,17 +4,17 @@ import java.util.Date;
import java.util.List;
import java.util.Optional;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import es.uvigo.esei.xcs.domain.entities.AnimalType;
import es.uvigo.esei.xcs.domain.entities.Pet;
import es.uvigo.esei.xcs.service.PetService;
@ManagedBean(name = "pet")
@SessionScoped
@Named("pet")
@RequestScoped
public class PetManagedBean {
@Inject
private PetService service;
......@@ -23,7 +23,7 @@ public class PetManagedBean {
private Date birth;
private AnimalType animal;
private Pet currentPet;
private Integer id;
private String errorMessage;
......@@ -62,7 +62,15 @@ public class PetManagedBean {
}
public boolean isEditing() {
return this.currentPet != null;
return this.id != null;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<Pet> getPets() {
......@@ -70,10 +78,10 @@ public class PetManagedBean {
}
public String edit(Pet pet) {
this.currentPet = pet;
this.name = this.currentPet.getName();
this.birth = this.currentPet.getBirth();
this.animal = this.currentPet.getAnimal();
this.id = pet.getId();
this.name = pet.getName();
this.birth = pet.getBirth();
this.animal = pet.getAnimal();
return this.getViewId();
}
......@@ -93,11 +101,12 @@ public class PetManagedBean {
public String store() {
try {
if (this.isEditing()) {
this.currentPet.setName(this.name);
this.currentPet.setBirth(this.birth);
this.currentPet.setAnimal(this.animal);
final Pet pet = this.service.get(this.id);
pet.setName(this.name);
pet.setBirth(this.birth);
pet.setAnimal(this.animal);
this.service.update(this.currentPet);
this.service.update(pet);
} else {
this.service.create(new Pet(name, animal, birth));
}
......@@ -113,10 +122,10 @@ public class PetManagedBean {
}
private void clear() {
this.currentPet = null;
this.name = null;
this.birth = null;
this.animal = null;
this.id = null;
this.errorMessage = null;
}
......
......@@ -5,14 +5,33 @@
<display-name>Pet Store JSF</display-name>
<welcome-file-list>
<welcome-file>/faces/index.xhtml</welcome-file>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<!--Defining security constraint for type of roles available -->
<security-constraint>
<web-resource-collection>
<web-resource-name>admin</web-resource-name>
<url-pattern>/faces/admin/*</url-pattern>
<url-pattern>/admin/*</url-pattern>
<http-method>POST</http-method>
<http-method>GET</http-method>
<http-method>PUT</http-method>
......@@ -26,7 +45,7 @@
<security-constraint>
<web-resource-collection>
<web-resource-name>owner</web-resource-name>
<url-pattern>/faces/owner/*</url-pattern>
<url-pattern>/owner/*</url-pattern>
<http-method>POST</http-method>
<http-method>GET</http-method>
<http-method>PUT</http-method>
......
......@@ -18,8 +18,9 @@
<h:inputText id="name-field" class="form-control" a:placeholder="Owner name" value="#{owner.login}" readonly="#{owner.editing}"></h:inputText>
<h:outputLabel for="password-field">Password</h:outputLabel>
<h:inputSecret id="password-field" class="form-control" a:placeholder="Owner password" value="#{owner.password}"></h:inputSecret>
<h:commandButton id="submit-button" class="btn btn-default" value="Store" action="#{owner.store}" />
<h:commandButton id="cancel-button" class="btn btn-default" value="Cancel" action="#{owner.cancelEditing()}" rendered="#{owner.editing}"/>
<h:inputHidden id="editing" value="#{owner.editing}"></h:inputHidden>
<h:commandButton id="submit-button" class="btn btn-default" value="Store" action="#{owner.store()}" />
<h:commandButton id="cancel-button" class="btn btn-default" value="Cancel" action="#{owner.cancelEditing()}" />
</h:form>
</h:panelGroup>
<div class="row">
......@@ -50,7 +51,7 @@
<h:column>
<h:form>
<h:commandButton class="owners-table-remove" value="Remove" action="#{owner.remove(c.login)}"/>
<h:commandButton class="owners-table-edit" value="Edit" action="#{owner.edit(c)}"/>
<h:commandButton class="owners-table-edit" value="Edit" action="#{owner.edit(c.login)}"/>
</h:form>
</h:column>
</h:dataTable>
......
......@@ -23,8 +23,9 @@
<f:selectItem itemValue="CAT" itemLabel="Cat" />
<f:selectItem itemValue="DOG" itemLabel="Dog" />
</h:selectOneMenu>
<h:inputHidden value="#{pet.id}"></h:inputHidden>
<h:commandButton id="submit" class="btn btn-default" value="Store" action="#{pet.store()}" />
<h:commandButton id="cancel" class="btn btn-default" value="Cancel" action="#{pet.cancelEditing()}" rendered="#{pet.editing}"/>
<h:commandButton id="cancel" class="btn btn-default" value="Cancel" action="#{pet.cancelEditing()}" />
</h:form>
</h:panelGroup>
<div class="row">
......
......@@ -64,8 +64,8 @@ public class OwnerJsfTest {
.addAsWebResource(WEBAPP.resolve("admin/owners.xhtml").toFile(), "admin/owners.xhtml")
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsWebInfResource(WEBAPP.resolve("WEB-INF/template.xhtml").toFile())
.addAsWebInfResource("jboss-web.xml")
.addAsWebInfResource("web.xml")
.addAsWebInfResource(WEBAPP.resolve("WEB-INF/web.xml").toFile())
.addAsWebInfResource(WEBAPP.resolve("WEB-INF/jboss-web.xml").toFile())
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}
......@@ -229,8 +229,6 @@ public class OwnerJsfTest {
assertDeleteOwner(ownerWithPets());
}
@Test @InSequence(36)
@ShouldMatchDataSet("owners-remove-with-pets.xml")
@CleanupUsingScript({ "cleanup.sql", "cleanup-autoincrement.sql" })
......@@ -246,6 +244,8 @@ public class OwnerJsfTest {
assertThat(ownersPage.areOwnersInTable(expectedOwners), is(true));
}
@Test @InSequence(41)
@UsingDataSet("owners.xml")
@Cleanup(phase = TestExecutionPhase.NONE)
......@@ -268,7 +268,7 @@ public class OwnerJsfTest {
}
@Test @InSequence(43)
@ShouldMatchDataSet("owners-update-password.xml")
@ShouldMatchDataSet(value = "owners-update-password.xml")
@CleanupUsingScript({ "cleanup.sql", "cleanup-autoincrement.sql" })
public void afterEdit() {}
......
......@@ -8,7 +8,7 @@ import org.jboss.arquillian.graphene.page.Location;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.FindBy;
@Location("faces/index.xhtml")
@Location("index.xhtml")
public class LoginPage {
@Drone
private WebDriver browser;
......@@ -21,6 +21,6 @@ public class LoginPage {
}
public void assertOnLoginPage() {
assertThat(browser.getCurrentUrl(), containsString("/faces/index.xhtml"));
assertThat(browser.getCurrentUrl(), containsString("/index.xhtml"));
}
}
package es.uvigo.esei.xcs.jsf.pages;
import static java.util.Arrays.stream;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertThat;
......@@ -25,16 +26,11 @@ public class OwnersPage {
private GrapheneElement storeError;
public void assertOnOwnersPage() {
assertThat(browser.getCurrentUrl(), containsString("/faces/admin/owners.xhtml"));
assertThat(browser.getCurrentUrl(), containsString("/owners.xhtml"));
}
public boolean areOwnersInTable(Owner ... owners) {
for (Owner owner : owners) {
if (!this.isOwnerInTable(owner))
return false;
}
return true;
return stream(owners).allMatch(this::isOwnerInTable);
}
public boolean isOwnerInTable(Owner owner) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment