ko/archetype-test/src/test/java/org/apidesign/bck2brwsr/ko/archetype/test/ArchetypeVersionTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 29 Aug 2013 16:49:00 +0000
changeset 1255 3d8730a21c74
parent 1236 b284b21de7a7
child 1261 50c09cb0a3fb
permissions -rw-r--r--
The archetype now needs Throwable.printStackTrace() thus providing some trivial implementation
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.ko.archetype.test;
    19 
    20 import java.io.IOException;
    21 import java.net.URL;
    22 import javax.xml.XMLConstants;
    23 import javax.xml.parsers.DocumentBuilderFactory;
    24 import javax.xml.parsers.ParserConfigurationException;
    25 import javax.xml.xpath.XPathConstants;
    26 import javax.xml.xpath.XPathExpression;
    27 import javax.xml.xpath.XPathExpressionException;
    28 import javax.xml.xpath.XPathFactory;
    29 import javax.xml.xpath.XPathFactoryConfigurationException;
    30 import org.testng.annotations.Test;
    31 import static org.testng.Assert.*;
    32 import org.testng.annotations.BeforeClass;
    33 import org.w3c.dom.Document;
    34 import org.w3c.dom.NodeList;
    35 import org.xml.sax.SAXException;
    36 
    37 /**
    38  *
    39  * @author Jaroslav Tulach <jtulach@netbeans.org>
    40  */
    41 public class ArchetypeVersionTest {
    42     private String version;
    43     
    44     public ArchetypeVersionTest() {
    45     }
    46     
    47     @BeforeClass public void readCurrentVersion() throws Exception {
    48         version = findCurrentVersion();
    49         assertFalse(version.isEmpty(), "There should be some version string");
    50     }
    51     
    52 
    53     @Test public void testComparePomDepsVersions() throws Exception {
    54         final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
    55         URL r = l.getResource("archetype-resources/pom.xml");
    56         assertNotNull(r, "Archetype pom found");
    57         
    58         final XPathFactory fact = XPathFactory.newInstance();
    59         XPathExpression xp2 = fact.newXPath().compile(
    60             "//properties/net.java.html.version/text()"
    61         );
    62         
    63         Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
    64         String arch = (String) xp2.evaluate(dom, XPathConstants.STRING);
    65         
    66         int snapshot = arch.indexOf("-SNAPSHOT");
    67         assertEquals(snapshot, -1, "Don't depend on snapshots: " + arch);
    68 
    69         assertTrue(arch.matches("[0-9\\.]+"), "net.java.html.json version seems valid: " + arch);
    70     }
    71     
    72     @Test public void testCheckLauncher() throws Exception {
    73         final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
    74         URL r = l.getResource("archetype-resources/pom.xml");
    75         assertNotNull(r, "Archetype pom found");
    76         
    77         final XPathFactory fact = XPathFactory.newInstance();
    78         XPathExpression xp2 = fact.newXPath().compile(
    79             "//properties/bck2brwsr.launcher.version/text()"
    80         );
    81         
    82         Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
    83         String arch = (String) xp2.evaluate(dom, XPathConstants.STRING);
    84 
    85         assertEquals(arch, version, "launcher dependency is on more recent version");
    86     }
    87     
    88     @Test public void testCheckBck2Brwsr() throws Exception {
    89         final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
    90         URL r = l.getResource("archetype-resources/pom.xml");
    91         assertNotNull(r, "Archetype pom found");
    92         
    93         final XPathFactory fact = XPathFactory.newInstance();
    94         XPathExpression xp2 = fact.newXPath().compile(
    95             "//properties/bck2brwsr.version/text()"
    96         );
    97         
    98         Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
    99         String arch = (String) xp2.evaluate(dom, XPathConstants.STRING);
   100         
   101         assertEquals(arch, version, "bck2brwsr dependency is on more recent version");
   102     }
   103     
   104     @Test public void testNbActions() throws Exception {
   105         final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
   106         URL r = l.getResource("archetype-resources/nbactions.xml");
   107         assertNotNull(r, "Archetype nb file found");
   108         
   109         final XPathFactory fact = XPathFactory.newInstance();
   110         XPathExpression xp2 = fact.newXPath().compile(
   111             "//goal/text()"
   112         );
   113         
   114         Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openStream());
   115         NodeList goals = (NodeList) xp2.evaluate(dom, XPathConstants.NODESET);
   116         
   117         for (int i = 0; i < goals.getLength(); i++) {
   118             String s = goals.item(i).getTextContent();
   119             if (s.contains("apidesign")) {
   120                 assertFalse(s.matches(".*apidesign.*[0-9].*"), "No numbers: " + s);
   121             }
   122         }
   123     }
   124 
   125     static String findCurrentVersion() throws XPathExpressionException, IOException, ParserConfigurationException, SAXException, XPathFactoryConfigurationException {
   126         final ClassLoader l = ArchetypeVersionTest.class.getClassLoader();
   127         URL u = l.getResource("META-INF/maven/org.apidesign.bck2brwsr/knockout4j-archetype/pom.xml");
   128         assertNotNull(u, "Own pom found: " + System.getProperty("java.class.path"));
   129 
   130         final XPathFactory fact = XPathFactory.newInstance();
   131         fact.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
   132 
   133         XPathExpression xp = fact.newXPath().compile("project/version/text()");
   134         
   135         Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(u.openStream());
   136         return xp.evaluate(dom);
   137     }
   138 }