ko-archetype/src/test/java/org/apidesign/html/archetype/ArchetypeVersionTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 13 May 2013 11:39:33 +0200
changeset 1201 b6fd8b9ccc7a
permissions -rw-r--r--
First sketch of the Knockout4J archetype
jaroslav@1201
     1
/**
jaroslav@1201
     2
 * HTML via Java(tm) Language Bindings
jaroslav@1201
     3
 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1201
     4
 *
jaroslav@1201
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1201
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1201
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1201
     8
 *
jaroslav@1201
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1201
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1201
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1201
    12
 * GNU General Public License for more details. apidesign.org
jaroslav@1201
    13
 * designates this particular file as subject to the
jaroslav@1201
    14
 * "Classpath" exception as provided by apidesign.org
jaroslav@1201
    15
 * in the License file that accompanied this code.
jaroslav@1201
    16
 *
jaroslav@1201
    17
 * You should have received a copy of the GNU General Public License
jaroslav@1201
    18
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1201
    19
 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
jaroslav@1201
    20
 */
jaroslav@1201
    21
package org.apidesign.html.archetype;
jaroslav@1201
    22
jaroslav@1201
    23
import java.net.URL;
jaroslav@1201
    24
import javax.xml.XMLConstants;
jaroslav@1201
    25
import javax.xml.parsers.DocumentBuilderFactory;
jaroslav@1201
    26
import javax.xml.xpath.XPathConstants;
jaroslav@1201
    27
import javax.xml.xpath.XPathExpression;
jaroslav@1201
    28
import javax.xml.xpath.XPathFactory;
jaroslav@1201
    29
import org.testng.annotations.Test;
jaroslav@1201
    30
import static org.testng.Assert.*;
jaroslav@1201
    31
import org.testng.annotations.BeforeClass;
jaroslav@1201
    32
import org.w3c.dom.Document;
jaroslav@1201
    33
import org.w3c.dom.NodeList;
jaroslav@1201
    34
jaroslav@1201
    35
/**
jaroslav@1201
    36
 *
jaroslav@1201
    37
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@1201
    38
 */
jaroslav@1201
    39
public class ArchetypeVersionTest {
jaroslav@1201
    40
    private String version;
jaroslav@1201
    41
    
jaroslav@1201
    42
    public ArchetypeVersionTest() {
jaroslav@1201
    43
    }
jaroslav@1201
    44
    
jaroslav@1201
    45
    @BeforeClass public void readCurrentVersion() throws Exception {
jaroslav@1201
    46
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@1201
    47
        URL u = l.getResource("META-INF/maven/org.apidesign.html/knockout4j-archetype/pom.xml");
jaroslav@1201
    48
        assertNotNull(u, "Own pom found: " + System.getProperty("java.class.path"));
jaroslav@1201
    49
jaroslav@1201
    50
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@1201
    51
        fact.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
jaroslav@1201
    52
jaroslav@1201
    53
        XPathExpression xp = fact.newXPath().compile("project/version/text()");
jaroslav@1201
    54
        
jaroslav@1201
    55
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(u.openStream());
jaroslav@1201
    56
        version = xp.evaluate(dom);
jaroslav@1201
    57
jaroslav@1201
    58
        assertFalse(version.isEmpty(), "There should be some version string");
jaroslav@1201
    59
    }
jaroslav@1201
    60
    
jaroslav@1201
    61
jaroslav@1201
    62
    @Test public void testComparePomDepsVersions() throws Exception {
jaroslav@1201
    63
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@1201
    64
        URL r = l.getResource("archetype-resources/pom.xml");
jaroslav@1201
    65
        assertNotNull(r, "Archetype pom found");
jaroslav@1201
    66
        
jaroslav@1201
    67
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@1201
    68
        XPathExpression xp2 = fact.newXPath().compile(
jaroslav@1201
    69
            "//properties/net.java.html.version/text()"
jaroslav@1201
    70
        );
jaroslav@1201
    71
        
jaroslav@1201
    72
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
jaroslav@1201
    73
        String arch = (String) xp2.evaluate(dom, XPathConstants.STRING);
jaroslav@1201
    74
jaroslav@1201
    75
        assertEquals(arch, version, "net.java.html.json dependency needs to be on latest version");
jaroslav@1201
    76
    }
jaroslav@1201
    77
    
jaroslav@1201
    78
    @Test public void testNbActions() throws Exception {
jaroslav@1201
    79
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@1201
    80
        URL r = l.getResource("archetype-resources/nbactions.xml");
jaroslav@1201
    81
        assertNotNull(r, "Archetype nb file found");
jaroslav@1201
    82
        
jaroslav@1201
    83
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@1201
    84
        XPathExpression xp2 = fact.newXPath().compile(
jaroslav@1201
    85
            "//goal/text()"
jaroslav@1201
    86
        );
jaroslav@1201
    87
        
jaroslav@1201
    88
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
jaroslav@1201
    89
        NodeList goals = (NodeList) xp2.evaluate(dom, XPathConstants.NODESET);
jaroslav@1201
    90
        
jaroslav@1201
    91
        for (int i = 0; i < goals.getLength(); i++) {
jaroslav@1201
    92
            String s = goals.item(i).getTextContent();
jaroslav@1201
    93
            if (s.contains("apidesign")) {
jaroslav@1201
    94
                assertFalse(s.matches(".*apidesign.*[0-9].*"), "No numbers: " + s);
jaroslav@1201
    95
            }
jaroslav@1201
    96
        }
jaroslav@1201
    97
    }
jaroslav@1201
    98
}