[jboss-cvs] jboss-seam/examples/nestedbooking/src/org/jboss/seam/example/booking/test ...

Peter Muir peter at bleepbleep.org.uk
Sun Nov 25 08:12:00 EST 2007


  User: pmuir   
  Date: 07/11/25 08:12:00

  Added:       examples/nestedbooking/src/org/jboss/seam/example/booking/test    
                        BookingTest.java ChangePasswordTest.java testng.xml
                        LoginTest.java
  Log:
  JBSEAM-2293 - nested conversations example - thanks to Jacob Orshalick
  
  Revision  Changes    Path
  1.1      date: 2007/11/25 13:12:00;  author: pmuir;  state: Exp;jboss-seam/examples/nestedbooking/src/org/jboss/seam/example/booking/test/BookingTest.java
  
  Index: BookingTest.java
  ===================================================================
  //$Id: BookingTest.java,v 1.1 2007/11/25 13:12:00 pmuir Exp $
  package org.jboss.seam.example.booking.test;
  
  import java.util.Calendar;
  import java.util.Date;
  import java.util.Iterator;
  
  import javax.faces.application.FacesMessage;
  import javax.faces.context.FacesContext;
  import javax.faces.model.DataModel;
  import javax.faces.model.ListDataModel;
  
  import org.jboss.seam.contexts.Contexts;
  import org.jboss.seam.core.Manager;
  import org.jboss.seam.example.booking.Booking;
  import org.jboss.seam.example.booking.Hotel;
  import org.jboss.seam.example.booking.HotelBooking;
  import org.jboss.seam.example.booking.User;
  import org.jboss.seam.mock.SeamTest;
  import org.testng.annotations.Test;
  
  public class BookingTest extends SeamTest
  {
     
     @Test
     public void testBookHotel() throws Exception
     {
        
        new FacesRequest() {
           
           @Override
           protected void invokeApplication() throws Exception
           {
              Contexts.getSessionContext().set("user", new User("Gavin King", "foobar", "gavin"));
              setValue("#{identity.username}", "gavin");
              setValue("#{identity.password}", "foobar");            
              invokeMethod("#{identity.login}");
           }
           
        }.run();
        
        new FacesRequest("/main.xhtml") {
  
           @Override
           protected void updateModelValues() throws Exception
           {
              setValue("#{hotelSearch.searchString}", "Union Square");
           }
  
           @Override
           protected void invokeApplication()
           {
              assert invokeMethod("#{hotelSearch.find}")==null;
           }
  
           @Override
           protected void renderResponse()
           {
              DataModel hotels = (DataModel) Contexts.getSessionContext().get("hotels");
              assert hotels.getRowCount()==1;
              assert ( (Hotel) hotels.getRowData() ).getCity().equals("NY");
              assert getValue("#{hotelSearch.searchString}").equals("Union Square");
              assert !Manager.instance().isLongRunningConversation();
           }
           
        }.run();
        
        String id = new FacesRequest("/hotel.xhtml") {
           
           @Override
           protected void invokeApplication() throws Exception {
              HotelBooking hotelBooking = (HotelBooking) getInstance("hotelBooking");
              DataModel hotels = (DataModel) Contexts.getSessionContext().get("hotels");
              assert hotels.getRowCount()==1;
              hotelBooking.selectHotel( (Hotel) hotels.getRowData() );
           }
  
           @Override
           protected void renderResponse()
           {
              Hotel hotel = (Hotel) Contexts.getConversationContext().get("hotel");
              assert hotel.getCity().equals("NY");
              assert hotel.getZip().equals("10011");
              assert Manager.instance().isLongRunningConversation();
           }
           
        }.run();
        
        id = new FacesRequest("/hotel.xhtml", id) {
  
           @Override
           protected void invokeApplication()
           {
              invokeMethod("#{hotelBooking.bookHotel}");
           }
  
           @Override
           protected void renderResponse()
           {
              assert getValue("#{booking.user}")!=null;
              assert getValue("#{booking.hotel}")!=null;
              assert getValue("#{booking.creditCard}")==null;
              assert getValue("#{booking.creditCardName}")==null;
              Booking booking = (Booking) Contexts.getConversationContext().get("booking");
              assert booking.getHotel()==Contexts.getConversationContext().get("hotel");
              assert booking.getUser()==Contexts.getSessionContext().get("user");
              assert Manager.instance().isLongRunningConversation();
           }
           
        }.run();
        
        new FacesRequest("/book.xhtml", id) {
  
           @Override
           protected void processValidations() throws Exception
           {
              validateValue("#{booking.creditCard}", "123");
              assert isValidationFailure();
           }
  
           @Override
           protected void renderResponse()
           {
              Iterator messages = FacesContext.getCurrentInstance().getMessages();
              assert messages.hasNext();
              assert ( (FacesMessage) messages.next() ).getSummary().equals("Credit card number must 16 digits long");
              assert !messages.hasNext();
              assert Manager.instance().isLongRunningConversation();
           }
           
        }.run();
        
        new FacesRequest("/book.xhtml", id) {
  
           @Override
           protected void processValidations() throws Exception
           {
              validateValue("#{booking.creditCardName}", "");
              assert isValidationFailure();
           }
  
           @Override
           protected void renderResponse()
           {
              Iterator messages = FacesContext.getCurrentInstance().getMessages();
              assert messages.hasNext();
              assert ( (FacesMessage) messages.next() ).getSummary().equals("Credit card name is required");
              assert !messages.hasNext();
              assert Manager.instance().isLongRunningConversation();
           }
           
        }.run();
        
        new FacesRequest("/book.xhtml", id) {
           
           @Override @SuppressWarnings("deprecation")
           protected void updateModelValues() throws Exception
           {  
              setValue("#{booking.creditCard}", "1234567891021234");
              setValue("#{booking.creditCardName}", "GAVIN KING");
              setValue("#{booking.beds}", 2);
              Date now = new Date();
              setValue("#{booking.checkinDate}", now);
              setValue("#{booking.checkoutDate}", now);
           }
  
           @Override
           protected void invokeApplication()
           {
              assert invokeMethod("#{hotelBooking.setBookingDetails}")==null;
           }
  
           @Override
           protected void renderResponse()
           {
              Iterator messages = FacesContext.getCurrentInstance().getMessages();
              assert messages.hasNext();
              FacesMessage message = (FacesMessage) messages.next();
              assert message.getSummary().equals("Check out date must be later than check in date");
              assert !messages.hasNext();
              assert Manager.instance().isLongRunningConversation();
           }
           
        }.run();
        
        new FacesRequest("/book.xhtml", id) {
           
           @Override @SuppressWarnings("deprecation")
           protected void updateModelValues() throws Exception
           {
              Calendar cal = Calendar.getInstance();
              cal.add(Calendar.DAY_OF_MONTH, 2);
              setValue("#{booking.checkoutDate}", cal.getTime() );
           }
  
           @Override
           protected void invokeApplication()
           {
              invokeMethod("#{hotelBooking.setBookingDetails}");
           }
  
           @Override
           protected void renderResponse()
           {
              assert Manager.instance().isLongRunningConversation();
           }
           
        }.run();
        
        new FacesRequest("/confirm.xhtml", id) {
  
           @Override
           protected void invokeApplication()
           {
              invokeMethod("#{hotelBooking.confirm}");
           }
           
        }.run();
        
        new NonFacesRequest("/main.xhtml") {
  
           @Override
           protected void renderResponse()
           {
              ListDataModel bookings = (ListDataModel) getInstance("bookings");
              assert bookings.getRowCount()==1;
              bookings.setRowIndex(0);
              Booking booking = (Booking) bookings.getRowData();
              assert booking.getHotel().getCity().equals("NY");
              assert booking.getUser().getUsername().equals("gavin");
              assert !Manager.instance().isLongRunningConversation();
           }
           
        }.run();
        
        new FacesRequest("/main.xhtml") {
           
           @Override
           protected void invokeApplication()
           {
              ListDataModel bookings = (ListDataModel) Contexts.getSessionContext().get("bookings");
              bookings.setRowIndex(0);
              invokeMethod("#{bookingList.cancel}");
           }
  
           @Override
           protected void renderResponse()
           {
              ListDataModel bookings = (ListDataModel) Contexts.getSessionContext().get("bookings");
              assert bookings.getRowCount()==0;
              assert !Manager.instance().isLongRunningConversation();
           }
           
        }.run();
        
     }
     
  }
  
  
  
  1.1      date: 2007/11/25 13:12:00;  author: pmuir;  state: Exp;jboss-seam/examples/nestedbooking/src/org/jboss/seam/example/booking/test/ChangePasswordTest.java
  
  Index: ChangePasswordTest.java
  ===================================================================
  //$Id: ChangePasswordTest.java,v 1.1 2007/11/25 13:12:00 pmuir Exp $
  package org.jboss.seam.example.booking.test;
  
  import org.jboss.seam.contexts.Contexts;
  import org.jboss.seam.core.Manager;
  import org.jboss.seam.example.booking.User;
  import org.jboss.seam.mock.SeamTest;
  import org.testng.annotations.Test;
  
  public class ChangePasswordTest extends SeamTest
  {
     
     @Test
     public void testChangePassword() throws Exception
     {
        
        new FacesRequest() {
           
           @Override
           protected void invokeApplication() throws Exception
           {
              Contexts.getSessionContext().set("user", new User("Gavin King", "foobar", "gavin"));
              setValue("#{identity.username}", "gavin");
              setValue("#{identity.password}", "foobar");
              invokeMethod("#{identity.login}");
           }
           
        }.run();
        
        new FacesRequest() {
           
           @Override
           protected void processValidations() throws Exception
           {
              validateValue("#{user.password}", "xxx");
              assert isValidationFailure();
           }
  
           @Override
           protected void renderResponse()
           {
              assert getValue("#{user.name}").equals("Gavin King");
              assert getValue("#{user.username}").equals("gavin");
              assert getValue("#{user.password}").equals("foobar");
              assert !Manager.instance().isLongRunningConversation();
              assert getValue("#{identity.loggedIn}").equals(true);
  
           }
           
        }.run();
        
        new FacesRequest() {
  
           @Override
           protected void updateModelValues() throws Exception
           {
              setValue("#{user.password}", "xxxyyy");
              setValue("#{changePassword.verify}", "xxyyyx");
           }
  
           @Override
           protected void invokeApplication()
           {
              assert invokeMethod("#{changePassword.changePassword}")==null;
           }
  
           @Override
           protected void renderResponse()
           {
              assert getValue("#{user.name}").equals("Gavin King");
              assert getValue("#{user.username}").equals("gavin");
              assert getValue("#{user.password}").equals("foobar");
              assert !Manager.instance().isLongRunningConversation();
              assert getValue("#{identity.loggedIn}").equals(true);
           }
           
        }.run();
        
        new FacesRequest() {
  
           @Override
           protected void updateModelValues() throws Exception
           {
              setValue("#{user.password}", "xxxyyy");
              setValue("#{changePassword.verify}", "xxxyyy");
           }
  
           @Override
           protected void invokeApplication()
           {
              invokeMethod("#{changePassword.changePassword}");
           }
  
           @Override
           protected void renderResponse()
           {
              assert getValue("#{user.name}").equals("Gavin King");
              assert getValue("#{user.username}").equals("gavin");
              assert getValue("#{user.password}").equals("xxxyyy");
              assert !Manager.instance().isLongRunningConversation();
              assert getValue("#{identity.loggedIn}").equals(true);
  
           }
           
        }.run();
        
        new FacesRequest() {
  
           @Override
           protected void updateModelValues() throws Exception
           {
              assert getValue("#{user.password}").equals("xxxyyy");
              setValue("#{user.password}", "foobar");
              setValue("#{changePassword.verify}", "foobar");
           }
  
           @Override
           protected void invokeApplication()
           {
              invokeMethod("#{changePassword.changePassword}");
           }
  
           @Override
           protected void renderResponse()
           {
              assert getValue("#{user.name}").equals("Gavin King");
              assert getValue("#{user.username}").equals("gavin");
              assert getValue("#{user.password}").equals("foobar");
              assert !Manager.instance().isLongRunningConversation();
              assert getValue("#{identity.loggedIn}").equals(true);
  
           }
           
        }.run();
        
     }
  
  }
  
  
  
  1.1      date: 2007/11/25 13:12:00;  author: pmuir;  state: Exp;jboss-seam/examples/nestedbooking/src/org/jboss/seam/example/booking/test/testng.xml
  
  Index: testng.xml
  ===================================================================
  <!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
  
  <suite name="Hotel Booking" verbose="2" parallel="false">
  
     <test name="Login">
       <classes>
         <class name="org.jboss.seam.example.booking.test.LoginTest"/>
       </classes>
     </test>
  	
     <test name="Booking">
       <classes>
         <class name="org.jboss.seam.example.booking.test.BookingTest"/>
       </classes>
     </test>
  	
     <test name="Change Password">
       <classes>
         <class name="org.jboss.seam.example.booking.test.ChangePasswordTest"/>
       </classes>
     </test>
  	
  </suite>
  
  
  1.1      date: 2007/11/25 13:12:00;  author: pmuir;  state: Exp;jboss-seam/examples/nestedbooking/src/org/jboss/seam/example/booking/test/LoginTest.java
  
  Index: LoginTest.java
  ===================================================================
  //$Id: LoginTest.java,v 1.1 2007/11/25 13:12:00 pmuir Exp $
  package org.jboss.seam.example.booking.test;
  
  import org.jboss.seam.Seam;
  import org.jboss.seam.core.Manager;
  import org.jboss.seam.mock.SeamTest;
  import org.testng.annotations.Test;
  
  public class LoginTest extends SeamTest
  {
     
     @Test
     public void testLogin() throws Exception
     {
        
        new FacesRequest() {
           
           @Override
           protected void invokeApplication()
           {
              assert !isSessionInvalid();
              assert getValue("#{identity.loggedIn}").equals(false);
           }
           
        }.run();
        
        new FacesRequest() {
  
           @Override
           protected void updateModelValues() throws Exception
           {
              assert !isSessionInvalid();
              setValue("#{identity.username}", "gavin");
              setValue("#{identity.password}", "foobar");
           }
  
           @Override
           protected void invokeApplication()
           {
              invokeMethod("#{identity.login}");
           }
  
           @Override
           protected void renderResponse()
           {
              assert getValue("#{user.name}").equals("Gavin King");
              assert getValue("#{user.username}").equals("gavin");
              assert getValue("#{user.password}").equals("foobar");
              assert !Manager.instance().isLongRunningConversation();
              assert getValue("#{identity.loggedIn}").equals(true);
           }
           
        }.run();
        
        new FacesRequest() {
  
           @Override
           protected void invokeApplication()
           {
              assert !isSessionInvalid();
              assert getValue("#{identity.loggedIn}").equals(true);
           }
           
        }.run();
        
        new FacesRequest() {
  
           @Override
           protected void invokeApplication()
           {
              assert !Manager.instance().isLongRunningConversation();
              assert !isSessionInvalid();
              invokeMethod("#{identity.logout}");
              assert Seam.isSessionInvalid();
           }
  
           @Override
           protected void renderResponse()
           {
              assert getValue("#{identity.loggedIn}").equals(false);
              assert Seam.isSessionInvalid();
           }
           
        }.run();
        
     }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list