Example usage for org.apache.commons.collections CollectionUtils find

List of usage examples for org.apache.commons.collections CollectionUtils find

Introduction

In this page you can find the example usage for org.apache.commons.collections CollectionUtils find.

Prototype

public static Object find(Collection collection, Predicate predicate) 

Source Link

Document

Finds the first element in the given collection which matches the given predicate.

Usage

From source file:uk.ac.ebi.bioinvindex.persistence.PropertyValuePersistenceTest.java

@Test
public void testPersistValue() throws Exception {

    out.println(/*from  w w w.j a  v a 2 s .co  m*/
            "\n\n\n _______________ PropertyValuePersistanceTest, Basic Test ____________________________\n");

    Characteristic prop = new Characteristic("Foo Property", 0);

    // New OE
    ReferenceSource source = new ReferenceSource("My fancy test ontology 2");
    source.setAcc("bii:tests:MY-SRC-2");
    OntologyTerm oe1 = new OntologyTerm("biionto:101", "test OE", source);
    prop.addOntologyTerm(oe1);

    // Existing OE
    source = new ReferenceSource("TEST ONTOLOGY");
    source.setAcc("BII-1");
    OntologyTerm oe2 = new OntologyTerm("OBI-EO1", "organism", source);
    prop.addOntologyTerm(oe2);

    // The Value
    CharacteristicValue value = new CharacteristicValue("Foo Value", prop);

    // Let's persist
    CharacteristicValue valueNew = persister.persist(value);
    transaction.commit();

    assertNotNull("Uh! Oh! Persister returns null!", valueNew);
    assertTrue("Ouch! Cannot find the persisted value in the DB!", entityManager.contains(valueNew));
    assertTrue("Argh! The persisted value and the original one should be the same!", value == valueNew);
    assertNotNull("Burp! The persisted value should have an ID!", valueNew.getId());

    Characteristic typeNew = valueNew.getType();
    assertNotNull("Urp! Saved Value has no type!", typeNew);
    assertNotNull("Urp! Type of Saved Value should have an ID!", typeNew.getId());
    assertEquals("Urp! Type of Saved Value has wrong value", "Foo Property", typeNew.getValue());

    Collection<OntologyTerm> oes = typeNew.getOntologyTerms();
    assertNotNull("Oh no! The persisted value does not return OEs!", oes);
    assertEquals("Oh no! The persisted value does not return correct no. of OEs !", 2, oes.size());

    OntologyTerm oeDB1 = (OntologyTerm) CollectionUtils.find(oes, new OESelector(oe1));
    assertNotNull("Urp! The term " + oe1 + "was not persisted with the value!", oeDB1);
    assertNotNull("The term: " + oeDB1 + "should have an ID!", oeDB1);

    OntologyTerm oeDB2 = (OntologyTerm) CollectionUtils.find(oes, new OESelector(oe2));
    assertNotNull("Urp! The term " + oe2 + "was not persisted with the value!", oeDB2);
    assertEquals("Wrong retrieved ID for the term : " + oeDB2 + "!", new Long(501), oeDB2.getId());

    out.println("Saved Value:" + valueNew);

    out.println(
            "\n _______________ /end: PropertyValuePersistanceTest, Basic Test ____________________________\n\n");
}

From source file:uk.ac.ebi.bioinvindex.persistence.ProtocolPersistenceTest.java

