View Javadoc

1   /*
2    * $Id: AjcXmlTaskTest.java,v 1.5 2011/07/29 20:20:17 oboehm Exp $
3    *
4    * Copyright (c) 2010 by Oliver Boehm
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   * (c)reated 26.03.2010 by oliver (ob@oasd.de)
19   */
20  
21  package patterntesting.tool.aspectj;
22  
23  import static org.junit.Assert.*;
24  
25  import java.io.*;
26  
27  import org.aspectj.tools.ant.taskdefs.AjcTask;
28  import org.junit.Test;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * This is the JUnit test for the AjcXml class.
33   *
34   * @author oliver
35   * @since 1.0 (26.03.2010)
36   */
37  public final class AjcXmlTaskTest {
38  
39      private static final org.slf4j.Logger log = LoggerFactory.getLogger(AjcXmlTaskTest.class);
40      private final AjcXmlTask compiler = new AjcXmlTask();
41  
42      /**
43       * Test method for {@link patterntesting.tool.aspectj.AjcXmlTask#execute()}.
44       * @throws IOException impossible
45       */
46      @Test
47      public void testEmptyExecute() throws IOException {
48          ByteArrayOutputStream baos = new ByteArrayOutputStream();
49          compiler.setOutputStream(baos);
50          compiler.execute();
51          String xmlString = baos.toString();
52          baos.close();
53          checkXML(xmlString);
54      }
55  
56      /**
57       * We call the compiler task but did not set the OutputStream or Writer
58       * here. The AjcXmlTask should do that for us.
59       */
60      @Test
61      public void testGetResult() {
62          compiler.execute();
63          String xmlString = compiler.getResult();
64          checkXML(xmlString);
65      }
66  
67      private static void checkXML(final String xmlString) {
68          log.debug(xmlString);
69          assertTrue(xmlString, xmlString.trim().startsWith("<"));
70      }
71  
72      /**
73       * Test method for hasAspectjPath(). To test this method we manipulate the
74       * classpath a little bit.
75       *
76       * @since 1.0
77       */
78      @Test
79      public void testHasAspectjPath() {
80          String classpath = System.getProperty("java.class.path");
81          log.info("classpath=" + classpath);
82          try {
83              System.setProperty("java.class.path", "/tmp/aspectjrt.jar");
84              assertTrue("aspectjrt.jar not recognized", AjcXmlTask
85                      .hasAspectjPath());
86              System.setProperty("java.class.path", "/tmp/aspectjrt-1.6.8.jar");
87              assertTrue("aspectjrt-1.6.8.jar not recognized", AjcXmlTask
88                      .hasAspectjPath());
89          } finally {
90              System.setProperty("java.class.path", classpath);
91          }
92      }
93  
94      /**
95       * We should find the AspectJ runtime lib in the local maven repository.
96       * Otherwise getApectjPathFromRepository() does not work correctly.
97       *
98       * @since 1.0
99       */
100     @Test
101     public void testFindAspectjPath() {
102         String classpath = System.getProperty("java.class.path");
103         try {
104             System.setProperty("java.class.path", ".");
105             File jarFile = AjcXmlTask.findAspectjPath();
106             log.info("aspectjrt.jar found: " + jarFile);
107             assertTrue(jarFile + " not found", jarFile.exists());
108         } finally {
109             System.setProperty("java.class.path", classpath);
110         }
111 
112     }
113 
114     /**
115      * The AjcTask offers findAspectjtoolsJar(). This is tested here.
116      * @since 1.0
117      */
118     @Test
119     public void testFindAspectjtoolsJar() {
120         File aspectjtoolsJar = AjcTask.findAspectjtoolsJar();
121         log.info("aspectjtoolsJar=" + aspectjtoolsJar);
122         assertNotNull(aspectjtoolsJar);
123         assertTrue(aspectjtoolsJar + " not found", aspectjtoolsJar.exists());
124     }
125 
126 }
127