001package com.avaje.ebean;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.List;
006
007/**
008 * Base functionality for capturing beans that are sent to the save(), delete() methods
009 * of an EbeanServer.
010 * <p/>
011 * Capture the beans in order to use with test asserts.
012 */
013public class BeanCapture {
014
015  protected boolean captureBeans = true;
016
017  /**
018   * The captured beans sent to the save() methods.
019   */
020  public List<Object> save = new ArrayList<Object>();
021
022  /**
023   * The captured beans sent to the insert() methods.
024   */
025  public List<Object> insert = new ArrayList<Object>();
026
027  /**
028   * The captured beans sent to the update() methods.
029   */
030  public List<Object> update = new ArrayList<Object>();
031
032  /**
033   * The captured beans sent to the delete() methods.
034   * <p/>
035   * Note that these can include MethodCall objects for the cases when
036   * delete by id is called.
037   */
038  public List<Object> delete = new ArrayList<Object>();
039
040  /**
041   * Captured beans sent to deletePermanent() methods.
042   */
043  public List<Object> deletePermanent = new ArrayList<Object>();
044
045
046  protected void addSaved(Object bean) {
047    if (captureBeans) {
048      save.add(bean);
049    }
050  }
051
052  protected void addSavedAll(Collection<?> beans) {
053    if (captureBeans) {
054      save.addAll(beans);
055    }
056  }
057
058  protected void addInsertedAll(Collection<?> beans) {
059    if (captureBeans) {
060      insert.addAll(beans);
061    }
062  }
063
064  protected void addInserted(Object bean) {
065    if (captureBeans) {
066      insert.add(bean);
067    }
068  }
069
070  protected void addUpdatedAll(Collection<?> beans) {
071    if (captureBeans) {
072      update.addAll(beans);
073    }
074  }
075
076  protected void addUpdated(Object bean) {
077    if (captureBeans) {
078      update.add(bean);
079    }
080  }
081
082  protected void addDeletedAll(Collection<?> beans) {
083    if (captureBeans) {
084      delete.addAll(beans);
085    }
086  }
087
088  protected void addDeleted(Object bean) {
089    if (captureBeans) {
090      delete.add(bean);
091    }
092  }
093
094  protected void addDeletePermanent(Object bean) {
095    if (captureBeans) {
096      deletePermanent.add(bean);
097    }
098  }
099
100  public void addDeletedAllPermanent(Collection<?> beans) {
101    if (captureBeans) {
102      deletePermanent.addAll(beans);
103    }
104  }
105}