View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar.cpptasks;
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.tools.ant.BuildException;
28  import org.apache.tools.ant.Project;
29  import org.apache.tools.ant.types.Reference;
30  
31  import com.github.maven_nar.cpptasks.compiler.LinkType;
32  import com.github.maven_nar.cpptasks.compiler.ProcessorConfiguration;
33  import com.github.maven_nar.cpptasks.types.ConditionalFileSet;
34  
35  /**
36   * Tests for ProcessorDef.
37   */
38  public abstract class TestProcessorDef extends TestCase {
39  
40    /**
41     * Constructor.
42     *
43     * @param name
44     *          test name
45     */
46    public TestProcessorDef(final String name) {
47      super(name);
48    }
49  
50    /**
51     * Creates a new processor definition.
52     *
53     * @return created processor definition
54     */
55    protected abstract ProcessorDef create();
56  
57    /**
58     * Creates a processor initialized to be an extension of the base processor.
59     *
60     * @param baseProcessor
61     *          base processor
62     * @return extending processor
63     */
64    protected final ProcessorDef createExtendedProcessorDef(final ProcessorDef baseProcessor) {
65      final Project project = new Project();
66      baseProcessor.setProject(project);
67      baseProcessor.setId("base");
68      project.addReference("base", baseProcessor);
69      final ProcessorDef extendedLinker = create();
70      extendedLinker.setProject(project);
71      extendedLinker.setExtends(new Reference("base"));
72      return extendedLinker;
73    }
74  
75    /**
76     * Gets the processor configuration.
77     *
78     * @param extendedProcessor
79     *          processor under test
80     * @return configuration
81     */
82    protected final ProcessorConfiguration getConfiguration(final ProcessorDef extendedProcessor) {
83      final CCTask cctask = new CCTask();
84      final LinkType linkType = new LinkType();
85      final File objDir = new File("dummy");
86      cctask.setObjdir(objDir);
87      return extendedProcessor.createConfiguration(cctask, linkType, null, null, null);
88    }
89  
90    /**
91     * Gets command line arguments that precede filenames.
92     *
93     * @param processor
94     *          processor under test
95     * @return array of command line parameters
96     */
97    protected abstract String[] getPreArguments(final ProcessorDef processor);
98  
99    /**
100    * Tests that the debug attribute in the base processor is effective when
101    * creating the command line for a processor that extends it.
102    */
103   public final void testExtendsDebug() {
104     final ProcessorDef baseLinker = create();
105     baseLinker.setDebug(true);
106     final ProcessorDef extendedLinker = createExtendedProcessorDef(baseLinker);
107     final String[] preArgs = getPreArguments(extendedLinker);
108     // FREEHEP, passes (sometimes) extra option
109     assertEquals("-g", preArgs[Math.max(preArgs.length - 2, 0)]);
110   }
111 
112   /**
113    * Tests that a fileset in the base processor is effective when evaluating
114    * the files included in an extending processor.
115    *
116    * @param tempFile
117    *          temporary file
118    * @throws IOException
119    *           if unable to delete file
120    */
121   protected final void testExtendsFileSet(final File tempFile) throws IOException {
122     final ProcessorDef baseLinker = create();
123     final ConditionalFileSet fileSet = new ConditionalFileSet();
124     final ProcessorDef extendedLinker = createExtendedProcessorDef(baseLinker);
125     fileSet.setProject(baseLinker.getProject());
126     fileSet.setDir(new File(tempFile.getParent()));
127     fileSet.setIncludes(tempFile.getName());
128     baseLinker.addFileset(fileSet);
129     final MockFileCollector collector = new MockFileCollector();
130     extendedLinker.visitFiles(collector);
131     tempFile.delete();
132     assertEquals(1, collector.size());
133   }
134 
135   /**
136    * Tests that the if attribute in the base processor is effective when
137    * evaluating if an extending processor is active.
138    */
139   public final void testExtendsIf() {
140     final ProcessorDef baseLinker = create();
141     baseLinker.setIf("bogus");
142     final ProcessorDef extendedLinker = createExtendedProcessorDef(baseLinker);
143     boolean isActive = extendedLinker.isActive();
144     assertEquals(false, isActive);
145     baseLinker.getProject().setProperty("bogus", "");
146     isActive = extendedLinker.isActive();
147     assertEquals(true, isActive);
148   }
149 
150   /**
151    * Tests that the rebuild attribute in the base processor is effective when
152    * creating the command line for a processor that extends it.
153    *
154    * @param baseProcessor
155    *          processor under test
156    */
157   protected final void testExtendsRebuild(final ProcessorDef baseProcessor) {
158     baseProcessor.setRebuild(true);
159     final ProcessorDef extendedLinker = createExtendedProcessorDef(baseProcessor);
160     final ProcessorConfiguration config = getConfiguration(extendedLinker);
161     final boolean rebuild = config.getRebuild();
162     assertEquals(true, rebuild);
163   }
164 
165   /**
166    * Tests that the unless attribute in the base processor is effective when
167    * evaluating if an extending processor is active.
168    */
169   public final void testExtendsUnless() {
170     final ProcessorDef baseLinker = create();
171     baseLinker.setUnless("bogus");
172     final ProcessorDef extendedLinker = createExtendedProcessorDef(baseLinker);
173     boolean isActive = extendedLinker.isActive();
174     assertEquals(true, isActive);
175     baseLinker.getProject().setProperty("bogus", "");
176     isActive = extendedLinker.isActive();
177     assertEquals(false, isActive);
178   }
179 
180   /**
181    * Tests that isActive returns true when "if" references a set property.
182    */
183   public final void testIsActive2() {
184     final ProcessorDef arg = create();
185     final Project project = new Project();
186     project.setProperty("cond", "");
187     arg.setProject(project);
188     arg.setIf("cond");
189     assertTrue(arg.isActive());
190   }
191 
192   /**
193    * Tests that isActive returns false when "if" references an unset property.
194    */
195   public final void testIsActive3() {
196     final ProcessorDef arg = create();
197     arg.setProject(new Project());
198     arg.setIf("cond");
199     assertTrue(!arg.isActive());
200   }
201 
202   /**
203    * Tests that evaluating isActive when "if" refernces a property with the
204    * value "false" throws an exception to warn of a suspicious value.
205    *
206    */
207   public final void testIsActive4() {
208     final ProcessorDef arg = create();
209     final Project project = new Project();
210     project.setProperty("cond", "false");
211     arg.setProject(project);
212     arg.setIf("cond");
213     try {
214       final boolean isActive = arg.isActive();
215     } catch (final BuildException ex) {
216       return;
217     }
218     fail("Should throw exception for suspicious value");
219   }
220 
221   /**
222    * Tests that isActive returns false when "unless" references a set
223    * property.
224    */
225   public final void testIsActive5() {
226     final ProcessorDef arg = create();
227     final Project project = new Project();
228     project.setProperty("cond", "");
229     arg.setProject(project);
230     arg.setUnless("cond");
231     assertTrue(!arg.isActive());
232   }
233 
234   /**
235    * Tests that isActive returns true when "unless" references an unset
236    * property.
237    */
238   public final void testIsActive6() {
239     final ProcessorDef arg = create();
240     arg.setProject(new Project());
241     arg.setUnless("cond");
242     assertTrue(arg.isActive());
243   }
244 
245   /**
246    * Tests that evaluating isActive when "unless" references a property with
247    * the value "false" throws an exception to warn of a suspicious value.
248    *
249    */
250   public final void testIsActive7() {
251     final ProcessorDef arg = create();
252     final Project project = new Project();
253     project.setProperty("cond", "false");
254     arg.setProject(project);
255     arg.setUnless("cond");
256     try {
257       final boolean isActive = arg.isActive();
258     } catch (final BuildException ex) {
259       return;
260     }
261     fail("Should throw exception for suspicious value");
262   }
263 
264   /**
265    * Tests if a processor is active when both "if" and "unless" are specified
266    * and the associated properties are set.
267    *
268    */
269   public final void testIsActive8() {
270     final ProcessorDef arg = create();
271     final Project project = new Project();
272     project.setProperty("cond", "");
273     arg.setProject(project);
274     arg.setIf("cond");
275     arg.setUnless("cond");
276     assertTrue(!arg.isActive());
277   }
278 }