-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase_AddTimeLineJUnitTest.java
More file actions
166 lines (129 loc) · 5.44 KB
/
Database_AddTimeLineJUnitTest.java
File metadata and controls
166 lines (129 loc) · 5.44 KB
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
160
161
162
163
164
165
166
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.AddTimelineController;
import timelineManager.controller.ModelAccess;
import timelineManager.model.Timeline;
import static org.junit.Assert.assertEquals;
public class Database_AddTimeLineJUnitTest {
private static Connection con;
private ModelAccess modelAccess = new ModelAccess();
private AddTimelineController controller = new AddTimelineController(modelAccess, null, null,null);
private String fxmlPath = "/timelineManager/view/AddTimelineView.fxml";
private ObservableList<Timeline> timelines;
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();
}
controller.setTestMode(true);
new JFXPanel();
timelines = modelAccess.timelineModel.timelineList;
// If the db file is already available the file will not be created again, Currently there are two table for tasks and timelines
try {
con = DriverManager.getConnection("jdbc:sqlite:DB_Testing.db");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlPath));
loader.setController(controller);
loader.load();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testTimeLines() throws Exception {
// Adding 200 timelines to db
addTimelines(200);
// Returning a set of all TimeLines from the db with .next() method to compare with expected data
ResultSet rsTestingTimeLines=this.displaySetOfAllTimeLines();
int counter=0;
// Test each row for integrity of data
while(rsTestingTimeLines.next()){
assertEquals(rsTestingTimeLines.getString("TimeLineTitle"), timelines.get(counter).getTitle());
assertEquals(rsTestingTimeLines.getString("TimeLineDesc"), timelines.get(counter).getDescription());
assertEquals(rsTestingTimeLines.getString("TimeLineStart"), timelines.get(counter).getStartTime().toString());
assertEquals(rsTestingTimeLines.getString("TimeLineEnd"), timelines.get(counter).getEndTime().toString());
counter++;
}
// Clear the TimelineTable data to make it ready for testing next time
deleteAllTimeLines();
}
public void addTimeLineToDB(String Title, String TimeLineDescription, String TimeLineStartDate, String TimeLineEndDate) 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 TimeLinesTable values(?,?,?,?,?,?,?);");
prep.setInt(2, UniqueName);
prep.setString(3, Title);
prep.setString(4, TimeLineDescription);
prep.setString(5, TimeLineStartDate);
prep.setString(6, TimeLineEndDate);
// TimeLine data type is just for testing
prep.setString(7, "For_Test");
//Increment ID number for timelines
UniqueName++;
prep.execute();
}
public void deleteAllTimeLines() {
String sql = "DELETE FROM TimeLinesTable WHERE type = 'For_Test'";
try {
con = DriverManager.getConnection("jdbc:sqlite:DB_Testing.db");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
private void addTimelines(int amount) throws SQLException, Exception {
int startNumber = timelines.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));
// Adding the data into db to check for integrity and expected data afterwards
addTimeLineToDB("TestTitle"+i,"TestDescription"+i,LocalDate.now().plusDays(i).toString(),LocalDate.now().plusDays(i + 1).toString());
controller.addTheTimeline(new ActionEvent());
}
}
private ResultSet displaySetOfAllTimeLines() 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 TimeLineID, TimeLineTitle, TimeLineDesc, TimeLineStart, TimeLineEnd, type from TimeLinesTable");
return res;
}
}