在我的最终项目中,我构建了测试JPA实体的工具测试,这个项目基于Web使用JSF和primefaces UI以及我的服务器glassfish服务器,我的工具测试crud(创建,使用JPQL读取,更新,删除),以及我尝试使用编译字符串完成测试,但是我从第2行得到错误,如果这个错误意味着需要jars库或需要其他解决方案?
和我的第二个问题,这是以这种方式测试任何实体的更好的解决方案吗?请支持我,谢谢..... 这是类测试Create(插入数据)......
public boolean ReturnCompleteCreateTesting(String FilePath) {
StringBuilder sb = new StringBuilder();
sb.append("package Testing;\n");
sb.append("import javax.persistence.EntityManagerFactory; \n");
sb.append("import javax.persistence.EntityManager; \n");
sb.append("import javax.persistence.Persistence; \n");
sb.append("import Entities.Employ; \n");
sb.append("public class TestCreation implements Testing.TestCreate.DoStuff {\n");
sb.append("@Override \n");
sb.append("public void doStuff() {\n");
sb.append("EntityManagerFactory emfactory=Persistence.createEntityManagerFactory(\"JPATestingPU\");\n");
sb.append("EntityManager emanager=emfactory.createEntityManager();\n");
sb.append("long ids=3;");
sb.append("emanager.getTransaction().begin();\n");
sb.append("Employ emp = new Employ(); \n");
sb.append("emp.setDegree(\"20\"); \n");
sb.append("emp.setDepartment(\"Software Engineering\"); \n");
sb.append("emp.setId(ids); \n");
sb.append("System.out.print(\"Save Seccessfuly\"); \n");
sb.append("emanager.persist(emp); \n");
sb.append("emanager.getTransaction().commit(); \n");
sb.append("emanager.close(); \n");
sb.append("emfactory.close(); \n");
sb.append(" \n");
sb.append(" }\n");
sb.append("}\n");
File TestEntity = new File(FilePath);
if (TestEntity.getParentFile()
.exists() || TestEntity.getParentFile().mkdirs()) {
try {
Writer writer = null;
try {
writer = new FileWriter(TestEntity);
writer.write(sb.toString());
writer.flush();
} finally {
try {
writer.close();
} catch (Exception e) {
}
}
/**
* Compilation Requirements
* ********************************************************************************************
*/
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
// This sets up the class path that the compiler will use.
// I've added the .jar file that contains the DoStuff interface within in it...
List<String> optionList = new ArrayList<String>();
optionList.add("-classpath");
optionList.add(System.getProperty("java.class.path") + ";dist/TestCreate.jar");
Iterable<? extends JavaFileObject> compilationUnit
= fileManager.getJavaFileObjectsFromFiles(Arrays.asList(TestEntity));
JavaCompiler.CompilationTask task = compiler.getTask(
null,
fileManager,
diagnostics,
optionList,
null,
compilationUnit);
if (task.call()) {
System.out.println("Yipe");
// Create a new custom class loader, pointing to the directory that contains the compiled
// classes, this should point to the top of the package structure!
URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("./").toURI().toURL()});
// Load the class from the classloader by name....
Class<?> loadedClass = classLoader.loadClass("Testing.TestCreation");
// Create a new instance...
Object obj = loadedClass.newInstance();
// Santity check
if (obj instanceof DoStuff) {
// Cast to the DoStuff interface
DoStuff stuffToDo = (DoStuff) obj;
// Run it baby
stuffToDo.doStuff();
}
} else {
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
System.out.format("Error on line %d in %s%n",
diagnostic.getLineNumber(),
diagnostic.getSource().toUri());
}
}
fileManager.close();
} catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException exp) {
exp.printStackTrace();
}
}
return true;
}
public static interface DoStuff {
public void doStuff();
}
答案 0 :(得分:1)
关于Glassfish的基本问题,您需要执行以下三项操作之一:使用war文件部署所需的jar文件,将这些jar文件放在Glassfish使用的类路径中,或将jar文件放在您目录下的lib目录中域。一般:
[Glassfish installation directory]\glassfish4\glassfish\domains\domain1\lib\appLib
或者你也可以把它们放在这里:
[Glassfish installation directory]\glassfish4\glassfish\lib
但前者是两者中最合适的。