package nxm.audio.test.junit.lib;

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

import org.junit.Before;

import nxm.audio.lib.hwplayer;
import nxm.sys.lib.Midas;
import nxm.sys.lib.Shell;
import nxm.sys.test.junit.NMTestCase;

/** This will test the hwplayer class.
 *
 *  @author  Zachary Matthews
 *  @since   NeXtMidas 4.1.2
 */
public final class hwplayerTest extends NMTestCase {

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

  private boolean skippingBecauseHeadless = false;

  @Before
  public void setUp () {
    if (Shell.isHeadless()) {
      skippingBecauseHeadless = true;
      return;
    }
  }

  /** Test the case for hwplayer serialization */
  public void testSerializable () {
    if (skippingBecauseHeadless) {
      getMidas().info("Skipping testSerializable because HEADLESS");
      return;
    }
    hwplayer hwp = new hwplayer();
    hwp.open();
    File ramFile = null;
    try {
      ramFile = File.createTempFile("hwplayerserializationtest", "temp");
      FileOutputStream fos = new FileOutputStream(ramFile);
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(hwp);
      oos.flush();
      fos.close();
      FileInputStream fis = new FileInputStream(ramFile);
      ObjectInputStream ois = new ObjectInputStream(fis);
      hwplayer hwpNew = (hwplayer)ois.readObject();
      fis.close();
      assertEquals("hwplayer max number of lines matches",hwp.getMaxLines(), hwpNew.getMaxLines());
    }
    catch (Exception e) {
      fail("Error during serialization:"+e);
    }
    finally {
      hwp.closeAll();
      ramFile.delete();
    }
  }
}
