package org.jdom.tests;

import junit.framework.Test;
import junit.framework.TestSuite;
import junit.ui.LoadingTestRunner;

import org.jdom.Comment;
import org.jdom.IllegalCommentException;


public class CommentTest extends junit.framework.TestCase {
   

  public CommentTest(String name) {
    super(name); 
  }

  private String [] texts = { "", "Here's a comment with some plain text",
    "  Here's a comment with some plain text and extra white space ",
    "<P>  Here's a comment with some markup &amp; such </b><P> " };
  private Comment[] fixtures;

  protected void setUp() {

    fixtures = new Comment[texts.length];
    for (int i = 0; i < texts.length; i++) {
      fixtures[i] = new Comment(texts[i]);
    }    
    
  }

  public void testGetText() {
    
    for (int i = 0; i < fixtures.length; i++) {
      assert(fixtures[i].getText().equals(texts[i]));
    }    
    
  }
  
  public void testClone() {
    
    for (int i = 0; i < fixtures.length; i++) {
      Comment c = (Comment) fixtures[i].clone();
      assert(!fixtures[i].equals(c));
      assertEquals(c.getText(), fixtures[i].getText());
      assertEquals(c.toString(), fixtures[i].toString());
      assertEquals(c.getSerializedForm(), fixtures[i].getSerializedForm());
    }    
    
  }
  
  public void testSetText() {
    
    Comment c = new Comment("");
    for (int i = 0; i < texts.length; i++) {
      c.setText(texts[i]);
      assert(c.getText().equals(texts[i]));
    }    
    
    try {
      c.setText("--");
      fail("Expected IllegalCommentException");
    }
    catch (IllegalCommentException ex) { // expected result
    } 
    
    try {
      c.setText(" -- ");
      fail("Expected IllegalCommentException");
    }
    catch (IllegalCommentException ex) { // expected result
    } 
  
    try {
      c.setText("----");
      fail("Expected IllegalCommentException");
    }
    catch (IllegalCommentException ex) { // expected result
    } 
  
    try {
      c.setText(" lkhdsjf---fdslkfjdlkf ");
      fail("Expected IllegalCommentException");
    }
    catch (IllegalCommentException ex) { // expected result
    } 
  
    try {
      c.setText("--fdslkfjdlkf ");
      fail("Expected IllegalCommentException");
    }
    catch (IllegalCommentException ex) { // expected result
    } 
  
  }
  
  public void testConstructor() {
    
    try {
      new Comment(" -->");
      fail("Expected IllegalCommentException");
    }
    catch (IllegalCommentException ex) { // expected result
    } 
    
    try {
      new Comment(" -- ");
      fail("Expected IllegalCommentException");
    }
    catch (IllegalCommentException ex) { // expected result
    } 
    
  }
  

  public void testToString() {
    
    for (int i = 0; i < fixtures.length; i++) {
      assertNotNull(fixtures[i].toString());
    }    
    
  }
  
  public void testGetSerializedForm() {
    
    for (int i = 0; i < fixtures.length; i++) {
      String s = fixtures[i].getSerializedForm();
      assertNotNull(s);
      assert(s.startsWith("<!--"));
      assert(s.endsWith("-->"));
    }    
    
  }
  
  public void testEquals() {
    
    Comment c1 = new Comment("skd");
    Comment c2 = new Comment("skd");
    assertEquals(c1, c1);
    assert(!c1.equals(c2));
    for (int i = 0; i < fixtures.length; i++) {
      assert(!fixtures[i].equals(c1));
    }    
    
  }
  
  public void testHashCode() {
    
    Comment c1 = new Comment("skd");
    Comment c2 = new Comment("skd");
    assert(c1.hashCode() != c2.hashCode());
    for (int i = 0; i < fixtures.length; i++) {
      assert(fixtures[i].hashCode() != c1.hashCode());
    }    
    
  }
  
  public static Test suite() { 
    return new TestSuite(CommentTest.class); 
  }

	public static void main (String[] args) {
	  String[] tests = {"org.jdom.tests.CommentTest"};
		LoadingTestRunner.main(tests);
	}

}