Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.55 KB | None | 0 0
  1. package ca.etsmtl.log240.financej;
  2.  
  3. import org.uispec4j.Button;
  4. import org.uispec4j.Table;
  5. import org.uispec4j.Trigger;
  6. import org.uispec4j.Window;
  7. import org.uispec4j.interception.WindowHandler;
  8. import org.uispec4j.interception.WindowInterceptor;
  9. import java.sql.Connection;
  10. import java.sql.PreparedStatement;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13.  
  14. public class LedgerTest extends FinancejAbstractTest {
  15. private Table ledgerTable;
  16. private String date, payee, description, category, amount, account;
  17. private boolean rec;
  18.  
  19. private void setValues(String date, String payee, Boolean rec, String description, String category, String amount,
  20. String account) {
  21. this.date = date;
  22. this.payee = payee;
  23. this.rec = rec;
  24. this.description = description;
  25. this.category = category;
  26. this.amount = amount;
  27. this.account = account;
  28. }
  29.  
  30. /* Valid */
  31. public void testT1_ValidLowerBounds() throws Exception {
  32. setValues("2017-11-19", "AL", false, "", "TestCategory", "0.01", "TestAccount");
  33. insertValid(date, payee, rec, description, category, amount, account);
  34. }
  35.  
  36. public void testT2_ValidUpperBounds() throws Exception {
  37. setValues("2017-11-19", generateString(50), true, generateString(250), "TestCategory", "100.00", "TestAccount");
  38. insertValid(date, payee, rec, description, category, amount, account);
  39. }
  40.  
  41. public void testT3_InvalidDateEmpty() throws Exception {
  42. setValues("", "AL", false, "", "TestCategory", "100.00", "TestAccount");
  43. insertInvalid(date, payee, rec, description, category, amount, account);
  44. }
  45.  
  46. public void testT4_InvalidDate() throws Exception {
  47. setValues("2020-01-02", "AL", false, "", "TestCategory", "100.00", "TestAccount");
  48. insertInvalid(date, payee, rec, description, category, amount, account);
  49. }
  50.  
  51. public void testT5_InvalidDateFormat() throws Exception {
  52. setValues("25-01-2017", "AL", false, "", "TestCategory", "100.00", "TestAccount");
  53. insertInvalid(date, payee, rec, description, category, amount, account);
  54. }
  55.  
  56. public void testT6_InvalidPayeeEmpty() throws Exception {
  57. setValues("2017-11-19", "", true, "Description", "TestCategory", "100.00", "TestAccount");
  58. insertInvalid(date, payee, rec, description, category, amount, account);
  59. }
  60.  
  61. public void testT7_InvalidPayeeLowerBound() throws Exception {
  62. setValues("2017-11-19", "AL", false, "", "TestCategory", "0.01", "TestAccount");
  63. insertInvalid(date, payee, rec, description, category, amount, account);
  64. }
  65.  
  66. public void testT8_InvalidPayeeUpperBound() throws Exception {
  67. setValues("2017-11-19", generateString(51), true, "Description", "TestCategory", "100.00", "TestAccount");
  68. insertInvalid(date, payee, rec, description, category, amount, account);
  69. }
  70.  
  71. /* Description */
  72. public void testT9_InvalidDescriptionUpperBound() throws Exception {
  73. setValues("2017-11-19", "AL", true, generateString(251), "TestCategory", "100.00", "TestAccount");
  74. insertInvalid(date, payee, rec, description, category, amount, account);
  75. }
  76.  
  77. /* Amount */
  78. public void testT10_InvalidAmountEmpty() throws Exception {
  79. setValues("2017-11-19", "AL", true, "Description", "TestCategory", "", "TestAccount");
  80. insertInvalid(date, payee, rec, description, category, amount, account);
  81. }
  82.  
  83. public void testT11_InvalidAmountNonNumericChar() throws Exception {
  84. setValues("2017-11-19", "AL", false, "", "TestCategory", "abc", "TestAccount");
  85. insertInvalid(date, payee, rec, description, category, amount, account);
  86. }
  87.  
  88. public void testT12_InvalidAmountLowerBound() throws Exception {
  89. setValues("2017-11-19", "AL", false, "", "TestCategory", "0", "TestAccount");
  90. insertInvalid(date, payee, rec, description, category, amount, account);
  91. }
  92.  
  93. public void testT13_InvalidAmountUpperBound() throws Exception {
  94. setValues("2017-11-19", "AL", false, "", "TestCategory", "1001", "TestAccount");
  95. insertInvalid(date, payee, rec, description, category, amount, account);
  96. }
  97.  
  98. /* Valid - Remove Transaction */
  99. public void testT14_RemoveTransaction() throws Exception {
  100. setValues("2017-11-19", "AL", false, "", "TestCategory", "50.00", "TestAccount");
  101. insertValid(date, payee, rec, description, category, amount, account);
  102. WindowInterceptor.init(ledgerButton.triggerClick()).process(new WindowHandler() {
  103. public Trigger process(Window window) {
  104. ledgerTable = window.getTable();
  105. int initialRowCount = ledgerTable.getRowCount();
  106. ledgerTable.addRowToSelection(0);
  107. window.getButton("Delete Transaction").click();
  108. assertEquals(ledgerTable.getRowCount(), initialRowCount - 1);
  109. return window.getButton("Close").triggerClick();
  110. }
  111. }).run();
  112. }
  113.  
  114. private void insertValid(String date, String payee, Boolean rec, String description, String category, String amount,
  115. String account) {
  116.  
  117. WindowInterceptor.init(ledgerButton.triggerClick()).process(new WindowHandler() {
  118. public Trigger process(Window window) throws SQLException {
  119. // setup
  120. ledgerTable = window.getTable();
  121. int initialRowCount = ledgerTable.getRowCount();
  122.  
  123. window.getTextBox("DATE_TEXT_FIELD").setText(date);
  124. window.getTextBox("PAYEE_TEXT_FIELD").setText(payee);
  125. window.getTextBox("DESC_TEXT_FIELD").setText(description);
  126. window.getTextBox("AMOUNT_TEXT_FIELD").setText(amount);
  127. if (rec)
  128. window.getCheckBox("REC_BOXCHECK_FIELD").select();
  129. else
  130. window.getCheckBox("REC_BOXCHECK_FIELD").unselect();
  131.  
  132. window.getComboBox("CATEGORY_COMBO_BOX").select("TestCategory");
  133. window.getComboBox("ACCOUNT_COMBO_BOX").select("TestAccount");
  134.  
  135. window.getButton("Add Transaction").click();
  136. assertEquals(ledgerTable.getRowCount(), initialRowCount + 1);
  137. return window.getButton("Close").triggerClick();
  138.  
  139. }
  140. }).run();
  141. }
  142.  
  143. private void insertInvalid(String date, String payee, Boolean rec, String description, String category,
  144. String amount, String account) {
  145. try {
  146. WindowInterceptor.init(ledgerButton.triggerClick()).process(new WindowHandler() {
  147. public Trigger process(Window window) {
  148. // setup
  149. ledgerTable = window.getTable();
  150. int initialRowCount = ledgerTable.getRowCount();
  151. window.getTextBox("DATE_TEXT_FIELD").setText(date);
  152. window.getTextBox("PAYEE_TEXT_FIELD").setText(payee);
  153. window.getTextBox("DESC_TEXT_FIELD").setText(description);
  154. window.getTextBox("AMOUNT_TEXT_FIELD").setText(amount);
  155. if (rec)
  156. window.getCheckBox("REC_BOXCHECK_FIELD").select();
  157. else
  158. window.getCheckBox("REC_BOXCHECK_FIELD").unselect();
  159. window.getComboBox("CATEGORY_COMBO_BOX").select(category);
  160. window.getComboBox("ACCOUNT_COMBO_BOX").select(account);
  161. Button addTransaction = window.getButton("Add Transaction");
  162. WindowInterceptor.init(addTransaction.triggerClick()).process(new WindowHandler() {
  163. public Trigger process(Window window) {
  164. return window.getButton("Ok").triggerClick();
  165. }
  166. }).run();
  167. assertEquals(ledgerTable.getRowCount(), initialRowCount);
  168. return window.getButton("Close").triggerClick();
  169. }
  170. }).run();
  171. } catch (Throwable e) {
  172. fail();
  173. }
  174.  
  175. }
  176.  
  177. private void insertValidAccount() {
  178. Connection conn = FinanceJ.getDbConnection();
  179. PreparedStatement psInsert;
  180. try {
  181. psInsert = conn.prepareStatement("insert into account(name, description) values(?,?)");
  182. psInsert.setString(1, "TestAccount");
  183. psInsert.setString(2, "");
  184. psInsert.executeUpdate();
  185. psInsert.close();
  186. } catch (Throwable e) {
  187. System.out.println(" . . . exception thrown: insertValidAccount");
  188. e.printStackTrace();
  189. }
  190. }
  191.  
  192. private void insertValidCategory() {
  193. Connection conn = FinanceJ.getDbConnection();
  194. PreparedStatement psInsert;
  195. try {
  196. psInsert = conn.prepareStatement("insert into category(name, description, budget) values(?,?,?)");
  197. psInsert.setString(1, "TestCategory");
  198. psInsert.setString(2, "");
  199. psInsert.setFloat(3, 100.00f);
  200. psInsert.executeUpdate();
  201. psInsert.close();
  202. } catch (Throwable e) {
  203. System.out.println(" . . . exception thrown: insertValidCategory");
  204. e.printStackTrace();
  205. }
  206. }
  207.  
  208. @Override
  209. public void setUp() throws Exception {
  210. super.setUp();
  211. insertValidAccount();
  212. insertValidCategory();
  213. }
  214.  
  215. @Override
  216. public void tearDown() throws Exception {
  217. super.tearDown();
  218. Connection conn = FinanceJ.getDbConnection();
  219. Statement s = conn.createStatement();
  220. Statement c = conn.createStatement();
  221. Statement l = conn.createStatement();
  222. s.execute("DELETE FROM account");
  223. s.close();
  224. c.execute("DELETE FROM category");
  225. c.close();
  226. l.execute("DELETE FROM ledger");
  227. l.close();
  228. }
  229.  
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement