boot/src/test/java/org/netbeans/html/boot/impl/Compile.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 531 ff48a4a875ff
child 790 30f20d9c0986
permissions -rw-r--r--
Updating copyright headers to mention current year
jtulach@3
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@3
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jtulach@3
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jtulach@3
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jtulach@3
    42
 */
jaroslav@362
    43
package org.netbeans.html.boot.impl;
jtulach@3
    44
jtulach@3
    45
import java.io.ByteArrayInputStream;
jtulach@3
    46
import java.io.ByteArrayOutputStream;
jtulach@3
    47
import java.io.IOException;
jtulach@3
    48
import java.io.InputStream;
jtulach@3
    49
import java.io.OutputStream;
jtulach@3
    50
import java.net.URI;
jtulach@3
    51
import java.net.URISyntaxException;
jtulach@3
    52
import java.util.ArrayList;
jtulach@3
    53
import java.util.Arrays;
jtulach@3
    54
import java.util.HashMap;
jtulach@3
    55
import java.util.List;
jaroslav@184
    56
import java.util.Locale;
jtulach@3
    57
import java.util.Map;
jaroslav@459
    58
import java.util.logging.Level;
jaroslav@459
    59
import java.util.logging.Logger;
jtulach@3
    60
import java.util.regex.Matcher;
jtulach@3
    61
import java.util.regex.Pattern;
jtulach@3
    62
import javax.tools.Diagnostic;
jtulach@3
    63
import javax.tools.DiagnosticListener;
jtulach@3
    64
import javax.tools.FileObject;
jtulach@3
    65
import javax.tools.ForwardingJavaFileManager;
jtulach@3
    66
import javax.tools.JavaFileManager;
jtulach@3
    67
import javax.tools.JavaFileObject;
jtulach@3
    68
import javax.tools.JavaFileObject.Kind;
jtulach@3
    69
import javax.tools.SimpleJavaFileObject;
jtulach@3
    70
import javax.tools.StandardJavaFileManager;
jtulach@3
    71
import javax.tools.StandardLocation;
jtulach@3
    72
import javax.tools.ToolProvider;
jaroslav@185
    73
import static org.testng.Assert.assertTrue;
jaroslav@184
    74
import static org.testng.Assert.assertFalse;
jaroslav@184
    75
import static org.testng.Assert.fail;
jtulach@3
    76
jtulach@3
    77
/**
jtulach@3
    78
 *
jtulach@3
    79
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@3
    80
 */
jtulach@3
    81
final class Compile implements DiagnosticListener<JavaFileObject> {
jaroslav@100
    82
    private final List<Diagnostic<? extends JavaFileObject>> errors = 
jaroslav@100
    83
            new ArrayList<Diagnostic<? extends JavaFileObject>>();
jtulach@3
    84
    private final Map<String, byte[]> classes;
jtulach@3
    85
    private final String pkg;
jtulach@3
    86
    private final String cls;
jtulach@3
    87
    private final String html;
jaroslav@68
    88
    private final String sourceLevel;
jtulach@3
    89
jaroslav@68
    90
    private Compile(String html, String code, String sl) throws IOException {
jtulach@3
    91
        this.pkg = findPkg(code);
jtulach@3
    92
        this.cls = findCls(code);
jtulach@3
    93
        this.html = html;
jaroslav@68
    94
        this.sourceLevel = sl;
jtulach@3
    95
        classes = compile(html, code);
jtulach@3
    96
    }
jtulach@3
    97
jtulach@3
    98
    /** Performs compilation of given HTML page and associated Java code
jtulach@3
    99
     */
jtulach@3
   100
    public static Compile create(String html, String code) throws IOException {
jaroslav@68
   101
        return create(html, code, "1.7");
jaroslav@68
   102
    }
jaroslav@68
   103
    static Compile create(String html, String code, String sourceLevel) throws IOException {
jaroslav@68
   104
        return new Compile(html, code, sourceLevel);
jtulach@3
   105
    }
jtulach@3
   106
    
jtulach@3
   107
    /** Checks for given class among compiled resources */
jtulach@3
   108
    public byte[] get(String res) {
jtulach@3
   109
        return classes.get(res);
jtulach@3
   110
    }
jtulach@3
   111
    
jtulach@3
   112
    /** Obtains errors created during compilation.
jtulach@3
   113
     */
jtulach@3
   114
    public List<Diagnostic<? extends JavaFileObject>> getErrors() {
jaroslav@100
   115
        List<Diagnostic<? extends JavaFileObject>> err;
jaroslav@100
   116
        err = new ArrayList<Diagnostic<? extends JavaFileObject>>();
jtulach@3
   117
        for (Diagnostic<? extends JavaFileObject> diagnostic : errors) {
jtulach@3
   118
            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
jtulach@3
   119
                err.add(diagnostic);
jtulach@3
   120
            }
jtulach@3
   121
        }
jtulach@3
   122
        return err;
jtulach@3
   123
    }
jtulach@3
   124
    
