package nxm.sys.test.junit.lib;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import nxm.sys.lib.Format;
import nxm.sys.lib.Midas;
import nxm.sys.lib.Shell;
import nxm.sys.test.junit.NMTestCase;

/** This will test the Format class.
 *
 *  @author  Jeanette McCall
 *  @since   NeXtMidas 4.1.1
 */
public final class FormatTest extends NMTestCase {

  private static final String READOUT_FORMAT_6PLACES = "###0.######";

  /** public constructor to allow running this test case directly with JUnit. */
  public FormatTest (String methodName) { this(methodName, null); }

  /** Creates a new instance of the class.
      @param methodName The name of the method being called.
      @param midas      The midas context to use when running the tests.
   */
  public FormatTest (String methodName, Midas midas) {
    super(methodName, midas);
  }

  /** Test the case for Format serialization */
  public void testSerializable () {
    Format fmt = new Format(READOUT_FORMAT_6PLACES);
    File ramFile = null;
    try {
      ramFile = File.createTempFile("formatserializationtest", "temp");
      FileOutputStream fos = new FileOutputStream(ramFile);
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(fmt);
      oos.flush();
      fos.close();
      FileInputStream fis = new FileInputStream(ramFile);
      ObjectInputStream ois = new ObjectInputStream(fis);
      Format    sfFmt       = (Format)ois.readObject();
      fis.close();
      assertEquals("Format pattern string matches",fmt.getFormat().toString(), sfFmt.getFormat().toString());
      assertEquals("Format pattern string matches",fmt.getFieldPosition().toString(), sfFmt.getFieldPosition().toString());
    }
    catch (Exception e) {
      fail("Error during serialization:"+e);
    }
    finally {
      ramFile.delete();
    }
  }

}
