Developer's Daily Java Education - Test Projects
  front page | java | perl | unix | DevDirectory
   
Front Page
Java
Education
   
 


package com.missiondata.dbtest.containerTest;

//UserContainerTest

import junit.extensions.TestSetup;
import junit.framework.*;
import java.sql.*;
import com.missiondata.dbtest.container.UserContainer;
import com.missiondata.dbtest.model.User;

public class UserContainerTest extends TestCase
{

  static Connection connection;

  /**
   * Set up work to be done before test cases.
   */
  public void setUp()
  {
    try
    {
      Class.forName("org.gjt.mm.mysql.Driver");
      String url = "jdbc:mysql://localhost/junit_test";
      connection = DriverManager.getConnection(url,"","");
      if ( connection == null )
      {
        System.err.println( "connection was null!" );
      }
    }
    catch (SQLException se)
    {
      System.err.println( "SQLException in setUp(): " + se.getMessage() );
    }
    catch (ClassNotFoundException cnfe)
    {
      System.err.println( "ClassNotFoundException in setUp(): " + cnfe.getMessage() );
    }
  }

  //--------------------------------------------------------------------------//

  protected void tearDown()
  {
    try
    {
      connection.close();
    }
    catch (Exception e)
    {
    }
  }

  //--------------------------------------------------------------------------//

  public void testGetUserByName()
  {
    String username = "user1";
    String password = "user1p";
    String email    = "user1@missiondata.com";
    try
    {
      User user = UserContainer.getUserByName(connection,username);
      assertEquals("username", username, user.getUsername() );
      assertEquals("password", password, user.getPassword() );
      assertEquals("email",    email,    user.getEmail() );
    }
    catch (SQLException se)
    {
    }
  }

  //--------------------------------------------------------------------------//

  public void testExceptionThrowing()
  {
    try
    {
      User user = UserContainer.getUserByName(null,null);
      fail("You should never see this.");
    }
    catch (NullPointerException npe)
    {
    }
    catch (SQLException se)
    {
    }
  }

  //--------------------------------------------------------------------------//

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

  public UserContainerTest(String name)
  {
    super(name);
  }

  /**
   * add one line here for each test in the suite
   */
  public static Test suite()
  {
    TestSuite suite = new TestSuite();

    // run tests manually
    //suite.addTest( new UserContainerTest("testNumericAddress1") );
    //return suite;
/*
    TestSetup wrapper = new TestSetup(suite) {
      public void setUp()
      {
        try
        {
          Class.forName("org.gjt.mm.mysql.Driver");
          String url = "jdbc:mysql://localhost/junit_test";
          connection = DriverManager.getConnection(url,"","");
        }
        catch (SQLException se)
        {
          System.err.println( "SQLException in setUp(): " + se.getMessage() );
        }
        catch (ClassNotFoundException cnfe)
        {
          System.err.println( "ClassNotFoundException in setUp(): " + cnfe.getMessage() );
        }
      }
    };
*/
    // or, run tests dynamically
    return new TestSuite(UserContainerTest.class);
  }

}


Copyright 1998-2003 DevDaily Interactive, Inc.
All Rights Reserved.