package org.flymine.util;

import junit.framework.TestCase;

import org.flymine.model.testmodel.*;

public class ModelUtilTest extends TestCase
{
    public ModelUtilTest(String arg) {
        super(arg);
    }

    public void testGetFieldTypeCollection() {
        assertEquals(ModelUtil.COLLECTION, ModelUtil.getFieldType(Company.class, "departments"));
    }

    public void testGetFieldTypeAttribute() {
        assertEquals(ModelUtil.ATTRIBUTE, ModelUtil.getFieldType(Company.class, "name"));
    }

    public void testGetFieldTypeReference() {
        assertEquals(ModelUtil.REFERENCE, ModelUtil.getFieldType(Company.class, "address"));
    }

    public void testGetFieldTypeInvalidField() {
        assertEquals(-1, ModelUtil.getFieldType(Company.class, "noSuchField"));
    }


    public void testHasValidKeyValidObject() throws Exception {
        Address address = new Address();
        address.setAddress("Employee Street, BVille");

        CEO employee = new CEO();
        employee.setName("EmployeeB1");
        employee.setFullTime(true);
        employee.setAddress(address);
        employee.setAge(40);
        employee.setTitle("Mr.");
        employee.setSalary(45000);

        assertTrue(ModelUtil.hasValidKey(employee));
    }

    public void testHasValidKeyInvalidObjectNoReference() throws Exception {

        CEO employee = new CEO();
        employee.setName("EmployeeB1");
        employee.setFullTime(true);
        employee.setAge(40);
        employee.setTitle("Mr.");
        employee.setSalary(45000);

        assertFalse(ModelUtil.hasValidKey(employee));
    }

    public void testHasValidKeyInvalidObjectNoAttribute() throws Exception {
        Address address = new Address();
        address.setAddress("Employee Street, BVille");

        CEO employee = new CEO();
        employee.setFullTime(true);
        employee.setAddress(address);
        employee.setAge(40);
        employee.setTitle("Mr.");
        employee.setSalary(45000);

        assertFalse(ModelUtil.hasValidKey(employee));
    }

    public void testHasValidKeyNullObject() throws Exception {
        try {
            ModelUtil.hasValidKey(null);
            fail ("Expected: NullPointerException");
        } catch (NullPointerException e) {
        }
    }


}

@@ -213,581 +213,43 @@
     
-public void testGetFieldTypeCollection() %7B%0A        assertEquals(ModelUtil.COLLECTION, ModelUtil.getFieldType(Company.class, %22departments%22));%0A    %7D%0A%0A    public void testGetFieldTypeAttribute() %7B%0A        assertEquals(ModelUtil.ATTRIBUTE, ModelUtil.getFieldType(Company.class, %22name%22));%0A    %7D%0A%0A    public void testGetFieldTypeReference() %7B%0A        assertEquals(ModelUtil.REFERENCE, ModelUtil.getFieldType(Company.class, %22address%22));%0A    %7D%0A%0A    public void testGetFieldTypeInvalidField() %7B%0A        assertEquals(-1, ModelUtil.getFieldType(Company.class, %22noSuchField%22));%0A    %7D%0A%0A
+// key of CEO is %22name age address%22
 %0A   
@@ -496,37 +496,30 @@
 employee.set
-FullTime(true
+Age(40
 );%0A        e
@@ -550,106 +550,8 @@
 ss);
-%0A        employee.setAge(40);%0A        employee.setTitle(%22Mr.%22);%0A        employee.setSalary(45000);
 %0A%0A  
@@ -685,17 +685,16 @@
 ption %7B%0A
-%0A
         
@@ -693,32 +693,27 @@
         CEO 
-employee
+ceo
  = new CEO()
@@ -718,32 +718,27 @@
 ();%0A        
-employee
+ceo
 .setName(%22Em
@@ -761,130 +761,20 @@
     
-employee.setFullTime(true);%0A        employee.setAge(40);%0A        employee.setTitle(%22Mr.%22);%0A        employee.setSalary(4500
+ceo.setAge(4
 0);%0A
@@ -812,32 +812,27 @@
 hasValidKey(
-employee
+ceo
 ));%0A    %7D%0A%0A 
@@ -1012,32 +1012,27 @@
         CEO 
-employee
+ceo
  = new CEO()
@@ -1045,33 +1045,21 @@
     
-employee.setFullTime(true
+ceo.setAge(40
 );%0A 
@@ -1061,32 +1061,27 @@
 0);%0A        
-employee
+ceo
 .setAddress(
@@ -1093,106 +1093,8 @@
 ss);
-%0A        employee.setAge(40);%0A        employee.setTitle(%22Mr.%22);%0A        employee.setSalary(45000);
 %0A%0A  
@@ -1129,32 +1129,27 @@
 hasValidKey(
-employee
+ceo
 ));%0A    %7D%0A%0A 
@@ -1377,12 +1377,10 @@
 %7D%0A    %7D%0A
-%0A%0A
 %7D%0A

make clear which are key fields