JSFUnit static analysis unit testing in ADF
Introduction
Currently I'm working on the framework design of an ADF application in JDeveloper 11.1.1.0.0 (yes sometimes a man's got to do what a man's got to do ;-). One of the parts is how we are going to do (unit) testing. And one of elements in there is testing the JSF part of the ADF application. Application Developer Framework consists of several layers where the View layer consists of a JSF implementation. This JSF implementation is named ADF Faces (RichFaces) and built on top of Trinidad, the open source JSF components implementation. ADF Faces has many components Ajax-ized and has more components than Trinidad.
My eye caught JSFUnit, an open source project from JBoss. I was only able to focus on the static analysis part JSFUnit offers (it offers also other dynamic "regular unit" tests). But little information was found about that. I couldn't find any installation tips for ADF (JBoss here and here, and Websphere setup tips were available).
Thus it was just me and the computer. Here's the steps that in the end did do the job (running on Windows XP).
Setup
A couple of things are needed for all types of static analysis.
- Download via JSFUnit Getting Started under the section “Files”: jboss-jsfunit-core-1.0.0.Beta3.jar en jboss-jsfunit-analysis-1.0.0.Beta3.jar
- Note that it was not necessary to download/install myfaces-api-1.2.0.jar or JSF 1.2 API.
- Start JDeveloper 11.1.1.0.0 and open an ADF application with Model and ViewController project.
- Right-click the ViewController project, select Properties and select Libraries and Classpath in the popup. Add the above downloaded jars there.
Three types of static tests are possible with JSFUnit. I'll describe setting up each type seperately. Most information was deducted from this part of the JSFUnit documentation.
Configuration static analysis
These tests test your JSF configuration. See the above mentioned documentation part for examples of what tests are performed.
- Create a JUnit testclass in your ViewController project. Modify it such that it matches this example class:
package test.com.project;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import org.jboss.jsfunit.analysis.AbstractFacesConfigTestCase;
public class JSFUnitStaticAnalysisConfigTest extends AbstractFacesConfigTestCase {
private static Setpaths = new HashSet() {{
// Example absolute path: add("C:/work/workspace/jsf-unit/src/faces-config.xml");
// Relative path example below.
add("public_html\\WEB-INF\\faces-config.xml");
}};
public JSFUnitStaticAnalysisConfigTest() {
super(paths);
}
}
Note the paths variable which points to the faces-config file to validate. Relative paths work too. Of course might not be valid in your test/production environment.
Tip: notice for easier separation at deployment, I put the test class in a separate package, starting with "test.". - And you're set: run the test. Of course you should see a green bar. If not, your config is probably not 100% ok (probably because what if a JSFUnit test is incorrect... Of course you then immediately contribute to the JSFUnit community project :-). I had for example several Serialization errors.
Below is a screenshot with the result of running the config testcase:
TLD analysis
These tests test your TLDs. See the above mentioned documentation part for examples of what tests are performed.
- Add the dependent libraries mentioned here, at the bottom of the page: maven-taglib and commons-logging. For the commons-logging you can use the one provided by JDeveloper: Commons Logging 1.0.4. The third one, jsp-api-2.1.jar does not seem to be necessary in the ADF project.
- Create a JUnit testclass in the ViewController project with .tld files. Modify it such that it matches this example class:
package test.com.project;
import java.util.HashSet;
import java.util.Set;
import org.jboss.jsfunit.analysis.AbstractTldTestCase;
import static org.junit.Assert.*;
public class JSFUnitStaticAnalysisTLDTest extends AbstractTldTestCase {
private static Setpaths = new HashSet() {{
// Example: add("C:/work/workspace/jsf-unit/src/demo.tld");
add("C:\\myprojects\\app\\src\\META-INF\\myjsf.tld");
}};
public JSFUnitStaticAnalysisTLDTest() {
super(paths);
}
}
Note the paths variable which points to the .tld to validate. Did not try if a relative path also works. - And you're set: run the test. Of course you should see a green bar. If not, your .tld is probably not 100% ok.
Below is a screenshot with the result of running the TLD testcase:
View static analysis
These tests test your JSF views. See the above mentioned documentation part for examples of what tests are performed.
- No extra libs are needed, so we can immediately create a JUnit testclass in the ViewController project. Modify it such that it matches this example class:
package test.com.project;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import org.jboss.jsfunit.analysis.AbstractFacesConfigTestCase;
import org.jboss.jsfunit.analysis.AbstractViewTestCase;
public class JSFUnitStaticAnalysisViewTest extends AbstractViewTestCase {
private static Set absoluteViewPaths = new HashSet() {{
// Example: add("C:/work/project/src/home.xhtml");
add("C:\\myprojects\\app\\mysite\\ViewController\\public_html\\detailsPage.jspx");
}};
private static Set recursiveViewPaths = new HashSet() {{
// Example: add("C:/work/project/src/views");
add("C:\\myprojects\\app\\mysite\\ViewController\\public_html");
}};
public JSFUnitStaticAnalysisViewTest() {
super(absoluteViewPaths, recursiveViewPaths,
"public_html\\WEB-INF\\faces-config.xml");
}
}
Note the paths variables. Nowhere I could find what they exactly mean. The above runs a test that passes, so I assume the paths are set correctly... - And you're set: run the test. Of course you should see a green bar. If not, your view is probably not 100% ok.
Below is a screenshot with the result of running the view testcase:
Conclusion
So that's it. Not hard at all! If you want to know a bit more high level stuff about JSFUnit, check this presentation by the project lead at the Javapolis conference (currently rebranded to Devox conference). And here's another introduction presentation.
The Shale Test Framework could be an interesting alternative for unittesting JSF. Needs some more investigation...
No comments:
Post a Comment