jakarta-cactus-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ronnie" <lormee2...@yahoo.com.sg>
Subject Re: WebRequest Disappears?
Date Mon, 07 Apr 2003 01:47:12 GMT
This is too much for me! I don't understand the principle of reflection.

Bty, I've managed to solve the problem. My beginXXX() method was wrongly named as beginTestXXX()
for my second test below...
I apologise for the obvious mistake and troubling you guys.


Regards,
Ronnie
  ----- Original Message ----- 
  From: Martin 
  To: Cactus Users List 
  Sent: Monday, 07 April, 2003 12:49 AM
  Subject: Re: WebRequest Disappears?


  So you are using reflection ..here is an example from the intelligent folks at Brandeis
University in Waltham
  package elf;
  import java.lang.reflect.*; //needed for reflection
  import java.util.Hashtable; //our class that will manipulate

  public class Reflect {
    public static void main(String[] args) {
      try { //watch how we use Hashtable.class.get methods to gain access to our class' meta-data
        // Hashtable ht = new Hashtable(10); 
       //get the classes constructor
        Constructor c = Hashtable.class.getConstructor
  (new Class[] { Integer.TYPE }); //identify that we will be populating hashtable with Integers
  //this is how we create a new Instance and indentify new Integers
        Hashtable ht = (Hashtable) c.newInstance
  (new Object[] { new Integer(10) });

        // ht.put("Three", new Integer(3));
  //we use Hashtable.class get methods to gain access to our class' meta data
        Method m = Hashtable.class.getMethod
  ("put", new Class[] { Object.class, Object.class }); //gain access to the put method
        m.invoke(ht, new Object[] { "Three", new Integer(3) }); //put three Integers in and
instantiate

        System.out.println(ht);                                    // Prints: {Three=3}
      } catch (Exception e) { e.printStackTrace(); }}}

  Make sense?
  Martin
    ----- Original Message ----- 
    From: Ronnie 
    To: Cactus Users List 
    Sent: Saturday, April 05, 2003 11:07 PM
    Subject: Re: WebRequest Disappears?


    The setUp is called before every testXXX() right? But the beginXXX() is called only when
starting the XXX test case. So I thought putting the request initialisation parameters relevant
to the test case in the beginXXX() would be the correct way to do it.

    And it worked for my first test case why shouldn't it work for my second test case in
the same class?
    I'm really stumped by this...

    Here's my entire test class, everything works in testAddCustomer() but the testLogin()
has all null values for "request.getParameter("")" and "config.getInitParameter("")"

      public class TestDispatcher1 extends ServletTestCase {

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

         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()

         /**
          * Set up parameters to be used in testAddCustomer()
          */
         public void beginAddCustomer(WebRequest request) throws Exception {
            request.addParameter("firstName", CUSTOMER1_FIRSTNAME);
            request.addParameter("lastName", CUSTOMER1_LASTNAME);
            request.addParameter("address", CUSTOMER1_ADDRESS);
            request.addParameter("userName", CUSTOMER1_USERNAME);
            request.addParameter("password", CUSTOMER1_PASSWORD);
            request.addParameter("email", CUSTOMER1_EMAIL);
            request.addParameter("action","newAccount2");
         }
         /**
          * Test 1
          *
          * Test adding of customer
          *
          */
         public void testAddCustomer() throws Exception{
            DispatcherServlet dispatcher = new DispatcherServlet();

            dispatcher.init(config);
            dispatcher.doPost(request, response);

            //Check if the customer is added
            findUser(CUSTOMER1_USERNAME);
            if(list!=null){
               Iterator it = list.iterator();
               AccountDetails acc = (AccountDetails)it.next();
               assertEquals("Customer first name wrong",
                            CUSTOMER1_FIRSTNAME,
                            acc.getFirstName());
               assertEquals("Customer last name wrong",
                            CUSTOMER1_LASTNAME,
                            acc.getLastName());
               assertEquals("Customer address wrong",
                            CUSTOMER1_ADDRESS,
                            acc.getAddress());
            } else {
               fail("Account not added!");
            }
         }//testAddCustomer()

         public void endAddCustomer(WebResponse response) throws Exception {

         }

          /**
          * Set up parameters to be used in testLogin()
          */
         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()

         public void endTestLogin(WebResponse response) throws Exception {

         }
    Regards,
    Ronnie
      ----- Original Message ----- 
      From: Martin 
      To: Cactus Users List 
      Sent: Sunday, 06 April, 2003 1:40 PM
      Subject: Re: WebRequest Disappears?


      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



Mime
View raw message