1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18/** 19* @author Vladimir N. Molotkov 20*/ 21 22package org.apache.harmony.auth.tests.support; 23 24import java.io.File; 25import java.io.FileOutputStream; 26import java.io.IOException; 27import java.security.Security; 28import java.util.Properties; 29 30/** 31 * Test utility class 32 */ 33public class TestUtils { 34 35 /** 36 * No need to instantiate 37 */ 38 private TestUtils() { 39 } 40 41 /** 42 * Prints byte array <code>data</code> as hex to the 43 * <code>System.out</code> in the customizable form. 44 * 45 * @param perLine how many numbers put on single line 46 * @param prefix custom output number prefix 47 * @param delimiter custom output number delimiter 48 * @param data data to be printed 49 */ 50 public static void printAsHex(int perLine, 51 String prefix, 52 String delimiter, 53 byte[] data) { 54 for (int i=0; i<data.length; i++) { 55 String tail = Integer.toHexString(0x000000ff & data[i]); 56 if (tail.length() == 1) { 57 tail = "0" + tail; 58 } 59 System.out.print(prefix + "0x" + tail + delimiter); 60 61 if (((i+1)%perLine) == 0) { 62 System.out.println(""); 63 } 64 } 65 System.out.println(""); 66 } 67 68 /** 69 * Sets system property 70 * 71 * @param key - the name of the system property. 72 * @param value - the value to be set 73 */ 74 public static void setSystemProperty(String key, String value) { 75 Properties properties = System.getProperties(); 76 if (value == null) { 77 properties.remove(key); 78 } else { 79 properties.setProperty(key, value); 80 } 81 System.setProperties(properties); 82 } 83 84 /** 85 * Creates security properties file. 86 * 87 * The method puts to the file all initial security providers and adds 88 * passed custom properties. 89 * 90 * @param props - 91 * security properties to be added to properties file 92 * @return path to created file. 93 * @throws IOException 94 * if failed to create the file 95 */ 96 public static String createJavaPropertiesFile(Properties props) 97 throws IOException { 98 99 File f = File.createTempFile("java", "security"); 100 f.deleteOnExit(); 101 102 FileOutputStream out = new FileOutputStream(f); 103 104 Properties propsToFlush = new Properties(); 105 106 int i = 1; 107 String provider = "security.provider.1"; 108 while (Security.getProperty(provider) != null) { 109 propsToFlush.setProperty(provider, Security.getProperty(provider)); 110 provider = "security.provider." + i++; 111 } 112 113 props.store(out, null); 114 propsToFlush.store(out, null); 115 116 out.close(); 117 118 return f.getAbsolutePath(); 119 } 120} 121