rt/archetype/src/test/java/org/apidesign/bck2brwsr/archetype/ArchetypeVersionTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 22 Mar 2013 00:02:21 +0100
changeset 870 448bed1f6d5a
parent 823 rt/mojo/src/test/java/org/apidesign/bck2brwsr/mojo/ArchetypeVersionTest.java@1e9d34a337f2
child 1008 ad7b9ae807a1
permissions -rw-r--r--
Separating the archetype into its own module - to give it better name among archetypes
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@870
    18
package org.apidesign.bck2brwsr.archetype;
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@870
    45
        URL u = l.getResource("META-INF/maven/org.apidesign.bck2brwsr/bck2brwsr-archetype-html-sample/pom.xml");
jaroslav@870
    46
        assertNotNull(u, "Own pom found: " + System.getProperty("java.class.path"));
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@870
    51
        XPathExpression xp = fact.newXPath().compile("project/version/text()");
jaroslav@870
    52
        
jaroslav@870
    53
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(u.openStream());
jaroslav@870
    54
        version = xp.evaluate(dom);
jaroslav@823
    55
jaroslav@817
    56
        assertFalse(version.isEmpty(), "There should be some version string");
jaroslav@823
    57
    }
jaroslav@823
    58
    
jaroslav@823
    59
jaroslav@823
    60
    @Test public void testComparePomDepsVersions() throws Exception {
jaroslav@823
    61
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@817
    62
        URL r = l.getResource("archetype-resources/pom.xml");
jaroslav@817
    63
        assertNotNull(r, "Archetype pom found");
jaroslav@817
    64
        
jaroslav@823
    65
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@817
    66
        XPathExpression xp2 = fact.newXPath().compile(
jaroslav@817
    67
            "//version[../groupId/text() = 'org.apidesign.bck2brwsr']/text()"
jaroslav@817
    68
        );
jaroslav@817
    69
        
jaroslav@817
    70
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
jaroslav@817
    71
        NodeList arch = (NodeList) xp2.evaluate(dom, XPathConstants.NODESET);
jaroslav@817
    72
jaroslav@817
    73
        if (arch.getLength() < 3) {
jaroslav@817
    74
            fail("There should be at least three dependencies to bck2brwsr APIs: " + arch.getLength());
jaroslav@817
    75
        }
jaroslav@817
    76
        
jaroslav@817
    77
        for (int i = 0; i < arch.getLength(); i++) {
jaroslav@817
    78
            assertEquals(arch.item(i).getTextContent(), version, i + "th dependency needs to be on latest version of bck2brwsr");
jaroslav@817
    79
        }
jaroslav@817
    80
    }
jaroslav@823
    81
    
jaroslav@823
    82
    @Test public void testNbActions() throws Exception {
jaroslav@823
    83
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@823
    84
        URL r = l.getResource("archetype-resources/nbactions.xml");
jaroslav@823
    85
        assertNotNull(r, "Archetype nb file found");
jaroslav@823
    86
        
jaroslav@823
    87
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@823
    88
        XPathExpression xp2 = fact.newXPath().compile(
jaroslav@823
    89
            "//goal/text()"
jaroslav@823
    90
        );
jaroslav@823
    91
        
jaroslav@823
    92
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
jaroslav@823
    93
        NodeList goals = (NodeList) xp2.evaluate(dom, XPathConstants.NODESET);
jaroslav@823
    94
        
jaroslav@823
    95
        for (int i = 0; i < goals.getLength(); i++) {
jaroslav@823
    96
            String s = goals.item(i).getTextContent();
jaroslav@823
    97
            if (s.contains("bck2brwsr")) {
jaroslav@823
    98
                String[] arr = s.split(":");
jaroslav@823
    99
                assertEquals(arr.length, 4, "Three :");
jaroslav@823
   100
                assertEquals(arr[2], version, "Proper version is used");
jaroslav@823
   101
            }
jaroslav@823
   102
        }
jaroslav@823
   103
    }
jaroslav@817
   104
}