package org.jdom.tests;

import junit.framework.Test;
import junit.framework.TestSuite;
import junit.ui.LoadingTestRunner;

import org.jdom.Attribute;
import org.jdom.IllegalNameException;
import org.jdom.DataConversionException;


public class AttributeTest extends junit.framework.TestCase {


  public AttributeTest(String name) {
    super(name);
  }

  protected void setUp() {

  }

  public void testConstructor() {
  	
    Attribute a = new Attribute("test", "true");    
    Attribute b = new Attribute("test", "");    
    try {    
      Attribute c = new Attribute(null, "true");
      fail("Attribute names can't be null");    
    }
    catch (NullPointerException e) {} // Expected result
    try {    
      Attribute d = new Attribute("aa", null);
      fail("Attribute values can't be null");    
    }
    catch (NullPointerException e) {} // Expected result
    try {    
      Attribute e = new Attribute("", "jh");
      fail("Attribute names can't be empty strings");    
    }
    catch (IllegalNameException e) {} // Expected result
    try {    
      Attribute f = new Attribute("12", "true");
      fail("Attribute names can't begin with numbers");    
    }
    catch (IllegalNameException e) {} // Expected result
    try {    
      Attribute g = new Attribute("test space", "true");
      fail("Attribute names can't contain spaces");    
    }
    catch (IllegalNameException e) {} // Expected result
    try {    
      Attribute g = new Attribute(":space", "true");
      fail("Attribute names begin with colons");    
    }
    catch (IllegalNameException e) {} // Expected result
    try {    
      Attribute g = new Attribute("spac?e", "true");
      fail("Attribute names can't contain question marks");    
    }
    catch (IllegalNameException e) {} // Expected result
    
  }
  
  public void testClone() {
  	
  }
  
  public void testGetBooleanValue() throws DataConversionException {

    Attribute a = new Attribute("test", "true");    
    assert(a.getBooleanValue());
    Attribute b = new Attribute("test", "false");    
    assert(!b.getBooleanValue());
    Attribute c = new Attribute("test", "yes");    
    assert(c.getBooleanValue());
    
    a = new Attribute("test", "TRUE");    
    assert(a.getBooleanValue());
    a = new Attribute("test", "tRuE");    
    assert(a.getBooleanValue());
    a = new Attribute("test", "False");    
    assert(!a.getBooleanValue());
    
    try {    
      c = new Attribute("lk", "dzkhfdk");
      c.getBooleanValue();
      fail("Expected DataConversionException");
    }
    catch (DataConversionException e) {} // Expected result
    try {
      Attribute d = new Attribute("test", ""); 
      d.getBooleanValue();
      fail("Expected DataConversionException");
    }
    catch (DataConversionException e) {} // Expected result
    
  }
  
  public void testGetBooleanValueBoolean() {

    Attribute a = new Attribute("test", "true");    
    assert(a.getBooleanValue(true));
    assert(a.getBooleanValue(false));
    Attribute b = new Attribute("test", "false");    
    assert(!b.getBooleanValue(true));
    assert(!b.getBooleanValue(false));
    Attribute c = new Attribute("test", "yes");    
    assert(a.getBooleanValue(true));
    assert(a.getBooleanValue(false));
    
    a = new Attribute("test", "TRUE");    
    assert(a.getBooleanValue(true));
    assert(a.getBooleanValue(false));
    a = new Attribute("test", "tRuE");    
    assert(a.getBooleanValue(true));
    assert(a.getBooleanValue(false));
    b = new Attribute("test", "False");    
    assert(!b.getBooleanValue(true));
    assert(!b.getBooleanValue(false));
    
    c = new Attribute("c", "dzkhfdk");
    assert(c.getBooleanValue(true));
    assert(!c.getBooleanValue(false));
    
    Attribute d = new Attribute("c", "");
    assert(d.getBooleanValue(true));
    assert(!d.getBooleanValue(false));
  	
  }
  
