package nxm.sys.test.junit.libg;

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

import nxm.sys.lib.Midas;
import nxm.sys.libg.MJPanel;
import nxm.sys.test.junit.NMTestCase;

/** This will test the MJPanel class.
 *  Initial testing is for this JDK warning
 *    warning: warning: [serial] non-transient instance field of a serializable class declared with a non-serializable type
 *  @author  Zachary Matthews
 *  @since   NeXtMidas 4.1.1
 */
public final class MJPanelTest extends NMTestCase {

  /** public constructor to allow running this test case directly with JUnit. */
  public MJPanelTest (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 MJPanelTest (String methodName, Midas midas) {
    super(methodName, midas);
  }

  /** Test the case where MJPanel serialization will fail */
  public void testUnserializable () {
    MJPanel mjPanel = new MJPanel();
    File ramFile = null;
    try {
      ramFile = File.createTempFile("mjpanelserializationtest", "temp");
      FileOutputStream fos = new FileOutputStream(ramFile);
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(mjPanel);
      oos.flush();
      fos.close();
      FileInputStream fis = new FileInputStream(ramFile);
      ObjectInputStream ois = new ObjectInputStream(fis);
      ois.readObject();
      fis.close();
    }
    catch (UnsupportedOperationException e) {
      String expected = "java.lang.UnsupportedOperationException: " + MJPanel.UNSERIALIZABLE_MJPANEL_OBJECT_MESSAGE;
      assertEquals("MJPanel can not serialize unserializable class", expected, e.toString());
    }
    catch (Exception e) {
      fail("Unexpected Exception during serialization:"+e);
    }
    finally {
      ramFile.delete();
    }
  }
}
