KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > runtime > multitable > TestClassRefMultitable


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
24 package org.objectweb.jorm.runtime.multitable;
25
26 import org.objectweb.jorm.runtime.TestRuntimeHelper;
27 import org.objectweb.jorm.runtime.PolyIdPNG;
28 import org.objectweb.jorm.naming.api.PBinder;
29 import org.objectweb.jorm.naming.api.PNameCoder;
30 import org.objectweb.jorm.facility.naming.basidir.BasidBinder;
31 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdBinder;
32 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdPNG;
33 import org.objectweb.jorm.api.PException;
34 import org.objectweb.jorm.api.PBinding;
35 import org.objectweb.util.monolog.api.BasicLevel;
36
37 import java.util.Map JavaDoc;
38 import java.util.HashMap JavaDoc;
39
40 /**
41  * Created by IntelliJ IDEA.
42  * User: chassase
43  * Date: 1 mai 2003
44  * Time: 08:18:53
45  * To change this template use Options | File Templates.
46  */

47 public class TestClassRefMultitable extends TestRuntimeHelper {
48
49     private final static String JavaDoc LOGGER_NAME
50         = "test.org.objectweb.jorm.multitable.classref";
51
52     private final static String JavaDoc ClassRef_SN_MT
53         = "org.objectweb.jorm.pobject.multitable.ClassRef_SN_MT";
54     private final static String JavaDoc ClassRef_CN_MT
55         = "org.objectweb.jorm.pobject.multitable.ClassRef_CN_MT";
56
57     private final static String JavaDoc StringReferencedClass_SN_MT
58         = "org.objectweb.jorm.pobject.multitable.StringReferencedClass_SN_MT";
59     private final static String JavaDoc PolyIdReferencedClass_CN_MT
60         = "org.objectweb.jorm.pobject.multitable.PolyIdReferencedClass_CN_MT";
61
62     Map JavaDoc cn2binder;
63
64     public TestClassRefMultitable(String JavaDoc s) throws Exception JavaDoc {
65         super(s);
66         cn2binder = new HashMap JavaDoc(1);
67     }
68
69     protected String JavaDoc getLoggerName() {
70         return LOGGER_NAME;
71     }
72
73     protected PBinder getBinder(String JavaDoc className) throws Exception JavaDoc {
74         PBinder binder = (PBinder) cn2binder.get(className);
75         if (binder != null) {
76             return binder;
77         }
78         try {
79             if (ClassRef_SN_MT.equals(className)) {
80                 binder = new BasidBinder();
81                 binder.setNullPName(new Long JavaDoc(-1));
82                 ((BasidBinder) binder).setCodingType(PNameCoder.CTLONG);
83             } else if (ClassRef_CN_MT.equals(className)
84                 || PolyIdReferencedClass_CN_MT.equals(className)) {
85                 binder = new PolymorphIdBinder();
86                 binder.setNullPName(new PolyIdPNG());
87             } else if (StringReferencedClass_SN_MT.equals(className)) {
88                 binder = new BasidBinder();
89                 binder.setNullPName(null);
90                 ((BasidBinder) binder).setCodingType(PNameCoder.CTSTRING);
91             }
92         } catch (PException e) {
93             Exception JavaDoc current = e;
94             while (current instanceof PException
95                     && ((PException) current).getNestedException() != null) {
96                 current = ((PException) current).getNestedException();
97             }
98             throw current;
99         }
100         if (binder == null) {
101             throw new PException("Unmanaged class " + className);
102         }
103         cn2binder.put(className, binder);
104         return binder;
105     }
106
107
108     private void classRefTest(PBinding crPb, PBinding rc1Pb, PBinding rc2Pb) {
109
110         //Make persistent the referencedClass
111
ReferencedClass_MT rc1Accw = new ReferencedClass_MT();
112         rc1Accw.f1 = 10;
113         ReferencedClass_MT rc1Accr = new ReferencedClass_MT();
114         logger.log(BasicLevel.DEBUG, "create and read referenced class 1");
115         writeRead(rc1Pb, rc1Accw, rc1Accr);
116         assertEquals("bad referenced class (1) values", rc1Accw, rc1Accr);
117
118         ReferencedClass_MT rc2Accw = new ReferencedClass_MT();
119         rc2Accw.f1 = 20;
120         ReferencedClass_MT rc2Accr = new ReferencedClass_MT();
121         logger.log(BasicLevel.DEBUG, "create and read referenced class 2");
122         writeRead(rc2Pb, rc2Accw, rc2Accr);
123         assertEquals("bad referenced class (2) values", rc2Accw, rc2Accr);
124
125         ClassRef_MT crAccw = new ClassRef_MT(rc1Pb.getPName(), rc2Pb.getPName());
126         ClassRef_MT crAccr = new ClassRef_MT();
127         logger.log(BasicLevel.DEBUG, "create and read classref");
128         writeRead(crPb, crAccw, crAccr);
129         assertEquals("bad class reference values", crAccw, crAccr);
130
131         ClassRef_MT crAccw2 = new ClassRef_MT(rc2Pb.getPName(), rc1Pb.getPName());
132         logger.log(BasicLevel.DEBUG, "update and read");
133         writeRead(crPb, crAccw2, crAccr);
134         assertEquals("bad class reference values", crAccw2, crAccr);
135
136         logger.log(BasicLevel.DEBUG, "remove referenced class 1");
137         unexport(rc1Pb, rc1Accw);
138         logger.log(BasicLevel.DEBUG, "remove referenced class 2");
139         unexport(rc2Pb, rc2Accw);
140         logger.log(BasicLevel.DEBUG, "remove class reference");
141         unexport(crPb, crAccw);
142     }
143
144     public void testClassRefSingle() {
145         changeLogger(LOGGER_NAME + ".single");
146         logger.log(BasicLevel.DEBUG, "testClassRefSingle begin");
147         classRefTest(
148             export(ClassRef_SN_MT, new Long JavaDoc(45)),
149             export(StringReferencedClass_SN_MT, "azerty"),
150             export(StringReferencedClass_SN_MT, "qsdfgh")
151             );
152         logger.log(BasicLevel.DEBUG, "testClassRefSingle end");
153     }
154
155     public void testClassRefComposite() {
156         changeLogger(LOGGER_NAME + ".composite");
157         logger.log(BasicLevel.DEBUG, "testPrimitiveComposite begin");
158         classRefTest(
159             export(ClassRef_CN_MT, new PolyIdPNG(12, 34)),
160             export(PolyIdReferencedClass_CN_MT, new PolyIdPNG(78, 90)),
161             export(PolyIdReferencedClass_CN_MT, new PolyIdPNG(13, 24))
162             );
163         logger.log(BasicLevel.DEBUG, "testPrimitiveComposite end");
164     }
165 }
166
Popular Tags