jtulach@3
   125
    private Map<String, byte[]> compile(final String html, final String code) throws IOException {
jtulach@3
   126
        StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(this, null, null);
jtulach@3
   127
jaroslav@100
   128
        final Map<String, ByteArrayOutputStream> class2BAOS;
jaroslav@100
   129
        class2BAOS = new HashMap<String, ByteArrayOutputStream>();
jtulach@3
   130
jtulach@3
   131
        JavaFileObject file = new SimpleJavaFileObject(URI.create("mem://mem"), Kind.SOURCE) {
jtulach@3
   132
            @Override
jtulach@3
   133
            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
jtulach@3
   134
                return code;
jtulach@3
   135
            }
jtulach@3
   136
        };
jtulach@3
   137
        final JavaFileObject htmlFile = new SimpleJavaFileObject(URI.create("mem://mem2"), Kind.OTHER) {
jtulach@3
   138
            @Override
jtulach@3
   139
            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
jtulach@3
   140
                return html;
jtulach@3
   141
            }
jtulach@3
   142
jtulach@3
   143
            @Override
jtulach@3
   144
            public InputStream openInputStream() throws IOException {
jtulach@3
   145
                return new ByteArrayInputStream(html.getBytes());
jtulach@3
   146
            }
jtulach@3
   147
        };
jtulach@3
   148
        
jtulach@3
   149
        final URI scratch;
jtulach@3
   150
        try {
jtulach@3
   151
            scratch = new URI("mem://mem3");
jtulach@3
   152
        } catch (URISyntaxException ex) {
jtulach@3
   153
            throw new IOException(ex);
jtulach@3
   154
        }
jtulach@3
   155
        
jtulach@3
   156
        JavaFileManager jfm = new ForwardingJavaFileManager<JavaFileManager>(sjfm) {
jtulach@3
   157
            @Override
jaroslav@459
   158
            public FileObject getFileForOutput(JavaFileManager.Location location, String packageName, String relativeName, FileObject sibling) throws IOException {
jaroslav@459
   159
                try {
jaroslav@459
   160
                    return new VirtFO(new URI("mem://resource/" + relativeName), Kind.OTHER, relativeName);
jaroslav@459
   161
                } catch (URISyntaxException ex) {
jaroslav@459
   162
                    throw new IllegalStateException(ex);
jaroslav@459
   163
                }
jaroslav@459
   164
            }
jaroslav@459
   165
            
jaroslav@459
   166
            
jaroslav@459
   167
            @Override
jtulach@3
   168
            public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
jtulach@3
   169
                if (kind  == Kind.CLASS) {
jtulach@3
   170
                    final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
jtulach@3
   171
jtulach@3
   172
                    class2BAOS.put(className.replace('.', '/') + ".class", buffer);
jtulach@3
   173
                    return new SimpleJavaFileObject(sibling.toUri(), kind) {
jtulach@3
   174
                        @Override
jtulach@3
   175
                        public OutputStream openOutputStream() throws IOException {
jtulach@3
   176
                            return buffer;
jtulach@3
   177
                        }
jtulach@3
   178
                    };
jtulach@3
   179
                }
jtulach@3
   180
                
jtulach@3
   181
                if (kind == Kind.SOURCE) {
jtulach@3
   182
                    final String n = className.replace('.', '/') + ".java";
jaroslav@68
   183
                    final URI un;
jaroslav@68
   184
                    try {
jaroslav@68
   185
                        un = new URI("mem://" + n);
jaroslav@68
   186
                    } catch (URISyntaxException ex) {
jaroslav@68
   187
                        throw new IOException(ex);
jaroslav@68
   188
                    }
jaroslav@70
   189
                    return new VirtFO(un/*sibling.toUri()*/, kind, n);
jtulach@3
   190
                }
jtulach@3
   191
                
jtulach@3
   192
                throw new IllegalStateException();
jtulach@3
   193
            }
jtulach@3
   194
jtulach@3
   195
            @Override
jtulach@3
   196
            public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
jtulach@3
   197
                if (location == StandardLocation.SOURCE_PATH) {
jtulach@3
   198
                    if (packageName.equals(pkg)) {
jtulach@3
   199
                        return htmlFile;
jtulach@3
   200
                    }
jaroslav@531
   201
                    if (packageName.isEmpty() && relativeName.startsWith(pkg.replace('.', '/'))) {
jaroslav@531
   202
                        return htmlFile;
jaroslav@531
   203
                    }
jtulach@3
   204
                }
jtulach@3
   205
                
jtulach@3
   206
                return null;
jtulach@3
   207
            }
jaroslav@70
   208
jaroslav@70
   209
            @Override
jaroslav@70
   210
            public boolean isSameFile(FileObject a, FileObject b) {
jaroslav@70
   211
                if (a instanceof VirtFO && b instanceof VirtFO) {
jaroslav@70
   212
                    return ((VirtFO)a).getName().equals(((VirtFO)b).getName());
jaroslav@70
   213
                }
jaroslav@70
   214
                
jaroslav@70
   215
                return super.isSameFile(a, b);
jaroslav@70
   216
            }
