jakarta-cactus-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Martin" <mgai...@hotmail.com>
Subject Re: WebRequest Disappears?
Date Sun, 06 Apr 2003 05:40:23 GMT
Ok-
setUp is like init as it is called only once before the Test case is executed
In fact if your initialisation code was moved from setUp to the constructor the initialasation
code would be executed on every invocation
leaving the setUp to handle just the setup for the test case.
Hth,
Martin

----- Original Message ----- 
  From: Ronnie 
  To: Cactus Users List 
  Sent: Saturday, April 05, 2003 9:58 PM
  Subject: Re: WebRequest Disappears?


  Here's my addAccount method. 
    //Add an account into the database
       private void addAccount(String firstName, String lastName, String address,
                               String userName, String password, String email)
                               throws Exception {
          Connection dbConnection = null;
          try {
             dbConnection = getConnection();
             add(dbConnection, firstName, lastName, address, userName, password, email);
          } catch (SQLException e) {
             System.out.println("SQLException caught: "+e);
             System.out.println(e.getMessage());
             while ((e = e.getNextException()) != null) {
                System.out.println("   next "+e.getMessage());
             }
             throw e; // Re-throw the SQLException
          } finally {
             closeConnection(dbConnection);
          }
       }
  and my add() method:
    private void add(Connection conn, String firstName, String lastName,
                        String address, String userName, String password,
                        String email) throws Exception{
          PreparedStatement ps = null;
          String admin = "FALSE";
          // Build the SQL statement
          StringBuffer sql = new StringBuffer();

          sql.append("INSERT INTO ACCOUNTS "); 
          sql.append("(firstName, lastName, address, username, password, email, admin)" );
          sql.append("VALUES (?, ?, ?, ?, ?, ?, ?)");

          // Execute SQL statement
          try {
             ps = conn.prepareStatement(sql.toString());
             ps.setString(1, firstName); // Set the missing values in the prepared statement
             ps.setString(2, lastName);
             ps.setString(3, address);
             ps.setString(4, userName);
             ps.setString(5, password);
             ps.setString(6, email);
             ps.setString(7, admin);
             count = ps.executeUpdate();
          } catch (SQLException e) {

             count = 0; // So we know the operation was not successful

             System.out.println("Caught SQLException: "+ e);
             System.out.println(e.getMessage());
             // Print any other nested exceptions
             while ((e = e.getNextException()) != null) {
                System.out.println("   next "+e.getMessage());
             }

             throw e; // Re-throw the SQLException

          } finally {
             ps.close();
          }
       } // add()
  Here how I called my test class:
    public class AllTests extends TestCase {
       public static Test suite() {

          TestSuite suite = new TestSuite("All Books.com Tests");
          suite.addTestSuite(TestDispatcher1.class);
          return suite;

       } // suite()

       public static void main(String[] args) {
          junit.textui.TestRunner.run(suite());
       }
    }
    ----- Original Message ----- 
    From: Martin 
    To: Cactus Users List 
    Sent: Sunday, 06 April, 2003 12:13 PM
    Subject: Re: WebRequest Disappears?


    Can you show us the code that calls TestDispatcher1 as well as addAccount?
    Martin
      ----- Original Message ----- 
      From: Ronnie 
      To: Cactus Users List 
      Sent: Saturday, April 05, 2003 8:48 PM
      Subject: WebRequest Disappears?


      Hi all,

      I've swiftly met another problem in my testing of servlet. I managed to user WebRequest
in my first test but in my second test in the same class, everything that was set disappears
and was replaced by null! I tried putting the test in another class but the result occurs.

      Here's how my code was written:

        public class TestDispatcher1 extends ServletTestCase {

        public void setUp() throws Exception {
              jndiName = "ass2DB";
              addAccount(CUSTOMER2_FIRSTNAME, CUSTOMER2_LASTNAME, CUSTOMER2_ADDRESS,
                         CUSTOMER2_USERNAME, CUSTOMER2_PASSWORD, CUSTOMER2_EMAIL);
        }//setUp()

        public void tearDown() throws Exception {
              deleteAccounts();
              session.invalidate();
        }//tearDown()

        public void beginTestLogin(WebRequest request) throws Exception {
              request.addParameter("userName", CUSTOMER2_USERNAME);
              request.addParameter("password", CUSTOMER2_PASSWORD);
              request.addParameter("action", "login1");
        }//beginTestLogin()

        public void testLogin() throws Exception {
              System.out.println("User Name: "+request.getParameter("userName"));
              System.out.println("Password: "+request.getParameter("password"));
              System.out.println("Action: "+request.getParameter("action"));
              System.out.println("login1: "+config.getInitParameter("login1"));
        }//testLogin()

        }
      All the "request.getParameter()" returns null and so does the config.getInitParameter().
I have another test in this class which is set up the same way and works perfectly.

      Any help is greatly appreciated!


      Ronnie

Mime
View raw message