/*
 * This is file DrakeEquationTest.java in project DrakeEquation2008, created on Dec 9, 2008
 */
package drake;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;


public class DrakeEquationTest {
    DrakeEquation de;

    private double g = 1.0e11;
    private double sg = 1.0e11;
    
    final long rStar = 5;
    final double fp = 0.125;
    final double ne = 1.625;
    final double fl = 0.375;
    final double fi = 0.0625;
    final double fc = 0.03125;
    final long l = 5000;
    
    @Before
    public void setUp() throws Exception {
        de = new DrakeEquation();
    }

    @Test
    public void testSetFractionWithCommunication() {
        try {
            de.setFractionWithCommunication(-0.5);
            de.setFractionWithCommunication(1.5);
            fail("setFractionWithCommunication(-0.5) doesn't throw IllegalArgumentException");
        }
        catch (IllegalArgumentException e) { }
    }

    @Test
    public void testSetFractionWithIntelligence() {
        try {
            de.setFractionWithIntelligence(-0.5);
            de.setFractionWithIntelligence(1.5);
            fail("setFractionWithIntelligence(-0.5) doesn't throw IllegalArgumentException");
        }
        catch (IllegalArgumentException e) { }
    }

    @Test
    public void testSetFractionWithLife() {
        try {
            de.setFractionWithLife(-0.5);
            de.setFractionWithLife(1.5);
            fail("setFractionWithLife(-0.5) doesn't throw IllegalArgumentException");
        }
        catch (IllegalArgumentException e) { } 
    }

    @Test
    public void testSetFractionWithPlanets() {
        try {
            de.setFractionWithPlanets(-0.5);
            de.setFractionWithPlanets(1.5);
            fail("setFractionWithPlanets(-0.5) doesn't throw IllegalArgumentException");
        }
        catch (IllegalArgumentException e) { }
    }

    @Test
    public void testSetAverageHabitablePlanets() {
        try {
            de.setAverageHabitablePlanets(-0.5);
            fail("setAverageHabitablePlanets(-0.5) doesn't throw IllegalArgumentException");
        }
        catch (IllegalArgumentException e) { }
    }

    @Test
    public void testSetExpectedLifetime() {
        try {
            de.setExpectedLifetime(-5);
            fail("setExpectedLifetime(-5) doesn't throw IllegalArgumentException");
        }
        catch (IllegalArgumentException e) { }
    }

    @Test
    public void testSetStarFormationRate() {
        try {
            de.setStarFormationRate(-5);
            fail("setStarFormationRate(-5) doesn't throw IllegalArgumentException");
        }
        catch (IllegalArgumentException e) { }
    }
    
    @Test
    public void setAllVariablesToLegalValues() {
        setFractionValues();
        setNonFractionValues();
    }

    @Test
    public void testSetStarFormationRate_Good() {
        try {
            de.setStarFormationRate(30);
        }
        catch (IllegalArgumentException e) {
            fail("setStarFormationRate(30) should be legal");
        }
    }

    @Test
    public void testGetLifeInGalaxy() {
        setFractionValues();
        setNonFractionValues();
//        assertEquals(1e11 * 0.5 * 1.0 * 0.5, de.getLifeInGalaxy());
        assertEquals(sg * fp * ne * fl, de.getLifeInGalaxy());
    }

    @Test
    public void testGetCommunicatingCivilizations() {
        setFractionValues();
        setNonFractionValues();
//        assertEquals(2 * 0.5 * 1 * 0.5 * 0.5 * 0.5 * 8, de.getCommunicatingCivilizations());
        assertEquals(rStar * fp * ne * fl * fi * fc * l, de.getCommunicatingCivilizations());
    }

    @Test
    public void testGetLifeInUniverse() {
        setFractionValues();
        setNonFractionValues();
//        assertEquals(1e11 * 1e11 * 0.5 * 1 * 0.5, de.getLifeInUniverse());
        assertEquals(sg * g * fp * ne * fl, de.getLifeInUniverse());
    }

    @Test
    public void testGetIntelligenceInUniverse() {
        setFractionValues();
        setNonFractionValues();
//        assertEquals(1e11 * 2 * 0.5 * 1 * 0.5 * 0.5 * 8, de.getIntelligenceInUniverse());
        assertEquals(g * rStar * fp * ne * fl * fi * l, de.getIntelligenceInUniverse());
    }

    @Test(expected=IllegalStateException.class)
    public void testGetCommunicatingCivilizationsWithIncompleteData() {
        setFractionValues();
        de.getCommunicatingCivilizations();
    }

    @Test(expected=IllegalStateException.class)
    public void testGetIntelligenceInUniverseWithIncompleteData() {
        setNonFractionValues();
        de.getIntelligenceInUniverse();
    }

    /**
     * 
     */
    private void setFractionValues() {
//        de.setFractionWithPlanets(0.5);
//        de.setFractionWithLife(0.5);
//        de.setFractionWithIntelligence(0.5);
//        de.setFractionWithCommunication(0.5);
        de.setFractionWithPlanets(fp);
        de.setFractionWithLife(fl);
        de.setFractionWithIntelligence(fi);
        de.setFractionWithCommunication(fc);
    }

    /**
     * 
     */
    private void setNonFractionValues() {
//        de.setStarFormationRate(2);
//        de.setAverageHabitablePlanets(1.0);
//        de.setExpectedLifetime(8);
        de.setStarFormationRate(rStar);
        de.setAverageHabitablePlanets(ne);
        de.setExpectedLifetime(l);
    }
}
