ko-archetype-test/src/test/java/org/apidesign/html/archetype/test/ArchetypeVersionTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 15 May 2013 10:45:23 +0200
changeset 1210 82300c4f5c54
parent 1202 5f04bdbc6ee1
permissions -rw-r--r--
Use special version for the launcher
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@1210
    72
    @Test public void testCheckLauncher() throws Exception {
jaroslav@1210
    73
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@1210
    74
        URL r = l.getResource("archetype-resources/pom.xml");
jaroslav@1210
    75
        assertNotNull(r, "Archetype pom found");
jaroslav@1210
    76
        
jaroslav@1210
    77
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@1210
    78
        XPathExpression xp2 = fact.newXPath().compile(
jaroslav@1210
    79
            "//properties/bck2brwsr.launcher.version/text()"
jaroslav@1210
    80
        );
jaroslav@1210
    81
        
jaroslav@1210
    82
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
jaroslav@1210
    83
        String arch = (String) xp2.evaluate(dom, XPathConstants.STRING);
jaroslav@1210
    84
jaroslav@1210
    85
        
jaroslav@1210
    86
        assertTrue(arch.matches("[0-9\\.]+"), "launcher version seems valid: " + arch);
jaroslav@1210
    87
    }
jaroslav@1210
    88
    
jaroslav@1210
    89
    @Test public void testCheckBck2Brwsr() throws Exception {
jaroslav@1210
    90
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@1210
    91
        URL r = l.getResource("archetype-resources/pom.xml");
jaroslav@1210
    92
        assertNotNull(r, "Archetype pom found");
jaroslav@1210
    93
        
jaroslav@1210
    94
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@1210
    95
        XPathExpression xp2 = fact.newXPath().compile(
jaroslav@1210
    96
            "//properties/bck2brwsr.version/text()"
jaroslav@1210
    97
        );
jaroslav@1210
    98
        
jaroslav@1210
    99
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
jaroslav@1210
   100
        String arch = (String) xp2.evaluate(dom, XPathConstants.STRING);
jaroslav@1210
   101
        
jaroslav@1210
   102
        assertTrue(arch.matches("[0-9\\.]+"), "bck2brwsr version seems valid: " + arch);
jaroslav@1210
   103
    }
jaroslav@1210
   104
    
jaroslav@1201
   105
    @Test public void testNbActions() throws Exception {
jaroslav@1201
   106
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@1201
   107
        URL r = l.getResource("archetype-resources/nbactions.xml");
jaroslav@1201
   108
        assertNotNull(r, "Archetype nb file found");
jaroslav@1201
   109
        
jaroslav@1201
   110
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@1201
   111
        XPathExpression xp2 = fact.newXPath().compile(
jaroslav@1201
   112
            "//goal/text()"
jaroslav@1201
   113
        );
jaroslav@1201
   114
        
jaroslav@1201
   115
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
jaroslav@1201
   116
        NodeList goals = (NodeList) xp2.evaluate(dom, XPathConstants.NODESET);
jaroslav@1201
   117
        
jaroslav@1201
   118
        for (int i = 0; i < goals.getLength(); i++) {
jaroslav@1201
   119
            String s = goals.item(i).getTextContent();
jaroslav@1201
   120
            if (s.contains("apidesign")) {
jaroslav@1201
   121
                assertFalse(s.matches(".*apidesign.*[0-9].*"), "No numbers: " + s);
jaroslav@1201
   122
            }
jaroslav@1201
   123
        }
jaroslav@1201
   124
    }
jaroslav@1202
   125
jaroslav@1202
   126
    static String findCurrentVersion() throws XPathExpressionException, IOException, ParserConfigurationException, SAXException, XPathFactoryConfigurationException {
jaroslav@1202
   127
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@1202
   128
        URL u = l.getResource("META-INF/maven/org.apidesign.html/knockout4j-archetype/pom.xml");
jaroslav@1202
   129
        assertNotNull(u, "Own pom found: " + System.getProperty("java.class.path"));
jaroslav@1202
   130
jaroslav@1202
   131
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@1202
   132
        fact.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
jaroslav@1202
   133
jaroslav@1202
   134
        XPathExpression xp = fact.newXPath().compile("project/version/text()");
jaroslav@1202
   135
        
jaroslav@1202
   136
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(u.openStream());
jaroslav@1202
   137
        return xp.evaluate(dom);
jaroslav@1202
   138
    }
jaroslav@1201
   139
}