rt/mojo/src/test/java/org/apidesign/bck2brwsr/mojo/ArchetypeVersionTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 08 Mar 2013 00:42:02 +0100
changeset 823 1e9d34a337f2
parent 817 d87558f94d92
permissions -rw-r--r--
Verify nbactions.xml have the right version. Update everything to 0.5-SNAPSHOT.
jaroslav@817
     1
/**
jaroslav@817
     2
 * Back 2 Browser Bytecode Translator
jaroslav@817
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@817
     4
 *
jaroslav@817
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@817
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@817
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@817
     8
 *
jaroslav@817
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@817
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@817
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@817
    12
 * GNU General Public License for more details.
jaroslav@817
    13
 *
jaroslav@817
    14
 * You should have received a copy of the GNU General Public License
jaroslav@817
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@817
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@817
    17
 */
jaroslav@817
    18
package org.apidesign.bck2brwsr.mojo;
jaroslav@817
    19
jaroslav@817
    20
import java.net.URL;
jaroslav@817
    21
import javax.xml.XMLConstants;
jaroslav@817
    22
import javax.xml.parsers.DocumentBuilderFactory;
jaroslav@817
    23
import javax.xml.xpath.XPathConstants;
jaroslav@817
    24
import javax.xml.xpath.XPathExpression;
jaroslav@817
    25
import javax.xml.xpath.XPathFactory;
jaroslav@817
    26
import org.testng.annotations.Test;
jaroslav@817
    27
import org.xml.sax.InputSource;
jaroslav@817
    28
import static org.testng.Assert.*;
jaroslav@823
    29
import org.testng.annotations.BeforeClass;
jaroslav@817
    30
import org.w3c.dom.Document;
jaroslav@817
    31
import org.w3c.dom.NodeList;
jaroslav@817
    32
jaroslav@817
    33
/**
jaroslav@817
    34
 *
jaroslav@817
    35
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@817
    36
 */
jaroslav@817
    37
public class ArchetypeVersionTest {
jaroslav@823
    38
    private String version;
jaroslav@817
    39
    
jaroslav@817
    40
    public ArchetypeVersionTest() {
jaroslav@817
    41
    }
jaroslav@823
    42
    
jaroslav@823
    43
    @BeforeClass public void readCurrentVersion() throws Exception {
jaroslav@817
    44
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@817
    45
        URL u = l.getResource("META-INF/maven/org.apidesign.bck2brwsr/mojo/plugin-help.xml");
jaroslav@817
    46
        assertNotNull(u, "Own pom found");
jaroslav@823
    47
jaroslav@817
    48
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@817
    49
        fact.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
jaroslav@823
    50
jaroslav@817
    51
        XPathExpression xp = fact.newXPath().compile("plugin/version/text()");
jaroslav@823
    52
        version = xp.evaluate(new InputSource(u.openStream()));
jaroslav@823
    53
jaroslav@817
    54
        assertFalse(version.isEmpty(), "There should be some version string");
jaroslav@823
    55
    }
jaroslav@823
    56
    
jaroslav@823
    57
jaroslav@823
    58
    @Test public void testComparePomDepsVersions() throws Exception {
jaroslav@823
    59
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@817
    60
        URL r = l.getResource("archetype-resources/pom.xml");
jaroslav@817
    61
        assertNotNull(r, "Archetype pom found");
jaroslav@817
    62
        
jaroslav@823
    63
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@817
    64
        XPathExpression xp2 = fact.newXPath().compile(
jaroslav@817
    65
            "//version[../groupId/text() = 'org.apidesign.bck2brwsr']/text()"
jaroslav@817
    66
        );
jaroslav@817
    67
        
jaroslav@817
    68
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
jaroslav@817
    69
        NodeList arch = (NodeList) xp2.evaluate(dom, XPathConstants.NODESET);
jaroslav@817
    70
jaroslav@817
    71
        if (arch.getLength() < 3) {
jaroslav@817
    72
            fail("There should be at least three dependencies to bck2brwsr APIs: " + arch.getLength());
jaroslav@817
    73
        }
jaroslav@817
    74
        
jaroslav@817
    75
        for (int i = 0; i < arch.getLength(); i++) {
jaroslav@817
    76
            assertEquals(arch.item(i).getTextContent(), version, i + "th dependency needs to be on latest version of bck2brwsr");
jaroslav@817
    77
        }
jaroslav@817
    78
    }
jaroslav@823
    79
    
jaroslav@823
    80
    @Test public void testNbActions() throws Exception {
jaroslav@823
    81
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@823
    82
        URL r = l.getResource("archetype-resources/nbactions.xml");
jaroslav@823
    83
        assertNotNull(r, "Archetype nb file found");
jaroslav@823
    84
        
jaroslav@823
    85
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@823
    86
        XPathExpression xp2 = fact.newXPath().compile(
jaroslav@823
    87
            "//goal/text()"
jaroslav@823
    88
        );
jaroslav@823
    89
        
jaroslav@823
    90
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
jaroslav@823
    91
        NodeList goals = (NodeList) xp2.evaluate(dom, XPathConstants.NODESET);
jaroslav@823
    92
        
jaroslav@823
    93
        for (int i = 0; i < goals.getLength(); i++) {
jaroslav@823
    94
            String s = goals.item(i).getTextContent();
jaroslav@823
    95
            if (s.contains("bck2brwsr")) {
jaroslav@823
    96
                String[] arr = s.split(":");
jaroslav@823
    97
                assertEquals(arr.length, 4, "Three :");
jaroslav@823
    98
                assertEquals(arr[2], version, "Proper version is used");
jaroslav@823
    99
            }
jaroslav@823
   100
        }
jaroslav@823
   101
    }
jaroslav@817
   102
}