7
\$\begingroup\$

I've Implemented a Serialization Utility which Serializes/DeSerializes a class, along with writing JUnit Test Cases for the same.

Please Review the code and Suggest me how to improve it. Particularly in generics as I've not written much code with Generics in java.

SerializableEmployee.java - Class to be Serialized

public class SerializableEmployee implements Serializable{

    /**
     * This Serial version UID will change once any new version of class has been created
     */
    private static final long serialVersionUID = 1L;

    private String name;

    public SerializableEmployee(String name) {
        this.name = name;
    }

    public String getName(){
        return name;
    }

    @Override
    public String toString() {
        return "SerializableEmployee{" +
                "name='" + name + '\'' +
                '}';
    }
}

SerializationUtil.java - Util for Serializing and Deserializing

/**
 * Created by shashi on 21/12/15.
 *
 * @param <T> Type of class to Serialize and Deserialize from
 */
public class SerializationUtil<T> {

    /**
     * Logger for the class
     */
    public Logger LOG = LoggerFactory.getLogger("WHAT to Write here instead of T.class");

    /**
     * Serializes the given {@link T} object and save it into a file
     * @param type {@link T} object to be Serialized
     * @param fileName File in which {@link T} object is to be saved
     *
     * @return boolean specifying if {@link T} object has been serialized
     */
    public boolean serialize(T type, String fileName) {

        try(FileOutputStream fileStream = new FileOutputStream(fileName);
                ObjectOutputStream objectStream = new ObjectOutputStream(fileStream)) {
            objectStream.writeObject(type);
        }catch(IOException inputOutputException){
            LOG.error("Exception Occurred while Serializing {}. Message is ", "WHAT to Write here instead of T.class", inputOutputException.getMessage());

            return false;
        }

        return true;
    }

    /**
     * De-Serializes the given {@link T} object from the given file
     * @param fileName File from which {@link T} object has to be retrieved
     *
     * @return deserialized {@link T} object from the file
     */
    public T deSerialize(String fileName) {

        T employee = null;
        try(FileInputStream fileStream = new FileInputStream(fileName);
            ObjectInputStream objectStream = new ObjectInputStream(fileStream)) {
            employee = (T)objectStream.readObject();
        }catch(IOException | ClassNotFoundException inputOutputException){
            LOG.error("Exception Occurred while Serializing {}. Message is ", "WHAT to Write here instead of T.class", inputOutputException.getMessage());
        }

        return employee;
    }
}

SerializationUtilTest.java - jUnit Test cases for SerializationUtil class

/**
 * Created by shashi on 21/12/15.
 */
public class SerializationUtilTest {

    // Employee to Deserialize
    private static SerializableEmployee employee;
    // De-Serialized Employee
    private static SerializableEmployee deSerializedEmployee;

    // Utility class for Serialization
    private static SerializationUtil<SerializableEmployee> serializationUtil;

    // Provide Abstraction for Constants used
    interface Constants {
        String EMP_NAME = "Shashi";
        String FILE_NAME = "Emp-File";
    }

    @Before
    public void setUp(){
        employee = new SerializableEmployee(Constants.EMP_NAME);

        serializationUtil = new SerializationUtil<SerializableEmployee>();

        serializationUtil.serialize(employee, Constants.FILE_NAME);
        deSerializedEmployee = serializationUtil.deSerialize(Constants.FILE_NAME);
    }

    @Test
    public void employeeNotNullTest(){
        assertTrue(deSerializedEmployee != null);
    }

    @Test
    public void employeeNameEqualTest(){
        assertTrue(Constants.EMP_NAME.equals(deSerializedEmployee.getName()));
    }
}
\$\endgroup\$
  • 3
    \$\begingroup\$ Welcome to the site! "I'll keep adding updated code so please review the latest one." - That's not how Code Review handles new versions of code. Instead you should wait a while after posting this to give people time to review, then when you have a new version of your code you can post a new question and link to this one, explaining that it's an update. For more info please see this meta post. \$\endgroup\$ – SuperBiasedMan Dec 21 '15 at 10:45
  • \$\begingroup\$ Sorry if it's asking too much, but will sharing a link to version control system work here. I don't know how code reviews are done here basically. Thanks anyway for the comment :) \$\endgroup\$ – Shashi Dec 21 '15 at 11:23
  • \$\begingroup\$ I believe that's ok if you want people to be able to follow what you've changed, but people will review the code that's posted here. It's so that it's clear that the reviews are relevant to the specific unchanged code here, there's no confusion with when a review was posted and which state the code was in at that point. \$\endgroup\$ – SuperBiasedMan Dec 21 '15 at 11:28
4
\$\begingroup\$
  • SerializationUtil should work on T extends Serializable instead of T. This makes misuse harder.

  • Prefixing serializable to a data-holder is unnecessary for almost all intents and purposes.

  • You may be interested in operating on Path instead of String when Serializing and deSerializing

  • Your Util does not provide a way to serialize multiple instances of the same class :/

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.