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