Skip to content

Commit

Permalink
Added JUnit tests to ClientDAO
Browse files Browse the repository at this point in the history
  • Loading branch information
nunoaac committed Jul 20, 2015
1 parent c8eb76d commit 2cc5702
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 399 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.nunoaac.balldraw_core.balldraw.domain.beans.BallDraw;
import com.nunoaac.balldraw_core.balldraw.domain.daos.BallDrawJpaDAO;
import com.nunoaac.balldraw_core.balldraw.domain.beans.Client;
import com.nunoaac.balldraw_core.balldraw.domain.daos.UserJpaDAO;
import com.nunoaac.balldraw_core.balldraw.domain.daos.ClientJpaDAO;
import com.nunoaac.balldraw_core.balldraw.logic.algorithm.BallDrawAlgorithmInterface.DrawAlgorithm;
import com.nunoaac.balldraw_core.balldraw.logic.generator.AutomaticBallDraw;
import com.nunoaac.balldraw_core.balldraw.logic.generator.ManualBallDraw;
Expand All @@ -23,13 +23,17 @@ public class Program {

public static void main(String[] args) throws InterruptedException, Exception {



Client user = new Client("nunoaac", "123qwe");

EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.nunoaac_balldraw_core_jar_1.0-SNAPSHOTPU");
UserJpaDAO jpac = new UserJpaDAO();
ClientJpaDAO jpac = new ClientJpaDAO();
jpac.create(user);

jpac.getHashPasswordForClient("nunoaac");

/*
BallDraw newDraw;
ManualBallDraw drawGen = new ManualBallDraw();
newDraw = drawGen.getBallDraw(50, 20, DrawAlgorithm.SIMPLERANDOM);
Expand All @@ -39,7 +43,7 @@ public static void main(String[] args) throws InterruptedException, Exception {
Client retrievedClient = jpac.findUser(user.getId());
System.out.println(retrievedClient.toString());

*/
//testBallDrawDao();
//testDaoAndPatterns();
//testManualAndAutoDraw();
Expand Down Expand Up @@ -128,8 +132,7 @@ public static void testBallDrawDao() throws Exception {
ManualBallDraw drawGen = new ManualBallDraw();
newDraw = drawGen.getBallDraw(50, 20, DrawAlgorithm.SIMPLERANDOM);

EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.nunoaac_balldraw_core_jar_1.0-SNAPSHOTPU");
BallDrawJpaDAO jpac = new BallDrawJpaDAO(emf);
BallDrawJpaDAO jpac = new BallDrawJpaDAO();
jpac.create(newDraw);

System.out.println(newDraw.toString());
Expand Down
5 changes: 5 additions & 0 deletions balldraw_core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,10 @@
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

/**
Expand All @@ -20,10 +20,10 @@ public class Client implements Serializable{

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@JoinColumn(nullable=false, unique=true)
private Long id = null;
@Column(nullable=false, unique=true) //we will remove the username setter method to make impossible to edit a client's username on the db
private String username;
@JoinColumn(nullable=false)
@Column(nullable=false)
private String hashPassword;
@OneToMany(mappedBy = "client")
private List<BallDraw> draws;
Expand All @@ -40,9 +40,12 @@ public String getUsername() {
return username;
}

/*
//commenting this method will forbid the system to change the username on the DB
public void setUsername(String username) {
this.username = username;
}
*/

public String getHashPassword() {
return hashPassword;
Expand Down Expand Up @@ -72,7 +75,31 @@ public void setDraws(List<BallDraw> draws) {
public String toString() {
return "Client{" + "id=" + id + ", username=" + username + ", hashPassword=" + hashPassword + '}';
}



@Override
public int hashCode() {
int hash = 3;
return hash;
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Client other = (Client) obj;
if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
return false;
}
if ((this.username == null) ? (other.username != null) : !this.username.equals(other.username)) {
return false;
}
if ((this.hashPassword == null) ? (other.hashPassword != null) : !this.hashPassword.equals(other.hashPassword)) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public class BallDrawJpaDAO extends GenericDAO implements Serializable {

public BallDrawJpaDAO() {
super();
super(BallDraw.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.nunoaac.balldraw_core.balldraw.domain.daos;

import com.nunoaac.balldraw_core.balldraw.domain.beans.Client;
import com.nunoaac.balldraw_core.balldraw.domain.daos.exceptions.NonexistentEntityException;
import com.nunoaac.balldraw_core.balldraw.domain.daos.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;

/**
*
* @author nunocosta
*/
public class ClientJpaDAO extends GenericDAO implements Serializable {

public ClientJpaDAO() {
super(Client.class);
}

public String getHashPasswordForClient (String username) throws NoResultException {

Query q3 = getEntityManager().createQuery("SELECT c.hashPassword FROM Client c WHERE c.username = :userParam", Client.class);
q3.setParameter("userParam", username);
String password = (String)q3.getSingleResult();

return password;
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.nunoaac.balldraw_core.balldraw.domain.daos;

import com.nunoaac.balldraw_core.balldraw.domain.beans.Client;
import com.nunoaac.balldraw_core.balldraw.domain.daos.exceptions.NonexistentEntityException;
import com.nunoaac.balldraw_core.balldraw.domain.daos.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
*
* @author nunocosta
*/
public abstract class GenericDAO <T> {
public abstract class GenericDAO<T, ID> {

private EntityManagerFactory emf = null;
private final Class<T> entityClass;

public GenericDAO() {
public GenericDAO(Class<T> entityClass) {
emf = Persistence.createEntityManagerFactory("com.nunoaac_balldraw_core_jar_1.0-SNAPSHOTPU");
this.entityClass = entityClass;
}

public EntityManager getEntityManager() {
Expand All @@ -42,4 +45,90 @@ public void create(T entity) throws Exception {
}
}

public void edit(T entity, ID id) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
entity = em.merge(entity);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
if (findById(id) == null) {
throw new NonexistentEntityException("The " + entityClass.getName() + " with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}

public void destroy(ID id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
T returnedRow = em.getReference(entityClass, id);
if (returnedRow == null) {
throw new NonexistentEntityException("The " + entityClass.getSimpleName() + " with id " + id + " no longer exists.");
}
em.remove(returnedRow);
em.getTransaction().commit();

} finally {
if (em != null) {
em.close();
}
}
}

public T findById(ID id) {
EntityManager em = getEntityManager();
try {
return em.find(entityClass, id);
} finally {
em.close();
}
}

public int getEntityCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Client> rt = cq.from(entityClass);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}

public List<T> findUserEntities() {
return findEntities(true, -1, -1);
}

public List<T> findUserEntities(int maxResults, int firstResult) {
return findEntities(false, maxResults, firstResult);
}

private List<T> findEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
}
Loading

0 comments on commit 2cc5702

Please sign in to comment.