@Test
public void testPersistNew() throws Exception {

    out.println(//from w ww  .j  a  va2  s. c  o m
            "\n\n\n _______________ ProtocolPersistanceTest, Testing new Protocol  ____________________________\n");

    ReferenceSource source = new ReferenceSource("My fancy test ontology 2");
    source.setAcc("bii:tests:MY-SRC-2");
    ProtocolType type = new ProtocolType("proto:type:999", "My Test Proto Type", source);

    Protocol protocol = new Protocol("A-NEW-PROTO-99", type);
    protocol.setAcc(protocol.getName());

    transaction.commit();

    // Let's attach few parameters
    //
    Parameter param1 = new Parameter("Time Before Killing the Operator", 0);
    ReferenceSource sourceOE1 = new ReferenceSource("My fancy test ontology 3");
    sourceOE1.setAcc("bii:tests:MY-SRC-3");
    OntologyTerm oe1 = new OntologyTerm("biionto:101", "test OE", source);
    param1.addOntologyTerm(oe1);
    protocol.addParameter(param1);

    Parameter param2 = new Parameter("Time To get rid of the corpse", 1);
    ReferenceSource sourceOE2 = new ReferenceSource("101 Ontology");
    sourceOE2.setAcc("BII-101");
    OntologyTerm oe2 = new OntologyTerm("test:101", "Time", sourceOE2);
    param2.addOntologyTerm(oe2);
    protocol.addParameter(param2);

    // Let's attach few components
    //
    ProtocolComponent comp1 = new ProtocolComponent("Chain saw");
    ReferenceSource sourceOE3 = new ReferenceSource("My fancy test ontology 3");
    sourceOE3.setAcc("bii:tests:MY-SRC-33");
    OntologyTerm oe3 = new OntologyTerm("biionto:303", "test OE", source);
    comp1.addOntologyTerm(oe3);
    protocol.addComponent(comp1);

    ProtocolComponent comp2 = new ProtocolComponent("Chain saw");
    comp2.addOntologyTerm(oe2);
    protocol.addComponent(comp2);

    transaction.begin();
    Protocol protocolDB = persister.persist(protocol);
    transaction.commit();

    assertNotNull("Uh! No object returned by the persister!", protocolDB);
    assertEquals("Gosh! The persister should return the same persisted object!", protocol, protocolDB);
    assertNotNull("Argh! The persisted protocol should have a non null ID", protocol.getId());
    assertTrue("Uhm... The returned object apparently is not in the DB", entityManager.contains(protocolDB));
    assertNotNull("The New Protocol should have a non null accession!", protocolDB.getAcc());

    // Type
    //
    ProtocolType typeDB = protocolDB.getType();
    assertNotNull("Urp! No type persisted for the protocol!", protocolDB);
    assertNotNull("Ouch! The persisted protocol type should have a non null ID", type.getId());
    assertTrue("Ouch! The protocol type is not in the DB!", entityManager.contains(typeDB));
    assertEquals("Argh! The persisted type should be == the initial one", type, typeDB);

    ReferenceSource typeSrcDB = typeDB.getSource();
    assertNotNull("Argh! Persisted protocol type has not a source!", typeSrcDB);
    assertTrue("Ouch! The protocol type is not in the DB!", entityManager.contains(typeDB));
    assertEquals("Urp! Persisted protocol type has bad source", source.getName(), typeSrcDB.getName());

    // Parameters
    //
    Collection<Parameter> params = protocolDB.getParameters();
    assertNotNull("Oh no! No parameter from the persisted protocol!", params);
    assertEquals("Oh no! The persisted protocol does not return correct no. of params !", 2, params.size());

    // Param 1
    FreeTextTerm param1DB = (FreeTextTerm) CollectionUtils.find(params, new FreeTextSelector(param1));
    assertNotNull("Urp! The param " + param1 + " was not persisted with the protocol!", param1DB);
    assertNotNull("The term: " + param1DB + "should have an ID!", param1DB);

    Collection<OntologyTerm> oes1 = param1DB.getOntologyTerms();
    assertNotNull("Oh no! The persisted parameter" + param1DB + " does not return OEs!", oes1);
    assertEquals("Oh no! The persisted parameter does not return correct no. of OEs !", 1, oes1.size());

    // Param 2
    FreeTextTerm param2DB = (FreeTextTerm) CollectionUtils.find(params, new FreeTextSelector(param2));
    assertNotNull("Urp! The param " + param2 + " was not persisted with the protocol!", param2DB);
    assertNotNull("The term: " + param2DB + "should have an ID!", param2DB.getId());

    Collection<OntologyTerm> oes2 = param2DB.getOntologyTerms();
    assertNotNull("Oh no! The persisted parameter" + param2DB + " does not return OEs!", oes2);
    assertEquals("Oh no! The persisted parameter" + param2DB + " does not return correct no. of OEs !", 1,
            oes2.size());
    OntologyTerm oeDB2 = oes2.iterator().next();
    assertEquals("Urp! The term " + oe2 + "was not persisted with the Parameter!", oe2.getAcc(),
            oeDB2.getAcc());
    assertEquals("Wrong retrieved ID for the term : " + oeDB2 + "!", new Long(507), oeDB2.getId());

    // Components
    //
    Collection<ProtocolComponent> comps = protocolDB.getComponents();
    assertNotNull("Oh no! No component from the persisted protocol!", comps);
    assertEquals("Oh no! The persisted protocol does not return correct no. of components!", 2, comps.size());

    // Component 1
    FreeTextTerm comp1DB = (FreeTextTerm) CollectionUtils.find(comps, new FreeTextSelector(comp1));
    assertNotNull("Urp! The component " + comp1 + " was not persisted with the protocol!", comp1DB);
    assertNotNull("The term: " + param1DB + "should have an ID!", comp1DB);

    Collection<OntologyTerm> coes1 = comp1DB.getOntologyTerms();
    assertNotNull("Oh no! The persisted parameter" + comp1DB + " does not return OEs!", coes1);
    assertEquals("Oh no! The persisted component does not return correct no. of OEs !", 1, coes1.size());

    // Component 2
    FreeTextTerm comp2DB = (FreeTextTerm) CollectionUtils.find(comps, new FreeTextSelector(comp2));
    assertNotNull("Urp! The param " + param2 + "was not persisted with the protocol!", comp2DB);
    assertNotNull("The term: " + comp2DB + "should have an ID!", comp2DB.getId());

    Collection<OntologyTerm> coes2 = param2DB.getOntologyTerms();
    assertNotNull("Oh no! The persisted parameter" + comp2DB + " does not return OEs!", coes2);
    assertEquals("Oh no! The persisted parameter" + comp2DB + " does not return correct no. of OEs !", 1,
            coes2.size());
    OntologyTerm coeDB2 = coes2.iterator().next();
    assertEquals("Urp! The term " + oe2 + "was not persisted with the Component!", oe2.getAcc(),
            oeDB2.getAcc());
    assertEquals("Wrong retrieved ID for the component term : " + coeDB2 + "!", new Long(507), coeDB2.getId());

    out.println(
            "\n _______________ /end: ProtocolPersistanceTest, Testing new Protocol ____________________________\n\n");

}

