ko/archetype-test/src/test/java/org/apidesign/bck2brwsr/ko/archetype/test/ArchetypeVersionTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 26 Jun 2013 19:29:54 +0200
branchclassloader
changeset 1228 462dd9238173
parent 1227 ko/archetype-test/src/test/java/org/apidesign/html/archetype/test/ArchetypeVersionTest.java@5a907f38608d
child 1236 b284b21de7a7
permissions -rw-r--r--
Cleaning the version mess a bit
jaroslav@1201
     1
/**
jaroslav@1227
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1227
     3
 * Copyright (C) 2012 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@1227
    12
 * GNU General Public License for more details.
jaroslav@1201
    13
 *
jaroslav@1201
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1201
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1227
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1201
    17
 */
jaroslav@1228
    18
package org.apidesign.bck2brwsr.ko.archetype.test;
jaroslav@1201
    19
jaroslav@1202
    20
import java.io.IOException;
jaroslav@1201
    21
import java.net.URL;
jaroslav@1201
    22
import javax.xml.XMLConstants;
jaroslav@1201
    23
import javax.xml.parsers.DocumentBuilderFactory;
jaroslav@1202
    24
import javax.xml.parsers.ParserConfigurationException;
jaroslav@1201
    25
import javax.xml.xpath.XPathConstants;
jaroslav@1201
    26
import javax.xml.xpath.XPathExpression;
jaroslav@1202
    27
import javax.xml.xpath.XPathExpressionException;
jaroslav@1201
    28
import javax.xml.xpath.XPathFactory;
jaroslav@1202
    29
import javax.xml.xpath.XPathFactoryConfigurationException;
jaroslav@1201
    30
import org.testng.annotations.Test;
jaroslav@1201
    31
import static org.testng.Assert.*;
jaroslav@1201
    32
import org.testng.annotations.BeforeClass;
jaroslav@1201
    33
import org.w3c.dom.Document;
jaroslav@1201
    34
import org.w3c.dom.NodeList;
jaroslav@1202
    35
import org.xml.sax.SAXException;
jaroslav@1201
    36
jaroslav@1201
    37
/**
jaroslav@1201
    38
 *
jaroslav@1201
    39
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@1201
    40
 */
jaroslav@1201
    41
