summaryrefslogtreecommitdiffstats
path: root/test/junit/net/sf/antcontrib/cpptasks/TestProcessorDef.java
blob: c4e29051c31a4b841d6bfc3b431fb18cc73d16f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 *
 * Copyright 2002-2004 The Ant-Contrib project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.sf.antcontrib.cpptasks;

import java.io.File;
import java.io.IOException;
import junit.framework.TestCase;
import net.sf.antcontrib.cpptasks.compiler.LinkType;
import net.sf.antcontrib.cpptasks.compiler.ProcessorConfiguration;
import net.sf.antcontrib.cpptasks.types.ConditionalFileSet;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Reference;

/**
 * Tests for ProcessorDef.
 */
public abstract class TestProcessorDef
    extends TestCase {

  /**
   * Constructor.
   *
   * @param name
   *            test name
   */
  public TestProcessorDef(final String name) {
    super(name);
  }

  /**
   * Creates a new processor definition.
   *
   * @return created processor definition
   */
  protected abstract ProcessorDef create();

  /**
   * Tests that isActive returns true when "if" references a set property.
   */
  public final void testIsActive2() {
    ProcessorDef arg = create();
    Project project = new Project();
    project.setProperty("cond", "");
    arg.setProject(project);
    arg.setIf("cond");
    assertTrue(arg.isActive());
  }

  /**
   * Tests that isActive returns false when "if" references an unset property.
   */
  public final void testIsActive3() {
    ProcessorDef arg = create();
    arg.setProject(new Project());
    arg.setIf("cond");
    assertTrue(!arg.isActive());
  }

  /**
   * Tests that evaluating isActive when "if" refernces a property with the
   * value "false" throws an exception to warn of a suspicious value.
   *
   */
  public final void testIsActive4() {
    ProcessorDef arg = create();
    Project project = new Project();
    project.setProperty("cond", "false");
    arg.setProject(project);
    arg.setIf("cond");
    try {
      boolean isActive = arg.isActive();
    } catch (BuildException ex) {
      return;
    }
    fail("Should throw exception for suspicious value");
  }

  /**
   * Tests that isActive returns false when "unless" references a set
   * property.
   */
  public final void testIsActive5() {
    ProcessorDef arg = create();
    Project project = new Project();
    project.setProperty("cond", "");
    arg.setProject(project);
    arg.setUnless("cond");
    assertTrue(!arg.isActive());
  }

  /**
   * Tests that isActive returns true when "unless" references an unset
   * property.
   */
  public final void testIsActive6() {
    ProcessorDef arg = create();
    arg.setProject(new Project());
    arg.setUnless("cond");
    assertTrue(arg.isActive());
  }

  /**
   * Tests that evaluating isActive when "unless" references a property with
   * the value "false" throws an exception to warn of a suspicious value.
   *
   */
  public final void testIsActive7() {
    ProcessorDef arg = create();
    Project project = new Project();
    project.setProperty("cond", "false");
    arg.setProject(project);
    arg.setUnless("cond");
    try {
      boolean isActive = arg.isActive();
    } catch (BuildException ex) {
      return;
    }
    fail("Should throw exception for suspicious value");
  }

  /**
   * Tests if a processor is active when both "if" and "unless" are specified
   * and the associated properties are set.
   *
   */
  public final void testIsActive8() {
    ProcessorDef arg = create();
    Project project = new Project();
    project.setProperty("cond", "");
    arg.setProject(project);
    arg.setIf("cond");
    arg.setUnless("cond");
    assertTrue(!arg.isActive());
  }

  /**
   * Creates a processor initialized to be an extension of the base processor.
   *
   * @param baseProcessor
   *            base processor
   * @return extending processor
   */
  protected final ProcessorDef createExtendedProcessorDef(
      final ProcessorDef baseProcessor) {
    Project project = new Project();
    baseProcessor.setProject(project);
    baseProcessor.setId("base");
    project.addReference("base", baseProcessor);
    ProcessorDef extendedLinker = create();
    extendedLinker.setProject(project);
    extendedLinker.setExtends(new Reference("base"));
    return extendedLinker;
  }

  /**
   * Gets the processor configuration.
   *
   * @param extendedProcessor
   *            processor under test
   * @return configuration
   */
  protected final ProcessorConfiguration getConfiguration(
      final ProcessorDef extendedProcessor) {
    CCTask cctask = new CCTask();
    LinkType linkType = new LinkType();
    return extendedProcessor.createConfiguration(cctask,
                                                 linkType,
                                                 null,
                                                 null,
                                                 null);
  }

  /**
   * Gets command line arguments that precede filenames.
   *
   * @param processor
   *            processor under test
   * @return array of command line parameters
   */
  protected abstract String[] getPreArguments(final ProcessorDef processor);

  /**
   * Tests that a fileset in the base processor is effective when evaluating
   * the files included in an extending processor.
   *
   * @param tempFile
   *            temporary file
   * @throws IOException
   *             if unable to delete file
   */
  protected final void testExtendsFileSet(final File tempFile) throws
      IOException {
    ProcessorDef baseLinker = create();
    ConditionalFileSet fileSet = new ConditionalFileSet();
    ProcessorDef extendedLinker = createExtendedProcessorDef(baseLinker);
    fileSet.setProject(baseLinker.getProject());
    fileSet.setDir(new File(tempFile.getParent()));
    fileSet.setIncludes(tempFile.getName());
    baseLinker.addFileset(fileSet);
    MockFileCollector collector = new MockFileCollector();
    extendedLinker.visitFiles(collector);
    tempFile.delete();
    assertEquals(1, collector.size());
  }

  /**
   * Tests that the if attribute in the base processor is effective when
   * evaluating if an extending processor is active.
   */
  public final void testExtendsIf() {
    ProcessorDef baseLinker = create();
    baseLinker.setIf("bogus");
    ProcessorDef extendedLinker = createExtendedProcessorDef(baseLinker);
    boolean isActive = extendedLinker.isActive();
    assertEquals(false, isActive);
    baseLinker.getProject().setProperty("bogus", "");
    isActive = extendedLinker.isActive();
    assertEquals(true, isActive);
  }

  /**
   * Tests that the unless attribute in the base processor is effective when
   * evaluating if an extending processor is active.
   */
  public final void testExtendsUnless() {
    ProcessorDef baseLinker = create();
    baseLinker.setUnless("bogus");
    ProcessorDef extendedLinker = createExtendedProcessorDef(baseLinker);
    boolean isActive = extendedLinker.isActive();
    assertEquals(true, isActive);
    baseLinker.getProject().setProperty("bogus", "");
    isActive = extendedLinker.isActive();
    assertEquals(false, isActive);
  }

  /**
   * Tests that the debug attribute in the base processor is effective when
   * creating the command line for a processor that extends it.
   */
  public final void testExtendsDebug() {
    ProcessorDef baseLinker = create();
    baseLinker.setDebug(true);
    ProcessorDef extendedLinker = createExtendedProcessorDef(baseLinker);
    String[] preArgs = getPreArguments(extendedLinker);
    assertEquals("-g", preArgs[preArgs.length - 1]);
  }

  /**
   * Tests that the rebuild attribute in the base processor is effective when
   * creating the command line for a processor that extends it.
   *
   * @param baseProcessor
   *            processor under test
   */
  protected final void testExtendsRebuild(
      final ProcessorDef baseProcessor) {
    baseProcessor.setRebuild(true);
    ProcessorDef extendedLinker = createExtendedProcessorDef(baseProcessor);
    ProcessorConfiguration config = getConfiguration(extendedLinker);
    boolean rebuild = config.getRebuild();
    assertEquals(true, rebuild);
  }
}