aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java
blob: 6e79432753b29e2c16c23ed9420e0b3b0f02ac81 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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 org.apache.tools.ant.taskdefs;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule;
import org.apache.tools.ant.Project;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static org.apache.tools.ant.AntAssert.assertContains;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * Testcase for the Manifest class used in the jar task.
 *
 */
public class ManifestTest {

    @Rule
    public final BuildFileRule buildRule = new BuildFileRule();

    private File expandedManifest;
    private File outDir;

    public static final String LONG_LINE
        = "AReallyLongLineToTestLineBreakingInManifests-ACapabilityWhich" +
          "IsSureToLeadToHundredsOfQuestionsAboutWhyAntMungesManifests" +
          "OfCourseTheAnswerIsThatIsWhatTheSpecRequiresAndIfAnythingHas" +
          "AProblemWithThatItIsNotABugInAnt";

    public static final String LONG_70_NAME 
        = "ThisNameIsJustSeventyCharactersWhichIsAllowedAccordingToTheSpecsFiller";
    public static final String LONG_68_NAME 
        = "ThisNameIsJustSixtyEightCharactersWhichIsAllowedAccordingToTheSpecsX";
    public static final String NOT_LONG_NAME 
        = "NameIsJustUnderSeventyCharactersWhichIsAllowedAccordingTheSpec";

    public static final String VALUE = "NOT_LONG";


    @Before
    public void setUp() {
        buildRule.configureProject("src/etc/testcases/taskdefs/manifest.xml");
        outDir = new File(buildRule.getProject().getProperty("output"));
        expandedManifest = new File(outDir, "manifests/META-INF/MANIFEST.MF");
    }

    @After
    public void tearDown() {
        buildRule.executeTarget("tearDown");
    }

    /**
     * Empty manifest - is OK
     */
    @Test
    public void test1() throws ManifestException, IOException {
        buildRule.executeTarget("test1");
        Manifest manifest = getManifest(expandedManifest);
        String version = manifest.getManifestVersion();
        assertEquals("Manifest was not created with correct version - ", "1.0", version);
    }

    /**
     * Simple Manifest with version 2.0
     */
    @Test
    public void test2() throws ManifestException, IOException {
        buildRule.executeTarget("test2");
        Manifest manifest = getManifest(expandedManifest);
        String version = manifest.getManifestVersion();
        assertEquals("Manifest was not created with correct version - ", "2.0", version);
    }

    /**
     * Malformed manifest - no : on the line
     */
    @Test
    public void test3() {
        try {
            buildRule.executeTarget("test3");
            fail("BuildException expected: Manifest is invalid - no colon on header line");
        } catch (BuildException ex) {
            assertContains("Invalid Manifest", ex.getMessage());
        }
    }

    /**
     * Malformed manifest - starts with continuation line
     */
    @Test
    public void test4() {
        try {
            buildRule.executeTarget("test4");
            fail("BuildException expected: Manifest is invalid - section starts with continuation line");
        } catch (BuildException ex) {
            assertContains("Invalid Manifest", ex.getMessage());
        }
   }

    /**
     * Malformed manifest - Name attribute in main section
     */
    @Test
    public void test5() {
        buildRule.executeTarget("test5");
        String output = buildRule.getLog();
        boolean hasWarning = output.indexOf("Manifest warning: \"Name\" attributes should not occur in the main section") != -1;
        assertTrue("Expected warning about Name in main section", hasWarning);
    }

    /**
     * New Section not starting with Name attribute.
     */
    @Test
    public void test6() {
        try {
            buildRule.executeTarget("test6");
            fail("BuildException expected: Manifest is invalid - section starts with incorrect attribute");
        } catch (BuildException ex) {
            assertContains("Invalid Manifest", ex.getMessage());
        }
        String output = buildRule.getLog();
        boolean hasWarning = output.indexOf("Manifest sections should start with a \"Name\" attribute") != -1;
        assertTrue("Expected warning about section not starting with Name: attribute", hasWarning);
    }


    /**
     * From attribute is illegal
     */
    @Test
    public void test7() {
        buildRule.executeTarget("test7");

        boolean hasWarning = buildRule.getLog().indexOf(Manifest.ERROR_FROM_FORBIDDEN) != -1;
        assertTrue("Expected warning about From: attribute", hasWarning);
    }

    /**
     * Inline manifest - OK
     */
    @Test
    public void test8() throws IOException, ManifestException {
        buildRule.executeTarget("test8");
        Manifest manifest = getManifest(expandedManifest);
        Manifest.Section mainSection = manifest.getMainSection();
        String classpath = mainSection.getAttributeValue("class-path");
        assertEquals("Class-Path attribute was not set correctly - ", "fubar", classpath);

        Manifest.Section testSection = manifest.getSection("Test");
        String testAttr = testSection.getAttributeValue("TestAttr");
        assertEquals("TestAttr attribute was not set correctly - ", "Test", testAttr);
    }

