src.test.org.hibernate.tool.ant.SeamAntTest Maven / Gradle / Ivy
/*
* Created on 13-Feb-2005
*
*/
package org.hibernate.tool.ant;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.hibernate.tool.hbm2x.HibernateUberspect;
import org.hibernate.tool.test.TestHelper;
/**
* A temporary test class for controlling the seam template generation
* via an antfile.
*
* @author max
*
*/
public class SeamAntTest extends BuildFileTestCase {
private String property;
public SeamAntTest(String name) {
super(name);
}
protected void tearDown() throws Exception {
assertEquals("Method not found errors during template processing:\n " + HibernateUberspect.getMethodErrors(),0,HibernateUberspect.getMethodNotFoundCount());
System.out.println(getLog() );
}
protected void setUp() throws Exception {
HibernateUberspect.initMethodNotFoundCount(); // enable some tracing of velocity errors
configureProject("src/testsupport/seamtest-build.xml");
}
public void testConfiguration() {
executeTarget("generate-crud");
File baseDir = new File(project.getProperty("destdir"));
assertFileAndExists(new File(baseDir, "build.xml"));
assertFileAndExists(new File(baseDir, "resources/seam.properties"));
validXMLFile( new File(baseDir, "resources/WEB-INF/web.xml") );
validXMLFile( new File(baseDir, "resources/WEB-INF/faces-config.xml") );
validXMLFile( new File(baseDir, "resources/META-INF/application.xml") );
validXMLFile( new File(baseDir, "resources/META-INF/jboss-app.xml") );
// cannot check for valid xml since the jsp's has <%@ in the top
validFile( new File(baseDir, "resources/jsp/editIssue.jsp") );
validFile( new File(baseDir, "resources/jsp/findIssue.jsp") );
validFile(new File(baseDir, "src/org/jboss/seam/example/crud/Issue.java"));
validFile(new File(baseDir, "src/org/jboss/seam/example/crud/IssueFinder.java"));
validFile(new File(baseDir, "src/org/jboss/seam/example/crud/IssueFinderBean.java"));
validFile(new File(baseDir, "src/org/jboss/seam/example/crud/IssueEditor.java"));
validFile(new File(baseDir, "src/org/jboss/seam/example/crud/IssueEditorBean.java"));
File srcDir = new File(baseDir, "src");
List files = TestHelper.visitAllFiles(srcDir, new ArrayList());
List jars = new ArrayList();
jars.add("ejb3-persistence.jar");
jars.add("jboss-ejb3x.jar");
jars.add("hibernate-annotations.jar");
jars.add("jboss-seam.jar");
jars.add("myfaces-api.jar");
TestHelper.compile( srcDir, new File(baseDir, "classes"), files, "1.5", TestHelper.buildClasspath(jars));
}
private void validXMLFile(File xmlfile) {
validFile( xmlfile );
assertParse(xmlfile);
}
private void validFile(File xmlfile) {
assertFileAndExists(xmlfile);
assertEquals( "$ found in " + xmlfile, null, TestHelper.findFirstString( "$", xmlfile ));
}
public static Test suite() {
return new TestSuite(SeamAntTest.class);
}
/** parse the url, fails if not valid xml. Does not validate against the DTD because they are remote */
public Document assertParse(File url) {
SAXReader reader = new SAXReader();
reader.setValidation(false);
Document document = null;
try {
document = reader.read(url);
}
catch (DocumentException e) {
fail("Could not parse " + url + ":" + e);
}
assertNotNull(document);
return document;
}
protected void assertFileAndExists(File file) {
assertTrue(file + " does not exist", file.exists() );
assertTrue(file + " not a file", file.isFile() );
assertTrue(file + " does not have any contents", file.length()>0);
}
}