Package com.google.code.mojo.license.header

Source Code of com.google.code.mojo.license.header.HeaderTest

/**
* Copyright (C) 2008 http://code.google.com/p/maven-license-plugin/
*
* 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 com.google.code.mojo.license.header;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

import java.io.File;
import java.net.MalformedURLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.testng.annotations.Test;

import com.google.code.mojo.license.HeaderSection;
import com.google.code.mojo.license.util.FileUtils;

/**
* @author Mathieu Carbou (mathieu.carbou@gmail.com)
*/
public final class HeaderTest {
    @Test
    public void test() throws Exception {
        Map<String, String> props = new HashMap<String, String>();
        props.put("year", "2008");
        Header header = new Header(getClass().getResource("/test-header1.txt"), props, null);
        assertEquals(header.getLineCount(), 13);
        assertTrue(header.asOneLineString().contains("2008"));
        assertEquals(header.getLocation(), getClass().getResource("/test-header1.txt"));

        //FileUtils.write(new File("src/test/resources/test-header2.txt"), header.buildForDefinition(HeaderType.ASP.getDefinition(), false));

        final File file = new File("src/test/resources/test-header2.txt");
        final String content = FileUtils.read(file, System.getProperty("file.encoding"));
        assertEquals(content, header.buildForDefinition(HeaderType.ASP.getDefinition(), content.indexOf("\n") == -1));
    }
   
    @Test
    public void testHeaderSections() throws MalformedURLException {
       
        HeaderSection section = new HeaderSection();
        section.setKey("COPYRIGHT_SECTION");
        section.setEnsureMatch("Copyright \\(C\\) \\d{4} .*");
        section.setDefaultValue("Copyright (C) 2011 http://code.google.com/p/maven-license-plugin/");
       
        HeaderSection[] sections = { section };
       
        Map<String, String> props = Collections.emptyMap();
        Header header = new Header(getClass().getResource("/test-header5.txt"), props, sections);
       
        HeaderDefinition headerDefinition = HeaderType.JAVADOC_STYLE.getDefinition();

        StringBuilder h1 = new StringBuilder();
        h1.append("/**\n");
        h1.append(" * Copyright (C) 2011 Some Guy\n");
        h1.append(" *\n");
        h1.append(" * My License\n");
        h1.append(" */\n");
        assertTrue(header.isMatchForText(h1.toString(), headerDefinition, true));
       
        /**
         * This potential header should fail because the date is invalid
         */
        StringBuilder h2 = new StringBuilder();
        h2.append("/**\n");
        h2.append(" * Copyright (C) 201 Some Guy\n");
        h2.append(" *\n");
        h2.append(" * My License\n");
        h2.append(" */\n");
        assertFalse(header.isMatchForText(h2.toString(), headerDefinition, true));
       
        /**
         * This potential header should match, even with multiple lines
         */
        StringBuilder h3 = new StringBuilder();
        h3.append("/**\n");
        h3.append(" * Copyright (C) 2011 Some Guy\n");
        h3.append(" * Copyright (C) 2011 Some Other Guy\n");
        h3.append(" *\n");
        h3.append(" * My License\n");
        h3.append(" */\n");
        assertTrue(header.isMatchForText(h3.toString(), headerDefinition, true));
       
        /**
         * Assert that the header, rendered with default values, looks correct
         */
        StringBuilder h4 = new StringBuilder();
        h4.append("/**\n");
        h4.append(" * Copyright (C) 2011 http://code.google.com/p/maven-license-plugin/\n");
        h4.append(" *\n");
        h4.append(" * My License\n");
        h4.append(" */\n");
        String headerText = header.applyDefinitionAndSections(headerDefinition, true);
        assertEquals(headerText,h4.toString());
    }
   
