Package org.spw.controller

Source Code of org.spw.controller.ContactControllerTest

/*
* ContactControllerTest.java
* JUnit based test
*
* Created on 26 May 2007, 19:12
*/

package org.spw.controller;

import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.EntityExistsException;
import junit.framework.*;
import org.spw.model.Contact;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.spw.model.Person;
import org.spw.model.Donation;
import org.spw.model.Document;

/**
*
* @author PSe
*/
public class ContactControllerTest extends TestCase {
    private static final long TEST_ID_ABC1 = 9999991L;
    private static final String TEST_FIRSTNAME_ABC1 = "ABCDEF";
    private static final long TEST_ID_BBC = 9999999L;
    private static final String TEST_FIRSTNAME_BBC = "BBC News";
    private static final PersonController ctrl = new PersonController();
   
   
    public ContactControllerTest(String testName) {
        super(testName);
    }
   
    protected void setUp() throws Exception {
        Person p1 = new Person();
        p1.setIdContact(TEST_ID_ABC1);
        p1.setFirstName(TEST_FIRSTNAME_ABC1);
        p1.setLastName(getName());
        ctrl.create(p1);
        for(long id = TEST_ID_ABC1+1; id < TEST_ID_ABC1 + 8; id++) {
            Person p = new Person();
            p.setIdContact(id);
            p.setFirstName(TEST_FIRSTNAME_ABC1 + id);
            p.setLastName(getName());
            ctrl.create(p);
        }
        Person p2 = new Person();
        p2.setIdContact(TEST_ID_BBC);
        p2.setFirstName(TEST_FIRSTNAME_BBC);
        p2.setLastName(getName());
        ctrl.create(p2);
    }
   
    protected void tearDown() throws Exception {
        for(long id = TEST_ID_ABC1; id < TEST_ID_ABC1 + 8; id++) {
            ctrl.delete(ctrl.read(id));
        }
        ctrl.delete(ctrl.read(TEST_ID_BBC));
       
        DonationController donationCtrl = new DonationController();
        for (Donation donation : donationCtrl.getDonations()) {
            if (donation.getStatus().startsWith("Test"))
                donationCtrl.delete(donation);
        }
        DocumentController docCtrl = new DocumentController();
        for (Document document : docCtrl.getDocuments()) {
            if (document.getDocumentType().startsWith("Test"))
                docCtrl.delete(document);
        }
    }
   
    /**
     * Test of parse method, of class org.spw.controller.ContactController.
     */
    public void testParse() {
        System.out.println("parse");
       
        String contact = "";
        ContactController instance = new ContactController();
       
        // --- Tests incorrects cases (return null)
        Contact expResult = null;
        //test empty string
        Contact result = instance.parse(contact);
        assertEquals("Parsing empty string should give a null contact",
                expResult, result);
       
        // test null string
        contact = null;
        result = instance.parse(contact);
        assertEquals("Parsing null string should give a null contact",
                expResult, result);
       
        // test incorrect number
        contact = "[199999999]"; //this case can't occur until few thousand of years
        result = instance.parse(contact);
        assertEquals("Parsing incorrect number should give a null contact",
                expResult, result);
       
        // test incorrect format number
        contact = "[1Z1]";
        result = instance.parse(contact);
        assertEquals("Parsing invalid format number should give a null contact",
                expResult, result);
       
        // --- Test correct case
        // test correct number
        expResult = new Contact();
        expResult.setNote("Test case");
        instance.create(expResult);
        contact = "[" + expResult.getIdContact().toString() + "]";
        result = instance.parse(contact);
        assertEquals("Parsing empty string should give a null contact",
                expResult, result);
        instance.delete(expResult);
    }
   
    /**
     * Test of getContacts method, of class org.spw.controller.ContactController.
     */
    public void testGetContacts() {
        System.out.println("getContacts");
       
        ContactController instance = new ContactController();
       
        List<Contact> result = instance.getContacts();
        assertNotNull(result);
        assertTrue("The contact list must contain some items", result.size() > 1);
       
    }
   
    /**
     * Test of getContactsBeginningBy method, of class org.spw.controller.ContactController.
     */
    public void testGetContactsBeginningBy() {
        System.out.println("getContactsBeginningBy");
       
        String beginning = TEST_FIRSTNAME_ABC1.substring(0,3);
        int maxResult = 0;
        ContactController instance = new ContactController();
       
        List<Contact> result = instance.getContactsBeginningBy(beginning, maxResult);
        assertNotNull(result);
        assertTrue("The test failed to find any contact beginning by " +
                beginning, result.size() > 0);
       
        boolean findABC1 = false;
        for(Contact contact : result) {
            assertTrue(contact.getName().startsWith(beginning));
            if (contact.getFirstName().equals(TEST_FIRSTNAME_ABC1)) findABC1 = true;
        }
        assertTrue("The test to find " + TEST_FIRSTNAME_ABC1 + " failed.", findABC1);
       
        // Test maxResult
        maxResult = 4;
        result = instance.getContactsBeginningBy(beginning, maxResult);
        assertNotNull(result);
        assertEquals("The test failed to limit the contact list beginning by " +
                beginning, 4, result.size());
       
    }
   
