KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > runtime > pnameiterator > TestCompositeNameIterator


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.pnameiterator;
25
26 import org.objectweb.jorm.api.PBinding;
27 import org.objectweb.jorm.api.PException;
28 import org.objectweb.jorm.api.PClassMapping;
29 import org.objectweb.jorm.naming.api.PBinder;
30 import org.objectweb.jorm.naming.api.PName;
31 import org.objectweb.jorm.pobject.compositename.CompositeIntIdPNG;
32 import org.objectweb.jorm.runtime.TestRuntimeHelper;
33 import org.objectweb.jorm.runtime.namedef.NameDefAccessor;
34 import org.objectweb.util.monolog.api.BasicLevel;
35
36 import java.util.Iterator JavaDoc;
37
38 /**
39  * @author P. Dechamboux
40  */

41 public class TestCompositeNameIterator
42     extends TestRuntimeHelper {
43
44     private final static String JavaDoc LOGGER_NAME
45         = "test.org.objectweb.jorm.compositename";
46
47     public TestCompositeNameIterator(String JavaDoc s) throws Exception JavaDoc {
48         super(s);
49     }
50
51     protected String JavaDoc getLoggerName() {
52         return LOGGER_NAME;
53     }
54
55     protected PBinder getBinder(String JavaDoc className) throws Exception JavaDoc {
56         int idx = className.lastIndexOf('.');
57         PBinder binder = null;
58         String JavaDoc cn = className.substring(idx + 1);
59         if (!cn.startsWith("AComposite")) {
60             throw new Exception JavaDoc(
61                 "Unable to find a binder for the class: " + className);
62         }
63         String JavaDoc bcn = className.substring(0, idx) + "." + cn.substring(1) + "Binder";
64         logger.log(BasicLevel.DEBUG, "Allocating the Binder " + bcn
65             + " to manage the class " + className);
66         if (CNINT.equals(className)) {
67             binder = (PBinder) Class.forName(bcn).newInstance();
68             binder.setNullPName(new IntIdPNG(new Integer JavaDoc(-1)));
69         } else {
70             throw new Exception JavaDoc(
71                     "Unable to find the type of the binder for the class: "
72                     + className);
73         }
74         return binder;
75     }
76
77     private final static String JavaDoc CNINT = "org.objectweb.jorm.pobject.compositename.ACompositeIntId";
78
79     class IntIdPNG implements CompositeIntIdPNG {
80         public Integer JavaDoc val;
81
82         public IntIdPNG(Integer JavaDoc val) {
83             this.val = val;
84         }
85
86         public int pnGetCnf1(Object JavaDoc ctx) throws PException {
87             return val.intValue();
88         }
89     }
90
91     public void trueTester(int NBOBJ, boolean prefetch) throws Exception JavaDoc {
92         PClassMapping pcm = getMapping(CNINT);
93         Object JavaDoc conn = mapper.getConnection();
94         for (int i = 0; i < NBOBJ; i++) {
95             PBinding pb = export(CNINT, new IntIdPNG(new Integer JavaDoc(i)));
96             NameDefAccessor accwrite = new NameDefAccessor(i, new Integer JavaDoc(i), pb.getPName(), pb.getPClassMapping().getPBinder().getNull());
97             pb.write(conn, accwrite);
98         }
99         Iterator JavaDoc it = pcm.getPNameIterator(conn, false, prefetch, conn);
100         int nbiter = 0;
101         PBinding[] pb = new PBinding[NBOBJ];
102         NameDefAccessor accread = new NameDefAccessor();
103         while (it.hasNext()) {
104             PName pn = (PName) it.next();
105             pb[nbiter] = bind(CNINT, pn);
106             pb[nbiter].read(conn, accread, conn);
107             assertEquals("Bad object pname and F1 should be equal", accread.f1,
108                          ((CompositeIntIdPNG) pn.encodeAbstract()).pnGetCnf1(null));
109             nbiter++;
110         }
111         assertEquals("Wrong number of objects iterated", NBOBJ, nbiter);
112         mapper.getPrefetchCache().invalidatePrefetchBuffer(conn);
113         mapper.closeConnection(conn);
114         mapper.getPMapCluster(CNINT).deleteData();
115     }
116
117     public void testIntNameIteratorNoPrefetchNoElem() throws Exception JavaDoc {
118         int NBOBJ = 0;
119         changeLogger(LOGGER_NAME + ".int." + NBOBJ);
120         trueTester(NBOBJ, false);
121         logger.log(BasicLevel.DEBUG, "testInt end");
122     }
123
124     public void testIntNameIteratorNoPrefetchOneElem() throws Exception JavaDoc {
125         int NBOBJ = 1;
126         changeLogger(LOGGER_NAME + ".int." + NBOBJ);
127         trueTester(NBOBJ, false);
128         logger.log(BasicLevel.DEBUG, "testInt end");
129     }
130
131     public void testIntNameIteratorNoPrefetch10Elem() throws Exception JavaDoc {
132         int NBOBJ = 10;
133         changeLogger(LOGGER_NAME + ".int." + NBOBJ);
134         trueTester(NBOBJ, false);
135         logger.log(BasicLevel.DEBUG, "testInt end");
136     }
137
138     public void testIntNameIteratorNoPrefetch100Elem() throws Exception JavaDoc {
139         int NBOBJ = 100;
140         changeLogger(LOGGER_NAME + ".int." + NBOBJ);
141         trueTester(NBOBJ, false);
142         logger.log(BasicLevel.DEBUG, "testInt end");
143     }
144
145     public void testIntNameIteratorNoPrefetch1000Elem() throws Exception JavaDoc {
146         int NBOBJ = 1000;
147         changeLogger(LOGGER_NAME + ".int." + NBOBJ);
148         trueTester(NBOBJ, false);
149         logger.log(BasicLevel.DEBUG, "testInt end");
150     }
151
152     public void testIntNameIteratorPrefetchNoElem() throws Exception JavaDoc {
153         int NBOBJ = 0;
154         changeLogger(LOGGER_NAME + ".int." + NBOBJ);
155         trueTester(NBOBJ, true);
156         logger.log(BasicLevel.DEBUG, "testInt end");
157     }
158
159     public void testIntNameIteratorPrefetchOneElem() throws Exception JavaDoc {
160         int NBOBJ = 1;
161         changeLogger(LOGGER_NAME + ".int." + NBOBJ);
162         trueTester(NBOBJ, true);
163         logger.log(BasicLevel.DEBUG, "testInt end");
164     }
165
166     public void testIntNameIteratorPrefetch10Elem() throws Exception JavaDoc {
167         int NBOBJ = 10;
168         changeLogger(LOGGER_NAME + ".int." + NBOBJ);
169         trueTester(NBOBJ, true);
170         logger.log(BasicLevel.DEBUG, "testInt end");
171     }
172
173     public void testIntNameIteratorPrefetch100Elem() throws Exception JavaDoc {
174         int NBOBJ = 100;
175         changeLogger(LOGGER_NAME + ".int." + NBOBJ);
176         trueTester(NBOBJ, true);
177         logger.log(BasicLevel.DEBUG, "testInt end");
178     }
179
180     public void testIntNameIteratorPrefetch1000Elem() throws Exception JavaDoc {
181         int NBOBJ = 1000;
182         changeLogger(LOGGER_NAME + ".int." + NBOBJ);
183         trueTester(NBOBJ, true);
184         logger.log(BasicLevel.DEBUG, "testInt end");
185     }
186 }
Popular Tags