test/micro/classes/org/openjdk/bench/java/lang/reflect/ClazzWithSecurityManager.java
branchJEP-230-microbenchmarks-branch
changeset 56936 923f1356c5ea
parent 56930 079075866d11
child 56975 3053039bdda3
equal deleted inserted replaced
56930:079075866d11 56936:923f1356c5ea
     1 /*
     1 /*
     2  * Copyright (c) 2014, 2018 Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    34 import java.security.URIParameter;
    34 import java.security.URIParameter;
    35 
    35 
    36 import org.openjdk.jmh.annotations.Scope;
    36 import org.openjdk.jmh.annotations.Scope;
    37 import org.openjdk.jmh.annotations.Setup;
    37 import org.openjdk.jmh.annotations.Setup;
    38 import org.openjdk.jmh.annotations.State;
    38 import org.openjdk.jmh.annotations.State;
    39 import org.openjdk.jmh.annotations.TearDown;
       
    40 
    39 
    41 /**
    40 /**
    42  * Reflection benchmark
    41  * Reflection benchmark
       
    42  *
       
    43  * @author sfriberg
    43  */
    44  */
    44 @State(Scope.Benchmark)
    45 @State(Scope.Benchmark)
    45 public class ClazzWithSecurityManager extends Clazz {
    46 public class ClazzWithSecurityManager extends Clazz {
    46 
    47 
    47     private static Policy originalPolicy;
       
    48     private static SecurityManager originalSM;
       
    49 
       
    50     @Setup
    48     @Setup
    51     public void setup() throws IOException, NoSuchAlgorithmException {
    49     public void setup() throws IOException, NoSuchAlgorithmException {
    52 
       
    53         originalPolicy = Policy.getPolicy();
       
    54         originalSM = System.getSecurityManager();
       
    55 
       
    56         File policyFile = File.createTempFile("security", "policy");
    50         File policyFile = File.createTempFile("security", "policy");
    57         policyFile.deleteOnExit();
    51         policyFile.deleteOnExit();
    58 
    52 
    59         PrintWriter writer = new PrintWriter(policyFile);
    53         PrintWriter writer = new PrintWriter(policyFile);
    60         BufferedReader reader = new BufferedReader(new InputStreamReader(
    54         BufferedReader reader = new BufferedReader(new InputStreamReader(
    61                 ClazzWithSecurityManager.class.getResourceAsStream("/org/openjdk/bench/java/security/security.policy")));
    55                 ClazzWithSecurityManager.class.getResourceAsStream("/org/openjdk/bench/java/security/security.policy")));
    62         for (String line = reader.readLine(); line != null; line = reader.readLine()) {
    56         for (String line = reader.readLine(); line != null; line = reader.readLine()) {
    63             System.out.println("Adding " + line);
    57             writer.println(line);
    64             writer.write(line);
       
    65         }
    58         }
    66         reader.close();
    59         reader.close();
    67         writer.close();
    60         writer.close();
    68 
    61 
    69         Policy policy = Policy.getInstance("JavaPolicy", new URIParameter(policyFile.toURI()));
    62         Policy policy = Policy.getInstance("JavaPolicy", new URIParameter(policyFile.toURI()));
    70         Policy.setPolicy(policy);
    63         Policy.setPolicy(policy);
    71         System.setSecurityManager(new SecurityManager());
    64         System.setSecurityManager(new SecurityManager());
    72     }
    65     }
    73 
       
    74     @TearDown
       
    75     public void tearDown() {
       
    76         Policy.setPolicy(originalPolicy);
       
    77         System.setSecurityManager(originalSM);
       
    78     }
       
    79 }
    66 }