1 24 package org.objectweb.jalisto.test.core.suite; 25 26 import junit.framework.Test; 27 import org.objectweb.jalisto.se.JalistoFactory; 28 import org.objectweb.jalisto.se.api.ClassDescription; 29 import org.objectweb.jalisto.se.api.MetaRepository; 30 import org.objectweb.jalisto.se.api.query.FieldDescription; 31 import org.objectweb.jalisto.se.exception.SchemaException; 32 import org.objectweb.jalisto.se.impl.LogicalOid; 33 import org.objectweb.jalisto.se.impl.meta.InternalMetaRepositoryImpl; 34 import org.objectweb.jalisto.se.impl.meta.type.IntegerType; 35 import org.objectweb.jalisto.se.impl.meta.type.StringType; 36 import org.objectweb.jalisto.se.test.data.Book; 37 import org.objectweb.jalisto.se.test.workbench.JalistoTestCase; 38 import org.objectweb.jalisto.se.test.workbench.JalistoTestSuite; 39 import org.objectweb.jalisto.se.tool.JalistoViewer; 40 41 import java.io.PrintWriter ; 42 import java.util.ArrayList ; 43 import java.util.Random ; 44 45 public class MetaRepositoryTestCase extends JalistoTestCase { 46 47 public MetaRepositoryTestCase() { 48 init(); 49 } 50 51 public MetaRepositoryTestCase(String name) { 52 super(name); 53 init(); 54 } 55 56 private void init() { 57 meta0 = Book.getMetaDescription(); 58 meta1 = JalistoFactory.createClassDescription(Book.class.getName()); 59 meta2 = JalistoFactory.createClassDescription(Book.class.getName()); 60 61 meta1.addField(JalistoFactory.createFieldDescription("title", new StringType())); 62 meta1.addField(JalistoFactory.createFieldDescription("comments", new StringType())); 63 meta1.addField(JalistoFactory.createFieldDescription("pages", new IntegerType())); 64 meta1.addField(JalistoFactory.createFieldDescription("price", new IntegerType())); 65 meta1.addField(JalistoFactory.createFieldDescription("author", new StringType())); 66 67 meta2.addField(JalistoFactory.createFieldDescription("title", new StringType())); 68 meta2.addField(JalistoFactory.createFieldDescription("comments", new StringType())); 69 meta2.addField(JalistoFactory.createFieldDescription("price", new IntegerType())); 70 meta2.addField(JalistoFactory.createFieldDescription("author", new StringType())); 71 } 72 73 public static Test suite() { 74 JalistoTestSuite suite = new JalistoTestSuite(); 75 MetaRepositoryTestCase tc = (MetaRepositoryTestCase) newTestCase(suite, new MetaRepositoryTestCase()); 76 77 tc.define(); 78 79 tc.populateTest(5000); 80 81 tc.modifySchema(); 82 83 tc.modifySchemaDuringTransaction(); 84 85 tc.removePersistentClass(); 86 87 tc.removeUnknownClass(); 88 89 tc.defineAgain(); 90 tc.defineAgainWithNotSameMeta(); 91 92 tc.erase(); 93 tc.finishTests(); 94 95 return suite; 96 } 97 98 101 102 public void populateTest(int nbrBook) { 103 super.populate(nbrBook); 104 105 tx.begin(); 106 ArrayList list = new ArrayList (session.getExtent(Book.class).readFully().collection()); 107 tx.commit(); 108 int index = (new Random ()).nextInt(list.size()); 109 oid = (LogicalOid) list.get(index); 110 } 111 112 public void modifySchema() { 113 if (session.getInternalSession().isRemoteSession()) { 114 System.out.println("---> can't execute this test with remote client"); 115 return; 116 } 117 118 Object [] o; 119 MetaRepository metaRepository = session.getMetaRepository(); 120 String bookClassName = Book.class.getName(); 121 122 ClassDescription meta = metaRepository.getMetaData(bookClassName); 123 assertTrue("must be equal", meta.equals(meta0)); 124 125 FieldDescription fieldDescription = JalistoFactory.createFieldDescription("comments", new StringType()); 126 fieldDescription.setIndex((short) 1); 127 metaRepository.addField(session, bookClassName, fieldDescription); 128 meta = metaRepository.getMetaData(bookClassName); 129 assertTrue("must be equal", meta.equals(meta1)); 130 131 tx.begin(); 132 o = session.readObjectByOid(oid); 133 assertNotNull(o[0]); 134 assertNull(o[1]); 135 assertNotNull(o[2]); 136 assertNotNull(o[3]); 137 assertNotNull(o[4]); 138 tx.commit(); 139 140 metaRepository.removeField(session, bookClassName, "pages"); 141 meta = metaRepository.getMetaData(bookClassName); 142 assertTrue("must be equal", meta.equals(meta2)); 143 144 tx.begin(); 145 o = session.readObjectByOid(oid); 146 assertNotNull(o[0]); 147 assertNull(o[1]); 148 assertNotNull(o[2]); 149 assertNotNull(o[3]); 150 try { 151 System.out.println(o[4]); 152 assertTrue("Should raise exception", false); 153 } catch (Exception e) { 154 } 156 tx.commit(); 157 } 158 159 public void modifySchemaDuringTransaction() { 160 if (session.getInternalSession().isRemoteSession()) { 161 System.out.println("---> can't execute this test with remote client"); 162 return; 163 } 164 165 try { 166 tx.begin(); 167 MetaRepository metaRepository = session.getMetaRepository(); 168 FieldDescription fieldDescription = JalistoFactory.createFieldDescription("author", new StringType()); 169 fieldDescription.setIndex((short) 1); 170 metaRepository.addField(session, Book.class.getName(), fieldDescription); 171 assertTrue("exception expected", false); 172 } catch (SchemaException e) { 173 assertTrue("not valide exception message", 174 e.getMessage().startsWith(InternalMetaRepositoryImpl.MODIFY_DURING_TRANSACTION_MESSAGE)); 175 } finally { 176 tx.commit(); 177 } 178 } 179 180 public void removePersistentClass() { 181 try { 182 assertTrue(session.isClassDefined(Book.getMetaDescription().getClassName())); 183 184 session.removeClass(Book.getMetaDescription().getClassName()); 185 186 tx.begin(); 187 try { 188 session.createObject(Book.newBook().toArray(), Book.class); 189 } catch (SchemaException jalistoSchemaExc) { 190 assertEquals("Should be equals", 191 "Class org.objectweb.jalisto.se.test.data.Book is not define in base", 192 jalistoSchemaExc.getMessage()); 193 } 194 } finally { 195 if (tx.isActive()) { 196 tx.commit(); 197 } 198 } 199 } 200 201 public void removeUnknownClass() { 202 try { 203 session.removeClass(MetaRepositoryTestCase.class.getName()); 204 assertTrue("should raise exception", false); 205 } catch (SchemaException jalistoSchemaExc) { 206 assertEquals("Should be equals", 207 "Class org.objectweb.jalisto.test.core.suite.MetaRepositoryTestCase is not define in base", 208 jalistoSchemaExc.getMessage()); 209 } 210 } 211 212 public void defineAgain() { 213 session.defineClass(Book.getMetaDescription()); 214 tx.begin(); 215 session.createObject(Book.newBook().toArray(), Book.class); 216 tx.commit(); 217 } 218 219 public void defineAgainWithNotSameMeta() { 220 try { 221 session.defineClass(meta2); 222 } catch (SchemaException jalistoSchemaExc) { 223 assertEquals("Should be equals", 224 "Class org.objectweb.jalisto.se.test.data.Book is already define in base with other metadescription", 225 jalistoSchemaExc.getMessage()); 226 } 227 } 228 229 public void showBase() { 230 String args[] = {"-m", "-file", "jalisto.properties"}; 231 JalistoViewer viewer = new JalistoViewer(session, args, new PrintWriter (System.out)); 232 viewer.readBase(); 233 } 234 235 public void erase() { 236 endTransaction(); 237 closeSession(); 238 if (session.getInternalSession().getProperties().allowsSpecialFunctionnalities()) { 239 session.eraseStorage(); 240 } else { 241 System.out.println("not allows reorganize functionnality"); 242 } 243 } 244 245 246 private LogicalOid oid; 247 248 private ClassDescription meta0; 249 private ClassDescription meta1; 250 private ClassDescription meta2; 251 252 255 256 public void define() { 257 super.initSession(true); 258 super.define(Book.getMetaDescription()); 259 } 260 261 262 public void finishTests() { 263 super.finishTests(); 264 } 265 } 266 | Popular Tags |