PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/castor-1.3.3/cpactf/src/test/java/org/castor/cpa/test/test74/TestManyToManyKeyGen.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 606 lines | 477 code | 59 blank | 70 comment | 156 complexity | 394eb0d837cb58fa8607c1b174a38704 MD5 | raw file
  1. /*
  2. * Copyright 2008 Udai Gupta, Ralf Joachim
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.castor.cpa.test.test74;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.Iterator;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. import org.castor.cpa.test.framework.CPATestCase;
  23. import org.castor.cpa.test.framework.xml.types.DatabaseEngineType;
  24. import org.exolab.castor.jdo.Database;
  25. import org.exolab.castor.jdo.OQLQuery;
  26. import org.exolab.castor.jdo.PersistenceException;
  27. import org.exolab.castor.jdo.QueryResults;
  28. /**
  29. * Test for many to many relationship with key generators being used in both
  30. * side of the relationship. The tests create data objects, modify it and verify
  31. * the modification is persisted properly. These tests also test for many to
  32. * many relationship with long transaction support: it loads a set of data
  33. * objects in one transaction, modify it and update it into another transaction.
  34. */
  35. public final class TestManyToManyKeyGen extends CPATestCase {
  36. private static final Log LOG = LogFactory.getLog(TestManyToManyKeyGen.class);
  37. private static final String DBNAME = "test74";
  38. private static final String MAPPING = "/org/castor/cpa/test/test74/mapping.xml";
  39. private Database _db;
  40. private OQLQuery _oql;
  41. private ManyPersonKeyGen _person1;
  42. private ManyPersonKeyGen _person2;
  43. private ManyPersonKeyGen _person3;
  44. private ManyPersonKeyGen _person4;
  45. private ManyGroupKeyGen _groupA;
  46. private ManyGroupKeyGen _groupB;
  47. private int _person1Id;
  48. private int _person2Id;
  49. private int _person3Id;
  50. private int _person4Id;
  51. private int _groupAId;
  52. private int _groupBId;
  53. /**
  54. * Constructor
  55. *
  56. * @param category
  57. * The test suite of these test cases.
  58. */
  59. public TestManyToManyKeyGen(final String name) {
  60. super(name);
  61. }
  62. // Test are only included/excluded for engines that have been tested with this test suite.
  63. public boolean include(final DatabaseEngineType engine) {
  64. return (engine == DatabaseEngineType.DERBY)
  65. || (engine == DatabaseEngineType.HSQL)
  66. || (engine == DatabaseEngineType.MYSQL)
  67. || (engine == DatabaseEngineType.ORACLE)
  68. || (engine == DatabaseEngineType.POSTGRESQL)
  69. || (engine == DatabaseEngineType.SQL_SERVER)
  70. || (engine == DatabaseEngineType.SAPDB);
  71. }
  72. public void setUp() throws Exception {
  73. _db = getJDOManager(DBNAME, MAPPING).getDatabase();
  74. }
  75. public void tearDown() throws Exception {
  76. if (_db.isActive()) {
  77. _db.rollback();
  78. }
  79. _db.close();
  80. }
  81. /**
  82. * The tests create data objects, modify it and verify the modification is
  83. * persisted properly. These tests also test for many to many relationship
  84. * with long transaction support: it loads a set of data objects in one
  85. * transaction, modify it and update it into another transaction.
  86. */
  87. public void testManytoManyKeyGen() throws PersistenceException {
  88. LOG.debug("Running...");
  89. deleteGroups();
  90. deletePersons();
  91. create();
  92. check1();
  93. check2();
  94. check3();
  95. check4();
  96. check5();
  97. check6();
  98. check7();
  99. check8();
  100. }
  101. private void deleteGroups() throws PersistenceException {
  102. _db.begin();
  103. OQLQuery oqlclean = _db.getOQLQuery("SELECT object FROM " + ManyGroupKeyGen.class.getName()
  104. + " object WHERE object.id < $1");
  105. oqlclean.bind(Integer.MAX_VALUE);
  106. QueryResults enumeration = oqlclean.execute();
  107. while (enumeration.hasMore()) {
  108. _groupA = (ManyGroupKeyGen) enumeration.next();
  109. LOG.debug("Retrieved object: " + _groupA);
  110. _db.remove(_groupA);
  111. LOG.debug("Deleted object: " + _groupA);
  112. }
  113. _db.commit();
  114. }
  115. private void deletePersons() throws PersistenceException {
  116. _db.begin();
  117. OQLQuery oqlclean = _db.getOQLQuery("SELECT object FROM "
  118. + ManyPersonKeyGen.class.getName() + " object WHERE object.id < $1");
  119. oqlclean.bind(Integer.MAX_VALUE);
  120. QueryResults enumeration = oqlclean.execute();
  121. while (enumeration.hasMore()) {
  122. _person1 = (ManyPersonKeyGen) enumeration.next();
  123. LOG.debug("Retrieved object: " + _person1);
  124. _db.remove(_person1);
  125. LOG.debug("Deleted object: " + _person1);
  126. }
  127. _db.commit();
  128. }
  129. private void create() throws PersistenceException {
  130. // create new group and new people, don't link them yet.
  131. // This test for null collection handling
  132. _db.begin();
  133. _oql = _db.getOQLQuery("SELECT object FROM " + ManyGroupKeyGen.class.getName()
  134. + " object WHERE id = $1");
  135. LOG.debug("Creating new group with people!");
  136. _person1 = new ManyPersonKeyGen();
  137. _person1.setValue1("I am person 1");
  138. _person1.setGroup(null);
  139. _person1.setSthelse("Something else");
  140. _person1.setHelloworld("Hello World!");
  141. _db.create(_person1);
  142. _db.commit();
  143. _person1Id = _person1.getId();
  144. // create new group with two people
  145. _db.begin();
  146. _oql = _db.getOQLQuery("SELECT object FROM " + ManyGroupKeyGen.class.getName()
  147. + " object WHERE id = $1");
  148. LOG.debug("Creating new group with people!");
  149. _person1 = _db.load(ManyPersonKeyGen.class, new Integer(_person1Id));
  150. _person1.setValue1("I am person 1");
  151. ArrayList < ManyGroupKeyGen > gPerson1 = new ArrayList < ManyGroupKeyGen > ();
  152. _person1.setGroup(gPerson1);
  153. _person1.setSthelse("Something else");
  154. _person1.setHelloworld("Hello World!");
  155. _person2 = new ManyPersonKeyGen();
  156. _person2.setValue1("I am person 2");
  157. ArrayList < ManyGroupKeyGen > gPerson2 = new ArrayList < ManyGroupKeyGen > ();
  158. _person2.setGroup(gPerson2);
  159. _person2.setSthelse("Something else");
  160. _person2.setHelloworld("Hello World!");
  161. _person3 = new ManyPersonKeyGen();
  162. _person3.setValue1("I am person 3");
  163. ArrayList < ManyGroupKeyGen > gPerson3 = new ArrayList < ManyGroupKeyGen > ();
  164. _person3.setGroup(gPerson3);
  165. _person3.setSthelse("Something else for person 3");
  166. _person3.setHelloworld("Hello World!");
  167. _person4 = new ManyPersonKeyGen();
  168. _person4.setValue1("I am person 4");
  169. ArrayList < ManyGroupKeyGen > gPerson4 = new ArrayList < ManyGroupKeyGen > ();
  170. _person4.setGroup(gPerson4);
  171. _person4.setSthelse("Something else for person 4");
  172. _person4.setHelloworld("Hello World!");
  173. _groupA = new ManyGroupKeyGen();
  174. _groupA.setValue1("Group A");
  175. ArrayList < ManyPersonKeyGen > al = new ArrayList < ManyPersonKeyGen > ();
  176. al.add(_person1);
  177. al.add(_person2);
  178. _groupA.setPeople(al);
  179. _groupB = new ManyGroupKeyGen();
  180. _groupB.setValue1("Group B");
  181. ArrayList < ManyPersonKeyGen > bl = new ArrayList < ManyPersonKeyGen > ();
  182. bl.add(_person2);
  183. _groupB.setPeople(bl);
  184. gPerson1.add(_groupA);
  185. gPerson2.add(_groupA);
  186. gPerson2.add(_groupB);
  187. _db.create(_groupA);
  188. _db.create(_person2);
  189. _db.create(_groupB);
  190. LOG.debug("object created: " + _groupA);
  191. _db.commit();
  192. _groupAId = _groupA.getId();
  193. _groupBId = _groupB.getId();
  194. _person1Id = _person1.getId();
  195. _person2Id = _person2.getId();
  196. }
  197. private void check1() throws PersistenceException {
  198. LOG.debug("Load the objects and modify it");
  199. _db.begin();
  200. _oql.bind(_groupAId);
  201. _groupA = null;
  202. _person1 = null;
  203. _person2 = null;
  204. QueryResults enumeration = _oql.execute();
  205. if (enumeration.hasMore()) {
  206. _groupA = (ManyGroupKeyGen) enumeration.next();
  207. LOG.debug("Retrieved object: " + _groupA);
  208. Collection < ManyPersonKeyGen > p = _groupA.getPeople();
  209. if (p != null) {
  210. Iterator < ManyPersonKeyGen > itor = p.iterator();
  211. if (itor.hasNext()) {
  212. _person1 = itor.next();
  213. }
  214. if (itor.hasNext()) {
  215. _person2 = itor.next();
  216. }
  217. if (itor.hasNext()) {
  218. fail("Error: more people than expected!");
  219. }
  220. if ((_person1 == null) || (_person2 == null)) {
  221. fail("Error: expect two people in group");
  222. }
  223. if ((_person1.getId() == _person2Id) && (_person2.getId() == _person1Id)) {
  224. ManyPersonKeyGen temp = _person1;
  225. _person1 = _person2;
  226. _person2 = temp;
  227. }
  228. if ((_person1.getId() == _person1Id) && (_person2.getId() == _person2Id)) {
  229. // check if the value is valid for person1 and chnage value
  230. // of person1
  231. if ((_person1.getValue1() == null)
  232. || !_person1.getValue1().equals("I am person 1")) {
  233. fail("Error: unexpected person value");
  234. } else {
  235. _person1.setValue1("New person 1 value");
  236. }
  237. // check if the value is valid for person1 and remove
  238. // person2
  239. if ((_person2.getValue1() == null)
  240. || !_person2.getValue1().equals("I am person 2")) {
  241. fail("Error: unexpected person value");
  242. }
  243. // make sure person 2 contains 2 groups
  244. if ((_person2.getGroup() == null) || (_person2.getGroup().size() != 2)) {
  245. fail("Error: expected group not found [2]");
  246. }
  247. Iterator < ManyGroupKeyGen > groupItor = _person2.getGroup().iterator();
  248. groupItor.hasNext();
  249. ManyGroupKeyGen tempGroup = groupItor.next();
  250. int tempId = tempGroup.getId();
  251. if ((tempId != _groupAId) && (tempId != _groupBId)) {
  252. fail("Error: unexpect group found");
  253. }
  254. groupItor.hasNext();
  255. tempGroup = groupItor.next();
  256. if (tempGroup.getId() == tempId) {
  257. fail("Error: duplicated group found");
  258. }
  259. if ((tempGroup.getId() != _groupAId) && (tempGroup.getId() != _groupBId)) {
  260. fail("Error: unexpect group found");
  261. }
  262. // remove person 2
  263. itor = p.iterator();
  264. while (itor.hasNext()) {
  265. _person2 = itor.next();
  266. if (_person2.getId() == _person2Id) {
  267. itor.remove();
  268. break;
  269. }
  270. }
  271. // add person 3
  272. _groupA.getPeople().add(_person3);
  273. _person3.getGroup().add(_groupA);
  274. _db.create(_person3);
  275. } else {
  276. fail("Error: people in group is not the same as expected!");
  277. }
  278. } else {
  279. fail("Error: related object not found!");
  280. }
  281. } else {
  282. fail("Error: object not found!");
  283. }
  284. _db.commit();
  285. _person3Id = _person3.getId();
  286. if (_person3Id == _person2Id) {
  287. fail("Error: unexpected id swapping ocurrs!");
  288. }
  289. }
  290. private void check2() throws PersistenceException {
  291. LOG.debug("Load the objects again to see if changes done are effective");
  292. _db.begin();
  293. _oql.bind(_groupAId);
  294. _groupA = null;
  295. _person1 = null;
  296. _person2 = null;
  297. QueryResults enumeration = _oql.execute();
  298. if (enumeration.hasMore()) {
  299. _groupA = (ManyGroupKeyGen) enumeration.next();
  300. LOG.debug("Retrieved object: " + _groupA);
  301. Collection < ManyPersonKeyGen > p = _groupA.getPeople();
  302. if (p != null) {
  303. Iterator < ManyPersonKeyGen > itor = p.iterator();
  304. if (itor.hasNext()) {
  305. _person1 = itor.next();
  306. }
  307. if (itor.hasNext()) {
  308. _person3 = itor.next();
  309. }
  310. // swap if the order is wrong
  311. if ((_person1.getId() == _person3Id) && (_person3.getId() == _person1Id)) {
  312. ManyPersonKeyGen temp = _person1;
  313. _person1 = _person3;
  314. _person3 = temp;
  315. }
  316. if (itor.hasNext()) {
  317. fail("Error: more people than expected! " + "1:(" + _person1 + ") 2: ("
  318. + itor.next() + ")");
  319. }
  320. if (_person1 == null) {
  321. fail("Error: expect person1 in group");
  322. }
  323. if (_person1.getId() == _person1Id) {
  324. // check if the value is valid for person1 and chnage value
  325. // of person1
  326. if ((_person1.getValue1() == null)
  327. || !_person1.getValue1().equals("New person 1 value")) {
  328. fail("Error: unexpected person value");
  329. }
  330. } else {
  331. fail("Error: people in group is not the same as expected!");
  332. }
  333. if (_person3.getId() == _person3Id) {
  334. // check if the value is valid for person1 and chnage value
  335. // of person1
  336. if ((_person3.getValue1() == null)
  337. || !_person3.getValue1().equals("I am person 3")) {
  338. fail("Error: unexpected person value");
  339. }
  340. } else {
  341. fail("Error: people in group is not the same as expected!");
  342. }
  343. } else {
  344. fail("Error: related object not found!");
  345. }
  346. } else {
  347. fail("Error: object not found!");
  348. }
  349. // check if person 2 contains only one group, and the group is B
  350. _person2 = _db.load(ManyPersonKeyGen.class, new Integer(_person2Id));
  351. // make sure person 2 contains 2 groups
  352. if ((_person2.getGroup() == null) || (_person2.getGroup().size() != 1)) {
  353. fail("Error: expected group not found [3]");
  354. }
  355. Iterator < ManyGroupKeyGen > groupItor = _person2.getGroup().iterator();
  356. groupItor.hasNext();
  357. ManyGroupKeyGen tempGroup = groupItor.next();
  358. if (tempGroup.getId() != _groupBId) {
  359. fail("Error: unexpected group found [1]: " + tempGroup.getId());
  360. }
  361. // remove all group from person2
  362. _person2.setGroup(null);
  363. _db.commit();
  364. }
  365. private void check3() throws PersistenceException {
  366. _db.begin();
  367. // check if person 2 contains no group
  368. _person2 = _db.load(ManyPersonKeyGen.class, new Integer(_person2Id));
  369. if ((_person2.getGroup() != null) && (_person2.getGroup().size() != 0)) {
  370. fail("Error: expected group not found [1]");
  371. }
  372. _db.remove(_person2);
  373. _db.commit();
  374. }
  375. private void check4() throws PersistenceException {
  376. _db.begin();
  377. // check if group a and group b contains no person2
  378. _groupA = _db.load(ManyGroupKeyGen.class, new Integer(_groupAId));
  379. Iterator < ManyPersonKeyGen > groupItor = _groupA.getPeople().iterator();
  380. while (groupItor.hasNext()) {
  381. _person2 = groupItor.next();
  382. if (_person2.getId() == _person2Id) {
  383. fail("Error: person2 is not removed");
  384. }
  385. }
  386. _groupB = _db.load(ManyGroupKeyGen.class, new Integer(_groupBId));
  387. if ((_groupB.getPeople() != null) && (_groupB.getPeople().size() != 0)) {
  388. fail("Error: person2 is not removed");
  389. }
  390. // make a dangerous add (add to only one side)
  391. // user shouldn't rely on this behavior, but
  392. // should always link both side before commit
  393. _person1 = _db.load(ManyPersonKeyGen.class, new Integer(_person1Id));
  394. _person1.getGroup().add(_groupB);
  395. _db.commit();
  396. }
  397. private void check5() throws PersistenceException {
  398. // check if adding group into existing collection work
  399. _db.begin();
  400. _person1 = _db.load(ManyPersonKeyGen.class, new Integer(_person1Id));
  401. Iterator < ManyGroupKeyGen > tempItor = _person1.getGroup().iterator();
  402. if (!tempItor.hasNext()) {
  403. fail("Error: expected group from person1 not found");
  404. }
  405. _groupA = tempItor.next();
  406. int tempGroupId = _groupA.getId();
  407. if ((tempGroupId != _groupAId) && (tempGroupId != _groupBId)) {
  408. fail("Error: unexpected group from person1 found");
  409. }
  410. if (!tempItor.hasNext()) {
  411. fail("Error: expected group from person1 not found");
  412. }
  413. _groupA = tempItor.next();
  414. if (tempGroupId == _groupA.getId()) {
  415. fail("Error: duplicated group found!");
  416. }
  417. if ((_groupA.getId() != _groupAId) && (_groupA.getId() != _groupBId)) {
  418. fail("Error: unexpected group from person1 found");
  419. }
  420. _db.commit();
  421. }
  422. private void check6() throws PersistenceException {
  423. // test long transaction support
  424. _db.begin();
  425. _groupA = _db.load(ManyGroupKeyGen.class, new Integer(_groupAId));
  426. _db.commit();
  427. LOG.debug("Modifing object outside of transaction");
  428. // remove person 3
  429. Iterator < ManyPersonKeyGen > it = _groupA.getPeople().iterator();
  430. while (it.hasNext()) {
  431. _person3 = it.next();
  432. if (_person3.getId() == _person3Id) {
  433. it.remove();
  434. break;
  435. }
  436. }
  437. _person3.getGroup().clear();
  438. // add person 4
  439. _groupA.getPeople().add(_person4);
  440. _person4.getGroup().add(_groupA);
  441. // find person 1
  442. _person1 = null;
  443. it = _groupA.getPeople().iterator();
  444. while (it.hasNext()) {
  445. _person1 = it.next();
  446. if (_person1.getId() == _person1Id) {
  447. break;
  448. }
  449. }
  450. _person1.setValue1("New new value for person 1");
  451. LOG.debug("Update object to a new transaction");
  452. _db.setAutoStore(true);
  453. _db.begin();
  454. _db.update(_groupA);
  455. _db.commit();
  456. _person4Id = _person4.getId();
  457. }
  458. private void check7() throws PersistenceException {
  459. LOG.debug("Load the objects again to see if changes done are effective");
  460. _db.begin();
  461. _oql.bind(_groupAId);
  462. _groupA = null;
  463. _person1 = null;
  464. _person2 = null;
  465. QueryResults enumeration = _oql.execute();
  466. if (enumeration.hasMore()) {
  467. _groupA = (ManyGroupKeyGen) enumeration.next();
  468. LOG.debug("Retrieved object: " + _groupA);
  469. Collection < ManyPersonKeyGen > p = _groupA.getPeople();
  470. if (p != null) {
  471. Iterator < ManyPersonKeyGen > itor = p.iterator();
  472. if (itor.hasNext()) {
  473. _person1 = itor.next();
  474. } else {
  475. fail("Erorr: less people than expected!");
  476. }
  477. if (itor.hasNext()) {
  478. _person4 = itor.next();
  479. } else {
  480. fail("Erorr: less people than expected!");
  481. }
  482. // swap if the order is wrong
  483. if ((_person1.getId() == _person4Id) && (_person4.getId() == _person1Id)) {
  484. ManyPersonKeyGen temp = _person1;
  485. _person1 = _person4;
  486. _person4 = temp;
  487. }
  488. if (itor.hasNext()) {
  489. fail("Error: more people than expected! " + "1:(" + _person1 + ") 2: ("
  490. + itor.next() + ")");
  491. }
  492. if (_person1 == null) {
  493. fail("Error: expect person1 in group");
  494. }
  495. if (_person1.getId() == _person1Id) {
  496. // check if the value is valid for person1 and chnage value
  497. // of person1
  498. if ((_person1.getValue1() == null)
  499. || !_person1.getValue1().equals("New new value for person 1")) {
  500. fail("Error: unexpected person value");
  501. }
  502. } else {
  503. fail("Error: people in group is not the same as expected!");
  504. }
  505. if (_person4.getId() == _person4Id) {
  506. // check if the value is valid for person1 and chnage value
  507. // of person1
  508. if ((_person4.getValue1() == null)
  509. || !_person4.getValue1().equals("I am person 4")) {
  510. fail("Error: unexpected person value");
  511. }
  512. } else {
  513. fail("Error: people in group is not the same as expected!");
  514. }
  515. } else {
  516. fail("Error: related object not found!");
  517. }
  518. } else {
  519. fail("Error: object not found!");
  520. }
  521. _person3 = _db.load(ManyPersonKeyGen.class, new Integer(_person3Id));
  522. _db.commit();
  523. }
  524. private void check8() throws PersistenceException {
  525. // modify and commit to long trans
  526. _groupA.getPeople().add(_person3);
  527. _person3.getGroup().add(_groupA);
  528. _db.begin();
  529. _db.update(_groupA);
  530. _db.commit();
  531. // load and check
  532. _db.begin();
  533. _person3 = _db.load(ManyPersonKeyGen.class, new Integer(_person3Id));
  534. Iterator < ManyGroupKeyGen > tempItor = _person3.getGroup().iterator();
  535. if (!tempItor.hasNext()) {
  536. fail("Error: group not found");
  537. }
  538. _groupA = tempItor.next();
  539. if (_groupA.getId() != _groupAId) {
  540. fail("Error: unexpected group found");
  541. }
  542. if (tempItor.hasNext()) {
  543. fail("Error: too many group");
  544. }
  545. _db.commit();
  546. }
  547. }