Skip to content

Commit 26d7f5b

Browse files
committed
Added Unit tests for main class
All reliant methods are also subsequently tested, including running with no parameters and invalid params.
1 parent f143ef5 commit 26d7f5b

1 file changed

Lines changed: 207 additions & 0 deletions

File tree

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package com.ffxivcensus.gatherer;
2+
3+
import com.sun.rowset.internal.Row;
4+
import org.jsoup.Connection;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
import org.w3c.dom.Document;
8+
import org.w3c.dom.Element;
9+
import org.w3c.dom.NodeList;
10+
import org.xml.sax.SAXException;
11+
12+
import javax.xml.parsers.DocumentBuilder;
13+
import javax.xml.parsers.DocumentBuilderFactory;
14+
import javax.xml.parsers.ParserConfigurationException;
15+
import java.io.File;
16+
import java.io.IOException;
17+
import java.sql.DriverManager;
18+
import java.sql.ResultSet;
19+
import java.sql.SQLException;
20+
import java.sql.Statement;
21+
import java.util.ArrayList;
22+
23+
import static org.junit.Assert.*;
24+
25+
/**
26+
* Test the core functionality of the program, including CLI parameters.
27+
*
28+
* @author Peter Reid
29+
* @since 1.0pre
30+
* @see com.ffxivcensus.gatherer.Player
31+
* @see com.ffxivcensus.gatherer.Gatherer
32+
* @see com.ffxivcensus.gatherer.GathererController
33+
* @see com.ffxivcensus.gatherer.PlayerTest
34+
*/
35+
public class GathererControllerTest {
36+
/**
37+
* The JDBC URL of the database to modify
38+
*/
39+
private static String dbUrl;
40+
/**
41+
* The Username of user of the SQL server user to use.
42+
*/
43+
private static String dbUser;
44+
/**
45+
* The password for the user, to use.
46+
*/
47+
private static String dbPassword;
48+
49+
/**
50+
* Before running drop the table.
51+
*/
52+
@BeforeClass
53+
public static void setUpBaseClass(){
54+
try {
55+
readConfig();
56+
} catch (ParserConfigurationException e) {
57+
e.printStackTrace();
58+
} catch (IOException e) {
59+
e.printStackTrace();
60+
} catch (SAXException e) {
61+
e.printStackTrace();
62+
}
63+
String strSQL = "DROP TABLE tblplayers;";
64+
java.sql.Connection conn = openConnection();
65+
try {
66+
Statement stmt = conn.createStatement();
67+
stmt.executeUpdate(strSQL);
68+
} catch (SQLException e) {
69+
System.out.println("Error executing SQL statement to DROP TABLE");
70+
}
71+
closeConnection(conn);
72+
}
73+
74+
/**
75+
* Test gathering run of range from 11886902 to 11887010
76+
* @throws IOException
77+
* @throws SAXException
78+
* @throws ParserConfigurationException
79+
*/
80+
@org.junit.Test
81+
public void testRunMain() throws IOException, SAXException, ParserConfigurationException {
82+
ResultSet rs;
83+
String[] arrParams = {"11886902","11887010"};
84+
GathererController.main(arrParams);
85+
//Array list of results
86+
ArrayList addedIDs = new ArrayList();
87+
88+
//Test that records were successfully written to db
89+
java.sql.Connection conn = openConnection();
90+
String strSQL = "SELECT * FROM tblPlayers WHERE `id`>=" + arrParams[0] + " AND `id`<=" + arrParams[1] +";";
91+
try {
92+
Statement stmt = conn.createStatement();
93+
rs = stmt.executeQuery(strSQL);
94+
95+
//Convert dataset to array list
96+
while(rs.next()){
97+
addedIDs.add(rs.getInt(1));
98+
}
99+
100+
} catch (SQLException e) {
101+
e.printStackTrace();
102+
}
103+
closeConnection(conn);
104+
105+
//Test for IDs we know exist
106+
assertTrue(addedIDs.contains(11886902));
107+
assertTrue(addedIDs.contains(11886903));
108+
assertTrue(addedIDs.contains(11886990));
109+
assertTrue(addedIDs.contains(11887010));
110+
111+
//Test that gatherer has not written records that don't exist
112+
assertFalse(addedIDs.contains(11886909));
113+
114+
//Test that gatherer has not 'overrun'
115+
assertFalse(addedIDs.contains(11887011));
116+
assertFalse(addedIDs.contains(11886901));
117+
}
118+
119+
/**
120+
* Run the program with invalid parameters.
121+
*/
122+
@org.junit.Test
123+
public void testRunMainInvalidParams(){
124+
String[] arrParams = {"11887010","11886902"};
125+
126+
//Store start time
127+
long startTime = System.currentTimeMillis();
128+
GathererController.main(arrParams);
129+
long endTime = System.currentTimeMillis();
130+
//Program will close in less than 3 seconds if invalid params supplied
131+
assertTrue((endTime - startTime) <= 3000);
132+
133+
}
134+
135+
/**
136+
* Run the program with no params
137+
*/
138+
@org.junit.Test
139+
public void testRunNoParams(){
140+
141+
//Store start time
142+
long startTime = System.currentTimeMillis();
143+
GathererController.main(new String[0]);
144+
long endTime = System.currentTimeMillis();
145+
//Program will close in less than 3 seconds if invalid params supplied
146+
assertTrue((endTime - startTime) <= 3000);
147+
}
148+
149+
//Utility methods
150+
/**
151+
* Open a connection to database.
152+
*
153+
* @return the opened connection
154+
* @throws SQLException exception thrown if unable to connect
155+
*/
156+
private static java.sql.Connection openConnection() {
157+
try {
158+
java.sql.Connection connection = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
159+
return connection;
160+
} catch (SQLException sqlEx) {
161+
System.out.println("Connection failed! Please see output console");
162+
sqlEx.printStackTrace();
163+
return null;
164+
}
165+
}
166+
167+
/**
168+
* Close the specified connection.
169+
*
170+
* @param conn the connection to throw
171+
* @throws SQLException exception thrown if unable to close connection.
172+
*/
173+
private static void closeConnection(java.sql.Connection conn) {
174+
try {
175+
conn.close();
176+
} catch (SQLException sqlEx) {
177+
System.out.println("Cannot close connection! Has it already been closed");
178+
}
179+
}
180+
181+
/**
182+
* Read configuration from config.xml
183+
*
184+
* @throws ParserConfigurationException Indicates a serious configuration error.
185+
* @throws IOException Indicates an error reading the file specified.
186+
* @throws SAXException Indicates an error parsing XML.
187+
*/
188+
public static void readConfig() throws ParserConfigurationException, IOException, SAXException {
189+
//Set config file location
190+
File xmlFile = new File("config.xml");
191+
//Initialize parsers
192+
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
193+
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
194+
//Parse the file
195+
Document doc = dBuilder.parse(xmlFile);
196+
197+
//Read out db config
198+
NodeList nodesJDBC = doc.getElementsByTagName("jdbc");
199+
Element elementJDBC = (Element) nodesJDBC.item(0);
200+
201+
String url = "jdbc:" + elementJDBC.getElementsByTagName("url").item(0).getTextContent() + elementJDBC.getElementsByTagName("database").item(0).getTextContent();
202+
dbUrl = url;
203+
dbUser = elementJDBC.getElementsByTagName("username").item(0).getTextContent();
204+
dbPassword = elementJDBC.getElementsByTagName("password").item(0).getTextContent();
205+
}
206+
207+
}

0 commit comments

Comments
 (0)