// @(#)$Id: FSEntityTest.scala,v 1.1 2005/10/28 09:03:35 leavens Exp leavens $
package scalahw;

import FSEntity._;
import testing._;

/** Testing for FSEntity
 * @author Gary T. Leavens
 */
object FSEntityTest with ConsoleTestRunner {

  type HashMap = scala.collection.mutable.HashMap[String,FSEntity];

  /** Cook a directory with fse as the value of .. */
  def dirWithDotDot(fse: FSEntity): Dir = {
    val d = Dir(new HashMap());
    d.m.update("..", fse);
    d
  }

  /** Cook a root directory */
  def rootDir: Dir = {
    val d: Dir = Dir(new HashMap());
    d.m.update("..", d);
    d
  } 
  val root1 = rootDir;

  val br2: FSEntity = dirWithDotDot(File(".."));

  val root2 = rootDir;
  val root3 = dirWithDotDot(root2);

  val br = Dir(new HashMap());
  val root4 = dirWithDotDot(br);

  val br5 = Dir(null);

  val root6 = {
    val d: Dir = Dir(new HashMap());
    d.m.update("..", d);
    d.m.update("home", File("junk"));
    d
  }

  val hd = {
    val r7: Dir = Dir(new HashMap());
    r7.m.update("..", r7);
    val d = dirWithDotDot(r7);
    r7.m.update("home", d);
    d.m.update("f1", File(""));
    d.m.update("junk", File("nada"));
    d
  }

  val str1 = "my contents";
  val str2 = "another one";
  val br7 = {
    val d = Dir(new HashMap());
    d.m.update("z", File(""));
    d
  }

  val r0: Dir = {
    val d = Dir(new HashMap());
    d.m.update("..", d);
    d
  }

  val (root8::hd8::rushome::r541::gtlhome::g541::g342::Nil): List[Dir] = {
    val root1 = Dir(new HashMap());
    root1.m.update("..", root1);
    val hd = dirWithDotDot(root1);
    val rushome = dirWithDotDot(hd);
    val r541 = dirWithDotDot(rushome);
    val gtlhome = dirWithDotDot(hd);
    val g541 = dirWithDotDot(gtlhome);
    val g342 = dirWithDotDot(gtlhome);
    root1.m.update("home", hd);
    hd.m.update("f1", File(""));
    hd.m.update("junk", File("nada"));
    hd.m.update("rhe", rushome);
    hd.m.update("gtl", gtlhome);
    rushome.m.update("cs541", r541);
    rushome.m.update(".login", File("!/bin/sh..."));
    r541.m.update("Fib.scala", File("object Fib..."));
    gtlhome.m.update("cs541", g541);
    gtlhome.m.update("cs342", g342);
    gtlhome.m.update("root", root1);
    g541.m.update("hw4.tex", File("Write ..."));
    g342.m.update("hw0.tex", File("Hand in ..."));
    root1::hd::rushome::r541::gtlhome::g541::g342::Nil
  }

  val dotdot = "..";
  val unk = "unk";
  val home = "home";
  val rhe = "rhe";
  val gtl = "gtl";
  val cs541 = "cs541";
  val hw4 = "hw4.tex";
  val x = "x";
  
  /** The actual tests to run. */    
  val suite = new TestSuite(
    // test okFSEntity
    new Expect("File(str1).okFSEntity", File(str1).okFSEntity, true),
    new Expect("File(str2).okFSEntity", File(str2).okFSEntity, true),
    new Expect("Dir(new HashMap()).okFSEntity", Dir(new HashMap()).okFSEntity, false),
    new Expect("br7.okFSEntity", br7.okFSEntity, false),
    new Expect("root1.okFSEntity", root1.okFSEntity, true),
    new Expect("br2.okFSEntity", br2.okFSEntity, false),
    new Expect("root3.okFSEntity", root3.okFSEntity, true),
    new Expect("root4.okFSEntity", root4.okFSEntity, true),
    new Expect("br5.okFSEntity", br5.okFSEntity, false),
    new Expect("root6.okFSEntity", root6.okFSEntity, true),
    new Expect("hd.okFSEntity", hd.okFSEntity, true),

    // test fetch
    new ExpectTrue("r0.fetch(List(dotdot)) eq r0",
		   r0.fetch(List(dotdot)) eq r0),
    new ExpectTrue("root8.fetch(List(dotdot)) eq root8",
		   root8.fetch(List(dotdot)) eq root8),
    new ExpectTrue("root8.fetch(List(home)) eq hd8",
		   root8.fetch(List(home)) eq hd8),
    new ExpectTrue("root8.fetch(List(dotdot, home)) eq hd8",
		   root8.fetch(List(dotdot, home)) eq hd8),
    new ExpectTrue("root8.fetch(List(dotdot, home, dotdot)) eq root8",
		   root8.fetch(List(dotdot, home, dotdot)) eq root8),
    new ExpectTrue("hd8.fetch(List(rhe)) eq rushome",
		   hd8.fetch(List(rhe)) eq rushome),
    new ExpectTrue("root8.fetch(List(home, rhe)) eq rushome",
		   root8.fetch(List(home, rhe)) eq rushome),
    new ExpectTrue("hd8.fetch(List(dotdot, home, rhe)) eq rushome",
		   hd8.fetch(List(dotdot, home, rhe)) eq rushome),
    new ExpectTrue("hd8.fetch(List(dotdot, home, dotdot, home, rhe)) eq rushome",
		   hd8.fetch(List(dotdot, home, dotdot, home, rhe)) eq rushome),
    new ExpectTrue("root8.fetch(List(home, gtl, cs541)) eq g541",
		   root8.fetch(List(home, gtl, cs541)) eq g541),
    new Expect("root8.fetch(List(home, gtl, cs541, hw4))",
	       root8.fetch(List(home, gtl, cs541, hw4)), File("Write ...")),
    new ExpectException("r0.fetch(List(unk))",
			r0.fetch(List(unk))),
    new ExpectException("r0.fetch(List(dotdot, unk))",
			r0.fetch(List(dotdot, unk))),
    new ExpectException("r0.fetch(List(unk, dotdot, x))",
			r0.fetch(List(unk, dotdot, x))),

    new Expect("Done!", 1, 1)
    );
}