From source file:uk.ac.ebi.bioinvindex.persistence.ProtocolPersistenceTest.java

@Test
public void testPersistExisting() throws Exception {

    out.println(//from   w  w  w .  java  2  s  .  com
            "\n\n\n _______________ ProtocolPersistanceTest, Testing existing Protocol ____________________________\n");

    Protocol protocol = new Protocol(null, null);
    protocol.setAcc("bii:proto:999");

    Protocol protocolDB = persister.persist(protocol);
    transaction.commit();

    assertNotNull("Uh! No object returned by the persister!", protocolDB);
    assertTrue("Gosh! The persister should return a different existing protocol!", protocol != protocolDB);
    assertEquals("Argh! Bad ID for the persisted protocol", new Long(-10), protocolDB.getId());
    assertTrue("Uhm... The returned object apparently is not in the DB", entityManager.contains(protocolDB));

    // Type
    ProtocolType typeDB = protocolDB.getType();
    assertNotNull("Urp! No type persisted for the protocol!", protocolDB);
    assertEquals("Ouch! Bad ID for the persisted Protocol Type", new Long(506), typeDB.getId());
    assertTrue("Ouch! The protocol type is not in the DB!", entityManager.contains(typeDB));

    // Parameters
    //
    Collection<Parameter> params = protocolDB.getParameters();
    assertNotNull("Oh no! No param types from the persisted protocol!", params);
    assertEquals("Oh no! The persisted protocol does not return correct no. of params !", 2, params.size());

    // Param 1
    Parameter param1 = new Parameter(null, 0);
    param1.setId(new Long(-1));
    FreeTextTerm param1DB = (FreeTextTerm) CollectionUtils.find(params, new FreeTextSelector(param1));
    assertNotNull("Urp! The existing protocol should have parameter " + param1 + "!", param1DB);

    Collection<OntologyTerm> oes1 = param1DB.getOntologyTerms();
    assertNotNull("Oh no! The persisted parameter" + param1DB + " does not return OEs!", oes1);
    assertEquals("Oh no! The persisted FreeTextTerm does not return correct no. of OEs !", 1, oes1.size());
    OntologyTerm oeDB1 = oes1.iterator().next();
    assertEquals("Wrong retrieved ID for the term : " + oeDB1 + "!", new Long(-2), oeDB1.getId());

    // Param 2
    Parameter param2 = new Parameter(null, 0);
    param2.setId(new Long(-2));
    FreeTextTerm param2DB = (FreeTextTerm) CollectionUtils.find(params, new FreeTextSelector(param2));
    assertNotNull("Urp! The param " + param2 + "was not persisted with the protocol!", param2DB);

    Collection<OntologyTerm> oes2 = param2DB.getOntologyTerms();
    assertNotNull("Oh no! The persisted parameter" + param2DB + " does not return OEs!", oes2);
    assertEquals("Oh no! The persisted parameter" + param2DB + " does not return correct no. of OEs !", 1,
            oes2.size());
    OntologyTerm oeDB2 = oes2.iterator().next();
    assertEquals("Wrong retrieved ID for the term : " + oeDB2 + "!", new Long(507), oeDB2.getId());

    out.println(
            "\n _______________ /end: ProtocolPersistanceTest, Testing existing Protocol ____________________________\n\n");

}

