Java Serialize serializeToSurefireDirectory(final Class testClass, final Object object)

Here you can find the source of serializeToSurefireDirectory(final Class testClass, final Object object)

Description

Serializes the given object to the target/surefire-reports/ directory.

License

Open Source License

Parameter

Parameter Description
testClass The test class, which will determine the filename.
object The object to serialize, or null if none.

Declaration

public static void serializeToSurefireDirectory(final Class<?> testClass, final Object object) 

Method Source Code


//package com.java2s;
/*//from   www .ja  va2 s  . c om
 *    Geotoolkit.org - An Open Source Java GIS Toolkit
 *    http://www.geotoolkit.org
 *
 *    (C) 2008-2012, Open Source Geospatial Foundation (OSGeo)
 *    (C) 2009-2012, Geomatys
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 */

import java.io.*;

public class Main {
    /**
     * Serializes the given object to the {@link target/surefire-reports/} directory.
     * This method can be invoked after a test failure in order to allow the developer
     * to examine the objects that caused the failure. In case of I/O error the problem
     * is reported on the standard error stream but no exception is thrown in order to
     * not mask the main problem, which was the test failure.
     *
     * @param  testClass The test class, which will determine the filename.
     * @param  object The object to serialize, or {@code null} if none.
     */
    public static void serializeToSurefireDirectory(final Class<?> testClass, final Object object) {
        if (object == null) {
            return;
        }
        File file = new File("target");
        if (!file.isDirectory() || !file.canWrite()) {
            System.err.println("Directory not found or read only: " + file.getAbsolutePath());
            return;
        }
        file = new File(file, "surefire-reports");
        if (!file.exists() && !file.mkdir()) {
            System.err.println("Failed to create the output directory: " + file.getAbsolutePath());
            return;
        }
        file = new File(file, testClass.getName() + ".serialized");
        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file))) {
            out.writeObject(object);
        } catch (IOException e) {
            System.err.println(e);
            file.delete();
        }
    }
}

Related

  1. serializeToBase64(Serializable object)
  2. serializeToFile(Object obj, String fileName)
  3. serializeToFile(Object object, File file)
  4. serializeToFile(Serializable s, String path)
  5. serializeToString(Object o)
  6. serializeUnserialize(Object ob)