1 4 package com.nightlabs.ipanema.store.test; 5 6 import java.io.FileNotFoundException ; 7 import java.io.IOException ; 8 import java.io.InputStream ; 9 import java.util.Properties ; 10 11 import javax.jdo.JDOHelper; 12 import javax.jdo.PersistenceManager; 13 import javax.jdo.PersistenceManagerFactory; 14 15 import com.nightlabs.inheritance.InheritenceManager; 16 import com.nightlabs.ipanema.store.Product; 17 import com.nightlabs.ipanema.store.id.ProductID; 18 19 22 public class TestStore 23 { 24 private static PersistenceManager createPersistenceManager() 25 throws IOException 26 { 27 Properties properties = new Properties (); 28 29 InputStream is = TestStore.class.getResourceAsStream("jpox.properties"); 30 try { 31 if (is == null) 32 throw new FileNotFoundException ("Could not find \"jpox.properties\"!"); 33 properties.load(is); 34 } finally { 35 if (is != null) 36 is.close(); 37 } 38 39 PersistenceManagerFactory pmfactory = JDOHelper.getPersistenceManagerFactory(properties); 40 PersistenceManager pm = pmfactory.getPersistenceManager(); 41 42 return pm; 43 } 44 45 public static final String ORGANISATIONID = "schulze.nightlabs.de"; 46 47 public void test() throws Exception 48 { 49 PersistenceManager pm = createPersistenceManager(); 50 try { 51 pm.currentTransaction().begin(); 52 try { 53 Container container = new Container(ORGANISATIONID, "container", null, Product.NATURE_TYPE); 54 pm.makePersistent(container); 55 56 Container bottle = new Container(ORGANISATIONID, "bottle", container, Product.NATURE_TYPE); 57 pm.makePersistent(bottle); 58 59 container.setColor("green"); 60 container.setHeight(500); 61 container.setWidth(200); 62 63 65 bottle.setColor("red"); 66 67 container.setColor("blue"); 68 69 77 pm.currentTransaction().commit(); 78 } finally { 79 if (pm.currentTransaction().isActive()) 80 pm.currentTransaction().rollback(); 81 } 82 } finally { 83 pm.close(); 84 } 85 86 87 pm = createPersistenceManager(); 88 try { 89 pm.currentTransaction().begin(); 90 try { 91 Container container = (Container)pm.getObjectById(ProductID.create(ORGANISATIONID, "container"), true); 92 Container bottle = (Container)pm.getObjectById(ProductID.create(ORGANISATIONID, "bottle"), true); 93 94 104 InheritenceManager im = new InheritenceManager(); 105 try { 106 im.inheritAllFields(container, bottle); 107 } catch (Exception x) { 108 x.printStackTrace(); 109 throw x; 110 } 111 112 pm.currentTransaction().commit(); 113 } finally { 114 if (pm.currentTransaction().isActive()) 115 pm.currentTransaction().rollback(); 116 } 117 } finally { 118 pm.close(); 119 } 120 } 121 122 public static void main(String [] args) 123 { 124 try { 125 TestStore test = new TestStore(); 126 test.test(); 127 } catch (Exception e) { 128 e.printStackTrace(); 129 } 130 } 131 } 132 | Popular Tags |