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.TableSorter;
import nxm.sys.test.junit.NMTestCase;

/** This will test the TableSorter 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 TableSorterTest extends NMTestCase {

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

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