At line 15 changed 2 lines. |
* [4] Modify User Junit test |
* [5] Run User Junit test |
* [4] Modify User Service Junit test |
* [5] Run User Service Junit test |
At line 325 added 70 lines. |
To support the execution of the User service level test, we need another helper file. This file (attached at the end) is called FOPHelper.java and lives in src/service/**/utils. |
|
Add the following method to UserManagerTest.java: |
|
[{Java2HtmlPlugin |
|
import java.io.File; |
import java.io.OutputStream; |
import java.io.IOException; |
|
//JAXP |
import javax.xml.transform.Transformer; |
import javax.xml.transform.TransformerFactory; |
import javax.xml.transform.TransformerException; |
import javax.xml.transform.Source; |
import javax.xml.transform.Result; |
import javax.xml.transform.stream.StreamSource; |
import javax.xml.transform.sax.SAXResult; |
|
//Avalon |
import org.apache.avalon.framework.ExceptionUtil; |
import org.apache.avalon.framework.logger.ConsoleLogger; |
import org.apache.avalon.framework.logger.Logger; |
|
//FOP |
import org.apache.fop.apps.Driver; |
import org.apache.fop.apps.FOPException; |
import org.apache.fop.messaging.MessageHandler; |
|
//Appfuse |
import org.appfuse.fop.UserInputSource; |
import org.appfuse.utils.FOPHelper; |
|
|
public void testGeneratePDF() throws Exception { |
|
try { |
User testData = new User(); |
testData.setUsername("tomcat"); |
testData.getRoles().add(new Role("user")); |
// set expected behavior on dao |
userDAO.expects(once()).method("getUser") |
.with(eq("tomcat")).will(returnValue(testData)); |
|
user = userManager.getUser("tomcat"); |
|
File baseDir = new File("."); |
File xslFile = new File(baseDir, "./src/web/org/appfuse/webapp/fop/user2fo.xsl"); |
if (xslFile.exists()) { |
log.info("xslFile exists"); |
} |
File pdfFile = new File(baseDir, "user.pdf"); |
File xmlFile = new File(baseDir, "user.xml"); |
|
log.debug("Input: a User object"); |
log.debug("Stylesheet: " + xslFile); |
log.debug("Output: PDF (" + pdfFile + ")"); |
log.debug("Transforming..."); |
|
FOPHelper fopHelper = new FOPHelper(); |
fopHelper.convertUser2PDF(user, xslFile, pdfFile); |
fopHelper.convertUser2XML(user, xmlFile); |
|
} |
catch (Throwable e) { |
log.error("Error [" + e.getMessage() + "]", e); |
} |
} |
}] |
|
At line 397 added 13 lines. |
Now execute ant test-service. The testGeneratePDF method will execute and generate the 3 files involved in the PDF generation |
process. user.xml, user.fo and user.pdf (all three attached at the end of this tutorial). |
|
You should see output: |
|
{{{ |
[junit] [appfuse] INFO [main] UserManagerTest.testGeneratePDF(83) | xslFile exists |
[junit] [appfuse] DEBUG [main] UserManagerTest.testGeneratePDF(88) | Input: a User object |
[junit] [appfuse] DEBUG [main] UserManagerTest.testGeneratePDF(89) | Stylesheet: ././src/web/org/appfuse/webapp/fop/user2fo.xsl |
[junit] [appfuse] DEBUG [main] UserManagerTest.testGeneratePDF(90) | Output: PDF (./user.pdf) |
[junit] [appfuse] DEBUG [main] UserManagerTest.testGeneratePDF(91) | Transforming... |
}}} |
|
At line 618 added 1 line. |
|