jaroslav@70
   217
jaroslav@70
   218
            class VirtFO extends SimpleJavaFileObject {
jaroslav@70
   219
jaroslav@70
   220
                private final String n;
jaroslav@70
   221
jaroslav@70
   222
                public VirtFO(URI uri, Kind kind, String n) {
jaroslav@70
   223
                    super(uri, kind);
jaroslav@70
   224
                    this.n = n;
jaroslav@70
   225
                }
jaroslav@70
   226
                private final ByteArrayOutputStream data = new ByteArrayOutputStream();
jaroslav@70
   227
jaroslav@70
   228
                @Override
jaroslav@70
   229
                public OutputStream openOutputStream() throws IOException {
jaroslav@70
   230
                    return data;
jaroslav@70
   231
                }
jaroslav@70
   232
jaroslav@70
   233
                @Override
jaroslav@70
   234
                public String getName() {
jaroslav@70
   235
                    return n;
jaroslav@70
   236
                }
jaroslav@70
   237
jaroslav@70
   238
                @Override
jaroslav@70
   239
                public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
jaroslav@70
   240
                    data.close();
jaroslav@70
   241
                    return new String(data.toByteArray());
jaroslav@70
   242
                }
jaroslav@70
   243
            }
jtulach@3
   244
        };
jtulach@3
   245
jaroslav@68
   246
        ToolProvider.getSystemJavaCompiler().getTask(null, jfm, this, /*XXX:*/Arrays.asList("-source", sourceLevel, "-target", "1.7"), null, Arrays.asList(file)).call();
jtulach@3
   247
jaroslav@100
   248
        Map<String, byte[]> result = new HashMap<String, byte[]>();
jtulach@3
   249
jtulach@3
   250
        for (Map.Entry<String, ByteArrayOutputStream> e : class2BAOS.entrySet()) {
jtulach@3
   251
            result.put(e.getKey(), e.getValue().toByteArray());
jtulach@3
   252
        }
jtulach@3
   253
jtulach@3
   254
        return result;
jtulach@3
   255
    }
jtulach@3
   256
jtulach@3
   257
jtulach@3
   258
    @Override
jtulach@3
   259
    public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
jtulach@3
   260
        errors.add(diagnostic);
jtulach@3
   261
    }
jtulach@3
   262
    private static String findPkg(String java) throws IOException {
jtulach@3
   263
        Pattern p = Pattern.compile("package\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}*;", Pattern.MULTILINE);
jtulach@3
   264
        Matcher m = p.matcher(java);
jtulach@3
   265
        if (!m.find()) {
jtulach@3
   266
            throw new IOException("Can't find package declaration in the java file");
jtulach@3
   267
        }
jtulach@3
   268
        String pkg = m.group(1);
jtulach@3
   269
        return pkg;
jtulach@3
   270
    }
jtulach@3
   271
    private static String findCls(String java) throws IOException {
jtulach@3
   272
        Pattern p = Pattern.compile("class\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}", Pattern.MULTILINE);
jtulach@3
   273
        Matcher m = p.matcher(java);
jtulach@3
   274
        if (!m.find()) {
jtulach@3
   275
            throw new IOException("Can't find package declaration in the java file");
jtulach@3
   276
        }
jtulach@3
   277
        String cls = m.group(1);
jtulach@3
   278
        return cls;
jtulach@3
   279
    }
jtulach@3
   280
jtulach@3
   281
    String getHtml() {
jtulach@3
   282
        String fqn = "'" + pkg + '.' + cls + "'";
jtulach@3
   283
        return html.replace("'${fqn}'", fqn);
jtulach@3
   284
    }
jaroslav@184
   285
    void assertErrors() {
jaroslav@184
   286
        assertFalse(getErrors().isEmpty(), "There are supposed to be some errors");
jaroslav@184
   287
    }
jaroslav@184
   288
jaroslav@184
   289
    void assertError(String expMsg) {
jaroslav@184
   290
        StringBuilder sb = new StringBuilder();
jaroslav@184
   291
        sb.append("Can't find ").append(expMsg).append(" among:");
jaroslav@184
   292
        for (Diagnostic<? extends JavaFileObject> e : errors) {
jaroslav@184
   293
            String msg = e.getMessage(Locale.US);
jaroslav@184
   294
            if (msg.contains(expMsg)) {
jaroslav@184
   295
                return;
jaroslav@184
   296
            }
jaroslav@184
   297
            sb.append("\n");
jaroslav@184
   298
            sb.append(msg);
jaroslav@184
   299
        }
jaroslav@184
   300
        fail(sb.toString());
jaroslav@184
   301
    }
jaroslav@185
   302
jaroslav@185
   303
    void assertNoErrors() {
jaroslav@185
   304
        assertTrue(getErrors().isEmpty(), "No errors expected: " + getErrors());
jaroslav@185
   305
    }
jtulach@3
   306
}