1 /*
   2  * Copyright (c) 2010, 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 /* @test
  25  * @bug 6917288
  26  * @summary Unnamed nested class is not generated
  27  * @modules jdk.compiler
  28  */
  29 
  30 import java.io.*;
  31 import java.util.*;
  32 
  33 public class GraphicalInstallerTest {
  34     public static void main(String... args) throws Exception {
  35         new GraphicalInstallerTest().run();
  36     }
  37 
  38     void run() throws Exception {
  39         File testSrc = new File(System.getProperty("test.src"));
  40         File classes = new File("classes");
  41         classes.mkdirs();
  42         List<String> opts = Arrays.asList("-d", classes.getPath());
  43         int rc = compile(opts, new File(testSrc, "GraphicalInstaller.java"));
  44         if (rc != 0) {
  45             error("compilation failed: rc=" + rc);
  46             return;
  47         }
  48 
  49         check(classes,
  50             "GraphicalInstaller$1X$1.class",
  51             "GraphicalInstaller$1X.class",
  52             "GraphicalInstaller$BackgroundInstaller.class",
  53             "GraphicalInstaller.class");
  54 
  55         if (errors > 0)
  56             throw new Exception(errors + " errors occurred");
  57     }
  58 
  59     /**
  60      *  Compile files with given options.
  61      *  Display any output from compiler, and return javac return code.
  62      */
  63     int compile(List<String> opts, File... files) {
  64         List<String> args = new ArrayList<String>();
  65         args.addAll(opts);
  66         for (File f: files)
  67             args.add(f.getPath());
  68         StringWriter sw = new StringWriter();
  69         PrintWriter pw = new PrintWriter(sw);
  70         int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
  71         pw.close();
  72         String out = sw.toString();
  73         if (out.length() > 0)
  74             System.err.println(out);
  75         return rc;
  76     }
  77 
  78     /**
  79      *  Check that a directory contains the expected files.
  80      */
  81     void check(File dir, String... paths) {
  82         Set<String> found = new TreeSet<String>(Arrays.asList(dir.list()));
  83         Set<String> expect = new TreeSet<String>(Arrays.asList(paths));
  84         if (found.equals(expect))
  85             return;
  86         for (String f: found) {
  87             if (!expect.contains(f))
  88                 error("Unexpected file found: " + f);
  89         }
  90         for (String e: expect) {
  91             if (!found.contains(e))
  92                 error("Expected file not found: " + e);
  93         }
  94     }
  95 
  96     /**
  97      *  Record an error message.
  98      */
  99     void error(String msg) {
 100         System.err.println(msg);
 101         errors++;
 102     }
 103 
 104     int errors;
 105 }