/** * If serializable attribute is false, value in session should be automatically removed when * session passivate. * * @throws Exception */ @Test(groups = "session") public void sessionSerializationNonSerializable() throws Exception { MockHttpSessionWithEvent session = new MockHttpSessionWithEvent(context); // Set result to 1. { MockRoundtrip trip = new MockRoundtrip(context, NonSerializeActionBean.class, session); trip.setParameter("number", "1"); trip.execute(); NonSerializeActionBean bean = trip.getActionBean(NonSerializeActionBean.class); Assert.assertEquals(new Integer(1), bean.getResult()); } // Simulate serialization. session.passivate(); // Simulate deserialization. session.activate(); // Add 2. // Total should be 2 since value is removed from session on serialization. { MockRoundtrip trip = new MockRoundtrip(context, NonSerializeActionBean.class, session); trip.setParameter("number", "2"); trip.execute(); NonSerializeActionBean bean = trip.getActionBean(NonSerializeActionBean.class); // Serialization will remove object from session, so result will be reset. Assert.assertEquals(new Integer(2), bean.getResult()); } // Even if serializable is true, field is not serialized if it does not implement // java.io.Serializable. // Set result to 1. { MockRoundtrip trip = new MockRoundtrip(context, PersonActionBean.class, session); trip.setParameter("person.firstName", "Christian"); trip.setParameter("person.lastName", "Poitras"); trip.execute(); PersonActionBean bean = trip.getActionBean(PersonActionBean.class); Assert.assertEquals("Christian", bean.getPerson().getFirstName()); Assert.assertEquals("Poitras", bean.getPerson().getLastName()); } // Simulate serialization. session.passivate(); // Simulate deserialization. session.activate(); // Add 2 (total 3). { MockRoundtrip trip = new MockRoundtrip(context, PersonActionBean.class, session); trip.execute(); PersonActionBean bean = trip.getActionBean(PersonActionBean.class); Assert.assertNull(bean.getPerson()); } }
/** * Test @Session annotation for complex object with a default value. * * @throws Exception */ @Test(groups = "session") public void complexDefault() throws Exception { MockHttpSessionWithEvent session = new MockHttpSessionWithEvent(context); // Set person to default. { MockRoundtrip trip = new MockRoundtrip(context, PersonActionBean.class, session); trip.execute("defaultPerson"); PersonActionBean bean = trip.getActionBean(PersonActionBean.class); Assert.assertEquals("Christian", bean.getPerson().getFirstName()); Assert.assertEquals("Poitras", bean.getPerson().getLastName()); } // Check if first name and last name are in session. { MockRoundtrip trip = new MockRoundtrip(context, PersonActionBean.class, session); trip.execute(); PersonActionBean bean = trip.getActionBean(PersonActionBean.class); Assert.assertEquals("Christian", bean.getPerson().getFirstName()); Assert.assertEquals("Poitras", bean.getPerson().getLastName()); } // Set new person values. { MockRoundtrip trip = new MockRoundtrip(context, PersonActionBean.class, session); trip.setParameter("person.firstName", "John"); trip.setParameter("person.lastName", "Smith"); trip.execute(); PersonActionBean bean = trip.getActionBean(PersonActionBean.class); Assert.assertEquals("John", bean.getPerson().getFirstName()); Assert.assertEquals("Smith", bean.getPerson().getLastName()); } // Check if first name and last name are still correct. { MockRoundtrip trip = new MockRoundtrip(context, PersonActionBean.class, session); trip.execute(); PersonActionBean bean = trip.getActionBean(PersonActionBean.class); Assert.assertEquals("John", bean.getPerson().getFirstName()); Assert.assertEquals("Smith", bean.getPerson().getLastName()); } }
/** * Test @Session annotation with complex objects. * * @throws Exception */ @Test(groups = "session") public void complexObject() throws Exception { MockHttpSessionWithEvent session = new MockHttpSessionWithEvent(context); // Set first name and last name. { MockRoundtrip trip = new MockRoundtrip(context, PersonActionBean.class, session); trip.setParameter("person.firstName", "Christian"); trip.setParameter("person.lastName", "Poitras"); trip.execute(); PersonActionBean bean = trip.getActionBean(PersonActionBean.class); Assert.assertEquals("Christian", bean.getPerson().getFirstName()); Assert.assertEquals("Poitras", bean.getPerson().getLastName()); } // Check if first name and last name are in session. { MockRoundtrip trip = new MockRoundtrip(context, PersonActionBean.class, session); trip.execute(); PersonActionBean bean = trip.getActionBean(PersonActionBean.class); Assert.assertEquals("Christian", bean.getPerson().getFirstName()); Assert.assertEquals("Poitras", bean.getPerson().getLastName()); } // Properties can be overwritten. { MockRoundtrip trip = new MockRoundtrip(context, PersonActionBean.class, session); trip.setParameter("person.firstName", "John"); trip.setParameter("person.lastName", "Smith"); trip.execute(); PersonActionBean bean = trip.getActionBean(PersonActionBean.class); Assert.assertEquals("John", bean.getPerson().getFirstName()); Assert.assertEquals("Smith", bean.getPerson().getLastName()); } // If only one property is set, object should be replaced. { MockRoundtrip trip = new MockRoundtrip(context, PersonActionBean.class, session); trip.setParameter("person.firstName", "Christian"); trip.execute(); PersonActionBean bean = trip.getActionBean(PersonActionBean.class); Assert.assertEquals("Christian", bean.getPerson().getFirstName()); Assert.assertEquals(null, bean.getPerson().getLastName()); } }