public class ArchetypeVersionTest {
jaroslav@1201
    42
    private String version;
jaroslav@1201
    43
    
jaroslav@1201
    44
    public ArchetypeVersionTest() {
jaroslav@1201
    45
    }
jaroslav@1201
    46
    
jaroslav@1201
    47
    @BeforeClass public void readCurrentVersion() throws Exception {
jaroslav@1202
    48
        version = findCurrentVersion();
jaroslav@1201
    49
        assertFalse(version.isEmpty(), "There should be some version string");
jaroslav@1201
    50
    }
jaroslav@1201
    51
    
jaroslav@1201
    52
jaroslav@1201
    53
    @Test public void testComparePomDepsVersions() throws Exception {
jaroslav@1201
    54
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@1201
    55
        URL r = l.getResource("archetype-resources/pom.xml");
jaroslav@1201
    56
        assertNotNull(r, "Archetype pom found");
jaroslav@1201
    57
        
jaroslav@1201
    58
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@1201
    59
        XPathExpression xp2 = fact.newXPath().compile(
jaroslav@1201
    60
            "//properties/net.java.html.version/text()"
jaroslav@1201
    61
        );
jaroslav@1201
    62
        
jaroslav@1201
    63
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
jaroslav@1201
    64
        String arch = (String) xp2.evaluate(dom, XPathConstants.STRING);
jaroslav@1201
    65
jaroslav@1201
    66
        assertEquals(arch, version, "net.java.html.json dependency needs to be on latest version");
jaroslav@1201
    67
    }
jaroslav@1201
    68
    
jaroslav@1210
    69
    @Test public void testCheckLauncher() throws Exception {
jaroslav@1210
    70
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@1210
    71
        URL r = l.getResource("archetype-resources/pom.xml");
jaroslav@1210
    72
        assertNotNull(r, "Archetype pom found");
jaroslav@1210
    73
        
jaroslav@1210
    74
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@1210
    75
        XPathExpression xp2 = fact.newXPath().compile(
jaroslav@1210
    76
            "//properties/bck2brwsr.launcher.version/text()"
jaroslav@1210
    77
        );
jaroslav@1210
    78
        
jaroslav@1210
    79
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
jaroslav@1210
    80
        String arch = (String) xp2.evaluate(dom, XPathConstants.STRING);
jaroslav@1210
    81
jaroslav@1210
    82
        
jaroslav@1210
    83
        assertTrue(arch.matches("[0-9\\.]+"), "launcher version seems valid: " + arch);
jaroslav@1210
    84
    }
jaroslav@1210
    85
    
jaroslav@1210
    86
    @Test public void testCheckBck2Brwsr() throws Exception {
jaroslav@1210
    87
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@1210
    88
        URL r = l.getResource("archetype-resources/pom.xml");
jaroslav@1210
    89
        assertNotNull(r, "Archetype pom found");
jaroslav@1210
    90
        
jaroslav@1210
    91
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@1210
    92
        XPathExpression xp2 = fact.newXPath().compile(
jaroslav@1210
    93
            "//properties/bck2brwsr.version/text()"
jaroslav@1210
    94
        );
jaroslav@1210
    95
        
jaroslav@1210
    96
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
jaroslav@1210
    97
        String arch = (String) xp2.evaluate(dom, XPathConstants.STRING);
jaroslav@1210
    98
        
jaroslav@1210
    99
        assertTrue(arch.matches("[0-9\\.]+"), "bck2brwsr version seems valid: " + arch);
jaroslav@1210
   100
    }
jaroslav@1210
   101
    
jaroslav@1201
   102
    @Test public void testNbActions() throws Exception {
jaroslav@1201
   103
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@1201
   104
        URL r = l.getResource("archetype-resources/nbactions.xml");
jaroslav@1201
   105
        assertNotNull(r, "Archetype nb file found");
jaroslav@1201
   106
        
jaroslav@1201
   107
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@1201
   108
        XPathExpression xp2 = fact.newXPath().compile(
jaroslav@1201
   109
            "//goal/text()"
jaroslav@1201
   110
        );
jaroslav@1201
   111
        
jaroslav@1201
   112
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
jaroslav@1201
   113
        NodeList goals = (NodeList) xp2.evaluate(dom, XPathConstants.NODESET);
jaroslav@1201
   114
        
jaroslav@1201
   115
        for (int i = 0; i < goals.getLength(); i++) {
jaroslav@1201
   116
            String s = goals.item(i).getTextContent();
jaroslav@1201
   117
            if (s.contains("apidesign")) {
jaroslav@1201
   118
                assertFalse(s.matches(".*apidesign.*[0-9].*"), "No numbers: " + s);
jaroslav@1201
   119
            }
jaroslav@1201
   120
        }
jaroslav@1201
   121
    }
jaroslav@1202
   122
jaroslav@1202
   123
    static String findCurrentVersion() throws XPathExpressionException, IOException, ParserConfigurationException, SAXException, XPathFactoryConfigurationException {
jaroslav@1202
   124
        final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
jaroslav@1202
   125
        URL u = l.getResource("META-INF/maven/org.apidesign.html/knockout4j-archetype/pom.xml");
jaroslav@1202
   126
        assertNotNull(u, "Own pom found: " + System.getProperty("java.class.path"));
jaroslav@1202
   127
jaroslav@1202
   128
        final XPathFactory fact = XPathFactory.newInstance();
jaroslav@1202
   129
        fact.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
jaroslav@1202
   130
jaroslav@1202
   131
        XPathExpression xp = fact.newXPath().compile("project/version/text()");
jaroslav@1202
   132
        
jaroslav@1202
   133
        Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(u.openStream());
jaroslav@1202
   134
        return xp.evaluate(dom);
jaroslav@1202
   135
    }
jaroslav@1201
   136
}