001: /*
002: * User: Michael Rettig
003: * Date: Aug 13, 2002
004: * Time: 3:51:15 PM
005: */
006: package net.sourceforge.jaxor.example.tests;
007:
008: import net.sourceforge.jaxor.ConcurrencyException;
009: import net.sourceforge.jaxor.JaxorContextImpl;
010: import net.sourceforge.jaxor.QueryParams;
011: import net.sourceforge.jaxor.api.JaxorContext;
012: import net.sourceforge.jaxor.example.domain.*;
013: import net.sourceforge.jaxor.example.simple.*;
014:
015: import java.util.ArrayList;
016: import java.util.List;
017:
018: public class ConcurrencyTest extends MultiTableTestCase {
019:
020: protected List getRows() {
021: List all = new ArrayList();
022: all.add(new AddressMetaRow());
023: all.add(new ContactMetaRow());
024: return all;
025: }
026:
027: public void testOptimisticConcurrency() {
028: //Mckoi doesn't support this level of transaction isolation
029: if (!isMckoiTest()) {
030: AddressEntity address = ObjectFactory.createAddress();
031: commit();
032: AddressEntity first = AddressFinder
033: .selectByPrimaryKey(address.getAddressId());
034: commit();
035: AddressEntity second = AddressFinder
036: .selectByPrimaryKey(address.getAddressId());
037: first.setCity("new city");
038: second.setCity("second city");
039: first.update();
040: try {
041: second.update();
042: fail("Should fail with an optimistic locking exception");
043: } catch (ConcurrencyException expected) {
044: }
045: }
046: }
047:
048: public void testOptimisticConcurrencyWithConnections() {
049: AddressEntity address = ObjectFactory.createAddress();
050: commit();
051: JaxorContext conn1 = new JaxorContextImpl(this .getTransaction());
052: AddressFinderBase finder = new AddressFinderBase(conn1);
053: AddressEntity first = finder.selectByPrimaryKey(address
054: .getAddressId());
055: AddressEntity second = AddressFinder.selectByPrimaryKey(address
056: .getAddressId());
057: first.setCity("new city");
058: second.setCity("second city");
059: conn1.flush();
060: try {
061: getJaxor().flush();
062: fail("Should fail with an optimistic locking exception");
063: } catch (ConcurrencyException expected) {
064: }
065: }
066:
067: public void testOptimisticConcurrencyUponDeleteWithConnections() {
068: AddressEntity address = ObjectFactory.createAddress();
069: commit();
070: JaxorContext conn1 = new JaxorContextImpl(this .getTransaction());
071: AddressFinderBase firstFinder = new AddressFinderBase(conn1);
072: AddressEntity first = firstFinder.selectByPrimaryKey(address
073: .getAddressId());
074: AddressEntity second = AddressFinder.selectByPrimaryKey(address
075: .getAddressId());
076: first.delete();
077: conn1.flush();
078: second.setCity("second city");
079: try {
080: second.update();
081: fail("Should fail with an optimistic locking exception");
082: } catch (ConcurrencyException expected) {
083: }
084: }
085:
086: public void testOptimisticConcurrencyUponDelete() {
087: //Mckoi doesn't support this level of transaction isolation
088: if (!isMckoiTest()) {
089: AddressEntity address = ObjectFactory.createAddress();
090: commit();
091: AddressEntity first = AddressFinder
092: .selectByPrimaryKey(address.getAddressId());
093: commit();
094: AddressEntity second = AddressFinder
095: .selectByPrimaryKey(address.getAddressId());
096: first.delete();
097: second.setCity("second city");
098: try {
099: second.update();
100: fail("Should fail with an optimistic locking exception");
101: } catch (ConcurrencyException expected) {
102: }
103: }
104: }
105:
106: public void testCodeFordocs() {
107: ContactFinderBase finder = new ContactFinderBase(getJaxor());
108: ContactEntity contact = ContactFactory.createContact(new Long(
109: 12345));
110: commit();
111: finder.selectByPrimaryKey(new Long(12345));
112: finder.selectByContactId(new Long(12345));
113: finder.findUnique("where contact_id=12345", false);
114: finder.queryUnique(
115: "select * from Contact where contact_id=12345", false);
116: QueryParams params = new QueryParams();
117: params.addLong(new Long(12345));
118: finder.findUnique("where contact_id=?", params, false);
119: }
120:
121: public void testConcurrencyWithOnlyPrimaryKey() {
122: ContactEntity contact = ContactFactory.createContact();
123: commit();
124: contact = ContactFinder.selectByPrimaryKey(contact
125: .getContactId());
126: commit();
127: ContactEntity second = ContactFinder.selectByPrimaryKey(contact
128: .getContactId());
129: second.setEmail("email@mail.com");
130: second.update();
131: second = ContactFinder
132: .selectByPrimaryKey(second.getContactId());
133: assertEquals("Didn't match" + second.getEmail(),
134: "email@mail.com", second.getEmail());
135: second.setEmail("newEmail@mail.com");
136: second.update();
137: second = ContactFinder.selectByPrimaryKey(contact
138: .getContactId());
139: assertEquals("newEmail@mail.com", second.getEmail());
140: }
141:
142: public void testContactResultSet() {
143: ContactEntity contact = ContactFactory.createContact();
144: commit();
145: ContactResultSet contactRs = ContactFinder
146: .selectByEmailResultSet(contact.getEmail());
147: try {
148: while (contactRs.hasNext())
149: contactRs.next();
150: assertEquals(1, contactRs.getEntityResultSet().count());
151: } finally {
152: contactRs.close();
153: }
154: }
155: }
|