boot/src/test/java/org/apidesign/html/boot/impl/Compile.java
branchnetbeans
changeset 362 92fb71afdc0e
parent 361 700087d2a5d3
child 364 2739565c7a45
     1.1 --- a/boot/src/test/java/org/apidesign/html/boot/impl/Compile.java	Mon Dec 16 15:48:09 2013 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,291 +0,0 @@
     1.4 -/**
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
     1.8 - *
     1.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    1.10 - * Other names may be trademarks of their respective owners.
    1.11 - *
    1.12 - * The contents of this file are subject to the terms of either the GNU
    1.13 - * General Public License Version 2 only ("GPL") or the Common
    1.14 - * Development and Distribution License("CDDL") (collectively, the
    1.15 - * "License"). You may not use this file except in compliance with the
    1.16 - * License. You can obtain a copy of the License at
    1.17 - * http://www.netbeans.org/cddl-gplv2.html
    1.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.19 - * specific language governing permissions and limitations under the
    1.20 - * License.  When distributing the software, include this License Header
    1.21 - * Notice in each file and include the License file at
    1.22 - * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    1.23 - * particular file as subject to the "Classpath" exception as provided
    1.24 - * by Oracle in the GPL Version 2 section of the License file that
    1.25 - * accompanied this code. If applicable, add the following below the
    1.26 - * License Header, with the fields enclosed by brackets [] replaced by
    1.27 - * your own identifying information:
    1.28 - * "Portions Copyrighted [year] [name of copyright owner]"
    1.29 - *
    1.30 - * Contributor(s):
    1.31 - *
    1.32 - * The Original Software is NetBeans. The Initial Developer of the Original
    1.33 - * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
    1.34 - *
    1.35 - * If you wish your version of this file to be governed by only the CDDL
    1.36 - * or only the GPL Version 2, indicate your decision by adding
    1.37 - * "[Contributor] elects to include this software in this distribution
    1.38 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.39 - * single choice of license, a recipient has the option to distribute
    1.40 - * your version of this file under either the CDDL, the GPL Version 2 or
    1.41 - * to extend the choice of license to its licensees as provided above.
    1.42 - * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.43 - * Version 2 license, then the option applies only if the new code is
    1.44 - * made subject to such option by the copyright holder.
    1.45 - */
    1.46 -package org.apidesign.html.boot.impl;
    1.47 -
    1.48 -import java.io.ByteArrayInputStream;
    1.49 -import java.io.ByteArrayOutputStream;
    1.50 -import java.io.IOException;
    1.51 -import java.io.InputStream;
    1.52 -import java.io.OutputStream;
    1.53 -import java.net.URI;
    1.54 -import java.net.URISyntaxException;
    1.55 -import java.util.ArrayList;
    1.56 -import java.util.Arrays;
    1.57 -import java.util.HashMap;
    1.58 -import java.util.List;
    1.59 -import java.util.Locale;
    1.60 -import java.util.Map;
    1.61 -import java.util.regex.Matcher;
    1.62 -import java.util.regex.Pattern;
    1.63 -import javax.tools.Diagnostic;
    1.64 -import javax.tools.DiagnosticListener;
    1.65 -import javax.tools.FileObject;
    1.66 -import javax.tools.ForwardingJavaFileManager;
    1.67 -import javax.tools.JavaFileManager;
    1.68 -import javax.tools.JavaFileObject;
    1.69 -import javax.tools.JavaFileObject.Kind;
    1.70 -import javax.tools.SimpleJavaFileObject;
    1.71 -import javax.tools.StandardJavaFileManager;
    1.72 -import javax.tools.StandardLocation;
    1.73 -import javax.tools.ToolProvider;
    1.74 -import static org.testng.Assert.assertTrue;
    1.75 -import static org.testng.Assert.assertFalse;
    1.76 -import static org.testng.Assert.fail;
    1.77 -
    1.78 -/**
    1.79 - *
    1.80 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.81 - */
    1.82 -final class Compile implements DiagnosticListener<JavaFileObject> {
    1.83 -    private final List<Diagnostic<? extends JavaFileObject>> errors = 
    1.84 -            new ArrayList<Diagnostic<? extends JavaFileObject>>();
    1.85 -    private final Map<String, byte[]> classes;
    1.86 -    private final String pkg;
    1.87 -    private final String cls;
    1.88 -    private final String html;
    1.89 -    private final String sourceLevel;
    1.90 -
    1.91 -    private Compile(String html, String code, String sl) throws IOException {
    1.92 -        this.pkg = findPkg(code);
    1.93 -        this.cls = findCls(code);
    1.94 -        this.html = html;
    1.95 -        this.sourceLevel = sl;
    1.96 -        classes = compile(html, code);
    1.97 -    }
    1.98 -
    1.99 -    /** Performs compilation of given HTML page and associated Java code
   1.100 -     */
   1.101 -    public static Compile create(String html, String code) throws IOException {
   1.102 -        return create(html, code, "1.7");
   1.103 -    }
   1.104 -    static Compile create(String html, String code, String sourceLevel) throws IOException {
   1.105 -        return new Compile(html, code, sourceLevel);
   1.106 -    }
   1.107 -    
   1.108 -    /** Checks for given class among compiled resources */
   1.109 -    public byte[] get(String res) {
   1.110 -        return classes.get(res);
   1.111 -    }
   1.112 -    
   1.113 -    /** Obtains errors created during compilation.
   1.114 -     */
   1.115 -    public List<Diagnostic<? extends JavaFileObject>> getErrors() {
   1.116 -        List<Diagnostic<? extends JavaFileObject>> err;
   1.117 -        err = new ArrayList<Diagnostic<? extends JavaFileObject>>();
   1.118 -        for (Diagnostic<? extends JavaFileObject> diagnostic : errors) {
   1.119 -            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   1.120 -                err.add(diagnostic);
   1.121 -            }
   1.122 -        }
   1.123 -        return err;
   1.124 -    }
   1.125 -    
   1.126 -    private Map<String, byte[]> compile(final String html, final String code) throws IOException {
   1.127 -        StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(this, null, null);
   1.128 -
   1.129 -        final Map<String, ByteArrayOutputStream> class2BAOS;
   1.130 -        class2BAOS = new HashMap<String, ByteArrayOutputStream>();
   1.131 -
   1.132 -        JavaFileObject file = new SimpleJavaFileObject(URI.create("mem://mem"), Kind.SOURCE) {
   1.133 -            @Override
   1.134 -            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.135 -                return code;
   1.136 -            }
   1.137 -        };
   1.138 -        final JavaFileObject htmlFile = new SimpleJavaFileObject(URI.create("mem://mem2"), Kind.OTHER) {
   1.139 -            @Override
   1.140 -            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.141 -                return html;
   1.142 -            }
   1.143 -
   1.144 -            @Override
   1.145 -            public InputStream openInputStream() throws IOException {
   1.146 -                return new ByteArrayInputStream(html.getBytes());
   1.147 -            }
   1.148 -        };
   1.149 -        
   1.150 -        final URI scratch;
   1.151 -        try {
   1.152 -            scratch = new URI("mem://mem3");
   1.153 -        } catch (URISyntaxException ex) {
   1.154 -            throw new IOException(ex);
   1.155 -        }
   1.156 -        
   1.157 -        JavaFileManager jfm = new ForwardingJavaFileManager<JavaFileManager>(sjfm) {
   1.158 -            @Override
   1.159 -            public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
   1.160 -                if (kind  == Kind.CLASS) {
   1.161 -                    final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
   1.162 -
   1.163 -                    class2BAOS.put(className.replace('.', '/') + ".class", buffer);
   1.164 -                    return new SimpleJavaFileObject(sibling.toUri(), kind) {
   1.165 -                        @Override
   1.166 -                        public OutputStream openOutputStream() throws IOException {
   1.167 -                            return buffer;
   1.168 -                        }
   1.169 -                    };
   1.170 -                }
   1.171 -                
   1.172 -                if (kind == Kind.SOURCE) {
   1.173 -                    final String n = className.replace('.', '/') + ".java";
   1.174 -                    final URI un;
   1.175 -                    try {
   1.176 -                        un = new URI("mem://" + n);
   1.177 -                    } catch (URISyntaxException ex) {
   1.178 -                        throw new IOException(ex);
   1.179 -                    }
   1.180 -                    return new VirtFO(un/*sibling.toUri()*/, kind, n);
   1.181 -                }
   1.182 -                
   1.183 -                throw new IllegalStateException();
   1.184 -            }
   1.185 -
   1.186 -            @Override
   1.187 -            public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
   1.188 -                if (location == StandardLocation.SOURCE_PATH) {
   1.189 -                    if (packageName.equals(pkg)) {
   1.190 -                        return htmlFile;
   1.191 -                    }
   1.192 -                }
   1.193 -                
   1.194 -                return null;
   1.195 -            }
   1.196 -
   1.197 -            @Override
   1.198 -            public boolean isSameFile(FileObject a, FileObject b) {
   1.199 -                if (a instanceof VirtFO && b instanceof VirtFO) {
   1.200 -                    return ((VirtFO)a).getName().equals(((VirtFO)b).getName());
   1.201 -                }
   1.202 -                
   1.203 -                return super.isSameFile(a, b);
   1.204 -            }
   1.205 -
   1.206 -            class VirtFO extends SimpleJavaFileObject {
   1.207 -
   1.208 -                private final String n;
   1.209 -
   1.210 -                public VirtFO(URI uri, Kind kind, String n) {
   1.211 -                    super(uri, kind);
   1.212 -                    this.n = n;
   1.213 -                }
   1.214 -                private final ByteArrayOutputStream data = new ByteArrayOutputStream();
   1.215 -
   1.216 -                @Override
   1.217 -                public OutputStream openOutputStream() throws IOException {
   1.218 -                    return data;
   1.219 -                }
   1.220 -
   1.221 -                @Override
   1.222 -                public String getName() {
   1.223 -                    return n;
   1.224 -                }
   1.225 -
   1.226 -                @Override
   1.227 -                public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.228 -                    data.close();
   1.229 -                    return new String(data.toByteArray());
   1.230 -                }
   1.231 -            }
   1.232 -        };
   1.233 -
   1.234 -        ToolProvider.getSystemJavaCompiler().getTask(null, jfm, this, /*XXX:*/Arrays.asList("-source", sourceLevel, "-target", "1.7"), null, Arrays.asList(file)).call();
   1.235 -
   1.236 -        Map<String, byte[]> result = new HashMap<String, byte[]>();
   1.237 -
   1.238 -        for (Map.Entry<String, ByteArrayOutputStream> e : class2BAOS.entrySet()) {
   1.239 -            result.put(e.getKey(), e.getValue().toByteArray());
   1.240 -        }
   1.241 -
   1.242 -        return result;
   1.243 -    }
   1.244 -
   1.245 -
   1.246 -    @Override
   1.247 -    public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.248 -        errors.add(diagnostic);
   1.249 -    }
   1.250 -    private static String findPkg(String java) throws IOException {
   1.251 -        Pattern p = Pattern.compile("package\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}*;", Pattern.MULTILINE);
   1.252 -        Matcher m = p.matcher(java);
   1.253 -        if (!m.find()) {
   1.254 -            throw new IOException("Can't find package declaration in the java file");
   1.255 -        }
   1.256 -        String pkg = m.group(1);
   1.257 -        return pkg;
   1.258 -    }
   1.259 -    private static String findCls(String java) throws IOException {
   1.260 -        Pattern p = Pattern.compile("class\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}", Pattern.MULTILINE);
   1.261 -        Matcher m = p.matcher(java);
   1.262 -        if (!m.find()) {
   1.263 -            throw new IOException("Can't find package declaration in the java file");
   1.264 -        }
   1.265 -        String cls = m.group(1);
   1.266 -        return cls;
   1.267 -    }
   1.268 -
   1.269 -    String getHtml() {
   1.270 -        String fqn = "'" + pkg + '.' + cls + "'";
   1.271 -        return html.replace("'${fqn}'", fqn);
   1.272 -    }
   1.273 -    void assertErrors() {
   1.274 -        assertFalse(getErrors().isEmpty(), "There are supposed to be some errors");
   1.275 -    }
   1.276 -
   1.277 -    void assertError(String expMsg) {
   1.278 -        StringBuilder sb = new StringBuilder();
   1.279 -        sb.append("Can't find ").append(expMsg).append(" among:");
   1.280 -        for (Diagnostic<? extends JavaFileObject> e : errors) {
   1.281 -            String msg = e.getMessage(Locale.US);
   1.282 -            if (msg.contains(expMsg)) {
   1.283 -                return;
   1.284 -            }
   1.285 -            sb.append("\n");
   1.286 -            sb.append(msg);
   1.287 -        }
   1.288 -        fail(sb.toString());
   1.289 -    }
   1.290 -
   1.291 -    void assertNoErrors() {
   1.292 -        assertTrue(getErrors().isEmpty(), "No errors expected: " + getErrors());
   1.293 -    }
   1.294 -}