utils.java.svn-base
来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 230 行
SVN-BASE
230 行
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.tck.j2me.utils;import java.io.File;import com.sun.javatest.TestEnvironment;import com.sun.tck.j2me.javatest.TestExportInfo;import java.net.URL;import java.util.ArrayList;import java.util.List;/** * This class contains static methods, which might be useful for other * classes. */public class Utils { /** * returns trace of the given Throwable instance as String. */ public static String getTrace(Throwable t) { if (t == null) { return null; } StringBuffer retVal = new StringBuffer(t.toString()); retVal.append('\n'); StackTraceElement[] list = t.getStackTrace(); for (int i = 0; i < list.length; i++) { retVal.append("\tat "); retVal.append(list[i].toString()); retVal.append('\n'); } return retVal.toString(); } /** * Get a boolean value from JavaTest environment variable. * @param env TestEnvironment where the variable is set. * @param name key of the variable * @param defaultValue default value if the variable is not set or an error occurs * when retrieving the value. */ public static boolean getBooleanFromEnv(TestEnvironment env, String name, boolean defaultValue) { String[] envData; boolean value = defaultValue; try { envData = env.lookup(name); if (envData != null && envData.length == 1) { value = Boolean.valueOf(envData[0]).booleanValue(); } } catch (TestEnvironment.Fault tef) { // Will use default value } return value; } /** * Get an integer value from JavaTest environment variable. * @param env TestEnvironment where the variable is set. * @param name key of the variable * @param defaultValue default value if the variable is not set or an error occurs * when retrieving the value. */ public static int getIntFromEnv(TestEnvironment env, String name, int defaultValue) { String[] envData = null; int n = defaultValue; try { envData = env.lookup(name); if (envData != null && envData.length == 1) { n = Integer.parseInt(envData[0]); } } catch (NumberFormatException nfe) { // Will use default } catch (TestEnvironment.Fault tef) { // Will use default } return n; } /** * @param env TestEnvironment to get the export information * @return true if test run is in export mode */ public static boolean isExport(TestEnvironment env) { return TestExportInfo.fromEnv(env).isExport(); } /** * Reads a jar file name from the environment and verifies that the jar file exists and * readable. * @param env TestEnvironment where the variable is set. * @param key key of the variable * @return Fully qualified filename of the jar * @throws TestEnvironment.Fault if there is an error looking up the variable. * @throws IllegalArgumentException if the returned file does not exist or is not readable, * in which case the message in the exception is the file name. * @throws NullPointerException if the returned entry for the key is null or a string array * of zero length. */ public static String getJarNameFromEnv(TestEnvironment env, String key) throws TestEnvironment.Fault, IllegalArgumentException, NullPointerException { File file = getJarFromEnv(env, key); if (file == null) { return null; } return file.getAbsolutePath(); } /** * Reads a jar filename from the environment and verifies * that the jar file exists and readable. * * @param env TestEnvironment where the variable is set. * @param key key of the variable * @return Jar file. * @throws TestEnvironment.Fault if there is an error looking up the variable. * @throws IllegalArgumentException if the returned file does not exist or is not readable, * in which case the message in the exception is the file name. * @throws NullPointerException if the returned entry for the key is null or a string array * of zero length. */ public static File getJarFromEnv(TestEnvironment env, String key) throws TestEnvironment.Fault, IllegalArgumentException, NullPointerException { String[] envData; String jarName; envData = env.lookup(key); if (envData == null || envData.length == 0) { throw new NullPointerException(); } jarName = envData[0]; File f = new File(jarName); if (!fileExistsAndReadable(f)) { throw new IllegalArgumentException(jarName); } return f; } private static boolean fileExistsAndReadable(File file) { return (file != null && !file.isDirectory() && file.canRead()); } /** * Reads a path from the environment and verifies that the jar files or * directories exist and readable. * * @param env TestEnvironment where the variable is set. * @param key key of the variable * @return List of the existing and readable files or directories. * @throws TestEnvironment.Fault if there is an error looking up the variable. * @throws IllegalArgumentException if the path does not contains at least * one existing and readable file or directory. * @throws NullPointerException if the returned entry for the key is null or a string array * of zero length. */ public static List<URL> getPathFromEnv(TestEnvironment env, String key) throws TestEnvironment.Fault, IllegalArgumentException, NullPointerException { ArrayList<URL> retVal = new ArrayList<URL>(); String[] envData = null; envData = env.lookup(key); if (envData == null || envData.length == 0) { throw new NullPointerException(); } String path = envData[0]; for (String entry : path.split(File.pathSeparator)) { if ((entry == null) || (entry.equals(""))) { continue; } try { File f = new File(entry); if (fileExistsAndReadable(f)) { retVal.add(new File(entry).toURI().toURL()); } } catch (Exception e) { // Ignore } } if (retVal.size() == 0) { throw new IllegalArgumentException(path); } return retVal; } /** * Converts host address to make it applicable for use in URL * accordingly with RFC 2732: "Format for Literal IPv6 Addresses in URLs" * * It means that if address contains ':' (which means it represents raw * IPv6 address) then it is put into square brackets * * @param addr host address * @return host address put into square brackets if it contains ':', * host address as is otherwise */ public static String getURLFmtAddress(String addr) { if (addr!=null && addr.indexOf(':')!=-1 && addr.indexOf('[') != 0) { return "[" + addr + "]"; } else { return addr; } } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?