From: Alexey L. <lou...@us...> - 2003-06-09 12:33:00
|
User: loubyansky Date: 03/06/09 05:32:56 Modified: src/main/org/jboss/test/cmp/jbossdo/collection CollectionUnitTestCase.java Menu.java Added: src/main/org/jboss/test/cmp/jbossdo/collection Order.java Log: tests for one-to-many bidirectional, refactoring Revision Changes Path 1.6 +127 -33 jbosstest/src/main/org/jboss/test/cmp/jbossdo/collection/CollectionUnitTestCase.java Index: CollectionUnitTestCase.java =================================================================== RCS file: /cvsroot/jboss/jbosstest/src/main/org/jboss/test/cmp/jbossdo/collection/CollectionUnitTestCase.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- CollectionUnitTestCase.java 7 Jun 2003 14:49:47 -0000 1.5 +++ CollectionUnitTestCase.java 9 Jun 2003 12:32:55 -0000 1.6 @@ -11,7 +11,7 @@ import org.jboss.persistence.jbossdo.sco.SCOSet; import javax.jdo.PersistenceManagerFactory; -import javax.jdo.PersistenceManager; +import javax.jdo.JDOHelper; import junit.framework.Test; @@ -19,6 +19,7 @@ import java.util.Collection; import java.util.Arrays; import java.util.List; +import java.util.ArrayList; /** * @@ -51,7 +52,13 @@ append("CREATE TABLE MENU ("). append("MENU_ID INTEGER NOT NULL, "). append("TITLE VARCHAR(250), "). - append("PRIMARY KEY (MENU_ID));"); + append("ORDER_FK INTEGER, "). + append("PRIMARY KEY (MENU_ID));"). + + append("DROP TABLE ORDERS IF EXISTS;"). + append("CREATE TABLE ORDERS ("). + append("ORDER_ID INTEGER NOT NULL, "). + append("PRIMARY KEY (ORDER_ID));"); executeSQL(sb.toString()); } @@ -59,39 +66,12 @@ { StringBuffer sb = new StringBuffer(500); sb.append("DROP TABLE DISH;"). - append("DROP TABLE MENU;"); + append("DROP TABLE MENU;"). + append("DROP TABLE ORDERS"); executeSQL(sb.toString()); } - public void testPersistenceByReachability() - throws Exception - { - PersistenceManagerFactory pmf = getPMF(); - PersistenceManager pm = pmf.getPersistenceManager(); - FieldCallbacksHistory fieldCallbacks = new FieldCallbacksHistory(); - ((JBossPersistenceManager)pm).setFieldCallbacks(fieldCallbacks); - - // establishing & persisting associations - List dishNames = Arrays.asList(new String[]{"Olivye", "Beefsteak"}); - Menu menu = new Menu("Hydropark lunch"); - for(Iterator dishNamesIter = dishNames.iterator(); dishNamesIter.hasNext();) - menu.addDish(new Dish((String)dishNamesIter.next())); - // persistence by reachability - pm.makePersistent(menu); - - assertPersistentNew(pm, menu); - for(Iterator dishIter = menu.getDishes().iterator(); dishIter.hasNext();) - assertPersistentNew(pm, dishIter.next()); - - ((JBossPersistenceManager)pm).flushAll(); - fieldCallbacks.storedExactly(new String[]{ - "Menu.title", // menu - "Dish.title", "Dish.jdoField_Menu_dishes", // salad - "Dish.title", "Dish.jdoField_Menu_dishes" // beefsteak - }); - } - - public void testFetchingExistentManySide() + public void testOne2ManyUnidirectional() throws Exception { PersistenceManagerFactory pmf = getPMF(); @@ -99,16 +79,26 @@ FieldCallbacksHistory fieldCallbacks = new FieldCallbacksHistory(); pm.setFieldCallbacks(fieldCallbacks); - // establishing & persisting associations + // establish & persist associations List dishNames = Arrays.asList(new String[]{"Olivye", "Beefsteak"}); Menu menu = new Menu("Hydropark lunch"); for(Iterator dishNamesIter = dishNames.iterator(); dishNamesIter.hasNext();) menu.addDish(new Dish((String)dishNamesIter.next())); + // by reachability pm.makePersistent(menu); Object menuOID = pm.getObjectId(menu); Collection storedDishes = menu.getDishes(); pm.flushAll(); + assertStoredExactly(fieldCallbacks, new HistoryItem[]{ + new HistoryItem("Menu.title", "Hydropark lunch"), + new HistoryItem("Menu.order", null), + new HistoryItem("Dish.title", "Olivye"), + new HistoryItem("Dish.jdoField_Menu_dishes", menu), + new HistoryItem("Dish.title", "Beefsteak"), + new HistoryItem("Dish.jdoField_Menu_dishes", menu) + }); + fieldCallbacks.dumpStoreHistory(); fieldCallbacks.clearHistory(); // fetching associations @@ -134,5 +124,109 @@ fieldCallbacks.storedExactly(new HistoryItem[]{ new HistoryItem("Dish.jdoField_Menu_dishes", null) }); + fieldCallbacks.clearHistory(); + + pm = (JBossPersistenceManager)pmf.getPersistenceManager(); + pm.setFieldCallbacks(fieldCallbacks); + menu = (Menu)pm.getObjectById(menuOID, true); + assertTrue("!menu.getDishes().contains(salad)", !menu.getDishes().contains(salad)); + } + + public void testOne2ManyBidirectional() throws Exception + { + PersistenceManagerFactory pmf = getPMF(); + JBossPersistenceManager pm = (JBossPersistenceManager)pmf.getPersistenceManager(); + FieldCallbacksHistory fieldCallbacks = new FieldCallbacksHistory(); + pm.setFieldCallbacks(fieldCallbacks); + + // establish & persist associations + List seaMenuNames = Arrays.asList(new String[]{"Shrimps", "Beer"}); + List milkMenuNames = Arrays.asList(new String[]{"Curds", "Kefir"}); + Menu seaMenu = new Menu("Black Sea Menu"); + for(Iterator dishNamesIter = seaMenuNames.iterator(); dishNamesIter.hasNext();) + seaMenu.addDish(new Dish((String)dishNamesIter.next())); + Menu milkMenu = new Menu("Milky pleasure"); + for(Iterator dishNamesIter = milkMenuNames.iterator(); dishNamesIter.hasNext();) + milkMenu.addDish(new Dish((String)dishNamesIter.next())); + Order seaOrder = new Order(); + seaOrder.addMenu(milkMenu); + seaOrder.addMenu(seaMenu); + + // persistence by reachability + pm.makePersistent(seaOrder); + Object seaOrderId = JDOHelper.getObjectId(seaOrder); + pm.flushAll(); + assertStoredExactly(fieldCallbacks, new HistoryItem[]{ + new HistoryItem("Dish.title", "Shrimps"), + new HistoryItem("Dish.jdoField_Menu_dishes", seaMenu), + new HistoryItem("Dish.title", "Beer"), + new HistoryItem("Dish.jdoField_Menu_dishes", seaMenu), + new HistoryItem("Dish.title", "Curds"), + new HistoryItem("Dish.jdoField_Menu_dishes", milkMenu), + new HistoryItem("Dish.title", "Kefir"), + new HistoryItem("Dish.jdoField_Menu_dishes", milkMenu), + new HistoryItem("Menu.title", "Black Sea Menu"), + new HistoryItem("Menu.order", seaOrder), + new HistoryItem("Menu.title", "Milky pleasure"), + new HistoryItem("Menu.order", seaOrder), + }); + fieldCallbacks.clearHistory(); + + // fetch stored seaOrder and move menus to a newly created one + log.debug("fetch stored seaOrder and move menus to a newly created one"); + pm = (JBossPersistenceManager)pmf.getPersistenceManager(); + pm.setFieldCallbacks(fieldCallbacks); + seaOrder = (Order)pm.getObjectById(seaOrderId, true); + Order milkOrder = new Order(); + pm.makePersistent(milkOrder); + Object milkOrderId = pm.getObjectId(milkOrder); + Object menuId = null; + for(Iterator menusIter = new ArrayList(seaOrder.getMenus()).iterator(); menusIter.hasNext();) + { + Menu menu = (Menu)menusIter.next(); + menuId = pm.getObjectId(menu); + milkOrder.addMenu(menu); + assertTrue("!seaOrder.getMenus().contains(menu)", !seaOrder.getMenus().contains(menu)); + } + pm.flushAll(); + assertStoredExactly(fieldCallbacks, new HistoryItem[]{ + new HistoryItem("Menu.order", milkOrder), + new HistoryItem("Menu.order", milkOrder) + }); + fieldCallbacks.clearHistory(); + + // fetch menu and change its seaOrder + log.debug("fetch menu and change its order"); + pm = (JBossPersistenceManager)pmf.getPersistenceManager(); + pm.setFieldCallbacks(fieldCallbacks); + Menu menu = (Menu)pm.getObjectById(menuId, true); + milkOrder = (Order)pm.getObjectById(milkOrderId, true); + seaOrder = (Order)pm.getObjectById(seaOrderId, true); + + assertTrue("milkOrder.getMenus().contains(menu)", milkOrder.getMenus().contains(menu)); + assertTrue("seaOrder.getMenus().isEmpty()", seaOrder.getMenus().isEmpty()); + menu.setOrder(seaOrder); + assertTrue("!milkOrder.getMenus().contains(menu)", !milkOrder.getMenus().contains(menu)); + assertTrue("seaOrder.getMenus().contains(menu)", seaOrder.getMenus().contains(menu)); + + pm.flushAll(); + assertStoredExactly(fieldCallbacks, new HistoryItem[]{ + new HistoryItem("Menu.order", seaOrder) + }); + fieldCallbacks.clearHistory(); + + // fetch menu and set its order to null + log.debug("fetch menu and set its order to null"); + pm = (JBossPersistenceManager)pmf.getPersistenceManager(); + pm.setFieldCallbacks(fieldCallbacks); + menu = (Menu)pm.getObjectById(menuId, true); + seaOrder = (Order)pm.getObjectById(seaOrderId, true); + + assertTrue("menu.getOrder().equals(seaOrder)", menu.getOrder().equals(seaOrder)); + menu.setOrder(null); + assertTrue("seaOrder.getMenus().isEmpty()", seaOrder.getMenus().isEmpty()); + + pm.flushAll(); + assertStoredExactly(fieldCallbacks, new HistoryItem[]{new HistoryItem("Menu.order", null)}); } } 1.3 +11 -0 jbosstest/src/main/org/jboss/test/cmp/jbossdo/collection/Menu.java Index: Menu.java =================================================================== RCS file: /cvsroot/jboss/jbosstest/src/main/org/jboss/test/cmp/jbossdo/collection/Menu.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- Menu.java 7 Jun 2003 14:01:18 -0000 1.2 +++ Menu.java 9 Jun 2003 12:32:55 -0000 1.3 @@ -17,6 +17,7 @@ { private String title; private Collection dishes = new ArrayList(); + private Order order; public Menu(){}; public Menu(String title) @@ -47,6 +48,16 @@ public boolean removeDish(Dish dish) { return dishes.remove(dish); + } + + public Order getOrder() + { + return order; + } + + public void setOrder(Order order) + { + this.order = order; } public String toString() 1.1 jbosstest/src/main/org/jboss/test/cmp/jbossdo/collection/Order.java Index: Order.java =================================================================== /* * JBoss, the OpenSource J2EE webOS * * Distributable under LGPL license. * See terms of license at gnu.org. */ package org.jboss.test.cmp.jbossdo.collection; import java.util.Collection; import java.util.ArrayList; /** * * @author <a href="mailto:al...@jb...">Alex Loubyansky</a> */ public class Order { private Collection menus = new ArrayList(); public Collection getMenus() { return menus; } public int getMenusNumber() { return menus.size(); } public boolean addMenu(Menu menu) { return menus.add(menu); } public boolean removeMenu(Menu menu) { return menus.remove(menu); } } |