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  import java.io.InputStream;
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.Vector;
28  
29  import junit.framework.TestCase;
30  
31  import com.github.maven_nar.cpptasks.compiler.CommandLineCompilerConfiguration;
32  import com.github.maven_nar.cpptasks.compiler.CompilerConfiguration;
33  import com.github.maven_nar.cpptasks.gcc.GccCCompiler;
34  
35  /**
36   * Tests for CCTask.
37   *
38   */
39  public final class TestCCTask extends TestCase {
40    /**
41     * Constructor.
42     * 
43     * @param name
44     *          test name
45     *
46     */
47    public TestCCTask(final String name) {
48      super(name);
49    }
50  
51    /**
52     * Test checks for the presence of antlib.xml.
53     * 
54     * @throws IOException
55     *           if stream can't be closed.
56     *
57     */
58    public void testAntlibXmlPresent() throws IOException {
59      final InputStream stream = TestCCTask.class.getClassLoader().getResourceAsStream(
60          "com/github/maven_nar/cpptasks/antlib.xml");
61      if (stream != null) {
62        stream.close();
63      }
64      assertNotNull("antlib.xml missing", stream);
65    }
66  
67    /**
68     * Tests that the default value of failonerror is true.
69     */
70    public void testGetFailOnError() {
71      final CCTask task = new CCTask();
72      final boolean failOnError = task.getFailonerror();
73      assertEquals(true, failOnError);
74    }
75  
76    /**
77     * Test that a target with no existing object file is
78     * returned by getTargetsToBuildByConfiguration.
79     */
80    public void testGetTargetsToBuildByConfiguration1() {
81      final CompilerConfiguration config1 = new CommandLineCompilerConfiguration(GccCCompiler.getInstance(), "dummy",
82          new File[0], new File[0], new File[0], "", new String[0], new ProcessorParam[0], true, new String[0]);
83      final TargetInfo target1 = new TargetInfo(config1, new File[] {
84        new File("src/foo.bar")
85      }, null, new File("foo.obj"), true);
86      final Map targets = new HashMap();
87      targets.put(target1.getOutput(), target1);
88      final Map targetsByConfig = CCTask.getTargetsToBuildByConfiguration(targets);
89      final Vector targetsForConfig1 = (Vector) targetsByConfig.get(config1);
90      assertNotNull(targetsForConfig1);
91      assertEquals(1, targetsForConfig1.size());
92      final TargetInfo targetx = (TargetInfo) targetsForConfig1.elementAt(0);
93      assertSame(target1, targetx);
94    }
95  
96    /**
97     * Test that a target that is up to date is not returned by
98     * getTargetsToBuildByConfiguration.
99     *
100    */
101   public void testGetTargetsToBuildByConfiguration2() {
102     final CompilerConfiguration config1 = new CommandLineCompilerConfiguration(GccCCompiler.getInstance(), "dummy",
103         new File[0], new File[0], new File[0], "", new String[0], new ProcessorParam[0], false, new String[0]);
104     //
105     // target doesn't need to be rebuilt
106     //
107     final TargetInfo target1 = new TargetInfo(config1, new File[] {
108       new File("src/foo.bar")
109     }, null, new File("foo.obj"), false);
110     final Map targets = new HashMap();
111     targets.put(target1.getOutput(), target1);
112     //
113     // no targets need to be built, return a zero-length hashtable
114     //
115     final Map targetsByConfig = CCTask.getTargetsToBuildByConfiguration(targets);
116     assertEquals(0, targetsByConfig.size());
117   }
118 
119   /**
120    * Tests that setting failonerror is effective.
121    */
122   public void testSetFailOnError() {
123     final CCTask task = new CCTask();
124     task.setFailonerror(false);
125     boolean failOnError = task.getFailonerror();
126     assertEquals(false, failOnError);
127     task.setFailonerror(true);
128     failOnError = task.getFailonerror();
129     assertEquals(true, failOnError);
130   }
131 }