1 23 24 package org.objectweb.medor.jorm; 25 26 import org.apache.tools.ant.types.DTDLocation; 27 import org.objectweb.jorm.api.PException; 28 import org.objectweb.jorm.api.PMapper; 29 import org.objectweb.jorm.metainfo.api.Class; 30 import org.objectweb.jorm.metainfo.api.Manager; 31 import org.objectweb.jorm.metainfo.api.MetaObject; 32 import org.objectweb.medor.api.MedorException; 33 import org.objectweb.medor.lib.Log; 34 import org.objectweb.medor.lib.TestMedorHelper; 35 import org.objectweb.medor.optim.api.QueryRewriter; 36 import org.objectweb.medor.query.api.QueryTree; 37 import org.objectweb.medor.query.jorm.lib.ClassExtent; 38 import org.objectweb.medor.query.lib.QueryTreePrinter; 39 import org.objectweb.jorm.util.io.lib.DirJavaExplorer; 40 import org.objectweb.jorm.compiler.lib.JormCompiler; 41 import org.objectweb.jorm.compiler.api.JormCompilerParameter; 42 import org.objectweb.util.monolog.api.BasicLevel; 43 import org.objectweb.util.monolog.api.Logger; 44 import org.objectweb.util.monolog.api.LoggerFactory; 45 46 import java.util.ArrayList ; 47 import java.util.Collection ; 48 import java.util.Iterator ; 49 import java.util.Collections ; 50 51 54 public abstract class TestJormHelper extends TestMedorHelper { 55 private static String mapperName; 56 private static String projectName; 57 private static String miMappingFactory; 58 private static String mappingParser; 59 private static String pdPath; 60 private static String dtdPath; 61 62 public static final String DEFAULT_PNAME_FIELD_NAME = "object"; 63 public static final String PROJECT_NAME = "test"; 64 65 static { 66 Logger localLogger = getLoggerFactory() 67 .getLogger(Log.MEDOR_PREFIX + ".jorm"); 68 mapperName = System.getProperty( 69 "medor.jorm.mapperName", "rdb"); 70 localLogger.log(BasicLevel.DEBUG, "MapperName = " + mapperName); 71 72 projectName = System.getProperty( 73 "medor.jorm.projectName", ""); 74 localLogger.log(BasicLevel.DEBUG, "ProjectName = " + projectName); 75 76 miMappingFactory = System.getProperty( 77 "medor.jorm.miMappingName", 78 "org.objectweb.jorm.metainfo.rdb.BasicRDBMappingFactory"); 79 localLogger.log( 80 BasicLevel.DEBUG, "MIMappingFactory = " + miMappingFactory); 81 82 mappingParser = System.getProperty( 83 "medor.jorm.miMappingName", 84 "org.objectweb.jorm.xml2mi.rdb.RDBParser"); 85 localLogger.log(BasicLevel.DEBUG, "MappingParser = " + mappingParser); 86 87 pdPath = System.getProperty( 88 "medor.jorm.pd.path", "./"); 89 localLogger.log(BasicLevel.DEBUG, "pd Path = " + pdPath); 90 dtdPath = System.getProperty( 91 "medor.jorm.dtd.path", "./"); 92 localLogger.log(BasicLevel.DEBUG, "dtd Path = " + dtdPath); 93 } 94 95 public static Manager manager = null; 96 public static PMapper mapper = null; 97 public static JormCompiler compiler = null; 98 99 public class Example { 100 public QueryTree qt; 101 public Class clazz; 102 public ClassExtent extent; 103 public String pnameFieldName; 104 } 105 106 public TestJormHelper(String testName, String loggerName) { 107 super(testName, loggerName); 108 synchronized (getClass()) { 109 if (compiler != null) { 110 return; 111 } 112 try { 113 compiler = new JormCompiler(); 114 compiler.getCompilerConfigurator().configure(); 115 mapper = newMappper(); 116 mapper.setMapperName(getMapperName()); 117 LoggerFactory loggerFactory = getLoggerFactory(); 118 compiler.getCompilerConfigurator().setLoggerFactory(loggerFactory); 119 compiler.setupLogger(); 120 compiler.setupMIManager(); 121 manager = compiler.getMIManager(); 122 compiler.setupGenerator(); 124 JormCompilerParameter cp = compiler.getCompilerParameter(); 125 DirJavaExplorer exp = new DirJavaExplorer(); 126 exp.addPath(getPDPath()); 127 cp.setClasspath(exp); 128 cp.setProjectName(getProjectName()); 129 DTDLocation dtdl = new DTDLocation(); 130 dtdl.setPublicId("-//ObjectWeb Consortium//DTD JORM DEFINITIONS 2.0//EN"); 131 dtdl.setLocation(getDTDPath()); 132 cp.getDtdLocations().add(dtdl); 133 compiler.setupParser(); 134 compiler.getCompilerParameter().setProjectName(PROJECT_NAME); 135 } 136 catch (PException e) { 137 fail(e.getMessage()); 138 } 139 } 140 } 141 142 public String getPDPath() { 143 return pdPath; 144 } 145 146 public String getDTDPath() { 147 return dtdPath; 148 } 149 150 public String getMapperName() { 151 return mapperName; 152 } 153 154 public String getProjectName() { 155 return projectName; 156 } 157 158 public String getMIMappingFactory() { 159 return miMappingFactory; 160 } 161 162 public String getMappingParser() { 163 return mappingParser; 164 } 165 166 public abstract PMapper newMappper(); 167 168 public abstract QueryRewriter createQueryRewriter(); 169 170 protected void setUp() { 171 } 172 173 protected void tearDown() { 174 } 175 176 public Example getExtent(Collection fileNames, String className) { 177 return getExtent(fileNames, className, DEFAULT_PNAME_FIELD_NAME, null); 178 } 179 180 public Example getExtent(Collection fileNames, 181 String fqClassName, 182 String pnameFieldName, 183 String [] fieldNames) { 184 185 logger.log(BasicLevel.DEBUG, "getExtentOf the class " + fqClassName); 186 187 logger.log(BasicLevel.DEBUG, "manager=" + manager); 188 Collection mos = null; 189 190 Class c = manager.getClass(fqClassName); 191 192 if (c == null) { 193 try { 194 mos = loadJormMetaInfo(fileNames); 195 logger.log(BasicLevel.DEBUG, "Jorm meta information loaded"); 196 } 197 catch (PException e) { 198 e.printStackTrace(System.out); 199 fail(e.getMessage()); 200 } 201 } else { 202 mos = Collections.singletonList(c); 203 } 204 205 for (Iterator it = mos.iterator(); it.hasNext();) { 206 MetaObject mo = (MetaObject) it.next(); 207 if (mo instanceof Class 208 && fqClassName.equals(((Class ) mo).getFQName())) { 209 Example e = new Example(); 210 e.pnameFieldName = pnameFieldName; 211 e.clazz = (Class ) mo; 212 if (manager == null) { 213 manager = (Manager) mo.getParent().getParent(); 214 throw new RuntimeException ("The test must be changed in according to JORM 2.2"); 215 } 217 logger.log(BasicLevel.DEBUG, "schema=" + mo.getParent()); 218 logger.log(BasicLevel.DEBUG, "parent(schema)=" + mo.getParent().getParent()); 219 try { 220 if (fieldNames == null) 221 e.extent = new ClassExtent( 222 e.clazz, e.clazz.getFQName(), pnameFieldName, false); 223 else 224 e.extent = new ClassExtent(e.clazz, e.clazz.getFQName(), 225 fieldNames, true, pnameFieldName); 226 logger.log(BasicLevel.DEBUG, "Extent created for the class " 227 + e.clazz.getName()); 228 e.extent.setPMapper(mapper, PROJECT_NAME); 229 230 QueryTreePrinter.printQueryTree(e.extent, logger); 231 return e; 232 } 233 catch (MedorException me) { 234 me.printStackTrace(); 235 fail(me.getMessage()); 236 } 237 } 238 } 239 return null; 240 } 241 242 public Example getRewritten(Example e) { 243 if (e == null) 244 return null; 245 try { 246 e.extent.setProjectName("test"); 248 e.extent.setPMapper(mapper, PROJECT_NAME); 249 250 QueryRewriter qr = createQueryRewriter(); 251 logger.log(BasicLevel.DEBUG, "QueryRewriter created"); 252 253 e.qt = qr.transform(e.extent); 254 logger.log(BasicLevel.DEBUG, "Extent of the class " 255 + e.clazz.getName() + " rewritten"); 256 257 QueryTreePrinter.printQueryTree(e.qt, logger); 258 } 259 catch (MedorException me) { 260 me.printStackTrace(); 261 fail(me.getMessage()); 262 } 263 return e; 264 } 265 266 public Collection loadJormMetaInfo(String fileName) throws PException { 267 ArrayList al = new ArrayList (1); 268 al.add(fileName); 269 return loadJormMetaInfo(al); 270 } 271 272 public Collection loadJormMetaInfo(Collection fileNames) throws PException { 273 Collection mos = compiler.parseFiles(fileNames); 274 logger.log(BasicLevel.DEBUG, "Files parsed (" + mos.size() + " item(s) found)"); 275 276 mos.iterator(); 277 284 return mos; 285 } 286 287 public String getFieldName(String alias, String fn) { 288 return (alias == null || alias.length() == 0 ? "" : alias + ".") + fn; 289 } 290 } 291 | Popular Tags |