1 /*
   2  * Copyright (c) 2018, 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.api.recording.options;
  27 
  28 import jdk.jfr.Recording;
  29 import jdk.testlibrary.Asserts;
  30 import jdk.testlibrary.jfr.CommonHelper;
  31 import jdk.testlibrary.jfr.VoidFunction;
  32 
  33 /*
  34  * @test
  35  * @summary Test setName().
  36  * @key jfr
  37  * @library /lib/testlibrary
  38  * @run main/othervm jdk.jfr.api.recording.options.TestName
  39  */
  40 public class TestName {
  41 
  42     public static void main(String[] args) throws Throwable {
  43         Recording r = new Recording();
  44 
  45         Asserts.assertNotNull(r.getName(), "Default name should not be null");
  46         System.out.println("name=" + r.getName());
  47         Asserts.assertEquals(r.getName(), Long.toString(r.getId()), "Default name != id");
  48 
  49         testNames(r);
  50         r.start();
  51         testNames(r);
  52         r.stop();
  53         testNames(r);
  54         r.close();
  55     }
  56 
  57     private static void testNames(Recording r) throws Throwable {
  58         System.out.println("Recording state=" + r.getState().name());
  59 
  60         // Set simple name
  61         String name = "myName";
  62         r.setName(name);
  63         System.out.println("name=" + r.getName());
  64         Asserts.assertEquals(name, r.getName(), "Wrong get/set name");
  65 
  66         // Set null. Should get Exception and old name should be kept.
  67         verifyNull(()->{r.setName(null);}, "No NullPointerException when setName(null)");
  68         Asserts.assertEquals(name, r.getName(), "Current name overwritten after null");
  69 
  70         // Set empty name. Should work.
  71         name = "";
  72         r.setName(name);
  73         System.out.println("name=" + r.getName());
  74         Asserts.assertEquals(name, r.getName(), "Empty name is expected to work");
  75 
  76         // Test a long name.
  77         StringBuilder sb = new StringBuilder(500);
  78         while (sb.length() < 400) {
  79             sb.append("LongName-");
  80         }
  81         name = sb.toString();
  82         System.out.println("Length of long name=" + name.length());
  83         r.setName(name);
  84         System.out.println("name=" + r.getName());
  85         Asserts.assertEquals(name, r.getName(), "Wrong get/set long name");
  86     }
  87 
  88     private static void verifyNull(VoidFunction f, String msg) throws Throwable {
  89         CommonHelper.verifyException(f, msg, NullPointerException.class);
  90     }
  91 
  92 }