Package org.prevayler.demos.memento

Source Code of org.prevayler.demos.memento.TestErrorRecoveryWithMementos

package org.prevayler.demos.memento;

import org.prevayler.Prevayler;
import org.prevayler.PrevaylerFactory;
import org.prevayler.demos.memento.commands.AccountCreation;
import org.prevayler.demos.memento.commands.AccountDeletion;
import org.prevayler.demos.memento.commands.Deposit;
import org.prevayler.demos.memento.commands.Transfer;
import org.prevayler.util.memento.MementoManagerCommand;
import org.prevayler.util.memento.MementoTransaction;

/**
* A simple test of the error recovery using mementos.
* <p/>
* Dialogs will pop up asking whether to fail at specific points.
* <p/>
* All these tests have to be moved to JUnit tests.
*
* @author Johan Stuyts
*/
public class TestErrorRecoveryWithMementos {

  public static void main(String[] args) {
    try {

      run();

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  private static Prevayler<Bank> prevayler;
  private static Bank bank;

  static {
    try {
      prevayler = PrevaylerFactory.createPrevayler(new Bank(), "demoMemento");
      bank = prevayler.prevalentSystem();
    } catch (Exception e) {
      System.out.println("FAILED TO CREATE PREVAYLER!");
    }
  }

  public static void run() throws Exception {
    Account account1 = null, account2 = null;

    System.out.println("*** Creating account 1");
    MementoTransaction command = new AccountCreation("Owner 1");
    account1 = execute(command);

    System.out.println("*** Creating account 2");
    command = new AccountCreation("Owner 2");
    account2 = execute(command);

    System.out.println("*** Depositing 500 into account 1");
    command = new Deposit(account1, 500);
    execute(command);

    System.out.println("*** Transferring 200 from account 1 into account 2");
    command = new Transfer(account1.number(), account2.number(), 200);
    execute(command);

    System.out.println("*** Deleting account 1");
    command = new AccountDeletion(account1);
    execute(command);

    System.out.println("*** Deleting account 1");
    command = new AccountDeletion(account2);
    execute(command);

    prevayler.takeSnapshot();
  }

  private static Account execute(MementoTransaction command) {
    try {
      return new MementoManagerCommand(command).executeAndQuery(prevayler.prevalentSystem(), prevayler.clock().time());
    } catch (Exception exception) {
      System.out.println("FAILURE!");
      exception.printStackTrace(System.out);
    } finally {
      System.out.println(bank.toString());
    }
    return null;
  }
}
TOP

Related Classes of org.prevayler.demos.memento.TestErrorRecoveryWithMementos

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.