From source file:uk.ac.ebi.bioinvindex.persistence.StudyPersistenceTest.java

@Test
public void testPersistBasic() throws Exception {

    out.println(//www  .j av a2  s .  c  o m
            "\n\n\n _______________  StudyPersistanceTest, Testing new Study  ____________________________\n");

    Study study = new Study("My super-scientific experiment");

    Design design = new Design("Whip the operator to see its performance improves");
    ReferenceSource designSource = new ReferenceSource("TEST ONTOLOGY");
    designSource.setAcc("BII-1");
    OntologyTerm oe = new OntologyTerm("biionto:OperatorPerturbationDesign", "Operator Perturbation Design",
            designSource);
    design.addOntologyTerm(oe);
    study.addDesign(design);

    design = new Design("Whip the PI too if nothing changes");
    study.addDesign(design);

    // Protocol 1 (does not exist)
    ReferenceSource source = new ReferenceSource("My fancy test ontology 2");
    source.setAcc("bii:tests:MY-SRC-2");
    ProtocolType type = new ProtocolType("proto:type:999", "My Test Proto Type", source);

    Protocol protocol1 = new Protocol("My New Protocol", type);
    protocol1.setAcc("A-NEW-PROTO-99");

    // Let's attach some parameters
    Parameter param1 = new Parameter("Time Before Killing the Operator", 0);
    ReferenceSource sourceOE1 = new ReferenceSource("My fancy test ontology 3");
    sourceOE1.setAcc("bii:tests:MY-SRC-3");
    OntologyTerm oe1 = new OntologyTerm("biionto:101", "test OE", source);
    param1.addOntologyTerm(oe1);
    protocol1.addParameter(param1);

    Parameter param2 = new Parameter("Time To get rid of the corpse", 1);
    ReferenceSource sourceOE2 = new ReferenceSource("101 Ontology");
    sourceOE2.setAcc("BII-101");
    OntologyTerm oe2 = new OntologyTerm("test:101", "Time", sourceOE2);
    param2.addOntologyTerm(oe2);
    protocol1.addParameter(param2);

    study.addProtocol(protocol1);

    // Protocol 2 (exists)
    Protocol protocol2 = new Protocol(null, null);
    protocol2.setAcc("bii:proto:999");
    study.addProtocol(protocol2);

    // Contacts
    Contact contact = new Contact("Mr", null, "Foo", "someone@somewhere.net");
    ReferenceSource roleSrc = new ReferenceSource(null);
    roleSrc.setAcc("BII-1");
    ContactRole contactRole = new ContactRole("bii:fooContactRole", "My Contact Role", roleSrc);
    contact.addRole(contactRole);
    contactRole = new ContactRole("bii:fooContactRole1", "My Contact Role 1", roleSrc);
    contact.addRole(contactRole);

    study.addContact(contact);

    // TODO: publications, needs role persistence

    // Let's go with the persistence job!
    //

    Study studyDB = persister.persist(study);
    transaction.commit();

    assertNotNull("Oh! No study returned by the persister!", studyDB);
    assertEquals("Ouch! The study rerturned by the persister should be the original one!", study, studyDB);
    assertTrue("Argh! Cannot find the persisted study in the DB!", entityManager.contains(studyDB));
    assertNotNull("Urp! The study should have an ID", studyDB.getId());

    Collection<Design> designsDB = studyDB.getDesigns();
    assertNotNull("Ops! The retuned study has no a design!", designsDB);
    assertEquals("Oh no! Bad no. of designs persisted!", 2, designsDB.size());
    for (Design designDB : designsDB)
        assertNotNull("Urp! The study design should have an ID", designDB.getId());

    final String dval = "Whip the operator to see its performance improves";
    Design designDB = (Design) CollectionUtils.find(designsDB, new Predicate() {
        public boolean evaluate(Object object) {
            return dval.equals(((Design) object).getValue());
        }
    });
    assertNotNull("Arg! I cannot find a design which shoulb have been persisted ('" + dval + "')", designDB);
    Collection<OntologyTerm> oesDesign = designDB.getOntologyTerms();
    assertNotNull("Oh no! The persisted parameter" + designDB + " does not return OEs!", oesDesign);
    assertEquals("Oh no! The persisted FreeTextTerm does not return correct no. of OEs !", 1, oesDesign.size());
    OntologyTerm oeDB1 = oesDesign.iterator().next();
    assertEquals("Wrong retrieved ID for the term : " + oeDB1 + "!", new Long(-3), oeDB1.getId());
    assertEquals("Oh no! The retrieved study design's OE has bad accession!",
            "biionto:OperatorPerturbationDesign", oeDB1.getAcc());

    final String dval1 = "Whip the PI too if nothing changes";
    designDB = (Design) CollectionUtils.find(designsDB, new Predicate() {
        public boolean evaluate(Object object) {
            return dval1.equals(((Design) object).getValue());
        }
    });
    assertNotNull("Arg! I cannot find a design which shoulb have been persisted ('" + dval1 + "')", designDB);

    // Protocols
    //
    Collection<Protocol> protocols = study.getProtocols();
    assertNotNull("Ugh! No protocols from the study!", protocols);
    assertEquals("Urgh! Wrong no. of protocols returned for the study!", 2, protocols.size());

    // Protocol 1
    //
    Protocol protocol1DB = (Protocol) CollectionUtils.find(protocols, new Predicate() {
        public boolean evaluate(Object object) {
            return "A-NEW-PROTO-99".equals(((Protocol) object).getAcc());
        }
    });
    assertNotNull("Ough! Cannot find the protocol A-NEW-PROTO-99 in the study!", protocol1DB);

    // Parameters
    //
    Collection<Parameter> params1 = protocol1DB.getParameters();
    assertNotNull("Oh no! No param types from the persisted protocol!", params1);
    assertEquals("Oh no! The persisted protocol does not return correct no. of params !", 2, params1.size());

    // Protocol 2
    //
    Protocol protocol2DB = (Protocol) CollectionUtils.find(protocols, new Predicate() {
        public boolean evaluate(Object object) {
            return "bii:proto:999".equals(((Protocol) object).getAcc());
        }
    });
    assertNotNull("Ough! Cannot find the protocol bii:proto:999 in the study!", protocol2DB);

    // Params
    //
    Collection<Parameter> params2 = protocol2DB.getParameters();
    assertNotNull("Oh no! No param types from the persisted protocol!", params2);
    assertEquals("Oh no! The persisted protocol does not return correct no. of params !", 2, params2.size());

    // Contact
    Collection<Contact> contactsDB = studyDB.getContacts();
    assertEquals("Arg! Wrong no. of contacts persisted!", 1, contactsDB.size());
    Contact contactDB = contactsDB.iterator().next();
    assertNotNull("Oh no! Persisted contact has not an ID!", contactDB.getId());
    assertEquals("Oh no! Persisted contact has wrong email", "someone@somewhere.net", contactDB.getEmail());
    Collection<ContactRole> rolesDB = contactDB.getRoles();
    assertEquals("Argh! Bad no. of contact roles persisted", 2, rolesDB.size());
    Iterator<ContactRole> rolesItr = rolesDB.iterator();
    ContactRole roleDB = rolesItr.next();
    Long id = roleDB.getId();
    assertTrue("Arg! Wrong role #0 for the persisted contact", id != null);
    ReferenceSource roleSrcDB = roleDB.getSource();
    assertEquals("Arg! Wrong role source for the persisted contact", (long) 100, roleSrcDB.getId().longValue());
    roleDB = rolesItr.next();
    Long id1 = roleDB.getId();
    assertNotNull("Arg! Wrong role #1 for the persisted contact", id1 != null || (id == -7 || id1 == -7));
    roleSrcDB = roleDB.getSource();
    assertEquals("Arg! Wrong role source for the persisted contact", (long) 100, roleSrcDB.getId().longValue());

    out.println("\n\nPersisted study: " + studyDB);

    out.println(
            "\n\n\n _______________  /end: StudyPersistanceTest, Testing new Study  ____________________________\n");

}

