Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ca.etsmtl.log240.financej;
- import org.uispec4j.Button;
- import org.uispec4j.Table;
- import org.uispec4j.Trigger;
- import org.uispec4j.Window;
- import org.uispec4j.interception.WindowHandler;
- import org.uispec4j.interception.WindowInterceptor;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class LedgerTest extends FinancejAbstractTest {
- private Table ledgerTable;
- private String date, payee, description, category, amount, account;
- private boolean rec;
- private void setValues(String date, String payee, Boolean rec, String description, String category, String amount,
- String account) {
- this.date = date;
- this.payee = payee;
- this.rec = rec;
- this.description = description;
- this.category = category;
- this.amount = amount;
- this.account = account;
- }
- /* Valid */
- public void testT1_ValidLowerBounds() throws Exception {
- setValues("2017-11-19", "AL", false, "", "TestCategory", "0.01", "TestAccount");
- insertValid(date, payee, rec, description, category, amount, account);
- }
- public void testT2_ValidUpperBounds() throws Exception {
- setValues("2017-11-19", generateString(50), true, generateString(250), "TestCategory", "100.00", "TestAccount");
- insertValid(date, payee, rec, description, category, amount, account);
- }
- public void testT3_InvalidDateEmpty() throws Exception {
- setValues("", "AL", false, "", "TestCategory", "100.00", "TestAccount");
- insertInvalid(date, payee, rec, description, category, amount, account);
- }
- public void testT4_InvalidDate() throws Exception {
- setValues("2020-01-02", "AL", false, "", "TestCategory", "100.00", "TestAccount");
- insertInvalid(date, payee, rec, description, category, amount, account);
- }
- public void testT5_InvalidDateFormat() throws Exception {
- setValues("25-01-2017", "AL", false, "", "TestCategory", "100.00", "TestAccount");
- insertInvalid(date, payee, rec, description, category, amount, account);
- }
- public void testT6_InvalidPayeeEmpty() throws Exception {
- setValues("2017-11-19", "", true, "Description", "TestCategory", "100.00", "TestAccount");
- insertInvalid(date, payee, rec, description, category, amount, account);
- }
- public void testT7_InvalidPayeeLowerBound() throws Exception {
- setValues("2017-11-19", "AL", false, "", "TestCategory", "0.01", "TestAccount");
- insertInvalid(date, payee, rec, description, category, amount, account);
- }
- public void testT8_InvalidPayeeUpperBound() throws Exception {
- setValues("2017-11-19", generateString(51), true, "Description", "TestCategory", "100.00", "TestAccount");
- insertInvalid(date, payee, rec, description, category, amount, account);
- }
- /* Description */
- public void testT9_InvalidDescriptionUpperBound() throws Exception {
- setValues("2017-11-19", "AL", true, generateString(251), "TestCategory", "100.00", "TestAccount");
- insertInvalid(date, payee, rec, description, category, amount, account);
- }
- /* Amount */
- public void testT10_InvalidAmountEmpty() throws Exception {
- setValues("2017-11-19", "AL", true, "Description", "TestCategory", "", "TestAccount");
- insertInvalid(date, payee, rec, description, category, amount, account);
- }
- public void testT11_InvalidAmountNonNumericChar() throws Exception {
- setValues("2017-11-19", "AL", false, "", "TestCategory", "abc", "TestAccount");
- insertInvalid(date, payee, rec, description, category, amount, account);
- }
- public void testT12_InvalidAmountLowerBound() throws Exception {
- setValues("2017-11-19", "AL", false, "", "TestCategory", "0", "TestAccount");
- insertInvalid(date, payee, rec, description, category, amount, account);
- }
- public void testT13_InvalidAmountUpperBound() throws Exception {
- setValues("2017-11-19", "AL", false, "", "TestCategory", "1001", "TestAccount");
- insertInvalid(date, payee, rec, description, category, amount, account);
- }
- /* Valid - Remove Transaction */
- public void testT14_RemoveTransaction() throws Exception {
- setValues("2017-11-19", "AL", false, "", "TestCategory", "50.00", "TestAccount");
- insertValid(date, payee, rec, description, category, amount, account);
- WindowInterceptor.init(ledgerButton.triggerClick()).process(new WindowHandler() {
- public Trigger process(Window window) {
- ledgerTable = window.getTable();
- int initialRowCount = ledgerTable.getRowCount();
- ledgerTable.addRowToSelection(0);
- window.getButton("Delete Transaction").click();
- assertEquals(ledgerTable.getRowCount(), initialRowCount - 1);
- return window.getButton("Close").triggerClick();
- }
- }).run();
- }
- private void insertValid(String date, String payee, Boolean rec, String description, String category, String amount,
- String account) {
- WindowInterceptor.init(ledgerButton.triggerClick()).process(new WindowHandler() {
- public Trigger process(Window window) throws SQLException {
- // setup
- ledgerTable = window.getTable();
- int initialRowCount = ledgerTable.getRowCount();
- window.getTextBox("DATE_TEXT_FIELD").setText(date);
- window.getTextBox("PAYEE_TEXT_FIELD").setText(payee);
- window.getTextBox("DESC_TEXT_FIELD").setText(description);
- window.getTextBox("AMOUNT_TEXT_FIELD").setText(amount);
- if (rec)
- window.getCheckBox("REC_BOXCHECK_FIELD").select();
- else
- window.getCheckBox("REC_BOXCHECK_FIELD").unselect();
- window.getComboBox("CATEGORY_COMBO_BOX").select("TestCategory");
- window.getComboBox("ACCOUNT_COMBO_BOX").select("TestAccount");
- window.getButton("Add Transaction").click();
- assertEquals(ledgerTable.getRowCount(), initialRowCount + 1);
- return window.getButton("Close").triggerClick();
- }
- }).run();
- }
- private void insertInvalid(String date, String payee, Boolean rec, String description, String category,
- String amount, String account) {
- try {
- WindowInterceptor.init(ledgerButton.triggerClick()).process(new WindowHandler() {
- public Trigger process(Window window) {
- // setup
- ledgerTable = window.getTable();
- int initialRowCount = ledgerTable.getRowCount();
- window.getTextBox("DATE_TEXT_FIELD").setText(date);
- window.getTextBox("PAYEE_TEXT_FIELD").setText(payee);
- window.getTextBox("DESC_TEXT_FIELD").setText(description);
- window.getTextBox("AMOUNT_TEXT_FIELD").setText(amount);
- if (rec)
- window.getCheckBox("REC_BOXCHECK_FIELD").select();
- else
- window.getCheckBox("REC_BOXCHECK_FIELD").unselect();
- window.getComboBox("CATEGORY_COMBO_BOX").select(category);
- window.getComboBox("ACCOUNT_COMBO_BOX").select(account);
- Button addTransaction = window.getButton("Add Transaction");
- WindowInterceptor.init(addTransaction.triggerClick()).process(new WindowHandler() {
- public Trigger process(Window window) {
- return window.getButton("Ok").triggerClick();
- }
- }).run();
- assertEquals(ledgerTable.getRowCount(), initialRowCount);
- return window.getButton("Close").triggerClick();
- }
- }).run();
- } catch (Throwable e) {
- fail();
- }
- }
- private void insertValidAccount() {
- Connection conn = FinanceJ.getDbConnection();
- PreparedStatement psInsert;
- try {
- psInsert = conn.prepareStatement("insert into account(name, description) values(?,?)");
- psInsert.setString(1, "TestAccount");
- psInsert.setString(2, "");
- psInsert.executeUpdate();
- psInsert.close();
- } catch (Throwable e) {
- System.out.println(" . . . exception thrown: insertValidAccount");
- e.printStackTrace();
- }
- }
- private void insertValidCategory() {
- Connection conn = FinanceJ.getDbConnection();
- PreparedStatement psInsert;
- try {
- psInsert = conn.prepareStatement("insert into category(name, description, budget) values(?,?,?)");
- psInsert.setString(1, "TestCategory");
- psInsert.setString(2, "");
- psInsert.setFloat(3, 100.00f);
- psInsert.executeUpdate();
- psInsert.close();
- } catch (Throwable e) {
- System.out.println(" . . . exception thrown: insertValidCategory");
- e.printStackTrace();
- }
- }
- @Override
- public void setUp() throws Exception {
- super.setUp();
- insertValidAccount();
- insertValidCategory();
- }
- @Override
- public void tearDown() throws Exception {
- super.tearDown();
- Connection conn = FinanceJ.getDbConnection();
- Statement s = conn.createStatement();
- Statement c = conn.createStatement();
- Statement l = conn.createStatement();
- s.execute("DELETE FROM account");
- s.close();
- c.execute("DELETE FROM category");
- c.close();
- l.execute("DELETE FROM ledger");
- l.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement