Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/TimeZone/SetDefaultSecurityTest.java
38811 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 800102926* @summary Make sure that TimeZone.setDefault throws a SecurityException if the27* security manager doesn't permit.28* @run main/othervm SetDefaultSecurityTest29*/3031import java.util.SimpleTimeZone;32import java.util.TimeZone;3334public class SetDefaultSecurityTest {35static final TimeZone NOWHERE = new SimpleTimeZone(Integer.MAX_VALUE, "Nowhere");3637public static void main(String[] args) {38TimeZone defaultZone = TimeZone.getDefault();3940// Make sure that TimeZone.setDefault works for trusted code41TimeZone.setDefault(NOWHERE);42if (!NOWHERE.equals(TimeZone.getDefault())) {43new RuntimeException("TimeZone.setDefault doesn't work for trusted code.");44}45// Restore defaultZone46TimeZone.setDefault(defaultZone);47if (!defaultZone.equals(TimeZone.getDefault())) {48new RuntimeException("TimeZone.setDefault doesn't restore defaultZone.");49}5051// Install a SecurityManager.52System.setSecurityManager(new SecurityManager());53try {54TimeZone.setDefault(NOWHERE);55throw new RuntimeException("TimeZone.setDefault doesn't throw a SecurityException.");56} catch (SecurityException se) {57// OK58}59TimeZone tz = TimeZone.getDefault();60if (!defaultZone.equals(tz)) {61throw new RuntimeException("Default TimeZone changed: " + tz);62}63}64}656667