    /**
     * Inline manifest - Invalid since has a Name attribute in the section element
     */
    @Test
    public void test9() {
        try {
            buildRule.executeTarget("test9");
            fail("BuildException expected: Construction is invalid - Name attribute should not be used");
        } catch (BuildException ex) {
            assertContains("Specify the section name using the \"name\" attribute of the <section> element",
                           ex.getMessage());
        }
    }

    /**
     * Inline manifest - Invalid attribute without name
     */
    @Test
    public void test10() {
        try {
            buildRule.executeTarget("test10");
            fail("BuildException expected: Attribute has no name");
        } catch (BuildException ex) {
            assertContains("Attributes must have name and value", ex.getMessage());
        }
    }

    /**
     * Inline manifest - Invalid attribute without value
     */
    @Test
    public void test11() {
        try {
            buildRule.executeTarget("test11");
            fail("BuildException expected: Attribute has no value");
        } catch (BuildException ex) {
            assertContains("Attributes must have name and value", ex.getMessage());
        }
    }

    /**
     * Inline manifest - Invalid attribute without value
     */
    @Test
    public void test12() {
        try {
            buildRule.executeTarget("test12");
            fail("BuildException expected: Section with no name");
        } catch (BuildException ex) {
            assertContains("Sections must have a name", ex.getMessage());
        }
    }

    /**
     * Inline manifest - Duplicate attribute
     */
    @Test
    public void test13() {
        try {
            buildRule.executeTarget("test13");
            fail("BuildException expected: Duplicate Attribute");
        } catch (BuildException ex) {
            assertContains("The attribute \"Test\" may not occur more than once in the same section", ex.getMessage());
        }
    }

    /**
     * Inline manifest - OK since classpath entries can be duplicated.
     */
    @Test
    public void test14() throws IOException, ManifestException {
        buildRule.executeTarget("test14");
        Manifest manifest = getManifest(expandedManifest);
        Manifest.Section mainSection = manifest.getMainSection();
        String classpath = mainSection.getAttributeValue("class-path");
        assertEquals("Class-Path attribute was not set correctly - ",
            "Test1 Test2 Test3 Test4", classpath);
    }

    /**
     * Tets long line wrapping
     */
    @Test
    public void testLongLine() throws IOException, ManifestException {
        Project p = buildRule.getProject();
        p.setUserProperty("test.longline", LONG_LINE);
        p.setUserProperty("test.long68name" , LONG_68_NAME);
        p.setUserProperty("test.long70name" , LONG_70_NAME);
        p.setUserProperty("test.notlongname" , NOT_LONG_NAME);
        p.setUserProperty("test.value", VALUE);
        buildRule.executeTarget("testLongLine");

        Manifest manifest = getManifest(expandedManifest);
        Manifest.Section mainSection = manifest.getMainSection();
        String classpath = mainSection.getAttributeValue("class-path");
        assertEquals("Class-Path attribute was not set correctly - ",
            LONG_LINE, classpath);

        String value = mainSection.getAttributeValue(LONG_68_NAME);
        assertEquals("LONG_68_NAME_VALUE_MISMATCH", VALUE, value);
        value = mainSection.getAttributeValue(LONG_70_NAME);
        assertEquals("LONG_70_NAME_VALUE_MISMATCH", VALUE, value);
        value = mainSection.getAttributeValue(NOT_LONG_NAME);
        assertEquals("NOT_LONG_NAME_VALUE_MISMATCH", VALUE, value);

        Set set = new HashSet();
        FileReader fin = new FileReader(expandedManifest);
        try {
            BufferedReader in = new BufferedReader(fin);

            String read = in.readLine();
            while (read != null) {
                set.add(read);
                read = in.readLine();
            }
            in.close();
        } finally {
            fin.close();
        }

        assertTrue("Manifest file should have contained string ", set
                .remove(" NOT_LONG"));
        assertTrue("Manifest file should have contained string ", set
                .remove(" NG"));
        assertTrue("Manifest file should have contained string ", set
                .remove(LONG_70_NAME + ": "));
        assertTrue("Manifest file should have contained string ", set
                .remove(NOT_LONG_NAME + ": NOT_LO"));
    }

