package edu.columbia.gemma.genome.gene;

import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.columbia.gemma.BaseDAOTestCase;
import edu.columbia.gemma.genome.Gene;
import edu.columbia.gemma.genome.GeneDao;
import edu.columbia.gemma.genome.Taxon;
import edu.columbia.gemma.genome.TaxonDao;

/**
 * <hr>
 * <p>
 * Copyright (c) 2004, 2005 Columbia University
 * 
 * @author daq2101
 * @version $Id$
 */
public class CandidateGeneListDAOImplTest extends BaseDAOTestCase {
    private final Log log = LogFactory.getLog( CandidateGeneListDAOImplTest.class );
    
    private CandidateGeneListDao daoCGL = null;
    private TaxonDao daoTaxon = null;
    private GeneDao daoGene = null;
    private Gene g = null;
    private Gene g2 = null;
    private Taxon t = null;
    private CandidateGeneList cgl = null;
    
    protected void setUp() throws Exception {
        super.setUp();

        daoCGL = ( CandidateGeneListDao ) ctx.getBean( "candidateGeneListDao" );
        daoGene = ( GeneDao ) ctx.getBean( "geneDao" );
        daoTaxon = ( TaxonDao ) ctx.getBean( "taxonDao" );

        cgl = CandidateGeneList.Factory.newInstance();

        t = daoTaxon.findByCommonName( "mouse" );
        if ( t == null ) {
            t = Taxon.Factory.newInstance();
            t.setCommonName( "mouse" );
            t.setScientificName( "mouse" );
            daoTaxon.create( t );
        }
        Collection col = daoGene.findByOfficalSymbol( "foo" );
        if ( col.size() > 0 )
            g = ( Gene ) col.iterator().next();
        else {
            g = Gene.Factory.newInstance();
            g.setName( "testmygene" );
            g.setOfficialSymbol( "foo" );
            g.setOfficialName( "testmygene" );
            g.setTaxon( t );
            daoGene.create( g );
        }
        col = daoGene.findByOfficalSymbol( "foo2" );
        if ( col.size() > 0 )
            g2 = ( Gene ) col.iterator().next();
        else {
            g2 = Gene.Factory.newInstance();
            g2.setName( "testmygene2" );
            g2.setOfficialSymbol( "foo2" );
            g2.setOfficialName( "testmygene2" );
            g2.setTaxon( t );
            daoGene.create( g2 );
        }

        daoCGL.create( cgl );
    }

    public final void testAddGeneToList() throws Exception {
        log.info("testing adding gene to list");
        CandidateGene cg = cgl.addCandidate( g );
        daoCGL.update( cgl );
        assertEquals( cgl.getCandidates().size(), 1 );
        assertEquals( cg.getGene().getName(), "testmygene" );
        cgl.removeCandidate( cg );
    }

    public final void testRemoveGeneFromList() throws Exception {
        log.info("testing removing gene from list");
        CandidateGene cg = cgl.addCandidate( g2 );
        daoCGL.update( cgl );
        cgl.removeCandidate( cg );
        daoCGL.update( cgl );
        assert ( cgl.getCandidates().size() == 0 );
    }

    public final void testRankingChanges() throws Exception {
        log.info("testing ranking changes");
        CandidateGene cg1 = cgl.addCandidate( g );
        CandidateGene cg2 = cgl.addCandidate( g2 );
        cgl.increaseRanking( cg2 );
        daoCGL.update( cgl );
        Collection c = cgl.getCandidates();
        for ( Iterator iter = c.iterator(); iter.hasNext(); ) {
            cg1 = ( CandidateGene ) iter.next();
            if ( cg1.getGene().getName().matches( "testmygene2" ) )
                assertEquals( cg1.getRank().intValue(), 0 );
            else
                assertEquals( cg1.getRank().intValue(), 1 );
        }
    }

    protected void tearDown() throws Exception {
        daoCGL.remove( cgl );
        daoCGL = null;
        if ( g != null ) daoGene.remove( g );
        daoGene = null;
        daoTaxon = null;
    }
}

@@ -1,8 +1,646 @@
+/*%0A * The Gemma project%0A * %0A * Copyright (c) 2005 Columbia University%0A * %0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *       http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A *%0A */%0A
 package 
@@ -1287,20 +1287,16 @@
 lass );%0A
-    
 %0A    pri
@@ -1535,20 +1535,16 @@
 = null;%0A
-    
 %0A    pro
@@ -3017,32 +3017,33 @@
        log.info(
+ 
 %22testing adding 
@@ -3051,24 +3051,25 @@
 ene to list%22
+ 
 );%0A        C
@@ -3113,38 +3113,8 @@
  );%0A
-        daoCGL.update( cgl );%0A
     
@@ -3230,43 +3230,8 @@
  );%0A
-        cgl.removeCandidate( cg );%0A
     
@@ -3308,32 +3308,33 @@
        log.info(
+ 
 %22testing removin
@@ -3350,16 +3350,17 @@
 om list%22
+ 
 );%0A     
@@ -3417,91 +3417,358 @@
     
-daoCGL.update( cgl );%0A        cgl.removeCandidate( cg );%0A        daoCGL.update( cgl
+log.info( cgl.getCandidates().size() + %22 candidates to start%22 );%0A        cg = ( CandidateGene ) cgl.getCandidates().iterator().next(); // get the persistent object.%0A        log.info( cgl.getCandidates().size() + %22 candidates just before deleting%22 );%0A        cgl.removeCandidate( cg );%0A        log.info( cgl.getCandidates().size() + %22 candidates left%22
  );%0A
@@ -3905,16 +3905,17 @@
 og.info(
+ 
 %22testing
@@ -3931,16 +3931,17 @@
 changes%22
+ 
 );%0A     
@@ -4078,38 +4078,8 @@
  );%0A
-        daoCGL.update( cgl );%0A
     

remove calls to dao.update() to fix tests.