geo/src/test/java/org/netbeans/html/geo/impl/Compile.java
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 80702021b851
child 365 5c93ad8c7a15
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/geo/src/test/java/org/netbeans/html/geo/impl/Compile.java	Mon Dec 16 16:59:43 2013 +0100
     1.3 @@ -0,0 +1,286 @@
     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.netbeans.html.geo.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.*;
    1.75 +
    1.76 +/**
    1.77 + *
    1.78 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.79 + */
    1.80 +final class Compile implements DiagnosticListener<JavaFileObject> {
    1.81 +    private final List<Diagnostic<? extends JavaFileObject>> errors = 
    1.82 +            new ArrayList<Diagnostic<? extends JavaFileObject>>();
    1.83 +    private final Map<String, byte[]> classes;
    1.84 +    private final String pkg;
    1.85 +    private final String cls;
    1.86 +    private final String html;
    1.87 +    private final String sourceLevel;
    1.88 +
    1.89 +    private Compile(String html, String code, String sl) throws IOException {
    1.90 +        this.pkg = findPkg(code);
    1.91 +        this.cls = findCls(code);
    1.92 +        this.html = html;
    1.93 +        this.sourceLevel = sl;
    1.94 +        classes = compile(html, code);
    1.95 +    }
    1.96 +
    1.97 +    /** Performs compilation of given HTML page and associated Java code
    1.98 +     */
    1.99 +    public static Compile create(String html, String code) throws IOException {
   1.100 +        return create(html, code, "1.7");
   1.101 +    }
   1.102 +    static Compile create(String html, String code, String sourceLevel) throws IOException {
   1.103 +        return new Compile(html, code, sourceLevel);
   1.104 +    }
   1.105 +    
   1.106 +    /** Checks for given class among compiled resources */
   1.107 +    public byte[] get(String res) {
   1.108 +        return classes.get(res);
   1.109 +    }
   1.110 +    
   1.111 +    /** Obtains errors created during compilation.
   1.112 +     */
   1.113 +    public List<Diagnostic<? extends JavaFileObject>> getErrors() {
   1.114 +        List<Diagnostic<? extends JavaFileObject>> err;
   1.115 +        err = new ArrayList<Diagnostic<? extends JavaFileObject>>();
   1.116 +        for (Diagnostic<? extends JavaFileObject> diagnostic : errors) {
   1.117 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   1.118 +                err.add(diagnostic);
   1.119 +            }
   1.120 +        }
   1.121 +        return err;
   1.122 +    }
   1.123 +    
   1.124 +    private Map<String, byte[]> compile(final String html, final String code) throws IOException {
   1.125 +        StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(this, null, null);
   1.126 +
   1.127 +        final Map<String, ByteArrayOutputStream> class2BAOS;
   1.128 +        class2BAOS = new HashMap<String, ByteArrayOutputStream>();
   1.129 +
   1.130 +        JavaFileObject file = new SimpleJavaFileObject(URI.create("mem://mem"), Kind.SOURCE) {
   1.131 +            @Override
   1.132 +            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.133 +                return code;
   1.134 +            }
   1.135 +        };
   1.136 +        final JavaFileObject htmlFile = new SimpleJavaFileObject(URI.create("mem://mem2"), Kind.OTHER) {
   1.137 +            @Override
   1.138 +            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.139 +                return html;
   1.140 +            }
   1.141 +
   1.142 +            @Override
   1.143 +            public InputStream openInputStream() throws IOException {
   1.144 +                return new ByteArrayInputStream(html.getBytes());
   1.145 +            }
   1.146 +        };
   1.147 +        
   1.148 +        final URI scratch;
   1.149 +        try {
   1.150 +            scratch = new URI("mem://mem3");
   1.151 +        } catch (URISyntaxException ex) {
   1.152 +            throw new IOException(ex);
   1.153 +        }
   1.154 +        
   1.155 +        JavaFileManager jfm = new ForwardingJavaFileManager<JavaFileManager>(sjfm) {
   1.156 +            @Override
   1.157 +            public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
   1.158 +                if (kind  == Kind.CLASS) {
   1.159 +                    final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
   1.160 +
   1.161 +                    class2BAOS.put(className.replace('.', '/') + ".class", buffer);
   1.162 +                    return new SimpleJavaFileObject(sibling.toUri(), kind) {
   1.163 +                        @Override
   1.164 +                        public OutputStream openOutputStream() throws IOException {
   1.165 +                            return buffer;
   1.166 +                        }
   1.167 +                    };
   1.168 +                }
   1.169 +                
   1.170 +                if (kind == Kind.SOURCE) {
   1.171 +                    final String n = className.replace('.', '/') + ".java";
   1.172 +                    final URI un;
   1.173 +                    try {
   1.174 +                        un = new URI("mem://" + n);
   1.175 +                    } catch (URISyntaxException ex) {
   1.176 +                        throw new IOException(ex);
   1.177 +                    }
   1.178 +                    return new VirtFO(un/*sibling.toUri()*/, kind, n);
   1.179 +                }
   1.180 +                
   1.181 +                throw new IllegalStateException();
   1.182 +            }
   1.183 +
   1.184 +            @Override
   1.185 +            public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
   1.186 +                if (location == StandardLocation.SOURCE_PATH) {
   1.187 +                    if (packageName.equals(pkg)) {
   1.188 +                        return htmlFile;
   1.189 +                    }
   1.190 +                }
   1.191 +                
   1.192 +                return null;
   1.193 +            }
   1.194 +
   1.195 +            @Override
   1.196 +            public boolean isSameFile(FileObject a, FileObject b) {
   1.197 +                if (a instanceof VirtFO && b instanceof VirtFO) {
   1.198 +                    return ((VirtFO)a).getName().equals(((VirtFO)b).getName());
   1.199 +                }
   1.200 +                
   1.201 +                return super.isSameFile(a, b);
   1.202 +            }
   1.203 +
   1.204 +            class VirtFO extends SimpleJavaFileObject {
   1.205 +
   1.206 +                private final String n;
   1.207 +
   1.208 +                public VirtFO(URI uri, Kind kind, String n) {
   1.209 +                    super(uri, kind);
   1.210 +                    this.n = n;
   1.211 +                }
   1.212 +                private final ByteArrayOutputStream data = new ByteArrayOutputStream();
   1.213 +
   1.214 +                @Override
   1.215 +                public OutputStream openOutputStream() throws IOException {
   1.216 +                    return data;
   1.217 +                }
   1.218 +
   1.219 +                @Override
   1.220 +                public String getName() {
   1.221 +                    return n;
   1.222 +                }
   1.223 +
   1.224 +                @Override
   1.225 +                public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.226 +                    data.close();
   1.227 +                    return new String(data.toByteArray());
   1.228 +                }
   1.229 +            }
   1.230 +        };
   1.231 +
   1.232 +        ToolProvider.getSystemJavaCompiler().getTask(null, jfm, this, /*XXX:*/Arrays.asList("-source", sourceLevel, "-target", "1.7"), null, Arrays.asList(file)).call();
   1.233 +
   1.234 +        Map<String, byte[]> result = new HashMap<String, byte[]>();
   1.235 +
   1.236 +        for (Map.Entry<String, ByteArrayOutputStream> e : class2BAOS.entrySet()) {
   1.237 +            result.put(e.getKey(), e.getValue().toByteArray());
   1.238 +        }
   1.239 +
   1.240 +        return result;
   1.241 +    }
   1.242 +
   1.243 +
   1.244 +    @Override
   1.245 +    public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.246 +        errors.add(diagnostic);
   1.247 +    }
   1.248 +    private static String findPkg(String java) throws IOException {
   1.249 +        Pattern p = Pattern.compile("package\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}*;", Pattern.MULTILINE);
   1.250 +        Matcher m = p.matcher(java);
   1.251 +        if (!m.find()) {
   1.252 +            throw new IOException("Can't find package declaration in the java file");
   1.253 +        }
   1.254 +        String pkg = m.group(1);
   1.255 +        return pkg;
   1.256 +    }
   1.257 +    private static String findCls(String java) throws IOException {
   1.258 +        Pattern p = Pattern.compile("class\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}", Pattern.MULTILINE);
   1.259 +        Matcher m = p.matcher(java);
   1.260 +        if (!m.find()) {
   1.261 +            throw new IOException("Can't find package declaration in the java file");
   1.262 +        }
   1.263 +        String cls = m.group(1);
   1.264 +        return cls;
   1.265 +    }
   1.266 +
   1.267 +    String getHtml() {
   1.268 +        String fqn = "'" + pkg + '.' + cls + "'";
   1.269 +        return html.replace("'${fqn}'", fqn);
   1.270 +    }
   1.271 +
   1.272 +    void assertErrors() {
   1.273 +        assertFalse(getErrors().isEmpty(), "There are supposed to be some errors");
   1.274 +    }
   1.275 +
   1.276 +    void assertError(String expMsg) {
   1.277 +        StringBuilder sb = new StringBuilder();
   1.278 +        sb.append("Can't find ").append(expMsg).append(" among:");
   1.279 +        for (Diagnostic<? extends JavaFileObject> e : errors) {
   1.280 +            String msg = e.getMessage(Locale.US);
   1.281 +            if (msg.contains(expMsg)) {
   1.282 +                return;
   1.283 +            }
   1.284 +            sb.append("\n");
   1.285 +            sb.append(msg);
   1.286 +        }
   1.287 +        fail(sb.toString());
   1.288 +    }
   1.289 +}