-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database_AddTaskJUnitTest.java
159 lines (127 loc) · 5.29 KB
/
Database_AddTaskJUnitTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package timelineManager.unitTests;
import static org.junit.Assert.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate;
import org.junit.Before;
import org.junit.Test;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import timelineManager.controller.AddTaskController;
import timelineManager.controller.ModelAccess;
import timelineManager.model.Task;
import timelineManager.model.Timeline;
public class Database_AddTaskJUnitTest {
private static Connection con;
private ModelAccess modelAccess = new ModelAccess();
private AddTaskController controller = new AddTaskController(modelAccess, null, null, null);
private String fxmlPath = "/timelineManager/view/AddTaskView.fxml";
private ObservableList<Task> tasks;
private int UniqueName=1;
@Before
public void setUp(){
// Testing the org.sqlite.JDBC if available
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// If the db file is already available the file will not be created again, Currently there are two table for tasks and timeline
try {
con = DriverManager.getConnection("jdbc:sqlite:DB_Testing.db");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
controller.setTestMode(true);
new JFXPanel();
modelAccess.setSelectedTimeline(new Timeline("Title", "Description", LocalDate.now().minusDays(1), LocalDate.now().plusDays(300)));
tasks = modelAccess.getSelectedTimeline().taskList;
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlPath));
loader.setController(controller);
loader.load();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testTasks() throws Exception {
addTasks(500);
// Returning a set of all tasks from the db with .next() method
ResultSet rsTestingTasks=this.displaySetOfAllTasks();
int counter=0;
// Test each row for integrity of data
while(rsTestingTasks.next()){
assertEquals(rsTestingTasks.getString("TTitle"), tasks.get(counter).getTitle());
assertEquals(rsTestingTasks.getString("TaskDesc"), tasks.get(counter).getDescription());
assertEquals(rsTestingTasks.getString("TaskStart"), tasks.get(counter).getStartTime().toString());
assertEquals(rsTestingTasks.getString("TaskEnd"), tasks.get(counter).getEndTime().toString());
counter++;
}
// Clear the TaskTable data to make it ready for testing next time
deleteAllTasks();
}
public void addTasksToDB(String Title, String TaskDescription, String TaskStartDate, String TaskEndDate) throws ClassNotFoundException, SQLException {
if(con == null) {
// get connection
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:DB_Testing.db");
}
PreparedStatement prep = con
.prepareStatement("insert into TasksTable values(?,?,?,?,?,?,?,?);");
prep.setInt(2, UniqueName);
prep.setString(3, Title);
prep.setString(4, TaskDescription);
prep.setString(5, TaskStartDate);
prep.setString(6, TaskEndDate);
// TimeLine ID is 1 in testing tasks for Timeline
prep.setInt(7, 1);
// This Task Is for test only, the type of data is For_Test
prep.setString(8, "For_Test");
//Increment ID number for tasks
UniqueName++;
prep.execute();
}
@Test
public void deleteAllTasks() {
String sql = "DELETE FROM TasksTable WHERE type = 'For_Test'";
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
// preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println("timelineManager.model.ApplicationDB.deleteAllTaskByID()");
System.out.println(e.getMessage());
}
}
private void addTasks(int amount) throws SQLException, Exception {
int startNumber = tasks.size();
for(int i = startNumber; i < amount + startNumber; i++) {
controller.setTitle("TestTitle" + i);
controller.setDescription("TestDescription" + i);
controller.setStartDate(LocalDate.now().plusDays(i));
controller.setEndDate(LocalDate.now().plusDays(i + 1));
addTasksToDB("TestTitle"+i,"TestDescription"+i,LocalDate.now().plusDays(i).toString(),LocalDate.now().plusDays(i + 1).toString());
controller.addTheTask(new ActionEvent());
}
}
private ResultSet displaySetOfAllTasks() throws SQLException, ClassNotFoundException {
if(con == null) {
// get connection
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:DB_Testing.db");
}
Statement state = con.createStatement();
ResultSet res = state.executeQuery("select TaskUniqueID, TTitle, TaskDesc, TaskStart, TaskEnd, TimeLineID from TasksTable");
return res;
}
}