001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vladimir N. Molotkov
020: * @version $Revision$
021: */package org.apache.harmony.auth.tests.support;
022:
023: import java.io.File;
024: import java.io.FileOutputStream;
025: import java.io.IOException;
026: import java.security.Security;
027: import java.util.Properties;
028:
029: /**
030: * Test utility class
031: */
032: public class TestUtils {
033:
034: /**
035: * No need to instantiate
036: */
037: private TestUtils() {
038: }
039:
040: /**
041: * Prints byte array <code>data</code> as hex to the
042: * <code>System.out</code> in the customizable form.
043: *
044: * @param perLine how many numbers put on single line
045: * @param prefix custom output number prefix
046: * @param delimiter custom output number delimiter
047: * @param data data to be printed
048: */
049: public static void printAsHex(int perLine, String prefix,
050: String delimiter, byte[] data) {
051: for (int i = 0; i < data.length; i++) {
052: String tail = Integer.toHexString(0x000000ff & data[i]);
053: if (tail.length() == 1) {
054: tail = "0" + tail;
055: }
056: System.out.print(prefix + "0x" + tail + delimiter);
057:
058: if (((i + 1) % perLine) == 0) {
059: System.out.println("");
060: }
061: }
062: System.out.println("");
063: }
064:
065: /**
066: * Sets system property
067: *
068: * @param key - the name of the system property.
069: * @param value - the value to be set
070: */
071: public static void setSystemProperty(String key, String value) {
072: Properties properties = System.getProperties();
073: if (value == null) {
074: properties.remove(key);
075: } else {
076: properties.setProperty(key, value);
077: }
078: System.setProperties(properties);
079: }
080:
081: /**
082: * Creates security properties file.
083: *
084: * The method puts to the file all initial security providers and adds
085: * passed custom properties.
086: *
087: * @param props -
088: * security properties to be added to properties file
089: * @return path to created file.
090: * @throws IOException
091: * if failed to create the file
092: */
093: public static String createJavaPropertiesFile(Properties props)
094: throws IOException {
095:
096: File f = File.createTempFile("java", "security");
097: f.deleteOnExit();
098:
099: FileOutputStream out = new FileOutputStream(f);
100:
101: Properties propsToFlush = new Properties();
102:
103: int i = 1;
104: String provider = "security.provider.1";
105: while (Security.getProperty(provider) != null) {
106: propsToFlush.setProperty(provider, Security
107: .getProperty(provider));
108: provider = "security.provider." + i++;
109: }
110:
111: props.store(out, null);
112: propsToFlush.store(out, null);
113:
114: out.close();
115:
116: return f.getAbsolutePath();
117: }
118: }
|