javaquery/demo-calculator/src/test/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/MinifiedTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 11 Mar 2015 08:50:51 +0100
changeset 1809 72a0dbfa2ae8
parent 1787 ea12a3bb4b33
permissions -rw-r--r--
Can run from unpackaged directories. E.g. mvn clean install -DskipTests; mvn test works now on JDK8.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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.demo.calc.staticcompilation;
    19 
    20 import java.io.BufferedReader;
    21 import java.io.File;
    22 import java.io.InputStreamReader;
    23 import java.net.URL;
    24 import java.util.Enumeration;
    25 import java.util.Map;
    26 import java.util.jar.Attributes;
    27 import java.util.jar.Manifest;
    28 import org.testng.annotations.Test;
    29 import static org.testng.Assert.*;
    30 import org.testng.SkipException;
    31 
    32 public class MinifiedTest {
    33     @Test public void minifiedVersionDoesNotContainFQN() throws Exception {
    34         final ClassLoader l = MinifiedTest.class.getClassLoader();
    35         Enumeration<URL> en = l.getResources("META-INF/MANIFEST.MF");
    36         while (en.hasMoreElements()) {
    37             URL u = en.nextElement();
    38             Manifest mf = new Manifest(u.openStream());
    39             for (Map.Entry<String, Attributes> entrySet : mf.getEntries().entrySet()) {
    40                 String key = entrySet.getKey();
    41                 if (!key.contains("-min.js")) {
    42                     continue;
    43                 }
    44                 if (!key.contains("javaquery.api")) {
    45                     continue;
    46                 }
    47                 try (BufferedReader r = new BufferedReader(new InputStreamReader(l.getResourceAsStream(key)))) {
    48                     for (;;) {
    49                         String line = r.readLine();
    50                         if (line == null) {
    51                             break;
    52                         }
    53                         if (line.contains(" org_apidesign_bck2brwsr_htmlpage_Knockout")) {
    54                             fail("Found FQN of non-public class. Is it minified version?: " + line + " in " + key);
    55                         } 
    56                     }
    57                 }
    58                 // success
    59                 return;
    60             }
    61         }
    62         final String cp = System.getProperty("java.class.path");
    63         if (cp.contains("javaquery" + File.separatorChar + "api" + File.separatorChar + "target" + File.separatorChar + "classes")) {
    64             throw new SkipException("Classpath seems to contain classes directories, not JARs. Cannot test.\nCP: " + cp);
    65         }
    66         fail("No minified javaquery found: " + cp);
    67     }
    68 
    69     @Test public void debugVersionContainsFQN() throws Exception {
    70         final ClassLoader l = MinifiedTest.class.getClassLoader();
    71         Enumeration<URL> en = l.getResources("META-INF/MANIFEST.MF");
    72         while (en.hasMoreElements()) {
    73             URL u = en.nextElement();
    74             Manifest mf = new Manifest(u.openStream());
    75             for (Map.Entry<String, Attributes> entrySet : mf.getEntries().entrySet()) {
    76                 String key = entrySet.getKey();
    77                 if (!key.contains("-debug.js")) {
    78                     continue;
    79                 }
    80                 if (!key.contains("javaquery.api")) {
    81                     continue;
    82                 }
    83                 try (BufferedReader r = new BufferedReader(new InputStreamReader(l.getResourceAsStream(key)))) {
    84                     for (;;) {
    85                         String line = r.readLine();
    86                         if (line == null) {
    87                             break;
    88                         }
    89                         if (line.contains(" org_apidesign_bck2brwsr_htmlpage_Knockout")) {
    90                             // ok, it should be there
    91                             return;
    92                         } 
    93                     }
    94                 }
    95                 fail("Found no FQN of non-public Knockout class. Is it debug version?:" + key);
    96             }
    97         }
    98         final String cp = System.getProperty("java.class.path");
    99         if (cp.contains("javaquery" + File.separatorChar + "api" + File.separatorChar + "target" + File.separatorChar + "classes")) {
   100             throw new SkipException("Classpath seems to contain classes directories, not JARs. Cannot test.\nCP: " + cp);
   101         }
   102         fail("No debug javaquery found: " + cp);
   103     }
   104     
   105 }