ko-archetype-test/src/test/java/org/apidesign/html/archetype/test/ArchetypeVersionTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 13 May 2013 14:25:37 +0200
changeset 1202 5f04bdbc6ee1
parent 1201 ko-archetype/src/test/java/org/apidesign/html/archetype/ArchetypeVersionTest.java@b6fd8b9ccc7a
child 1210 82300c4f5c54
permissions -rw-r--r--
Moving archetype tests into separate module - at that moment it is possible to use the previously generated 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@1202
    21
package org.apidesign.html.archetype.test;
jaroslav@1201
    22
jaroslav@1202
    23
import java.io.IOException;
jaroslav@1201
    24
import java.net.URL;
jaroslav@1201
    25
import javax.xml.XMLConstants;
jaroslav@1201
    26
import javax.xml.parsers.DocumentBuilderFactory;
jaroslav@1202
    27
import javax.xml.parsers.ParserConfigurationException;
jaroslav@1201
    28
import javax.xml.xpath.XPathConstants;
jaroslav@1201
    29
import javax.xml.xpath.XPathExpression;
jaroslav@1202
    30
import javax.xml.xpath.XPathExpressionException;
jaroslav@1201
    31
import javax.xml.xpath.XPathFactory;
jaroslav@1202
    32
import javax.xml.xpath.XPathFactoryConfigurationException;
jaroslav@1201
    33
import org.testng.annotations.Test;
jaroslav@1201
    34
import static org.testng.Assert.*;
jaroslav@1201
    35
import org.testng.annotations.BeforeClass;
jaroslav@1201
    36
import org.w3c.dom.Document;
jaroslav@1201
    37
import org.w3c.dom.NodeList;
jaroslav@1202
    38
import org.xml.sax.SAXException;
jaroslav@1201
    39
jaroslav@1201
    40
/**
jaroslav@1201
    41
 *
jaroslav@1201
    42
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@1201
    43
 */
jaroslav@1201
    44
public class ArchetypeVersionTest {
jaroslav@1201
    45
    private String version;
jaroslav@1201
    46
    
jaroslav@1201
    47
    public ArchetypeVersionTest() {
jaroslav@1201
    48
    }
jaroslav@1201
    49
    
jaroslav@1201
    50
    @BeforeClass public void readCurrentVersion() throws Exception {
jaroslav@1202
    51
        version = findCurrentVersion();
jaroslav@1201
    52
        assertFalse(version.isEmpty(), "There should be some version string");
jaroslav@1201
    53
    }
jaroslav@1201
    54
    
jaroslav@1201
    55
jaroslav@1201
    56
    @Test public void testComparePomDepsVersions() throws Exception {
jaroslav@1201
    57
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@1201
    58
        URL r = l.getResource("archetype-resources/pom.xml");
jaroslav@1201
    59
        assertNotNull(r, "Archetype pom found");
jaroslav@1201
    60
        
jaroslav@1201
    61
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@1201
    62
        XPathExpression xp2 = fact.newXPath().compile(
jaroslav@1201
    63
            "//properties/net.java.html.version/text()"
jaroslav@1201
    64
        );
jaroslav@1201
    65
        
jaroslav@1201
    66
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
jaroslav@1201
    67
        String arch = (String) xp2.evaluate(dom, XPathConstants.STRING);
jaroslav@1201
    68
jaroslav@1201
    69
        assertEquals(arch, version, "net.java.html.json dependency needs to be on latest version");
jaroslav@1201
    70
    }
jaroslav@1201
    71
    
jaroslav@1201
    72
    @Test public void testNbActions() throws Exception {
jaroslav@1201
    73
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@1201
    74
        URL r = l.getResource("archetype-resources/nbactions.xml");
jaroslav@1201
    75
        assertNotNull(r, "Archetype nb file found");
jaroslav@1201
    76
        
jaroslav@1201
    77
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@1201
    78
        XPathExpression xp2 = fact.newXPath().compile(
jaroslav@1201
    79
            "//goal/text()"
jaroslav@1201
    80
        );
jaroslav@1201
    81
        
jaroslav@1201
    82
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
jaroslav@1201
    83
        NodeList goals = (NodeList) xp2.evaluate(dom, XPathConstants.NODESET);
jaroslav@1201
    84
        
jaroslav@1201
    85
        for (int i = 0; i < goals.getLength(); i++) {
jaroslav@1201
    86
            String s = goals.item(i).getTextContent();
jaroslav@1201
    87
            if (s.contains("apidesign")) {
jaroslav@1201
    88
                assertFalse(s.matches(".*apidesign.*[0-9].*"), "No numbers: " + s);
jaroslav@1201
    89
            }
jaroslav@1201
    90
        }
jaroslav@1201
    91
    }
jaroslav@1202
    92
jaroslav@1202
    93
    static String findCurrentVersion() throws XPathExpressionException, IOException, ParserConfigurationException, SAXException, XPathFactoryConfigurationException {
jaroslav@1202
    94
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@1202
    95
        URL u = l.getResource("META-INF/maven/org.apidesign.html/knockout4j-archetype/pom.xml");
jaroslav@1202
    96
        assertNotNull(u, "Own pom found: " + System.getProperty("java.class.path"));
jaroslav@1202
    97
jaroslav@1202
    98
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@1202
    99
        fact.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
jaroslav@1202
   100
jaroslav@1202
   101
        XPathExpression xp = fact.newXPath().compile("project/version/text()");
jaroslav@1202
   102
        
jaroslav@1202
   103
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(u.openStream());
jaroslav@1202
   104
        return xp.evaluate(dom);
jaroslav@1202
   105
    }
jaroslav@1201
   106
}