PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/castor-1.3.3/cpactf/src/test/java/org/castor/cpa/test/test73/TestManyToMany.java

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