    /**
     * Test of create method, of class org.spw.controller.ContactController.
     */
    public void testCreate() {
        // check create a double thows an exception
        Person p1 = new Person();
        p1.setIdContact(TEST_ID_ABC1);
        p1.setFirstName(TEST_FIRSTNAME_ABC1);
        p1.setLastName(getName());
        try {
            ctrl.create(p1);
            fail("create a double should send an error");
        } catch (EntityExistsException eee) {
            //OK
        }
       
    }
   
    /**
     * Test of read method, of class org.spw.controller.ContactController.
     */
    public void testRead() {
        // assume that JPA is bug free
    }
   
    /**
     * Test of update method, of class org.spw.controller.ContactController.
     */
    public void testUpdate() {
        // assume that JPA is bug free
    }
   
    /**
     * Test of delete method, of class org.spw.controller.ContactController.
     */
    public void testDelete() {
        // assume that JPA is bug free
    }
   
    /**
     * Test of addDonation method, of class org.spw.controller.ContactController.
     */
    public void testAddDonation() {
        boolean found1 = false;
        boolean found2 = false;
        System.out.println("addDonation");
       
        ContactController instance = new ContactController();
        Contact contact = instance.read(TEST_ID_ABC1);
        Donation donation1 = new Donation();
        donation1.setDueDate(new Date());
        donation1.setProposalTitle(getName());
        donation1.setReasonNotFunded(getName());
        donation1.setStatus("Test1");
        donation1.setAmount(new BigDecimal(1.11));
       
        Donation donation2 = new Donation();
        donation2.setDueDate(new Date());
        donation2.setProposalTitle(getName());
        donation2.setReasonNotFunded(getName());
        donation2.setStatus("Test2");
        donation2.setAmount(new BigDecimal(2.22));
       
        donation1.setDonor(contact);
        donation2.setDonor(contact);
        donation1 = instance.addDonation(donation1);
        donation2 = instance.addDonation(donation2);
        assertEquals(contact, donation1.getDonor());
        assertEquals(contact, donation2.getDonor());
        for(Donation aDonation : donation2.getDonor().getDonations()) {
            if (aDonation.getProposalTitle().equals(getName())
            && aDonation.getReasonNotFunded().equals(getName())
            && aDonation.getStatus().equals("Test1")
            && aDonation.getAmount().equals(new BigDecimal(1.11))) {
                found1 = true;
            }
            if (aDonation.getProposalTitle().equals(getName())
            && aDonation.getReasonNotFunded().equals(getName())
            && aDonation.getStatus().equals("Test2")
            && aDonation.getAmount().equals(new BigDecimal(2.22))) {
                found2 = true;
            }
        }
        assertTrue("The added 1rst test donation is not found", found1);
        assertTrue("The added 2nd test donation is not found", found2);
       
        //check if donations can be read
        DonationController donationCtrl = new DonationController();
        assertNotNull(donationCtrl.read(donation1.getIdDonation()));
        assertNotNull(donationCtrl.read(donation2.getIdDonation()));
       
        //check update an existing donation
        donation1.setStatus("Test3");
        instance.addDonation(donation1);
        assertEquals("Test3", donationCtrl.read(donation1.getIdDonation()).getStatus());
    }

    /**
     * Test of removeDonation method, of class org.spw.controller.ContactController.
     */
    public void testRemoveDonation() {
        boolean found2 = false;
        boolean found1 = false;
        System.out.println("removeDonation");
       
        ContactController instance = new ContactController();
        DonationController donationCtrl = new DonationController();

        Contact contact = instance.read(TEST_ID_ABC1);
        Donation donation1 = new Donation();
        donation1.setDueDate(new Date());
        donation1.setProposalTitle(getName());
        donation1.setReasonNotFunded(getName());
        donation1.setStatus("Test1");
        donation1.setAmount(new BigDecimal(1.11));
       
        Donation donation2 = new Donation();
        donation2.setDueDate(new Date());
        donation2.setProposalTitle(getName());
        donation2.setReasonNotFunded(getName());
        donation2.setStatus("Test2");
        donation2.setAmount(new BigDecimal(2.22));
       
        donation1.setDonor(contact);
        donation2.setDonor(contact);
        donation1 = instance.addDonation(donation1);
        donation2 = instance.addDonation(donation2);
        contact = instance.removeDonation(donation1);
        contact = instance.removeDonation(donation2);
        for(Donation aDonation : contact.getDonations()) {
            if (aDonation.getProposalTitle().equals(getName())
            && aDonation.getReasonNotFunded().equals(getName())
            && aDonation.getStatus().equals("Test1")
            && aDonation.getAmount().equals(new BigDecimal(1.11))) {
                found1 = true;
                break;
            }
            if (aDonation.getProposalTitle().equals(getName())
            && aDonation.getReasonNotFunded().equals(getName())
            && aDonation.getStatus().equals("Test2")
            && aDonation.getAmount().equals(new BigDecimal(2.22))) {
                found2 = true;
                break;
            }
        }
        assertFalse("The removed 1rst test donation is found", found1);
        assertFalse("The removed 2nd test donation is found", found2);
       
        //check if donations can be read
        assertNull(donationCtrl.read(donation1.getIdDonation()));
        assertNull(donationCtrl.read(donation2.getIdDonation()));
       
    }

    /**
     * Test of addDocument method, of class org.spw.controller.ContactController.
     */
    public void testAddDocument() {
        System.out.println("addDocument");
       
        boolean found1 = false;
        boolean found2 = false;
       
        ContactController instance = new ContactController();
        Contact contact = instance.read(TEST_ID_ABC1);
        Document doc1 = new Document();
        doc1.setContact(contact);
        doc1.setDocumentType("Test1");
        doc1.setTitle(getName());
       
        Document doc2 = new Document();
        doc2.setContact(contact);
        doc2.setDocumentType("Test2");
        doc2.setTitle(getName());
       
        doc1 = instance.addDocument(doc1);
        doc2 = instance.addDocument(doc2);
        assertEquals(contact, doc1.getContact());
        assertEquals(contact, doc2.getContact());
        for(Document aDoc : doc2.getContact().getDocumentLinks()) {
            if (aDoc.getTitle().equals(getName())
            && aDoc.getDocumentType().equals("Test1")) {
                found1 = true;
            }
            if (aDoc.getTitle().equals(getName())
            && aDoc.getDocumentType().equals("Test2")) {
                found2 = true;
            }
        }
        assertTrue("The added 1rst test document is not found", found1);
        assertTrue("The added 2nd test document is not found", found2);
       
        //check if donations can be read
        DocumentController documentCtrl = new DocumentController();
        assertNotNull(documentCtrl.read(doc1.getIdDocument()));
        assertNotNull(documentCtrl.read(doc2.getIdDocument()));
       
        //check update an existing donation
        doc1.setDocumentType("Test3");
        instance.addDocument(doc1);
        assertEquals("Test3", documentCtrl.read(doc1.getIdDocument()).getDocumentType());
    }

    /**
     * Test of removeDocument method, of class org.spw.controller.ContactController.
     */
    public void testRemoveDocument() {
        System.out.println("removeDocument");
       
        boolean found1 = false;
        boolean found2 = false;
       
        ContactController instance = new ContactController();
        DocumentController docCtrl = new DocumentController();
        Contact contact = instance.read(TEST_ID_ABC1);
        Document doc1 = new Document();
        doc1.setContact(contact);
        doc1.setDocumentType("Test1");
        doc1.setTitle(getName());
       
        Document doc2 = new Document();
        doc2.setContact(contact);
        doc2.setDocumentType("Test2");
        doc2.setTitle(getName());
       
        doc1 = instance.addDocument(doc1);
        doc2 = instance.addDocument(doc2);
        assertEquals(contact, doc1.getContact());
        assertEquals(contact, doc2.getContact());

        contact = instance.removeDocument(doc1);
        contact = instance.removeDocument(doc2);
        for(Document aDoc : contact.getDocumentLinks()) {
            if (aDoc.getTitle().equals(getName())
            && aDoc.getDocumentType().equals("Test1")) {
                found1 = true;
                break;
            }
            if (aDoc.getTitle().equals(getName())
            && aDoc.getDocumentType().equals("Test2")) {
                found2 = true;
                break;
            }
        }
        assertFalse("The removed 1rst test is found", found1);
        assertFalse("The removed 2nd test is found", found2);
       
        //check if donations can be read
        assertNull(docCtrl.read(doc1.getIdDocument()));
        assertNull(docCtrl.read(doc2.getIdDocument()));
    }
   
}
TOP

Related Classes of org.spw.controller.ContactControllerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.