Example #1
0
  /**
   * 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));
    }
  }
Example #2
0
  /**
   * 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());
  }
Example #3
0
  /**
   * 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());
    }
  }
Example #4
0
  /**
   * When serializable attribute is true, value should stay in session when session passivate.
   *
   * @throws Exception
   */
  @Test(groups = "session")
  public void sessionSerialization() throws Exception {
    MockHttpSessionWithEvent session = new MockHttpSessionWithEvent(context);
    // Set result to 1.
    {
      MockRoundtrip trip = new MockRoundtrip(context, SerializeActionBean.class, session);
      trip.setParameter("number", "1");
      trip.execute();

      SerializeActionBean bean = trip.getActionBean(SerializeActionBean.class);
      Assert.assertEquals(new Integer(1), bean.getResult());
    }

    // Simulate serialization.
    session.passivate();
    // Simulate deserialization.
    session.activate();

    // Add 2 (total 3).
    {
      MockRoundtrip trip = new MockRoundtrip(context, SerializeActionBean.class, session);
      trip.setParameter("number", "2");
      trip.execute();

      SerializeActionBean bean = trip.getActionBean(SerializeActionBean.class);
      Assert.assertEquals(new Integer(3), bean.getResult());
    }

    // Serializable is true by default.
    // Set result to 1.
    {
      MockRoundtrip trip = new MockRoundtrip(context, SessionActionBean.class, session);
      trip.setParameter("number", "1");
      trip.execute();

      SessionActionBean bean = trip.getActionBean(SessionActionBean.class);
      Assert.assertEquals(new Integer(1), bean.getResult());
    }

    // Simulate serialization.
    session.passivate();
    // Simulate deserialization.
    session.activate();

    // Add 2 (total 3).
    {
      MockRoundtrip trip = new MockRoundtrip(context, SessionActionBean.class, session);
      trip.setParameter("number", "2");
      trip.execute();

      SessionActionBean bean = trip.getActionBean(SessionActionBean.class);
      Assert.assertEquals(new Integer(3), bean.getResult());
    }
  }