From source file:uk.ac.ebi.bioinvindex.persistence.StudyPersistenceTest.java

@Test
public void testPersistStudyAndInvestigation() throws Exception {

    out.println(/*ww w.j a va 2s  .  co  m*/
            "\n\n\n _______________  StudyPersistanceTest, Testing Study/Investigation  _____________________\n");

    Study study = new Study("My super-scientific experiment");
    study.setDescription("A new fantastic study used for tests");

    Design design = new Design("Whip the operator to see its performance improves");
    ReferenceSource designSource = new ReferenceSource(null);
    designSource.setAcc("BII-1");
    OntologyTerm oe = new OntologyTerm("biionto:OperatorPerturbationDesign", null, designSource);
    design.addOntologyTerm(oe);
    study.addDesign(design);

    Investigation investigation1 = new Investigation("The New Test Investigation");
    investigation1.setDescription("The Investigation used for testing");
    study.addInvestigation(investigation1);

    Investigation investigation2 = new Investigation("");
    investigation2.setAcc("bii:test:inv:01");
    study.addInvestigation(investigation2);

    Study studyDB = persister.persist(study);
    transaction.commit();

    assertNotNull("Arg! No study returned by the persister!", studyDB);
    assertEquals("Ouch! The study retuned by the persister should be == the original one", studyDB, study);
    assertNotNull("Ops! null ID for the persisted study", studyDB.getId());
    assertTrue("Oh! The study is not in the DB!", entityManager.contains(studyDB));

    Collection<Investigation> investigationsDB = studyDB.getInvestigations();
    assertEquals("Gosh! Wrong # of investigations associated to the persisted study!", 2,
            investigationsDB.size());

    final String title1 = investigation1.getTitle();
    Investigation investigation1DB = (Investigation) CollectionUtils.find(investigationsDB, new Predicate() {
        public boolean evaluate(Object object) {
            return title1.equals(((Investigation) object).getTitle());
        }
    });
    assertNotNull("Urp! The investigation \"" + title1 + "\" was not persisted!", investigation1DB);
    assertNotNull("Argh! The investigation \"" + title1 + "\" should have an ID", investigation1DB.getId());
    assertNotNull("Urp! The investigation \"" + title1 + "\" should have an accession",
            investigation1DB.getAcc());
    assertTrue("Oh! The investigation \"" + title1 + "\" is not in the DB!",
            entityManager.contains(investigation1DB));
    assertTrue("Urgh! The investigation " + title1 + "doesn't contain the persisted study!",
            investigation1DB.getStudies().contains(studyDB));

    final String acc2 = investigation2.getAcc();
    Investigation investigation2DB = (Investigation) CollectionUtils.find(investigationsDB, new Predicate() {
        public boolean evaluate(Object object) {
            return acc2.equals(((Investigation) object).getAcc());
        }
    });
    assertNotNull("Urp! The investigation \"" + acc2 + "\" was not persisted!", investigation2DB);
    assertNotNull("Argh! The investigation \"" + acc2 + "\" should have an ID", investigation2DB.getId());
    assertNotNull("Urp! The investigation \"" + acc2 + "\" should have an accession",
            investigation2DB.getAcc());
    assertTrue("Oh! The investigation \"" + acc2 + "\" is not in the DB!",
            entityManager.contains(investigation2DB));
    assertTrue("Urgh! The investigation " + acc2 + "doesn't contain the persisted study!",
            investigation2DB.getStudies().contains(studyDB));

    out.println("\n\nPersisted study: " + studyDB);

    out.println("\n\nAnd its investigations:");
    for (Investigation investigation : studyDB.getInvestigations())
        out.println(investigation);

    out.println(
            "\n\n\n _______________  StudyPersistanceTest, Testing Study/Investigation  _____________________\n");

}