package nxm.vnc.test.junit.libvnc.tightvnc;

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.test.junit.NMTestCase;
import nxm.vnc.libvnc.tightvnc.NxmViewer;

/** This will test the NxmViewer 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.2
 */
public final class NxmViewerTest extends NMTestCase {

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

  /** Test the case where NxmViewer serialization will fail */
  public void testUnserializable () {
    NxmViewer nxmViewer = new NxmViewer(getMidas(),null);
    File ramFile = null;
    try {
      ramFile = File.createTempFile("nxmviewerserializationtest", "temp");
      FileOutputStream fos = new FileOutputStream(ramFile);
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(nxmViewer);
      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: " + NxmViewer.UNSERIALIZABLE_VNCVIEWER_OBJECT_MESSAGE;
      assertEquals("NxmViewer can not serialize unserializable class", expected, e.toString());
    }
    catch (Exception e) {
      fail("Unexpected Exception during serialization:"+e);
    }
    finally {
      ramFile.delete();
    }
  }
}