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. 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.test.lib.Asserts; 30 import jdk.test.lib.jfr.CommonHelper; 31 import jdk.test.lib.jfr.VoidFunction; 32 33 /** 34 * @test 35 * @summary Test setName(). 36 * @key jfr 37 * @requires vm.hasJFR 38 * @library /test/lib 39 * @run main/othervm jdk.jfr.api.recording.options.TestName 40 */ 41 public class TestName { 42 43 public static void main(String[] args) throws Throwable { 44 Recording r = new Recording(); 45 46 Asserts.assertNotNull(r.getName(), "Default name should not be null"); 47 System.out.println("name=" + r.getName()); 48 Asserts.assertEquals(r.getName(), Long.toString(r.getId()), "Default name != id"); 49 50 testNames(r); 51 r.start(); 52 testNames(r); 53 r.stop(); 54 testNames(r); 55 r.close(); 56 } 57 58 private static void testNames(Recording r) throws Throwable { 59 System.out.println("Recording state=" + r.getState().name()); 60 61 // Set simple name 62 String name = "myName"; 63 r.setName(name); 64 System.out.println("name=" + r.getName()); 65 Asserts.assertEquals(name, r.getName(), "Wrong get/set name"); 66 67 // Set null. Should get Exception and old name should be kept. 68 verifyNull(()->{r.setName(null);}, "No NullPointerException when setName(null)"); 69 Asserts.assertEquals(name, r.getName(), "Current name overwritten after null"); 70 71 // Set empty name. Should work. 72 name = ""; 73 r.setName(name); 74 System.out.println("name=" + r.getName()); 75 Asserts.assertEquals(name, r.getName(), "Empty name is expected to work"); 76 77 // Test a long name. 78 StringBuilder sb = new StringBuilder(500); 79 while (sb.length() < 400) { 80 sb.append("LongName-"); 81 } 82 name = sb.toString(); 83 System.out.println("Length of long name=" + name.length()); 84 r.setName(name); 85 System.out.println("name=" + r.getName()); 86 Asserts.assertEquals(name, r.getName(), "Wrong get/set long name"); 87 } 88 89 private static void verifyNull(VoidFunction f, String msg) throws Throwable { 90 CommonHelper.verifyException(f, msg, NullPointerException.class); 91 } 92 93 }