ko/archetype-test/src/test/java/org/apidesign/bck2brwsr/ko/archetype/test/VerifyArchetypeTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 09 Sep 2013 20:33:52 +0200
changeset 1275 913732a84aa3
parent 1273 37ad459579bc
child 1347 9cc0e1034f92
permissions -rw-r--r--
Verify all placeholders are substituted when building final ZIP distribution
     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.File;
    21 import java.io.IOException;
    22 import java.io.InputStream;
    23 import java.util.Properties;
    24 import java.util.zip.ZipEntry;
    25 import java.util.zip.ZipFile;
    26 import org.apache.maven.it.Verifier;
    27 import org.testng.annotations.Test;
    28 import static org.testng.Assert.*;
    29 import org.testng.reporters.Files;
    30 
    31 /**
    32  *
    33  * @author Jaroslav Tulach <jtulach@netbeans.org>
    34  */
    35 public class VerifyArchetypeTest {
    36     @Test public void fxBrwsrCompiles() throws Exception {
    37         final File dir = new File("target/tests/fxcompile/").getAbsoluteFile();
    38         generateFromArchetype(dir);
    39         
    40         File created = new File(dir, "o-a-test");
    41         assertTrue(created.isDirectory(), "Project created");
    42         assertTrue(new File(created, "pom.xml").isFile(), "Pom file is in there");
    43         
    44         Verifier v = new Verifier(created.getAbsolutePath());
    45         v.executeGoal("verify");
    46         
    47         v.verifyErrorFreeLog();
    48         
    49         for (String l : v.loadFile(v.getBasedir(), v.getLogFileName(), false)) {
    50             if (l.contains("j2js")) {
    51                 fail("No pre-compilaton:\n" + l);
    52             }
    53         }
    54         
    55         v.verifyTextInLog("org.apidesign.bck2brwsr.launcher.FXBrwsrLauncher");
    56         v.verifyTextInLog("fxcompile/o-a-test/target/o-a-test-1.0-SNAPSHOT-fxbrwsr.zip");
    57     }
    58     
    59     @Test public void bck2BrwsrCompiles() throws Exception {
    60         final File dir = new File("target/tests/b2bcompile/").getAbsoluteFile();
    61         generateFromArchetype(dir);
    62         
    63         File created = new File(dir, "o-a-test");
    64         assertTrue(created.isDirectory(), "Project created");
    65         assertTrue(new File(created, "pom.xml").isFile(), "Pom file is in there");
    66         
    67         Verifier v = new Verifier(created.getAbsolutePath());
    68         Properties sysProp = v.getSystemProperties();
    69         if (Boolean.getBoolean("java.awt.headless")) {
    70             sysProp.put("java.awt.headless", "true");
    71         }
    72         v.addCliOption("-Pbck2brwsr");
    73         v.executeGoal("verify");
    74         
    75         v.verifyErrorFreeLog();
    76         
    77         // does pre-compilation to JavaScript
    78         v.verifyTextInLog("j2js");
    79         // uses Bck2BrwsrLauncher
    80         v.verifyTextInLog("BaseHTTPLauncher showBrwsr");
    81         // building zip:
    82         v.verifyTextInLog("b2bcompile/o-a-test/target/o-a-test-1.0-SNAPSHOT-bck2brwsr.zip");
    83         
    84         for (String l : v.loadFile(v.getBasedir(), v.getLogFileName(), false)) {
    85             if (l.contains("fxbrwsr")) {
    86                 fail("No fxbrwsr:\n" + l);
    87             }
    88         }
    89 
    90         File zip = new File(new File(created, "target"), "o-a-test-1.0-SNAPSHOT-bck2brwsr.zip");
    91         assertTrue(zip.isFile(), "Zip file with website was created");
    92         
    93         ZipFile zf = new ZipFile(zip);
    94         final ZipEntry index = zf.getEntry("public_html/index.html");
    95         assertNotNull(index, "index.html found");
    96         
    97         String txt = readText(zf.getInputStream(index));
    98         final int beg = txt.indexOf("${");
    99         if (beg >= 0) {
   100             int end = txt.indexOf("}", beg);
   101             if (end < beg) {
   102                 end = txt.length();
   103             }
   104             fail("No substitutions in index.html. Found: " + txt.substring(beg, end));
   105         }
   106     }
   107 
   108     private Verifier generateFromArchetype(final File dir, String... params) throws Exception {
   109         Verifier v = new Verifier(dir.getAbsolutePath());
   110         v.setAutoclean(false);
   111         v.setLogFileName("generate.log");
   112         v.deleteDirectory("");
   113         dir.mkdirs();
   114         Properties sysProp = v.getSystemProperties();
   115         sysProp.put("groupId", "org.apidesign.test");
   116         sysProp.put("artifactId", "o-a-test");
   117         sysProp.put("package", "org.apidesign.test.oat");
   118         sysProp.put("archetypeGroupId", "org.apidesign.bck2brwsr");
   119         sysProp.put("archetypeArtifactId", "knockout4j-archetype");
   120         sysProp.put("archetypeVersion", ArchetypeVersionTest.findCurrentVersion());
   121         
   122         for (String p : params) {
   123             v.addCliOption(p);
   124         }
   125         v.executeGoal("archetype:generate");
   126         v.verifyErrorFreeLog();
   127         return v;
   128     }
   129     
   130     private static String readText(InputStream is) throws IOException {
   131         return Files.readFile(is);
   132     }
   133 }