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 /**
  25  * @test
  26  * @bug 8199871
  27  * @modules jdk.jartool
  28  * @summary jar -n should print out deprecation warning
  29  * @run testng DeprecateOptionN
  30  */
  31 
  32 import java.io.File;
  33 import java.io.IOException;
  34 import java.io.PrintWriter;
  35 import java.io.StringWriter;
  36 import java.util.spi.ToolProvider;
  37 import org.testng.annotations.Test;
  38 
  39 import static org.testng.Assert.assertFalse;
  40 import static org.testng.Assert.assertTrue;
  41 
  42 public class DeprecateOptionN {
  43     private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
  44             .orElseThrow(() ->
  45                     new RuntimeException("jar tool not found")
  46             );
  47 
  48     protected static String jar(String... options) {
  49         StringWriter writer = new StringWriter();
  50         PrintWriter pw = new PrintWriter(writer);
  51 
  52         JAR_TOOL.run(pw, pw, options);
  53         String output = writer.toString();
  54         System.err.println(output);
  55         return output;
  56     }
  57 
  58     @Test
  59     public void helpCompatWithWarning() {
  60         String output = jar("--help:compat");
  61         assertTrue(output.contains("this option is planned to be removed in a future JDK release"));
  62     }
  63 
  64     @Test
  65     public void helpExtraWithWarning() {
  66         String output = jar("--help-extra");
  67         assertTrue(output.contains("This option is planned to be\n"));
  68         assertTrue(output.contains("removed in a future JDK release"));
  69     }
  70 
  71     @Test
  72     public void normalizeWithWarning() throws IOException {
  73         File tmp = File.createTempFile("test", null);
  74         String output = jar("cnf", "test.jar", tmp.getAbsolutePath());
  75         tmp.delete();
  76         assertTrue(output.contains("Warning: The -n option is planned to be removed in a future JDK release"));
  77     }
  78 
  79     @Test
  80     public void NoWarningWithoutN() throws IOException {
  81         File tmp = File.createTempFile("test", null);
  82         String output = jar("cf", "test.jar", tmp.getAbsolutePath());
  83         tmp.delete();
  84         assertFalse(output.contains("Warning: The -n option is planned to be removed in a future JDK release"));
  85     }
  86 
  87 
  88     @Test
  89     public void SuppressWarning() throws IOException {
  90         File tmp = File.createTempFile("test", null);
  91         String output = jar("-c", "-n", "-XDsuppress-tool-removal-message",
  92                 "-f", "test.jar", tmp.getAbsolutePath());
  93         tmp.delete();
  94         assertFalse(output.contains("Warning: The -n option is planned to be removed in a future JDK release"));
  95     }
  96 }