1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.IOException;
  25 import java.util.Arrays;
  26 import java.util.List;
  27 import java.util.stream.Collectors;
  28 
  29 import org.testng.ITestResult;
  30 import org.testng.annotations.AfterMethod;
  31 import org.testng.annotations.Test;
  32 import tools.javac.combo.JavacTemplateTestBase;
  33 
  34 import static java.util.stream.Collectors.toList;
  35 
  36 @Test
  37 public class ScopeTest extends JavacTemplateTestBase {
  38 
  39     private static String st_block(String... statements) {
  40         return Arrays.stream(statements).collect(Collectors.joining("", "{", "}"));
  41     }
  42 
  43     private static String st_if(String condition, String then, String els) {
  44         return "if (" + condition + ") " + then + " else " + els;
  45     }
  46 
  47     private static String st_while(String condition, String body) {
  48         return "while (" + condition + ") " + body;
  49     }
  50 
  51     private static String st_do_while(String body, String condition) {
  52         return "do " + body + " while (" + condition + ");";
  53     }
  54 
  55     private static String st_for(String init, String condition, String update, String body) {
  56         return "for (" + init + "; " + condition + "; " + update + ") " + body;
  57     }
  58 
  59     private static String st_s_use() {
  60         return "s.length();";
  61     }
  62 
  63     private static String st_break() {
  64         return "break;";
  65     }
  66 
  67     private static String st_return() {
  68         return "return;";
  69     }
  70 
  71     private static String st_noop() {
  72         return ";";
  73     }
  74 
  75     private static String expr_empty() {
  76         return "";
  77     }
  78 
  79     private static String expr_o_match_str() {
  80         return "o instanceof String s";
  81     }
  82 
  83     private static String expr_not(String expr) {
  84         return "!(" + expr + ")";
  85     }
  86 
  87     @AfterMethod
  88     public void dumpTemplateIfError(ITestResult result) {
  89         // Make sure offending template ends up in log file on failure
  90         if (!result.isSuccess()) {
  91             System.err.printf("Diagnostics: %s%nTemplate: %s%n", diags.errorKeys(), sourceFiles.stream().map(p -> p.snd).collect(toList()));
  92         }
  93     }
  94 
  95     private void program(String block) {
  96         String s = "class C { void m(Object o) " + block + "}";
  97         addSourceFile("C.java", new StringTemplate(s));
  98     }
  99 
 100     private void assertOK(String block) {
 101         reset();
 102         program(block);
 103         try {
 104             compile();
 105         }
 106         catch (IOException e) {
 107             throw new RuntimeException(e);
 108         }
 109         assertCompileSucceeded();
 110     }
 111 
 112     private void assertFail(String expectedDiag, String block) {
 113         String sourceVersion = Integer.toString(Runtime.version().feature());
 114 
 115         reset();
 116         addCompileOptions("--enable-preview", "-source", sourceVersion);
 117         program(block);
 118         try {
 119             compile();
 120         }
 121         catch (IOException e) {
 122             throw new RuntimeException(e);
 123         }
 124         assertCompileFailed(expectedDiag);
 125     }
 126 
 127     public void testIf() {
 128         assertOK(st_block(st_if(expr_o_match_str(), st_s_use(), st_return()), st_s_use()));
 129         assertOK(st_block(st_if(expr_not(expr_o_match_str()), st_return(), st_s_use()), st_s_use()));
 130         assertFail("compiler.err.cant.resolve.location", st_block(st_if(expr_o_match_str(), st_s_use(), st_noop()), st_s_use()));
 131         assertFail("compiler.err.cant.resolve.location", st_block(st_if(expr_not(expr_o_match_str()), st_noop(), st_s_use()), st_s_use()));
 132     }
 133 
 134     public void testWhile() {
 135         assertOK(st_block(st_while(expr_not(expr_o_match_str()), st_noop()), st_s_use()));
 136         assertFail("compiler.err.cant.resolve.location", st_block(st_while(expr_not(expr_o_match_str()), st_break()), st_s_use()));
 137     }
 138 
 139     public void testDoWhile() {
 140         assertOK(st_block(st_do_while(st_noop(), expr_not(expr_o_match_str())), st_s_use()));
 141         assertFail("compiler.err.cant.resolve.location", st_block(st_do_while(st_break(), expr_not(expr_o_match_str())), st_s_use()));
 142     }
 143 
 144     public void testFor() {
 145         assertOK(st_block(st_for(expr_empty(), expr_not(expr_o_match_str()), expr_empty(), st_noop()), st_s_use()));
 146         assertFail("compiler.err.cant.resolve.location", st_block(st_for(expr_empty(), expr_not(expr_o_match_str()), expr_empty(), st_break()), st_s_use()));
 147     }
 148 
 149 }