Guest User

Untitled

a guest
Nov 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.30 KB | None | 0 0
  1. import org.junit.*;
  2. import org.junit.rules.TestWatcher;
  3. import org.junit.runner.Description;
  4.  
  5. import static org.hamcrest.CoreMatchers.*;
  6.  
  7. import java.util.*;
  8. import java.lang.reflect.*;
  9. import java.util.function.Predicate;
  10.  
  11. public class ATMTests {
  12. private static boolean hasFailed = false;
  13. private static final int numberOfTests = 50;
  14. private static final int numberOfCases = 6;
  15.  
  16. @Rule
  17. public TestWatcher watcher = new TestWatcher() {
  18. @Override
  19. protected void failed(Throwable e, Description description) {
  20. hasFailed = true;
  21. super.failed(e, description);
  22. }
  23. };
  24.  
  25. private TesterTransaction convertFromTransaction(Transaction t){
  26. if(t.hasComment()){
  27. return new TesterTransaction(t.getType(), t.getAmount(), t.getComment().get());
  28. }
  29. return new TesterTransaction(t.getType(), t.getAmount());
  30. }
  31. /**
  32. * Class that implements equals and toString in order to make test writing more manageable and the output
  33. * more constructive
  34. */
  35. private class TesterTransaction extends Transaction implements Comparable<Transaction> {
  36.  
  37. public TesterTransaction(TransactionType type, double amount) {
  38. super(type, amount);
  39. }
  40.  
  41. public TesterTransaction(TransactionType type, double amount, String s) {
  42. super(type,amount,s);
  43. }
  44.  
  45. @Override
  46. public boolean equals(Object o){
  47. if(this == o) return true;
  48. if(o == null || !(o instanceof TesterTransaction)) return false;
  49. TesterTransaction other = (TesterTransaction) o;
  50. return this.getType() == other.getType()
  51. && this.getAmount() == other.getAmount()
  52. && this.getComment().equals(other.getComment());
  53. }
  54.  
  55. @Override
  56. public String toString() {
  57. String s = "";
  58. s = s + getType().name() + " " + getAmount();
  59. if(hasComment()) {
  60. s = s + " " + getComment().get();
  61. }
  62. return s;
  63. }
  64.  
  65. @Override
  66. public int compareTo(Transaction o) {
  67. if(this.getType().ordinal() != o.getType().ordinal()) return (this.getType() == TransactionType.DEPOSIT) ? 1 : -1;
  68. if(this.getAmount() != o.getAmount()) return ((this.getAmount() - o.getAmount()) > 0) ? 1 : -1;
  69. if(this.hasComment() && !o.hasComment()) return 1;
  70. if(o.hasComment() && !this.hasComment()) return -1;
  71. if(!o.hasComment() && !this.hasComment()) return 0;
  72. return this.getComment().get().compareTo(o.getComment().get());
  73. }
  74. }
  75.  
  76. /**
  77. * A class that allows for testing if two lists are equal via the equals method (allowing for use of JUnits equalTo)
  78. */
  79. private class TestList{
  80. private ArrayList<TesterTransaction> list;
  81.  
  82. public ArrayList<TesterTransaction> getList(){
  83. return list;
  84. }
  85.  
  86.  
  87. @Override
  88. public boolean equals(Object o) {
  89. if (this == o) return true;
  90. if (o == null || !(o instanceof TestList)) return false;
  91. TestList other = (TestList) o;
  92. List<TesterTransaction> thisList = new ArrayList<>(list);
  93. List<TesterTransaction> otherList = new ArrayList<>(other.getList());
  94. if(otherList.size() != thisList.size()) return false;
  95. Collections.sort(otherList);
  96. Collections.sort(thisList);
  97. for(int i = 0; i < thisList.size(); i++){
  98. if(thisList.get(i) == otherList.get(i)) continue;
  99. if(thisList.get(i) == null) return false;
  100. if(!thisList.get(i).equals(otherList.get(i))) return false;
  101. }
  102. return true;
  103. }
  104.  
  105. @Override
  106. public String toString(){
  107. return list.toString();
  108. }
  109.  
  110. public TestList(List<Transaction> c){
  111. this.list = new ArrayList<>();
  112. for(Transaction t : c){
  113. list.add(convertFromTransaction(t));
  114. }
  115. }
  116.  
  117. public TestList(){
  118. this.list = new ArrayList<>();
  119. }
  120. }
  121.  
  122. /**
  123. * Creates an interface that allows for code efficient generation of random tests
  124. */
  125. private static interface TransactionGenerator{
  126. Random r = new Random(0);
  127. /**
  128. * Gives the test generator something that should return true;
  129. * @return a correct TesterTransaction
  130. */
  131. TesterTransaction trueTransaction();
  132. /**
  133. * Gives the test generator something that should return false
  134. * @return an invalid TesterTransaction
  135. */
  136. TesterTransaction falseTransaction();
  137. }
  138.  
  139. /**
  140. * Creates a object that can generator arbitrary tests based on the testGenerator interface
  141. */
  142. private static class RandomTestGenerator{
  143. private TransactionGenerator gen;
  144. private int numberOfCases;
  145. private List<Transaction> expected;
  146. public RandomTestGenerator(TransactionGenerator gen, int numberOfCases){
  147. this.gen = gen;
  148. this.numberOfCases = numberOfCases;
  149. }
  150.  
  151. public List<Transaction> getInputList(){
  152. return getInputList(new Random().nextInt(numberOfCases-1)+1);
  153. }
  154.  
  155.  
  156. public List<Transaction> getInputList(int dispersion) {
  157. List<Transaction> returnedList = new ArrayList<>();
  158. expected = new ArrayList<>();
  159. for(int i = 0; i < numberOfCases; i++) {
  160. if (i % dispersion == 0 && !(i == 0 && dispersion > numberOfCases)){
  161. TesterTransaction t = gen.trueTransaction();
  162. returnedList.add(t);
  163. expected.add(t);
  164. } else {
  165. returnedList.add(gen.falseTransaction());
  166. }
  167. }
  168. return returnedList;
  169. }
  170.  
  171. public List<Transaction> getExpected(){
  172. return expected;
  173. }
  174. }
  175. @Test
  176. public void testTransactionThreeArgConstructor() throws Throwable {
  177. String methodName = "Transaction's three arg constructor: ";
  178. Transaction t1 = new Transaction(TransactionType.DEPOSIT, 100.5, "A test comment");
  179. Transaction t2 = new Transaction(TransactionType.WITHDRAWAL, 50, "asdf");
  180. Field type = t1.getClass().getDeclaredField("type");
  181. type.setAccessible(true);
  182. TransactionType t1type = (TransactionType) type.get(t1);
  183. TransactionType t2type = (TransactionType) type.get(t2);
  184. Assert.assertThat(methodName + "type field is not properly set", t1type, equalTo(TransactionType.DEPOSIT));
  185. Assert.assertThat(methodName + "type field is not properly set", t2type, equalTo(TransactionType.WITHDRAWAL));
  186.  
  187. Field amount = t1.getClass().getDeclaredField("amount");
  188. amount.setAccessible(true);
  189. double t1amt = (Double) amount.get(t1);
  190. double t2amt = (Double) amount.get(t2);
  191. Assert.assertThat(methodName + "amount is not properly set", t1amt, equalTo(100.5));
  192. Assert.assertThat(methodName + "amount is not properly set", t2amt, equalTo(50.0));
  193.  
  194. Field comment = t1.getClass().getDeclaredField("comment");
  195. comment.setAccessible(true);
  196. Optional<String> t1comment = (Optional<String>) comment.get(t1);
  197. Optional<String> t2comment = (Optional<String>) comment.get(t2);
  198. Assert.assertThat(methodName + "comment is null, should be Optional.Empty()", t1comment, is(notNullValue()));
  199. Assert.assertThat(methodName + "comment is improperly set", t1comment, equalTo(Optional.of("A test comment")));
  200. Assert.assertThat(methodName + "comment is improperly set", t2comment, equalTo(Optional.of("asdf")));
  201.  
  202. //Test the null case (if this proves valid)
  203. try {
  204. Transaction t3 = new Transaction(TransactionType.WITHDRAWAL, 100, null);
  205. Optional<String> t3comment = (Optional<String>) comment.get(t3);
  206. Assert.assertThat("the comment field not set to Optional.empty for null", t3comment, equalTo(Optional.empty()));
  207. }catch (NullPointerException e){
  208. Assert.fail(methodName + "Three arg constructor failed to properly handle the comment being null; led to a NullPointerException");
  209. e.printStackTrace();
  210. }
  211.  
  212. }
  213.  
  214. @Test
  215. public void testTransactionTwoArgConstructor() throws Throwable{
  216. Transaction t1 = new Transaction(TransactionType.WITHDRAWAL, 100.2);
  217. Transaction t2 = new Transaction(TransactionType.DEPOSIT, 57);
  218.  
  219. Field type = t1.getClass().getDeclaredField("type");
  220. type.setAccessible(true);
  221. TransactionType t1type = (TransactionType) type.get(t1);
  222. TransactionType t2type = (TransactionType) type.get(t2);
  223. Assert.assertThat("type not properly set", t1type, equalTo(TransactionType.WITHDRAWAL));
  224. Assert.assertThat("type not properly set", t2type, equalTo(TransactionType.DEPOSIT));
  225.  
  226. Field amt = t1.getClass().getDeclaredField("amount");
  227. amt.setAccessible(true);
  228. double t1amount = (Double) amt.get(t1);
  229. double t2amount = (Double) amt.get(t2);
  230. Assert.assertThat("amount not properly set", t1amount, equalTo(100.2));
  231. Assert.assertThat("amount not properly set", t2amount, equalTo(57.0));
  232.  
  233. Field comment = t1.getClass().getDeclaredField("comment");
  234. comment.setAccessible(true);
  235. Optional<String> t1comment = (Optional<String>) comment.get(t1);
  236. Optional<String> t2comment = (Optional<String>) comment.get(t2);
  237. Assert.assertThat("comment not set properly" , t1comment, equalTo(Optional.empty()));
  238. Assert.assertThat("comment not set properly", t2comment, equalTo(Optional.empty()));
  239. }
  240.  
  241. @Test
  242. public void testTransactionGetters(){
  243. Transaction t1 = new Transaction(TransactionType.WITHDRAWAL, 100.2, "asdf");
  244. Transaction t2 = new Transaction(TransactionType.DEPOSIT, 50.5);
  245.  
  246. Assert.assertThat("getType() returns improper value", t1.getType(), equalTo(TransactionType.WITHDRAWAL));
  247. Assert.assertThat("getType() returns improper value", t2.getType(), equalTo(TransactionType.DEPOSIT));
  248.  
  249. Assert.assertThat("amount() returns improper value", t1.getAmount(), equalTo(100.2));
  250. Assert.assertThat("amount() returns improper value", t2.getAmount(), equalTo(50.5));
  251.  
  252. Assert.assertThat("comment() returns improper value", t1.getComment(), equalTo(Optional.of("asdf")));
  253. Assert.assertThat("comment() returns improper value", t2.getComment(), equalTo(Optional.empty()));
  254. }
  255.  
  256. @Test
  257. public void testTransactionHasComment() {
  258. Transaction t1 = new Transaction(TransactionType.WITHDRAWAL, 100.2, "asdf");
  259. Transaction t2 = new Transaction(TransactionType.DEPOSIT, 50.5);
  260.  
  261. Assert.assertThat("hasComment() returns improper value when there is a comment", t1.hasComment(), equalTo(true));
  262. Assert.assertThat("hasComment() returns improper value when there isn't a comment", t2.hasComment(), equalTo(false));
  263. }
  264.  
  265. @Test
  266. public void testAccountConstructor() throws Throwable{
  267. List<Transaction> pastTransactions = new ArrayList<>();
  268. pastTransactions.add(new TesterTransaction(TransactionType.DEPOSIT, 100));
  269. pastTransactions.add(new TesterTransaction(TransactionType.WITHDRAWAL,100, "A comment"));
  270.  
  271. Account testAccount = new Account(pastTransactions);
  272. Field f = testAccount.getClass().getDeclaredField("pastTransactions");
  273. f.setAccessible(true);
  274. List<Transaction> testAccountTransactions = (List<Transaction>) f.get(testAccount);
  275. Assert.assertThat("Constructor did not set List<Transactions> properly", testAccountTransactions , equalTo(pastTransactions));
  276. //Assert.fail();
  277. }
  278.  
  279. @Test
  280. public void testAccountGetTransaction(){
  281. List<Transaction> testTransactions = new ArrayList<>();
  282. Random r = new Random(0); //keep the tests consistent
  283. for(int i = 0; i < 20; i++) {
  284. TransactionType randType = (r.nextBoolean()) ? TransactionType.WITHDRAWAL : TransactionType.DEPOSIT;
  285. double amount = r.nextDouble();
  286. String comment = (r.nextBoolean()) ? null : "A test comment";
  287. testTransactions.add(new TesterTransaction(randType, amount, comment));
  288. }
  289.  
  290. Account testAccount = new Account(testTransactions);
  291. for(int i = 0; i < testTransactions.size(); i++) {
  292. Transaction returned = testAccount.getTransaction(i);
  293. Transaction testTransaction = testTransactions.get(i);
  294. Assert.assertThat("getTransaction() failed to properly get the right transaction for n = " + i, returned, equalTo(testTransaction));
  295. }
  296. }
  297.  
  298. //TODO: clean up variable names and fix the rest of the tests
  299.  
  300. @Test
  301. public void testAccountFindTransactionByPredicate() throws Exception{
  302. List<Transaction> pastTransactions = new ArrayList<>();
  303. pastTransactions.add(new TesterTransaction(TransactionType.DEPOSIT, 100));
  304. pastTransactions.add(new TesterTransaction(TransactionType.WITHDRAWAL,100, "A comment"));
  305.  
  306. List<Transaction> clonedPastTransactions = new ArrayList<>(pastTransactions);
  307.  
  308. Account testAccount = new Account(pastTransactions);
  309. List<Transaction> output;
  310. List<Transaction> expectedOutput;
  311. TestList expected;
  312. TestList actual;
  313.  
  314. String failString = "findTransactionByPredicate failed to properly filter predicate ";
  315. String changedString = "findTransactionByPredicate changed pastTransactions field during execution";
  316. Field f = testAccount.getClass().getDeclaredField("pastTransactions");
  317. f.setAccessible(true);
  318. List<Transaction> currentTransactions;
  319.  
  320. expectedOutput = new ArrayList<>();
  321. output = testAccount.findTransactionsByPredicate((Transaction t) -> false);
  322. currentTransactions = (List<Transaction>) f.get(testAccount);
  323. expected = new TestList(expectedOutput);
  324. actual = new TestList(output);
  325. Assert.assertThat(changedString, currentTransactions, equalTo(clonedPastTransactions));
  326. Assert.assertThat(failString + "for all false", expected, equalTo(actual));
  327.  
  328. output = testAccount.findTransactionsByPredicate((Transaction t) -> true);
  329. currentTransactions = (List<Transaction>) f.get(testAccount);
  330. expected = new TestList(clonedPastTransactions);
  331. actual = new TestList(output);
  332. Assert.assertThat(changedString, currentTransactions, equalTo(clonedPastTransactions));
  333. Assert.assertThat(failString + "for all true" , expected, equalTo(actual));
  334. for(int i = 0; i < numberOfTests; i++){
  335. doRandomPredicate();
  336. }
  337. }
  338.  
  339. public void doRandomPredicate() throws Exception{
  340. List<Transaction> testList = new ArrayList<>();
  341. Account testAccount = new Account(testList);
  342. List<Transaction> correctList = new ArrayList<>();
  343. Random r;
  344.  
  345. for(int i = 0; i < 100; i++) {
  346. testList.add(new TesterTransaction(TransactionType.WITHDRAWAL, 100, "A comment"));
  347. }
  348. List<Transaction> clonedTestList = List.copyOf(testList);
  349.  
  350. for (Transaction t: testList){
  351. r = new Random(t.hashCode());
  352. if (r.nextBoolean()) correctList.add(t);
  353. }
  354.  
  355. List<Transaction> results = testAccount.findTransactionsByPredicate(new Predicate<Transaction>() {
  356. @Override
  357. public boolean test(Transaction transaction) {
  358. Random r = new Random(transaction.hashCode());
  359. return r.nextBoolean();
  360. }
  361. });
  362.  
  363. Field f = testAccount.getClass().getDeclaredField("pastTransactions");
  364. f.setAccessible(true);
  365. List<Transaction> currentTransaction = (List<Transaction>) f.get(testAccount);
  366. Assert.assertThat("findTransactionByPredicate modified the pastTransactions field", currentTransaction, equalTo(clonedTestList));
  367. Assert.assertThat("findTransactionByPredicate failed to return expected value", new TestList(results), equalTo(new TestList(correctList)));
  368. }
  369.  
  370. private class GetTransactionsByAmountGenerator implements TransactionGenerator{
  371. int correctAmount;
  372. public GetTransactionsByAmountGenerator(){
  373. this.correctAmount = new Random().nextInt(100);
  374. }
  375.  
  376. @Override
  377. public TesterTransaction trueTransaction() {
  378. TransactionType t = r.nextBoolean() ? TransactionType.WITHDRAWAL : TransactionType.DEPOSIT;
  379. int amount = correctAmount;
  380. if(r.nextBoolean()){
  381. return new TesterTransaction(t, amount, "A comment");
  382. }
  383. return new TesterTransaction(t, amount);
  384. }
  385.  
  386. @Override
  387. public TesterTransaction falseTransaction(){
  388. TransactionType t = r.nextBoolean() ? TransactionType.WITHDRAWAL : TransactionType.DEPOSIT;
  389. int amount;
  390. do {
  391. amount = r.nextInt(100);
  392. }while(amount == correctAmount);
  393. if(r.nextBoolean()){
  394. return new TesterTransaction(t, amount, "A comment");
  395. }
  396. return new TesterTransaction(t, amount);
  397. }
  398.  
  399. public int getCorrectAmount(){
  400. return correctAmount;
  401. }
  402. }
  403.  
  404. @Test
  405. public void testAccountGetTransactionsByAmount() throws Exception {
  406. Account testAccount = new Account(new ArrayList<>());
  407. Field f = testAccount.getClass().getDeclaredField("pastTransactions");
  408. f.setAccessible(true);
  409. TestList actual;
  410. TestList expected;
  411.  
  412. String changedString = "getTransactionsByAmount changed the pastTransactions field";
  413. String failString = "getTransactionsByAmount failed to return expected list";
  414.  
  415. for(int i = 0; i < numberOfTests; i++){
  416. GetTransactionsByAmountGenerator gen = new GetTransactionsByAmountGenerator();
  417. RandomTestGenerator rnd = new RandomTestGenerator(gen, numberOfCases);
  418. int dispersion = new Random().nextInt(numberOfCases-1)+1;
  419. if (i == 0) dispersion = 1;
  420. if (i == 1) dispersion = 2;
  421. if (i == 2) dispersion = numberOfCases+1;//forces a test on empty list
  422. List<Transaction> inputList = rnd.getInputList(dispersion);
  423. List<Transaction> correctList = rnd.getExpected();
  424. testAccount = new Account(inputList);
  425. List<Transaction> clonedInputList = new ArrayList<>(inputList);
  426. List<Transaction> output = testAccount.getTransactionsByAmount(gen.getCorrectAmount());
  427. List<Transaction> currentTransactions = (List<Transaction>) f.get(testAccount);
  428. Assert.assertThat(changedString, currentTransactions, equalTo(clonedInputList));
  429. actual = new TestList(output);
  430. expected = new TestList(correctList);
  431. Assert.assertThat(failString, actual, equalTo(expected));
  432. }
  433. }
  434.  
  435. private class GetByTypeTransactionGenerator implements TransactionGenerator{
  436. private TransactionType correctType;
  437.  
  438. public GetByTypeTransactionGenerator(TransactionType correctType){
  439. this.correctType = correctType;
  440. }
  441.  
  442. @Override
  443. public TesterTransaction trueTransaction() {
  444. int amount = r.nextInt(100);
  445. if(r.nextBoolean()){
  446. return new TesterTransaction(correctType, amount, "A comment String");
  447. }
  448. return new TesterTransaction(correctType, amount);
  449. }
  450.  
  451. @Override
  452. public TesterTransaction falseTransaction() {
  453. TransactionType t = (correctType == TransactionType.DEPOSIT) ? TransactionType.WITHDRAWAL : TransactionType.DEPOSIT;
  454. int amount = r.nextInt(100);
  455. if(r.nextBoolean()){
  456. return new TesterTransaction(t, amount, "A comment String");
  457. }
  458. return new TesterTransaction(t, amount);
  459. }
  460. }
  461.  
  462. public void testAccountTypeGetters(TransactionType correctType, String methodName) throws Exception{
  463. RandomTestGenerator rnd = new RandomTestGenerator(new GetByTypeTransactionGenerator(correctType), numberOfCases);
  464. Account testAccount = new Account(new ArrayList<>());
  465. Field f = testAccount.getClass().getDeclaredField("pastTransactions");
  466. f.setAccessible(true);
  467.  
  468. for(int i = 0; i < numberOfTests; i++) {
  469. List<Transaction> inputList = rnd.getInputList(2);
  470. List<Transaction> clonedInputList = List.copyOf(inputList);
  471. List<Transaction> expectedOutput = rnd.getExpected();
  472. testAccount = new Account(inputList);
  473. List<Transaction> output = (correctType == TransactionType.WITHDRAWAL) ? testAccount.getWithdrawals() : testAccount.getDeposits();
  474. List<Transaction> currentTransactions = (List<Transaction>) f.get(testAccount);
  475. TestList actual = new TestList(output);
  476. TestList expected = new TestList(expectedOutput);
  477. Assert.assertThat(methodName + " modified pastTransactions field", currentTransactions, equalTo(clonedInputList));
  478. Assert.assertThat(methodName + " failed to return expected result", actual, equalTo(expected));
  479. }
  480. }
  481.  
  482. @Test
  483. public void testAccountGetWithdrawals() throws Exception {
  484. testAccountTypeGetters(TransactionType.WITHDRAWAL, "getWithdrawals");
  485. }
  486.  
  487.  
  488. @Test
  489. public void testAccountGetDeposits() throws Exception{
  490. testAccountTypeGetters(TransactionType.DEPOSIT, "getDeposits");
  491. }
  492.  
  493. private class GetTransactionsWithCommentLengthGenerator implements TransactionGenerator {
  494. private int correctLength;
  495.  
  496. public GetTransactionsWithCommentLengthGenerator() {
  497. correctLength = r.nextInt(10) + 2;
  498. }
  499.  
  500. public int getCorrectLength() {
  501. return correctLength;
  502. }
  503.  
  504. private String createStringOfLength(int n){
  505. StringBuilder s = new StringBuilder();
  506. for(int i = 0; i < n; i++){
  507. s.append("a");
  508. }
  509. return s.toString();
  510. }
  511.  
  512. @Override
  513. public TesterTransaction trueTransaction() {
  514. TransactionType t = (r.nextBoolean()) ? TransactionType.DEPOSIT : TransactionType.WITHDRAWAL;
  515. int amount = r.nextInt(100);
  516. String comment = createStringOfLength(r.nextInt(10)+correctLength+1);
  517. return new TesterTransaction(t, amount, comment);
  518. }
  519.  
  520. @Override
  521. public TesterTransaction falseTransaction() {
  522. TransactionType t = (r.nextBoolean()) ? TransactionType.DEPOSIT : TransactionType.WITHDRAWAL;
  523. int amount = r.nextInt(100);
  524. int commentLength;
  525. do {
  526. commentLength = r.nextInt(20);
  527. }while(commentLength > correctLength);
  528. if(r.nextBoolean()) commentLength = correctLength; //ensures a test of > vs >=
  529. String comment = createStringOfLength(commentLength);
  530. return new TesterTransaction(t, amount, comment);
  531. }
  532. }
  533.  
  534. @Test
  535. public void testAccountGetTransactionsWithCommentLongerThan() throws Exception {
  536. Account testAccount = new Account(new ArrayList<>());
  537. Field f = testAccount.getClass().getDeclaredField("pastTransactions");
  538. f.setAccessible(true);
  539.  
  540. for(int i = 0; i < numberOfTests; i++) {
  541. GetTransactionsWithCommentLengthGenerator gen = new GetTransactionsWithCommentLengthGenerator();
  542. RandomTestGenerator rnd = new RandomTestGenerator(gen, 6);
  543. List<Transaction> inputList = rnd.getInputList(2);
  544. List<Transaction> clonedInputList = List.copyOf(inputList);
  545. List<Transaction> correctList = rnd.getExpected();
  546.  
  547. testAccount = new Account(inputList);
  548. List<Transaction> output = testAccount.getTransactionsWithCommentLongerThan(gen.getCorrectLength());
  549. List<Transaction> currentTransactions = (List<Transaction>) f.get(testAccount);
  550. Assert.assertThat("getTransactionsWithCommentLongerThan changed the pastTransactions field", currentTransactions, equalTo(clonedInputList));
  551. Assert.assertThat("getTransactionsWithCommentLongerThan failed to return the expected value", new TestList(correctList), equalTo(new TestList(output)));
  552.  
  553. }
  554. }
  555.  
  556. private class GetCommentWithTransactionCommentsGenerator implements TransactionGenerator {
  557.  
  558. @Override
  559. public TesterTransaction trueTransaction() {
  560. Random r = new Random();
  561. TransactionType t = (r.nextBoolean()) ? TransactionType.WITHDRAWAL : TransactionType.DEPOSIT;
  562.  
  563. double amount = r.nextDouble();
  564.  
  565. String alphabet = "abcdefghijklmnopqrstuvwxyz";
  566. alphabet = alphabet + alphabet.toUpperCase();
  567. StringBuilder s = new StringBuilder();
  568. for(int i = 0; i < r.nextInt(25); i++) {
  569. int index = r.nextInt(alphabet.length());
  570. s.append(alphabet.charAt(index));
  571. }
  572.  
  573. return new TesterTransaction(t, amount, s.toString());
  574. }
  575.  
  576. @Override
  577. public TesterTransaction falseTransaction() {
  578. Random r = new Random();
  579. TransactionType t = (r.nextBoolean()) ? TransactionType.WITHDRAWAL : TransactionType.DEPOSIT;
  580.  
  581. double amount = r.nextDouble();
  582.  
  583. if(r.nextBoolean()) {
  584. return new TesterTransaction(t, amount);
  585. } else {
  586. return new TesterTransaction(t, amount, null);
  587. }
  588. }
  589. }
  590.  
  591. @Test
  592. public void testAccountGetTransactionWithComments() throws Exception{
  593. Account testAccount = new Account(new ArrayList<>());
  594. Field f = testAccount.getClass().getDeclaredField("pastTransactions");
  595. f.setAccessible(true);
  596.  
  597. for(int i = 0; i < numberOfTests; i++){
  598. RandomTestGenerator rnd = new RandomTestGenerator( new GetCommentWithTransactionCommentsGenerator(), numberOfCases);
  599. List<Transaction> pastTransactions = rnd.getInputList();
  600. List<Transaction> clonedPastTransactions = List.copyOf(pastTransactions);
  601. List<Transaction> expected = rnd.getExpected();
  602.  
  603. testAccount = new Account(pastTransactions);
  604. List<Transaction> output = testAccount.getTransactionsWithComment();
  605. List<Transaction> currentTransactions = (List<Transaction>) f.get(testAccount);
  606. Assert.assertThat("getTransactionsWithComments modified the pastTransactions field", currentTransactions, equalTo(clonedPastTransactions));
  607. Assert.assertThat("getTransactionsWithComments did not return the expected value", new TestList(output), equalTo(new TestList(expected)));
  608. }
  609. }
  610.  
  611. @AfterClass
  612. public static void present(){
  613. if(!hasFailed){
  614. System.out.println("All tests have succeeded. Please note that this does not test for using the required syntax or checkstyle errors. If you do those things correctly, you shall pass.");
  615. printGandalf();
  616. }
  617. }
  618.  
  619. public static void printGandalf(){
  620. System.out.println(" ....\n" +
  621. " .'' .'''\n" +
  622. ". .' :\n" +
  623. "\\\\ .: :\n" +
  624. " \\\\ _: : ..----.._\n" +
  625. " \\\\ .:::.....:::.. .' ''.\n" +
  626. " \\\\ .' #-. .-######' # '.\n" +
  627. " \\\\ '.##'/ ' ################ :\n" +
  628. " \\\\ ##################### :\n" +
  629. " \\\\ ..##.-.#### .''''###'.._ :\n" +
  630. " \\\\ :--:########: '. .' :\n" +
  631. " \\\\..__...--.. :--:#######.' '. '. :\n" +
  632. " : : : : '':'-:'':':: . '. .'\n" +
  633. " '---'''..: : ': '..'''. '. :'\n" +
  634. " \\\\ :: : : ' ''''''. '. .:\n" +
  635. " \\\\ :: : : ' '. ' :\n" +
  636. " \\\\:: : : ....' ..: ' '.\n" +
  637. " \\\\:: : : .....####\\\\ .~~.:. :\n" +
  638. " \\\\':.:.:.:'#########.===. ~ |.'-. . '''.. :\n" +
  639. " \\\\ .' ########## \\ \\ _.' '. '-. '''.\n" +
  640. " :\\\\ : ######## \\ \\ '. '-. :\n" +
  641. " : \\\\' ' #### : \\ \\ :. '-. :\n" +
  642. " : .'\\\\ :' : : \\ \\ : '-. :\n" +
  643. " : .' .\\\\ ' : : :\\ \\ : '. :\n" +
  644. " :: : \\\\' :. : : \\ \\ : '. :\n" +
  645. " ::. : \\\\ : : : ; \\ \\ : '.:\n" +
  646. " : ': '\\\\ : : : : \\:\\ : ..'\n" +
  647. " : ' \\\\ : : ; \\| : .'''\n" +
  648. " '. ' \\\\: :.''\n" +
  649. " .:..... \\\\: : ..''\n" +
  650. " '._____|'.\\\\......'''''''.:..'''\n" +
  651. " \\\\");
  652. }
  653. }
Add Comment
Please, Sign In to add comment