  public void testGetByteValue() {
  
    for (int i = -128; i <= 127; i++) {
      Attribute a = new Attribute("byte", String.valueOf(i));
      try {
        assertEquals(a.getByteValue(), i);
      }
      catch (DataConversionException e) {
      	fail("Could not convert " + a.getValue() + " to " + i);
      }	
    }

    String[] badValues = {"543", "65.5", "-987", "", " 10 ", "10 Q", "false"};
    for (int i = 0; i < badValues.length; i++) {
      try {
        Attribute b = new Attribute("bad", badValues[i]);
      	b.getByteValue();
      	fail("Should have thrown a DataConversionException for " + badValues[i]);
      }
      catch (DataConversionException e) { // expected result
      }
    }
  	
  }
  
  public void testGetByteValueByte() {
  
    for (int i = -127; i <= 127; i++) {
      Attribute a = new Attribute("byte", String.valueOf(i));
      assertEquals(a.getByteValue((byte) -128), i);	
    }
    
    String[] badValues = {"543", "65.5", "-987", "", " 10 ", "10 Q", "false"};
    for (int i = 0; i < badValues.length; i++) {
        Attribute b = new Attribute("bad", badValues[i]);
      	assertEquals(-128, b.getByteValue((byte) -128));
    }
    	
  }
  
  public void testGetCharValue() {

    // what are rules for white space normalization and how do they affect us here????
    for (int c = 32; c <= 65535; c++) {
      if (Character.isLetterOrDigit((char) c)) {
        Attribute a = new Attribute("char", String.valueOf((char) c));
        try {
          assertEquals(a.getCharValue(), (char) c);
        }
        catch (DataConversionException e) {
          fail("Could not convert " + a.getValue() + " to " + (char) c);
        }
      }	
    }

    String[] badValues = {"543", "65.5", "-987", "", " 10 ", "10 Q", "false", "yt"};
    for (int i = 0; i < badValues.length; i++) {
      try {
        Attribute b = new Attribute("bad", badValues[i]);
      	b.getCharValue();
      	fail("Should have thrown a DataConversionException for " + badValues[i]);
      }
      catch (DataConversionException e) { // expected result
      }
    }
  	
  }
  
  public void testGetCharValueChar() {
  	
    // what are rules for white space normalization and how do they affect us here????
    for (int c = 32; c <= 65535; c++) {
      if (Character.isLetterOrDigit((char) c)) {
        Attribute a = new Attribute("char", String.valueOf((char) c));
        assertEquals(a.getCharValue((char) 0), (char) c);
      }	
    }

    String[] badValues = {"543", "65.5", "-987", "", " 10 ", "10 Q", "false", "yt"};
    for (int i = 0; i < badValues.length; i++) {
        Attribute b = new Attribute("bad", badValues[i]);
      	assertEquals(0, b.getCharValue((char) 0));
    }  	
    
  }
  
  public void testGetDoubleValue() {
  	
  }
  
  public void testGetDoubleValueDouble() {
  	
  }
  
  public void testGetFloatValue() {
  	
  }
  
  public void testGetFloatValueFloat() {
  	
  }
  
  public void testGetIntValue() {
  	
  }
  
  public void testGetIntValueInt() {
  	
  }
  
  public void testGetLongValue() {
  	
  }
  
  public void testGetLongValueLong() {
  	
  }
  
  public void testGetName() {
  	
  }
  
  public void testGetNamespacePrefix() {
  	
  }
  
  public void testGetNamespaceURI() {
  	
  }
  
  public void testGetQualifiedName() {
  	
  }
  
  public void testGetValue() {
  	
  }
  
  public void testSetValue() {
 
  }
  
  public static Test suite() {
    return new TestSuite(AttributeTest.class);
  }

  public static void main (String[] args) {
    String[] tests = {"org.jdom.tests.AttributeTest"};
    LoadingTestRunner.main(tests);
  }

}