001: package org.apache.ojb.broker;
002:
003: import java.util.Iterator;
004:
005: import org.apache.ojb.broker.query.Criteria;
006: import org.apache.ojb.broker.query.Query;
007: import org.apache.ojb.broker.query.QueryByCriteria;
008: import org.apache.ojb.junit.PBTestCase;
009:
010: /**
011: *
012: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
013: */
014: public class TransactionDemarcationTest extends PBTestCase {
015: private Article[] articleArr;
016: private Person[] personArr;
017: private static final int COUNT = 20;
018:
019: public TransactionDemarcationTest(String s) {
020: super (s);
021: }
022:
023: public void setUp() throws Exception {
024: super .setUp();
025: articleArr = new Article[COUNT];
026: for (int i = 0; i < COUNT; i++) {
027: Article a = createArticle(i);
028: articleArr[i] = a;
029: }
030:
031: personArr = new Person[COUNT];
032: for (int i = 0; i < COUNT; i++) {
033: Person a = createPerson(i);
034: personArr[i] = a;
035: }
036: }
037:
038: public void testInsertDelete() throws Exception {
039: doInsert();
040: doDelete();
041: }
042:
043: public void doInsert() throws Exception {
044: int beforeStore = getArticleCount();
045: broker.beginTransaction();
046: for (int i = 0; i < articleArr.length; i++) {
047: broker.store(articleArr[i]);
048: }
049: broker.commitTransaction();
050: int afterStore = getArticleCount();
051: assertEquals("Wrong number of articles stored", beforeStore
052: + articleArr.length, afterStore);
053: }
054:
055: public void doDelete() throws Exception {
056: int beforeDelete = getArticleCount();
057: broker.beginTransaction();
058: for (int i = 0; i < COUNT; i++) {
059: broker.delete(articleArr[i]);
060: }
061: broker.commitTransaction();
062: int afterDelete = getArticleCount();
063: assertEquals("Wrong number of articles deleted", beforeDelete
064: - articleArr.length, afterDelete);
065: }
066:
067: public void testInsertDifferentObjects() throws Exception {
068: int beforeStore = getArticleCount();
069: int beforeStorePersons = getPersonCount();
070: broker.beginTransaction();
071: for (int i = 0; i < articleArr.length; i++) {
072: broker.store(articleArr[i]);
073: broker.store(personArr[i]);
074: }
075: broker.commitTransaction();
076: int afterStore = getArticleCount();
077: int afterStorePersons = getPersonCount();
078: assertEquals("Wrong number of articles stored", beforeStore
079: + articleArr.length, afterStore);
080: assertEquals("Wrong number of articles stored",
081: beforeStorePersons + personArr.length,
082: afterStorePersons);
083:
084: int beforeDelete = getArticleCount();
085: int beforeDeletePerson = getPersonCount();
086: broker.beginTransaction();
087: for (int i = 0; i < COUNT; i++) {
088: broker.delete(personArr[i]);
089: broker.delete(articleArr[i]);
090: }
091: broker.commitTransaction();
092: int afterDelete = getArticleCount();
093: int afterDeletePerson = getPersonCount();
094: assertEquals("Wrong number of articles deleted", beforeDelete
095: - articleArr.length, afterDelete);
096: assertEquals("Wrong number of articles deleted",
097: beforeDeletePerson - personArr.length,
098: afterDeletePerson);
099:
100: }
101:
102: public void testInsertDifferentObjectsWithinTransaction()
103: throws Exception {
104: int beforeStore = getArticleCount();
105: int beforeStorePersons = getPersonCount();
106:
107: broker.beginTransaction();
108: broker.store(createPerson(99));
109: broker.store(createArticle(99));
110: broker.commitTransaction();
111:
112: int afterStore = getArticleCount();
113: int afterStorePersons = getPersonCount();
114: assertEquals("Wrong number of articles stored",
115: beforeStore + 1, afterStore);
116: assertEquals("Wrong number of articles stored",
117: beforeStorePersons + 1, afterStorePersons);
118: }
119:
120: public void testIterator() throws Exception {
121: Criteria c = new Criteria();
122: Query q = new QueryByCriteria(Article.class, c);
123: Iterator it = broker.getIteratorByQuery(q);
124: it.hasNext();
125: }
126:
127: public int getArticleCount() {
128: Criteria c = new Criteria();
129: Query q = new QueryByCriteria(Article.class, c);
130: int count = 0;
131: count = broker.getCount(q);
132: return count;
133: }
134:
135: public int getPersonCount() {
136: Criteria c = new Criteria();
137: Query q = new QueryByCriteria(Person.class, c);
138: int count = 0;
139: count = broker.getCount(q);
140: return count;
141: }
142:
143: private Article createArticle(int counter) {
144: Article a = new Article();
145: a.setArticleName("New Performance Article " + counter);
146: a.setMinimumStock(100);
147: a.setOrderedUnits(17);
148: a.setPrice(0.45);
149: a.setStock(234);
150: a.setSupplierId(4);
151: a.setUnit("bottle");
152: return a;
153: }
154:
155: private Person createPerson(int counter) {
156: Person p = new Person();
157: p.setFirstname("firstname " + counter);
158: p.setLastname("lastname " + counter);
159: return p;
160: }
161:
162: public static void main(String[] args) {
163: String[] arr = { TransactionDemarcationTest.class.getName() };
164: junit.textui.TestRunner.main(arr);
165: }
166: }
|