KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > runtime > colocated > TestOneMany2T


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23 package org.objectweb.jorm.runtime.colocated;
24
25 import org.objectweb.jorm.runtime.TestRuntimeHelper;
26 import org.objectweb.jorm.runtime.PolyIdPNG;
27 import org.objectweb.jorm.naming.api.PBinder;
28 import org.objectweb.jorm.naming.api.PNameCoder;
29 import org.objectweb.jorm.naming.api.PNamingContext;
30 import org.objectweb.jorm.facility.naming.basidir.BasidBinder;
31 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdBinder;
32 import org.objectweb.jorm.api.PException;
33 import org.objectweb.jorm.api.PBinding;
34 import org.objectweb.jorm.api.PAccessor;
35 import org.objectweb.util.monolog.api.BasicLevel;
36
37 import java.util.Map JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.ArrayList JavaDoc;
41
42 /**
43  *
44  * @author S.Chassande-Barrioz
45  */

46 public class TestOneMany2T extends TestRuntimeHelper {
47
48     private final static String JavaDoc LOGGER_NAME
49         = "test.org.objectweb.jorm.colocated.omemany2t";
50
51     private final static String JavaDoc GenClassRef_SN_C
52         = "org.objectweb.jorm.pobject.colocated.GenClassRef_SN_C";
53     private final static String JavaDoc GenClassRef_CN_C
54         = "org.objectweb.jorm.pobject.colocated.GenClassRef_CN_C";
55
56     private final static String JavaDoc StringReferencedClass_C
57         = "org.objectweb.jorm.pobject.colocated.StringReferencedClass_C";
58     private final static String JavaDoc PolyIdReferencedClass_C
59         = "org.objectweb.jorm.pobject.colocated.PolyIdReferencedClass_C";
60
61     Map JavaDoc cn2binder;
62
63     public TestOneMany2T(String JavaDoc s) throws Exception JavaDoc {
64         super(s);
65         cn2binder = new HashMap JavaDoc(1);
66     }
67
68     protected String JavaDoc getLoggerName() {
69         return LOGGER_NAME;
70     }
71
72     protected PBinder getBinder(String JavaDoc className) throws Exception JavaDoc {
73         PBinder binder = (PBinder) cn2binder.get(className);
74         if (binder != null) {
75             return binder;
76         }
77         try {
78             if (className.startsWith(GenClassRef_SN_C)) {
79                 binder = new BasidBinder( PNamingContext.CTLONG);
80                 binder.setNullPName(new Long JavaDoc(-1));
81                 ((BasidBinder) binder).setCodingType(PNameCoder.CTLONG);
82             } else if (className.startsWith(GenClassRef_CN_C)
83                 || className.startsWith(PolyIdReferencedClass_C)) {
84                 binder = new PolymorphIdBinder();
85                 binder.setNullPName(new PolyIdPNG());
86             } else if (className.startsWith(StringReferencedClass_C)) {
87                 binder = new BasidBinder( PNamingContext.CTSTRING);
88                 binder.setNullPName(null);
89                 ((BasidBinder) binder).setCodingType(PNameCoder.CTSTRING);
90             }
91         } catch (PException e) {
92             Exception JavaDoc current = e;
93             while (current instanceof PException
94                     && ((PException) current).getNestedException() != null) {
95                 current = ((PException) current).getNestedException();
96             }
97             throw current;
98         }
99         if (binder == null) {
100             throw new PException("Unmanaged class " + className);
101         }
102         logger.log(BasicLevel.DEBUG,
103             "className=" + className + " / binder=" + binder);
104         cn2binder.put(className, binder);
105         return binder;
106     }
107
108     private void genclassRefTestOrder1(PBinding gcrPb, PBinding gcPb, List JavaDoc rcPbs) {
109
110         //Make persistent the referencedClass instances
111
List JavaDoc rcAccws = new ArrayList JavaDoc();
112         List JavaDoc rcPns = new ArrayList JavaDoc();
113         for(int i=0; i<rcPbs.size(); i++) {
114             rcPns.add(((PBinding) rcPbs.get(i)).getPName());
115             ReferencedClass_C rcAccw = new ReferencedClass_C();
116             rcAccws.add(rcAccw);
117             rcAccw.f1 = 10;
118             ReferencedClass_C rcAccr = new ReferencedClass_C();
119             logger.log(BasicLevel.DEBUG, "create and read referenced class " + i);
120             writeRead((PBinding) rcPbs.get(i), rcAccw, rcAccr);
121             assertEquals("bad referenced class values", rcAccw, rcAccr);
122         }
123
124         //Make persistent the genclass
125
GenClass_C gcAccw = new GenClass_C(rcPns);
126         GenClass_C gcAccr = new GenClass_C(rcPns.size());
127         logger.log(BasicLevel.DEBUG, "create and read genclas");
128         writeRead(gcPb, gcAccw, gcAccr);
129         assertEquals("bad gen class values", gcAccw, gcAccr);
130
131         //Make persistent the genclassref
132
GenClassRef_C gcrAccw = new GenClassRef_C(gcPb.getPName());
133         GenClassRef_C gcrAccr = new GenClassRef_C();
134         logger.log(BasicLevel.DEBUG, "create and read genclass");
135         writeRead(gcrPb, gcrAccw, gcrAccr);
136         assertEquals("bad gen class values", gcrAccw, gcrAccr);
137
138         for(int i=0; i<rcPbs.size(); i++) {
139             logger.log(BasicLevel.DEBUG, "remove referenced class");
140             unexport((PBinding) rcPbs.get(i), (PAccessor) rcAccws.get(i));
141         }
142         logger.log(BasicLevel.DEBUG, "remove gen class");
143         unexport(gcPb, gcAccw);
144         logger.log(BasicLevel.DEBUG, "remove gen class ref");
145         unexport(gcrPb, gcrAccw);
146     }
147
148     private void genclassRefTestOrder2(PBinding gcrPb, PBinding gcPb, List JavaDoc rcPbs) {
149
150         //Make persistent the referencedClass instances
151
List JavaDoc rcAccws = new ArrayList JavaDoc();
152         List JavaDoc rcPns = new ArrayList JavaDoc();
153         for(int i=0; i<rcPbs.size(); i++) {
154             rcPns.add(((PBinding) rcPbs.get(i)).getPName());
155             ReferencedClass_C rcAccw = new ReferencedClass_C();
156             rcAccws.add(rcAccw);
157             rcAccw.f1 = 10;
158         }
159
160         //Make persistent the genclass
161
GenClass_C gcAccw = new GenClass_C(rcPns);
162         GenClass_C gcAccr = new GenClass_C(rcPns.size());
163         logger.log(BasicLevel.DEBUG, "create and read genclas");
164         writeRead(gcPb, gcAccw, gcAccr);
165         assertEquals("bad gen class values", gcAccw, gcAccr);
166
167         for(int i=0; i<rcPbs.size(); i++) {
168             ReferencedClass_C rcAccw = (ReferencedClass_C) rcAccws.get(i);
169             ReferencedClass_C rcAccr = new ReferencedClass_C();
170             logger.log(BasicLevel.DEBUG, "create and read referenced class " + i);
171             writeRead((PBinding) rcPbs.get(i), rcAccw, rcAccr);
172             assertEquals("bad referenced class values", rcAccw, rcAccr);
173         }
174
175         //Make persistent the genclassref
176
GenClassRef_C gcrAccw = new GenClassRef_C(gcPb.getPName());
177         GenClassRef_C gcrAccr = new GenClassRef_C();
178         logger.log(BasicLevel.DEBUG, "create and read genclass");
179         writeRead(gcrPb, gcrAccw, gcrAccr);
180         assertEquals("bad gen class values", gcrAccw, gcrAccr);
181
182         for(int i=0; i<rcPbs.size(); i++) {
183             logger.log(BasicLevel.DEBUG, "remove referenced class");
184             unexport((PBinding) rcPbs.get(i), (PAccessor) rcAccws.get(i));
185         }
186         logger.log(BasicLevel.DEBUG, "remove gen class");
187         unexport(gcPb, gcAccw);
188         logger.log(BasicLevel.DEBUG, "remove gen class ref");
189         unexport(gcrPb, gcrAccw);
190     }
191
192     public void testGenClassRefSingle1() {
193         changeLogger(LOGGER_NAME + ".single1");
194         logger.log(BasicLevel.DEBUG, "testGenClassRefSingle2 begin");
195         List JavaDoc rcPbs = new ArrayList JavaDoc();
196         rcPbs.add(export(StringReferencedClass_C, "azerty"));
197         rcPbs.add(export(StringReferencedClass_C, "qsdfgh"));
198         genclassRefTestOrder1(
199                 export(GenClassRef_SN_C, new Long JavaDoc(45)),
200                 export(GenClassRef_SN_C + "/c", new Long JavaDoc(45)),
201                 rcPbs);
202         logger.log(BasicLevel.DEBUG, "testGenClassRefSingle1 end");
203     }
204
205     public void _testGenClassRefSingle2() {
206         changeLogger(LOGGER_NAME + ".single2");
207         logger.log(BasicLevel.DEBUG, "testGenClassRefSingle2 begin");
208         List JavaDoc rcPbs = new ArrayList JavaDoc();
209         rcPbs.add(export(StringReferencedClass_C, "azerty"));
210         rcPbs.add(export(StringReferencedClass_C, "qsdfgh"));
211         genclassRefTestOrder2(
212                 export(GenClassRef_SN_C, new Long JavaDoc(45)),
213                 export(GenClassRef_SN_C + "/c", new Long JavaDoc(45)),
214                 rcPbs);
215         logger.log(BasicLevel.DEBUG, "testGenClassRefSingle2 end");
216     }
217
218     public void testGenClassRefComposite1() {
219         changeLogger(LOGGER_NAME + ".composite1");
220         logger.log(BasicLevel.DEBUG, "testGenClassRefComposite1 begin");
221         List JavaDoc rcPbs = new ArrayList JavaDoc();
222         rcPbs.add(export(PolyIdReferencedClass_C, new PolyIdPNG(78, 90)));
223         rcPbs.add(export(PolyIdReferencedClass_C, new PolyIdPNG(13, 24)));
224         genclassRefTestOrder1(
225             export(GenClassRef_CN_C, new PolyIdPNG(12, 34)),
226             export(GenClassRef_CN_C + "/c", new PolyIdPNG(12, 34)),
227             rcPbs);
228         logger.log(BasicLevel.DEBUG, "testGenClassRefComposite1 end");
229     }
230
231     public void _testGenClassRefComposite2() {
232         changeLogger(LOGGER_NAME + ".composite1");
233         logger.log(BasicLevel.DEBUG, "testGenClassRefComposite1 begin");
234         List JavaDoc rcPbs = new ArrayList JavaDoc();
235         rcPbs.add(export(PolyIdReferencedClass_C, new PolyIdPNG(78, 90)));
236         rcPbs.add(export(PolyIdReferencedClass_C, new PolyIdPNG(13, 24)));
237         genclassRefTestOrder2(
238             export(GenClassRef_CN_C, new PolyIdPNG(12, 34)),
239             export(GenClassRef_CN_C + "/c", new PolyIdPNG(12, 34)),
240             rcPbs);
241         logger.log(BasicLevel.DEBUG, "testGenClassRefComposite1 end");
242     }
243
244 }
245
Popular Tags