869a36e2649ae064c98063cf1e55198488d78d12Allan Foster/**
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * Copyright (c) 2008 Sun Microsystems Inc. All Rights Reserved
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * The contents of this file are subject to the terms
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * of the Common Development and Distribution License
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * (the License). You may not use this file except in
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * compliance with the License.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * You can obtain a copy of the License at
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * opensso/legal/CDDLv1.0.txt
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * See the License for the specific language governing
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * permission and limitations under the License.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * When distributing Covered Code, include this CDDL
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * Header Notice in each file and include the License file
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * at opensso/legal/CDDLv1.0.txt.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * If applicable, add the following below the CDDL Header,
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * with the fields enclosed by brackets [] replaced by
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * your own identifying information:
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster *
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * $Id: UnittestLog.java,v 1.1 2009/08/19 05:41:03 veiming Exp $
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster */
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterpackage com.sun.identity.unittest;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.iplanet.am.util.SystemProperties;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport com.sun.identity.shared.Constants;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.io.File;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.io.FileWriter;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.io.IOException;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.util.ArrayList;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.util.Iterator;
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterimport java.util.List;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster/**
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster * Logging the test activities.
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster */
869a36e2649ae064c98063cf1e55198488d78d12Allan Fosterpublic class UnittestLog {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static List buffer = new ArrayList();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public static synchronized List flush(String datestamp) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster List tmp = buffer;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (datestamp != null) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster writeToFile(datestamp);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster buffer = new ArrayList();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster return tmp;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static void writeToFile(String datestamp) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster String basedir = SystemProperties.get(SystemProperties.CONFIG_PATH) +
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster SystemProperties.get(Constants.AM_SERVICES_DEPLOYMENT_DESCRIPTOR);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (basedir.equals("nullnull")) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster for(Object obj : buffer) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster System.out.println(obj);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster return;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster File f = new File(basedir + "/unittest");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster f.mkdir();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster StringBuffer buff = new StringBuffer();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster for (Iterator i = buffer.iterator(); i.hasNext(); ) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster String s = (String)i.next();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster buff.append(s).append("\n");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster writeToFile(basedir + "/unittest/" + datestamp, buff.toString());
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster private static void writeToFile(String fileName, String content) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster FileWriter fout = null;
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster try {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster fout = new FileWriter(fileName, true);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster fout.write(content);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster } catch (IOException e) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster //No handling requried
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster } finally {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster if (fout != null) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster try {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster fout.close();
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster } catch (Exception ex) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster //No handling requried
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public static synchronized void logMessage(String msg) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster buffer.add("MESSAGE: " + msg);
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public static synchronized void logError(String msg) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster buffer.add("<font color=\"red\">ERROR: " + msg + "</font>");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster public static synchronized void logError(String err, Throwable e) {
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster buffer.add("<font color=\"red\">ERROR: " + err + " exception: " +
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster e.getMessage() + "</font>");
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster }
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster}
869a36e2649ae064c98063cf1e55198488d78d12Allan Foster