    @Test
    public void testHeaderSectionsWithAmbiguousSeparation() throws MalformedURLException {
       
        HeaderSection sectionA = new HeaderSection();
        sectionA.setKey("COPYRIGHT_SECTION");
        sectionA.setDefaultValue("Copyright (C) 2011");
       
        HeaderSection sectionB = new HeaderSection();
        sectionB.setKey("NAME_SECTION");
        sectionB.setEnsureMatch("\\w+");
        sectionB.setDefaultValue("SomeGuy");
       
        HeaderSection[] sections = { sectionA, sectionB };
       
        Map<String, String> props = Collections.emptyMap();
        Header header = new Header(getClass().getResource("/test-header6.txt"), props, sections);
       
        HeaderDefinition headerDefinition = HeaderType.JAVADOC_STYLE.getDefinition();

        /**
         * This is a test of matching because the " " between COPYRIGHT_SECTION
         * and NAME_SECTION in the header could potentially be matched in
         * multiple places, but only one will result in a valid match that
         * allows the NAME_SECTION validation regex to match correctly. This is
         * admittedly a totally contrived example, but you never know what users
         * might do in practice.
         */
        StringBuilder h1 = new StringBuilder();
        h1.append("/**\n");
        h1.append(" * Copyright (C) 2011 SomeGuy\n");
        h1.append(" *\n");
        h1.append(" * My License\n");
        h1.append(" */\n");
        assertTrue(header.isMatchForText(h1.toString(), headerDefinition, true));
       
        /**
         * This potential header should fail because there is no space in there to match
         */
        StringBuilder h2 = new StringBuilder();
        h2.append("/**\n");
        h2.append(" * Copyright(C)2011SomeGuy\n");
        h2.append(" *\n");
        h2.append(" * My License\n");
        h2.append(" */\n");
        assertFalse(header.isMatchForText(h2.toString(), headerDefinition, true));
       
        /**
         * Assert that the header, rendered with default values, looks correct
         */
        StringBuilder h3 = new StringBuilder();
        h3.append("/**\n");
        h3.append(" * Copyright (C) 2011 SomeGuy\n");
        h3.append(" *\n");
        h3.append(" * My License\n");
        h3.append(" */\n");
        String headerText = header.applyDefinitionAndSections(headerDefinition, true);
        assertEquals(headerText,h3.toString());
    }
   
    @Test
    public void testHeaderSectionsWithMultiLineMatch() throws MalformedURLException {
       
        HeaderSection section = new HeaderSection();
        section.setKey("COPYRIGHT_SECTION");
        section.setEnsureMatch("Copyright \\(C\\) \\d{4}\nName: .*");
        section.setMultiLineMatch(true);
        section.setDefaultValue("Copyright (C) 2011\nName: http://code.google.com/p/maven-license-plugin/");
       
        HeaderSection[] sections = { section };
       
        Map<String, String> props = Collections.emptyMap();
        Header header = new Header(getClass().getResource("/test-header5.txt"), props, sections);
       
        HeaderDefinition headerDefinition = HeaderType.JAVADOC_STYLE.getDefinition();

        StringBuilder h1 = new StringBuilder();
        h1.append("/**\n");
        h1.append(" * Copyright (C) 2011\n");
        h1.append(" * Name: Some Guy\n");
        h1.append(" *\n");
        h1.append(" * My License\n");
        h1.append(" */\n");
        assertTrue(header.isMatchForText(h1.toString(), headerDefinition, true));
       
        /**
         * This potential header should fail because "Name" => "NamE"
         */
        StringBuilder h2 = new StringBuilder();
        h2.append("/**\n");
        h2.append(" * Copyright (C) 2011\n");
        h2.append(" * NamE: Some Guy\n");
        h2.append(" *\n");
        h2.append(" * My License\n");
        h2.append(" */\n");
        assertFalse(header.isMatchForText(h2.toString(), headerDefinition, true));
       
        /**
         * Assert that the header, rendered with default values, looks correct
         */
        StringBuilder h3 = new StringBuilder();
        h3.append("/**\n");
        h3.append(" * Copyright (C) 2011\n");
        h3.append(" * Name: http://code.google.com/p/maven-license-plugin/\n");
        h3.append(" *\n");
        h3.append(" * My License\n");
        h3.append(" */\n");
        String headerText = header.applyDefinitionAndSections(headerDefinition, true);
        assertEquals(headerText,h3.toString());
    }
}
TOP

Related Classes of com.google.code.mojo.license.header.HeaderTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.