FileDocCategorySizeDatePackage
ManifestTest.javaAPI DocApache Ant 1.7013137Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.taskdefs

ManifestTest

public class ManifestTest extends org.apache.tools.ant.BuildFileTest
Testcase for the Manifest class used in the jar task.

Fields Summary
public static final String
EXPANDED_MANIFEST
public static final String
LONG_LINE
public static final String
LONG_70_NAME
public static final String
LONG_68_NAME
public static final String
NOT_LONG_NAME
public static final String
VALUE
Constructors Summary
public ManifestTest(String name)


       
        super(name);
    
Methods Summary
private ManifestgetManifest(java.lang.String filename)
Reads mftest.mf.

        FileReader r = new FileReader(new File(System.getProperty("root"), filename));
        try {
            return new Manifest(r);
        } finally {
            r.close();
        }
    
public voidsetUp()

        configureProject("src/etc/testcases/taskdefs/manifest.xml");
    
public voidtearDown()

        executeTarget("clean");
    
public voidtest1()
Empty manifest - is OK

        executeTarget("test1");
        Manifest manifest = getManifest(EXPANDED_MANIFEST);
        String version = manifest.getManifestVersion();
        assertEquals("Manifest was not created with correct version - ", "1.0", version);
    
public voidtest10()
Inline manifest - Invalid attribute without name

        expectBuildExceptionContaining("test10", "Attribute has no name",
                                       "Attributes must have name and value");
    
public voidtest11()
Inline manifest - Invalid attribute without value

        expectBuildExceptionContaining("test11", "Attribute has no value",
                                       "Attributes must have name and value");
    
public voidtest12()
Inline manifest - Invalid attribute without value

        expectBuildExceptionContaining("test12", "Section with no name",
                                       "Sections must have a name");
    
public voidtest13()
Inline manifest - Duplicate attribute

        expectBuildExceptionContaining("test13", "Duplicate Attribute",
                                       "The attribute \"Test\" may not occur more than once in the same section");
    
public voidtest14()
Inline manifest - OK since classpath entries can be duplicated.

        executeTarget("test14");
        Manifest manifest = getManifest(EXPANDED_MANIFEST);
        Manifest.Section mainSection = manifest.getMainSection();
        String classpath = mainSection.getAttributeValue("class-path");
        assertEquals("Class-Path attribute was not set correctly - ",
            "Test1 Test2 Test3 Test4", classpath);
    
public voidtest2()
Simple Manifest with version 2.0

        executeTarget("test2");
        Manifest manifest = getManifest(EXPANDED_MANIFEST);
        String version = manifest.getManifestVersion();
        assertEquals("Manifest was not created with correct version - ", "2.0", version);
    
public voidtest3()
Malformed manifest - no : on the line

        expectBuildExceptionContaining("test3", "Manifest is invalid - no colon on header line",
                                       "Invalid Manifest");
    
public voidtest4()
Malformed manifest - starts with continuation line

        expectBuildExceptionContaining("test4", "Manifest is invalid - section starts with continuation line",
                                       "Invalid Manifest");
   
public voidtest5()
Malformed manifest - Name attribute in main section

        executeTarget("test5");
        String output = 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);
    
public voidtest6()
New Section not starting with Name attribute.

        expectBuildExceptionContaining("test6", "Manifest is invalid - section starts with incorrect attribute",
                                       "Invalid Manifest");
        String output = 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);
    
public voidtest7()
From attribute is illegal

        executeTarget("test7");

        boolean hasWarning = getLog().indexOf(Manifest.ERROR_FROM_FORBIDDEN) != -1;
        assertTrue("Expected warning about From: attribute", hasWarning);
    
public voidtest8()
Inline manifest - OK

        executeTarget("test8");
        Manifest manifest = getManifest(EXPANDED_MANIFEST);
        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);
    
public voidtest9()
Inline manifest - Invalid since has a Name attribute in the section element

        expectBuildExceptionContaining("test9", "Construction is invalid - Name attribute should not be used",
                                       "Specify the section name using the \"name\" attribute of the <section> element");
    
public voidtestFrom()

        expectLogContaining("testFrom", Manifest.ERROR_FROM_FORBIDDEN);
    
public voidtestLongLine()
Tets long line wrapping

        Project p = 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);
        executeTarget("testLongLine");

        Manifest manifest = getManifest(EXPANDED_MANIFEST);
        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);
        
        BufferedReader in = new BufferedReader(new FileReader(EXPANDED_MANIFEST));
        
        Set set = new HashSet();
        String read = in.readLine();
        while (read != null)
        {
            set.add(read);
            read = in.readLine();
        }
        
        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"));
    
public voidtestNoFile()
file attribute for manifest task is required.

        expectBuildException("testNoFile", "file is required");
    
public voidtestOrder1()
Tests ordering of sections

        executeTarget("testOrder1");

        Manifest manifest = getManifest(EXPANDED_MANIFEST);
        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);
    
public voidtestOrder2()
Tests ordering of sections

        executeTarget("testOrder2");

        Manifest manifest = getManifest(EXPANDED_MANIFEST);
        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);
    
public voidtestReplace()
replace changes Manifest-Version from 2.0 to 1.0

        executeTarget("testReplace");
        Manifest mf = getManifest("src/etc/testcases/taskdefs/mftest.mf");
        assertNotNull(mf);
        assertEquals(Manifest.getDefaultManifest(), mf);
    
public voidtestUpdate()
update keeps the Manifest-Version and adds a new attribute Foo

        executeTarget("testUpdate");
        Manifest mf = getManifest("src/etc/testcases/taskdefs/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("src/etc/testcases/taskdefs/mftest2.mf");
        assertNotNull(mf);
        mfAsString = mf.toString();
        assertNotNull(mfAsString);
        assertEquals(-1, mfAsString.indexOf("Foo: Bar"));
        assertTrue(mfAsString.indexOf("Foo: Baz") > -1);