/*
 * Created on 02-Jan-2005
 */
package casino;

import java.io.*;

import casino.Konto;

import junit.framework.TestCase;

/**
 * @author oliver
 */
public class KontoTest extends TestCase {

    private Konto konto;

    public static void main(String[] args) {
        junit.textui.TestRunner.run(KontoTest.class);
    }

    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        konto = new Konto("Max Mustermann");
        super.setUp();
    }
    
    public void testKonto() {
        Konto kto = new Konto("Theo Tester", 1000);
        assertEquals(1000, kto.abfragen());
        assertEquals("Theo Tester", kto.getInhaber());
    }

    public void testAbfragen() {
        assertEquals(0, konto.abfragen());
    }

    public void testEinzahlen() {
        konto.einzahlen(123);
        assertEquals(123, konto.abfragen());
    }

    public void testAbheben() {
        konto.einzahlen(450);
        konto.abheben(345);
        assertEquals(105, konto.abfragen());
    }
    
    public void testStoreAndLoad() throws IOException {
        Konto kto = new Konto("meins", 321);
        kto.store();
        kto.load();
        assertEquals(321, kto.abfragen());
    }
    
    /**
     * Mit diesem Testfall kann ausprobiert werden, ob der handleExceptions()-
     * Pointcut aus LogAspect tatsaechlich greift.
     * 
     * @author <a href="mailto:boehm@javatux.de">oliver</a>
     * @since  03.01.2005
     */
    public void testExceptionProvocation() {
        try {
            Reader reader = new FileReader("gibtsnet");
            fail("hier sollte eigentlich eine Exception auftreten");
        } catch (FileNotFoundException expected) {}
    }
    
    public void testRuntimeExceptionProvocation() {
        try {
            throw new UnsupportedOperationException();
        } catch (RuntimeException expected) {}
    }
    
    public void testKontoUeberziehung() {
        konto.einzahlen(50);    // damit was auf dem Konto ist
        int betrag = konto.abfragen() + 1;
        try {
            konto.abheben(betrag);
            fail("Kontoueberziehung nicht zulaessig!");
        } catch (RuntimeException expected) {}
    }
    
    /**
     * wird das Konto wirklich bei jeder Kontostand-Aenderung abgespeichert?
     * Test it!
     */
    public void testAutomaticStore() throws IOException {
        Konto kto = new Konto("Hugo", 20);
        kto.load();
        assertEquals(20, kto.abfragen());
    }

}
