6.8. Executing the Logifier Plug-inAt this point you must be anxious to see the Logifier plug-in executing... As you are a good developer, your first instinct is to write tests for the plug-in which will also serve as an execution bed. Good call! 6.8.1. How do I do that?You already saw in a previous lab how to write a plug-in test, so focus on the parts specific to the Logifier plug-in. Figure 6-5 show the typical test directory structure. Figure 6-5. Logifier plug-in directory structure showing the plug-in testsThe testLogifierExecution subproject is a Maven project that has some source (Main.java) and which generates an executable JAR (the project.properties file defines the maven.jar.mainclass property to specify the main class: maven.jar.mainclass = mdn.logifier.test.Main). The Main.java class contains some very simple code meant to trigger the Logging Aspect: package mdn.logifier.test; public class Main { public static void main(String[ ] args) { Main main = new Main( ); main.display1("It works1..."); main.display2("It works2..."); } public void display1(String message) { System.out.println(message); } public int display2(String message) { System.out.println(message); return 1; } } The maven.xml file is very similar in content to the one you saw in the "Testing a Plug-in" lab earlier in this chapter, and it verifies that the Logging Aspect has executed by asserting the content of the console output: <project default="testPlugin" xmlns:ant="jelly:ant" xmlns:assert="assert"> <goal name="testPlugin" prereqs="clean"> <attainGoal name="logifier:logify"/> <ant:record name="${maven.build.dir}/log.txt" action="start"/> <attainGoal name="jarexec:run"/> <ant:record name="${maven.build.dir}/log.txt" action="stop"/> <assert:assertFileContains file="${maven.build.dir}/log.txt" match="display1([It works1...])"/> <assert:assertFileContains file="${maven.build.dir}/log.txt" match="display2([It works2...])"/> </goal> </project> Notice that you have cleverly reused the Jarexec plug-in that you created in the first lab. Execute it by building the plug-in (maven plugin:install) and running the test (maven plugin:test): C:\dev\mavenbook\code\plugins\logifier>maven plugin:test [...] logifier:init: java:prepare-filesystem: [mkdir] Created dir: C:\dev\mavenbook\code\plugins\logifier\src\plugin-test\ testLogifierExecution\target\classes java:compile: [echo] Compiling to C:\dev\mavenbook\code\plugins\logifier\src\plugin-test\ testLogifierExecution/target/classes [javac] Compiling 1 source file to C:\dev\mavenbook\code\plugins\logifier\src\ plugin-test\testLogifierExecution\target\classes logifier:compile: [...] jar:jar: [jar] Building jar: C:\dev\mavenbook\code\plugins\logifier\src\plugin-test\testLogifierExecution\target\logifier-testLogifierExecution-1.0.jar jar: logifier:logify: [jar] Updating jar: C:\dev\mavenbook\code\plugins\logifier\src\plugin-test\testLogifierExecution\target\logifier-testLogifierE xecution-1.0.jar [...] jarexec:run: [java] <display1([It works1...]) [java] It works1... [java] >display1 (0ms) [java] <display2([It works2...]) [java] It works2... [java] >display2 = [1] (0ms) BUILD SUCCESSFUL Total time: 5 seconds Wow! You're done. And it works! |