Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.71 KB | None | 0 0
  1. import static org.junit.Assert.assertEquals;
  2. import static org.junit.Assert.assertNotNull;
  3.  
  4. import org.junit.Before;
  5. import org.junit.*;
  6. import java.sql.*;
  7.  
  8.  
  9. /**
  10. * TestDBsetup class creates a suite of tests to check if the DatabaseSetup class makes the
  11. * connection to the server, creates and drops tables, but also if the Database can be queried.
  12. *
  13. * @author Dragos Preda and Stephen House
  14. *
  15. */
  16. public class TestDBsetup {
  17. DatabaseSetup db;
  18. String username;
  19. String password;
  20. String dbUrl;
  21. String tableDesc;
  22. String tableDesc2;
  23. String tableDesc3;
  24. String print;
  25. Statement st;
  26.  
  27. /**
  28. * Initalises the username, password and dbUrl fields with the appropriate heroku db login
  29. * details.
  30. */
  31. @Before
  32. public void setUp() {
  33. username = "pukdztdrlwizrd";
  34. password = "9fcf6d1c0c337ebe8d481aa56f8657b23b0020d403150c76e22ba58763d31fd3";
  35. dbUrl = "jdbc:postgresql://ec2-54-217-214-201.eu-west-1.compute.amazonaws.com:"
  36. + "5432/d8cvilvhht35us?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory";
  37. }
  38.  
  39. /**
  40. * Tests if the getConnection method works. Done using JUnits assertNotNull method to
  41. * check if the connection returned from the getConnection method is null or not.
  42. * Not null meaning a connection was made.
  43. */
  44. @Test
  45. public void testConnection1() {
  46. DatabaseSetup db = new DatabaseSetup(username, password, dbUrl);
  47. assertNotNull(db.getConnection(username, password, dbUrl));
  48. }
  49.  
  50. /**
  51. * Tests to see if the constructor that calls the getConnection method makes the connection with
  52. * the server.
  53. */
  54. @Test
  55. public void testConnection2() {
  56. DatabaseSetup db = new DatabaseSetup(username, password, dbUrl);
  57. assertNotNull(db.conn);
  58. }
  59.  
  60.  
  61. @Test
  62. /**
  63. * Tests to see if tables can be dropped and created. The tables are dropped and then created.
  64. * How: If something is wrong, usually an SQLException is thrown. Execute method will return false
  65. * if it's an update to the Database. Also in the console prints will show when tables are created
  66. * and dropped.
  67. *
  68. * @throws SQLException
  69. */
  70. public void testCreateDropTable() throws SQLException {
  71. DatabaseSetup db = new DatabaseSetup(username, password, dbUrl);
  72. st = null;
  73. st = db.conn.createStatement();
  74. tableDesc = "orders(ORDER_NUMBER int primary key, TABLE_NUMBER int, PRODUCT_ID varchar(10),"
  75. + " STAFF_ID varchar(10), STATUS varchar(10), TIME TIME, foreign key(PRODUCT_ID)"
  76. + " references menu, foreign key(STAFF_ID) references staff);";
  77. tableDesc2 = "menu(PRODUCT_ID varchar(10) primary key, PRODUCT_NAME varchar(30),"
  78. + "DESCRIPTION varchar(100), PRICE float);";
  79. tableDesc3 = "staff(STAFF_ID varchar(10) primary key, NAME varchar(30), ROLE varchar(10));";
  80. DatabaseMetaData dbm = db.conn.getMetaData();
  81. ResultSet tableOrders = dbm.getTables(null, null, "orders", null);
  82. ResultSet tableMenu = dbm.getTables(null, null, "menu", null);
  83. ResultSet tableStaff = dbm.getTables(null, null, "staff", null);
  84. System.out.println("------testCreateDropTable-------");
  85. if (tableOrders.next()) {
  86. System.out.println(">Table orders already in the database. =>Table Dropped");
  87. st.execute("DROP TABLE " + "orders");
  88. }
  89. if (tableMenu.next()) {
  90. System.out.println(">Table menu already in the database. =>Table Dropped");
  91. st.execute("DROP TABLE " + "menu");
  92. }
  93. if (tableStaff.next()) {
  94. System.out.println(">Table staff already in the database. =>Table Dropped");
  95. st.execute("DROP TABLE " + "staff");
  96. }
  97. if ((st.execute("CREATE TABLE " + tableDesc2) == false)
  98. && (st.execute("CREATE TABLE " + tableDesc3) == false)) {
  99. System.out.println(">Tables orders, menu, staff succsesfully created.");
  100. } else {
  101. System.out.println(">Tables failed to create.");
  102. }
  103. assertEquals(st.execute("CREATE TABLE " + tableDesc), false);
  104. st.close();
  105. }
  106.  
  107. @Test
  108. /**
  109. * Tests to see if data can be inserted into tables and also tests if a SELECT query can be
  110. * executed on the Database. 1 row for each table is inserted, then the orders table is SELECT *
  111. * queried. The numbers in the single Orders table row represent orderNr,tableNr,productID,status
  112. * and time(7). How: If something is wrong, usually an SQLException is thrown. Asserts the single
  113. * row in table Orders with a similar String.
  114. *
  115. * @throws SQLException
  116. */
  117. public void testInsertAndQueryData() throws SQLException {
  118. System.out.println("------testInsertAndQueryData-------");
  119. DatabaseSetup db = new DatabaseSetup(username, password, dbUrl);
  120. db.tableSetup(db.conn);
  121. st = null;
  122. st = db.conn.createStatement();
  123. st.executeUpdate("INSERT INTO staff VALUES(" + "'" + Integer.parseInt("4024") + "','" + "John"
  124. + "','Waiter')");
  125. st.executeUpdate("INSERT INTO menu VALUES(" + "'" + Integer.parseInt("101")
  126. + "','Waffle','Dessert','" + Float.parseFloat("4") + "')");
  127. st.executeUpdate("INSERT INTO orders VALUES(" + "'" + Integer.parseInt("13") + "'" + "," + "'"
  128. + Integer.parseInt("4") + "'" + "," + "'" + Integer.parseInt("101") + "'" + "," + "'"
  129. + Integer.parseInt("4024") + "'" + "," + "'" + "not-ready" + "'" + "," + "'"
  130. + "2018-01-13 12:35" + "')");
  131.  
  132. String query = "SELECT * FROM orders";
  133. ResultSet rs = null;
  134. rs = st.executeQuery(query);
  135. if (rs.next() == true) {
  136. print = rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " "
  137. + rs.getString(4) + " " + rs.getString(5) + " " + rs.getString(6);
  138. System.out.println(print);
  139. }
  140.  
  141. assertEquals(print, "13 4 101 4024 not-ready 12:35:00");
  142. st.close();
  143.  
  144. }
  145.  
  146. @Test
  147. /**
  148. * Tests the checkTable method. How: If something is wrong, usually an SQLException is thrown; If
  149. * table is dropped it prints: "The 'tablename' has been dropped" or if it's in the database"The
  150. * table 'tablename' does not exist". Then the table is check if created with checkTable method:
  151. * false if not in the database, true if it is.
  152. *
  153. * @throws SQLException
  154. */
  155. public void testCheckTableDropTable() throws SQLException {
  156. DatabaseSetup db = new DatabaseSetup(username, password, dbUrl);
  157. db.tableSetup(db.conn);
  158. System.out.println("-----testCheckTabledropTable-----");
  159. db.dropTable(db.conn, "orders");
  160. assertEquals(db.checkTable(db.conn, "orders"), false);
  161.  
  162. }
  163.  
  164. @Test
  165. /**
  166. * Tests the createTable method. How: If something is wrong, usually an SQLException is thrown;
  167. * Checking if the table was created with checkTable method (tested above).
  168. */
  169. public void testCreateTable() {
  170. DatabaseSetup db = new DatabaseSetup(username, password, dbUrl);
  171. db.dropTable(db.conn, "orders");
  172. System.out.println("-----testCreateTable-----");
  173. String tableDesc =
  174. "orders(ORDER_NUMBER int primary key, TABLE_NUMBER int, PRODUCT_ID varchar(10),"
  175. + " STAFF_ID varchar(10), STATUS varchar(10), TIME TIME,"
  176. + " foreign key(PRODUCT_ID) references menu , foreign key(STAFF_ID)"
  177. + " references staff);";
  178. db.createTable(db.conn, tableDesc);
  179. assertEquals(db.checkTable(db.conn, "orders"), true);
  180. }
  181.  
  182. @Test
  183. /**
  184. * Tests the executeQuery method, to see if queries can be made and tableSetup to create all the
  185. * tables at once. How:If something is wrong, usually an SQLException is thrown. Creating tables
  186. * with tableSetup then, Inserting data into the tables, then using the result of a SELECT query
  187. * to assertEquals with the string it should produce.
  188. *
  189. * @throws SQLException Thrown so that the SQL methods can be used.
  190. */
  191. public void testExecuteQuery() throws SQLException {
  192. DatabaseSetup db = new DatabaseSetup(username, password, dbUrl);
  193. db.tableSetup(db.conn);
  194. Statement st = db.conn.createStatement();
  195. st.executeUpdate("INSERT INTO staff VALUES(" + "'" + Integer.parseInt("4024") + "','" + "John"
  196. + "','Waiter')");
  197. st.executeUpdate("INSERT INTO menu VALUES(" + "'" + Integer.parseInt("101")
  198. + "','Waffle','Dessert','" + Float.parseFloat("4") + "')");
  199. st.executeUpdate("INSERT INTO orders VALUES(" + "'" + Integer.parseInt("13") + "'" + "," + "'"
  200. + Integer.parseInt("4") + "'" + "," + "'" + Integer.parseInt("101") + "'" + "," + "'"
  201. + Integer.parseInt("4024") + "'" + "," + "'" + "not-ready" + "'" + "," + "'"
  202. + "2018-01-13 12:35" + "')");
  203. String query = "SELECT STATUS FROM orders WHERE STATUS = 'not-ready'";
  204. ResultSet rs = db.executeQuery(db.conn, query);
  205. String result = null;
  206. if (rs.next()) {
  207. result = rs.getString(1);
  208. }
  209. assertEquals(result, "not-ready");
  210. }
  211.  
  212.  
  213. /**
  214. * Tests the ReadBasicFileMenu method to see if the file is read.
  215. * Done by using the readBasicFileMenu method and then querying the database
  216. * to see if the file was read and then comparing the result of the query against
  217. * data that should be in the database using assertEquals. Tested using the testMenu.txt file.
  218. * @throws SQLException Thrown so that the JDBC SQL result set methods can be used.
  219. */
  220. @Test
  221. public void testReadBasicFileMenu() throws SQLException {
  222. System.out.println("------testReadBasicMenu------");
  223. DatabaseSetup db = new DatabaseSetup(username, password, dbUrl);
  224. db.tableSetup(db.conn);
  225. String filePath = "testMenu.txt";
  226. db.readBasicMenuFile(db.conn, filePath);
  227. String query = "SELECT PRODUCT_ID FROM menu WHERE PRODUCT_ID = '1111'";
  228. ResultSet rs = db.executeQuery(db.conn, query);
  229. String result = null;
  230. if (rs.next()) {
  231. result = rs.getString(1);
  232. }
  233. assertEquals(result, "1111");
  234. }
  235.  
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement