<commit_before><commit_msg>Added tests for getObjectByExample()<commit_after>package org.flymine.objectstore.ojb;

import junit.framework.TestCase;

import org.flymine.objectstore.ObjectStoreQueriesTestCase;
import org.flymine.objectstore.ObjectStoreFactory;
import org.flymine.objectstore.ObjectStoreException;
import org.flymine.sql.DatabaseFactory;
import org.flymine.model.testmodel.*;

public class ObjectStoreWriterOjbImplTest extends ObjectStoreQueriesTestCase
{
    public ObjectStoreWriterOjbImplTest(String arg) {
        super(arg);
    }

    public void setUp() throws Exception {
        super.setUp();
        writer = new ObjectStoreWriterOjbImpl((ObjectStoreOjbImpl) ObjectStoreFactory.getObjectStore("os.unittest"));

        db = DatabaseFactory.getDatabase("db.unittest");

        storeData();
        // clear the cache to ensure that objects are materialised later (in case broker reused)
        ((ObjectStoreWriterOjbImpl) writer).pb.clearCache();
    }

    public void tearDown() throws Exception {
        removeDataFromStore();
    }

    // Not doing the Query tests here
    public void executeTest(String type) throws Exception {

    }

    /**
     * Test that we can get back one of the data objects by example
     */
    public void testGetByExampleExistingObject() 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);

        Object returned = writer.getObjectByExample(employee);

        assertTrue(returned instanceof CEO);
        assertEquals(data.get("EmployeeB1"), returned);

    }

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

        Employee employee = new Employee();
        employee.setName("EmployeeNotThere");
        employee.setFullTime(true);
        employee.setAddress(address);
        employee.setAge(40);

        Object returned = writer.getObjectByExample(employee);

        assertNull(returned);
    }

    public void testGetByExampleKeysNotSet() throws Exception {
        Employee employee = new Employee();
        employee.setName("EmployeeB1");
        employee.setFullTime(true);
        employee.setAge(40);
        // Address not set

        try {
            writer.getObjectByExample(employee);
            fail("Expected: IllegalArgumentException");
        } catch (IllegalArgumentException e) {
        }
    }

    public void testGetByExampleNullObject() throws Exception {
        try {
            writer.getObjectByExample(null);
            fail("Expected: NullPointerException");
        } catch (NullPointerException e) {
        }
    }

}
{'fraction_non_alphanumeric': 0.09419311276164753, 'fraction_numerical': 0.004726536124240378, 'mean_word_length': 2.98252688172043, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1223062', 'n_tokens_mistral': 792, 'n_tokens_neox': 745, 'n_words': 174}