    /**
     * Tests ordering of sections
     */
    @Test
    public void testOrder1() throws IOException, ManifestException {
        buildRule.executeTarget("testOrder1");

        Manifest manifest = getManifest(expandedManifest);
        Enumeration e = manifest.getSectionNames();
        String section1 = (String)e.nextElement();
        String section2 = (String)e.nextElement();
        assertEquals("First section name unexpected", "Test1", section1);
        assertEquals("Second section name unexpected", "Test2", section2);

        Manifest.Section section = manifest.getSection("Test1");
        e = section.getAttributeKeys();
        String attr1Key = (String)e.nextElement();
        String attr2Key = (String)e.nextElement();
        String attr1 = section.getAttribute(attr1Key).getName();
        String attr2 = section.getAttribute(attr2Key).getName();
        assertEquals("First attribute name unexpected", "TestAttr1", attr1);
        assertEquals("Second attribute name unexpected", "TestAttr2", attr2);
    }

    /**
     * Tests ordering of sections
     */
    @Test
    public void testOrder2() throws IOException, ManifestException {
        buildRule.executeTarget("testOrder2");

        Manifest manifest = getManifest(expandedManifest);
        Enumeration e = manifest.getSectionNames();
        String section1 = (String)e.nextElement();
        String section2 = (String)e.nextElement();
        assertEquals("First section name unexpected", "Test2", section1);
        assertEquals("Second section name unexpected", "Test1", section2);

        Manifest.Section section = manifest.getSection("Test1");
        e = section.getAttributeKeys();
        String attr1Key = (String)e.nextElement();
        String attr2Key = (String)e.nextElement();
        String attr1 = section.getAttribute(attr1Key).getName();
        String attr2 = section.getAttribute(attr2Key).getName();
        assertEquals("First attribute name unexpected", "TestAttr2", attr1);
        assertEquals("Second attribute name unexpected", "TestAttr1", attr2);
    }

    /**
     * file attribute for manifest task is required.
     */
    @Test
    public void testNoFile() {
        try {
            buildRule.executeTarget("testNoFile");
            fail("BuildException expected: file is required");
        } catch (BuildException ex) {
            //TODO assert value
        }
    }

    /**
     * replace changes Manifest-Version from 2.0 to 1.0
     */
    @Test
    public void testReplace() throws IOException, ManifestException {
        buildRule.executeTarget("testReplace");
        Manifest mf = getManifest(new File(outDir, "mftest.mf"));
        assertNotNull(mf);
        assertEquals(Manifest.getDefaultManifest(), mf);
    }

    /**
     * update keeps the Manifest-Version and adds a new attribute Foo
     */
    @Test
    public void testUpdate() throws IOException, ManifestException {
        buildRule.executeTarget("testUpdate");
        Manifest mf = getManifest(new File(outDir, "mftest.mf"));
        assertNotNull(mf);
        assertTrue(!Manifest.getDefaultManifest().equals(mf));
        String mfAsString = mf.toString();
        assertNotNull(mfAsString);
        assertTrue(mfAsString.startsWith("Manifest-Version: 2.0"));
        assertTrue(mfAsString.indexOf("Foo: Bar") > -1);

        mf = getManifest(new File(outDir, "mftest2.mf"));
        assertNotNull(mf);
        mfAsString = mf.toString();
        assertNotNull(mfAsString);
        assertEquals(-1, mfAsString.indexOf("Foo: Bar"));
        assertTrue(mfAsString.indexOf("Foo: Baz") > -1);
    }

    @Test
    public void testFrom() {
        buildRule.executeTarget("testFrom");
        assertContains(Manifest.ERROR_FROM_FORBIDDEN, buildRule.getLog());
    }

    @Test
    public void testIllegalName() {
        try {
            buildRule.executeTarget("testIllegalName");
            fail("BuildException expected: Manifest attribute names must not contain ' '");
        } catch (BuildException ex) {
            //TODO assert value
        }
    }

    @Test
    public void testIllegalNameInSection() {
        try {
            buildRule.executeTarget("testIllegalNameInSection");
            fail("BuildException expected: Manifest attribute names must not contain ' '");
        } catch (BuildException ex) {
            //TODO assert value
        }
    }

    @Test
    public void testIllegalNameBegin() {
        try {
            buildRule.executeTarget("testIllegalNameInSection");
            fail("BuildException expected: Manifest attribute names must not start with '-' at the begin.");
        } catch (BuildException ex) {
            //TODO assert value
        }
    }

    @Test
    public void testIllegalName2() {
        try {
            buildRule.executeTarget("testIllegalName");
            fail("BuildException expected: Manifest attribute names must not contain '.'");
        } catch (BuildException ex) {
            //TODO assert value
        }
    }

    @Test
    public void testIllegalName3() {
        try {
            buildRule.executeTarget("testIllegalName");
            fail("BuildException expected: Manifest attribute names must not contain '*'");
        } catch (BuildException ex) {
            //TODO assert value
        }
    }

    /**
     * Reads mftest.mf.
     */
    private Manifest getManifest(File file) throws IOException, ManifestException {
        FileReader r = new FileReader(file);
        try {
            return new Manifest(r);
        } finally {
            r.close();
        }
    }
}