diff --git a/src/main/java/dao/UserDAO.java b/src/main/java/dao/UserDAO.java index 4c37d1f..ae47bee 100644 --- a/src/main/java/dao/UserDAO.java +++ b/src/main/java/dao/UserDAO.java @@ -7,6 +7,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.List; /** @@ -23,7 +24,23 @@ public class UserDAO { db = new DBConnection(); } - public List findAll(){ + public ArrayList findAll(){ + ArrayList users = new ArrayList(); + PreparedStatement ps; + try { + ps = db.connection.prepareStatement("SELECT id, username, full_name FROM user"); + ResultSet rs = ps.executeQuery(); + while (rs.next()) { + Integer userid = Integer.parseInt(rs.getString("id")); + String username = rs.getString("username"); + String fullName = rs.getString("full_name"); + User user = new User(userid, username, fullName); + users.add(user); + } + return users; + } catch (SQLException e) { + e.printStackTrace(); + } return null; } public User getById(int id){ diff --git a/src/main/java/db/DBConnection.java b/src/main/java/db/DBConnection.java index 87ebcd8..806176e 100644 --- a/src/main/java/db/DBConnection.java +++ b/src/main/java/db/DBConnection.java @@ -12,7 +12,6 @@ public class DBConnection { public DBConnection() { try { -// conn = DriverManager.getConnection("jdbc:mysql://localhost/hourtracker?user=hourtracker&password=hours"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hourtracker","hourtracker","hours"); } catch (SQLException e) { System.out.println("SQLException: " + e.getMessage()); diff --git a/src/test/java/HourTrackerTest.java b/src/test/java/HourTrackerTest.java index dbf03d2..ccff419 100644 --- a/src/test/java/HourTrackerTest.java +++ b/src/test/java/HourTrackerTest.java @@ -1,14 +1,8 @@ -import dao.UserDAO; -import db.DBConnection; - import junit.framework.Assert; import model.User; -import org.junit.Before; import org.junit.Test; -import java.util.List; - /* * @author tyrelsouza, @date 2/27/17 9:23 PM */ @@ -23,18 +17,4 @@ public class HourTrackerTest { Assert.assertEquals("tyrel", user.getUsername()); Assert.assertEquals("Tyrel Souza", user.getFullName()); } - - @Test - public void testDBConnection(){ - DBConnection db = new DBConnection(); - Assert.assertNotNull(db.connection); - } - - @Test - public void testUserDAO(){ - UserDAO userDao = new UserDAO(); - User user = userDao.getById(1); - Assert.assertEquals("tyrel", user.getUsername()); - - } } diff --git a/src/test/java/dao/UserDAOTest.java b/src/test/java/dao/UserDAOTest.java new file mode 100644 index 0000000..9b278fe --- /dev/null +++ b/src/test/java/dao/UserDAOTest.java @@ -0,0 +1,37 @@ +package dao; + +import dao.UserDAO; +import db.DBConnection; + +import junit.framework.Assert; +import model.User; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/* + * @author tyrelsouza, @date 2/27/17 9:23 PM + */ +public class UserDAOTest { + UserDAO userDao; + + @Before + public void setUp() throws Exception { + userDao = new UserDAO(); + } + + @Test + public void testUserDAO(){ + User user = userDao.getById(1); + Assert.assertEquals("tyrel", user.getUsername()); + } + + @Test + public void testUserDAOgetAll(){ + ArrayList users = userDao.findAll(); + Assert.assertEquals("tyrel", users.get(0).getUsername()); + } + +} diff --git a/src/test/java/db/DBConnectionTest.java b/src/test/java/db/DBConnectionTest.java new file mode 100644 index 0000000..3d63daf --- /dev/null +++ b/src/test/java/db/DBConnectionTest.java @@ -0,0 +1,16 @@ +package db; + +import junit.framework.Assert; +import org.junit.Test; + +/** + * Created by tyrelsouza on 2/28/17. + */ +public class DBConnectionTest { + @Test + public void testDBConnection(){ + DBConnection db = new DBConnection(); + Assert.assertNotNull(db.connection); + } + +}