/** * Test removal of object from session when event set it to null. * * @throws Exception */ @Test(groups = "session") public void removeNull() throws Exception { MockHttpSessionWithEvent session = new MockHttpSessionWithEvent(context); String personKey = FirstKeyActionBean.class .getDeclaredField("person") .getAnnotation(org.stripesstuff.plugin.session.Session.class) .key(); // Set person. { MockRoundtrip trip = new MockRoundtrip(context, FirstKeyActionBean.class, session); trip.setParameter("person.firstName", "Christian"); trip.setParameter("person.lastName", "Poitras"); trip.execute(); FirstKeyActionBean bean = trip.getActionBean(FirstKeyActionBean.class); Assert.assertEquals("Christian", bean.getPerson().getFirstName()); Assert.assertEquals("Poitras", bean.getPerson().getLastName()); // Person should be in session. Assert.assertNotNull(session.getAttribute(personKey)); } // Remove person from session using action bean. { MockRoundtrip trip = new MockRoundtrip(context, FirstKeyActionBean.class, session); trip.execute("remove"); // Person should not be in session. Assert.assertNull(session.getAttribute(personKey)); } }
/** * Test {@link SessionStoreInterceptor#getAttribute(javax.servlet.http.HttpSession, String) * SessionStoreInterceptor.get} to get object from session. * * @throws Exception */ @Test(groups = "session") public void getAttibute() throws Exception { MockHttpSessionWithEvent session = new MockHttpSessionWithEvent(context); String personKey = FirstKeyActionBean.class .getDeclaredField("person") .getAnnotation(org.stripesstuff.plugin.session.Session.class) .key(); // Set person. { MockRoundtrip trip = new MockRoundtrip(context, FirstKeyActionBean.class, session); trip.setParameter("person.firstName", "Christian"); trip.setParameter("person.lastName", "Poitras"); trip.execute(); FirstKeyActionBean bean = trip.getActionBean(FirstKeyActionBean.class); Assert.assertEquals("Christian", bean.getPerson().getFirstName()); Assert.assertEquals("Poitras", bean.getPerson().getLastName()); } // Get person from Interceptor. // Test kept for compatibility reasons. @SuppressWarnings("deprecation") Object person = SessionStoreInterceptor.getAttribute(session, personKey); Assert.assertTrue(Person.class.isAssignableFrom(person.getClass())); Assert.assertEquals("Christian", ((Person) person).getFirstName()); Assert.assertEquals("Poitras", ((Person) person).getLastName()); // Compare with session content. Assert.assertTrue(Person.class.isAssignableFrom(session.getAttribute(personKey).getClass())); Assert.assertEquals("Christian", ((Person) session.getAttribute(personKey)).getFirstName()); Assert.assertEquals("Poitras", ((Person) session.getAttribute(personKey)).getLastName()); }