added new junit tests and deps to suport it
[owl2java:owl2java.git] / src / main / java / owl2java / core / generator / jena / JenaTestWriter.java
1 package owl2java.core.generator.jena;
2
3 import java.io.FileWriter;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.apache.velocity.Template;
8 import org.apache.velocity.VelocityContext;
9 import org.apache.velocity.app.VelocityEngine;
10 import org.apache.velocity.exception.ParseErrorException;
11 import org.apache.velocity.exception.ResourceNotFoundException;
12
13 import owl2java.core.model.jmodel.JModel;
14 import owl2java.core.model.jmodel.utils.NamingUtils;
15 import owl2java.core.utils.JavaUtils;
16
17
18 public class JenaTestWriter {
19         
20         private static Log log = LogFactory.getLog(JenaFactoryWriter.class);
21         private static final String TEMPLATE_NAME = "test.vm";
22
23         private VelocityEngine vEngine;
24         private VelocityContext vContext;
25         private String testClassName;
26         private String toolsPackage;
27
28         public JenaTestWriter(VelocityEngine vEngine, VelocityContext vContext) {
29                 this.vEngine = vEngine;
30                 this.vContext = vContext;
31         }
32
33         public void writeTestCases(JModel jmodel, String baseDir, String basePackage) {
34                 String pkgName = NamingUtils.getJavaPackageName(basePackage, toolsPackage);
35                 String outDir = JavaUtils.toDirectoryFromPackage(pkgName, baseDir);
36                 String outName = testClassName;
37                 String outPath = outDir + "/" + outName + ".java";
38                 log.debug("Creating test cases " + outPath);
39
40                 Template template;
41                 try {
42                         String templatePath = JenaWriter.getTemplatePath(TEMPLATE_NAME);
43                         template = vEngine.getTemplate(templatePath);
44                 } catch (ResourceNotFoundException e) {
45                         throw new RuntimeException();
46                 } catch (ParseErrorException e) {
47                         throw new RuntimeException();
48                 } catch (Exception e) {
49                         throw new RuntimeException();
50                 }
51
52                 try {
53                         FileWriter fWriter = new FileWriter(outPath);
54                         template.merge(vContext, fWriter);
55                         fWriter.close();
56                 } catch (Exception e) {
57                         throw new RuntimeException(e);
58                 }
59         }
60
61         public void setTestClassName(String testClassName) {
62                 this.testClassName = testClassName;
63         }
64
65         public void setToolsPackage(String toolsPackage) {
66                 this.toolsPackage = toolsPackage;
67         }
68
69 }
70
71