/vt-ldap/tags/vt-ldap-3.0/src/test/java/edu/vt/middleware/ldap/servlets/session/SessionManagerTest.java

http://vt-middleware.googlecode.com/ · Java · 143 lines · 88 code · 19 blank · 36 comment · 1 complexity · 09e69e5f84971da7c39d50a527d3ddc0 MD5 · raw file

  1. /*
  2. $Id: SessionManagerTest.java 268 2009-05-28 14:21:40Z dfisher $
  3. Copyright (C) 2003-2008 Virginia Tech.
  4. All rights reserved.
  5. SEE LICENSE FOR MORE INFORMATION
  6. Author: Middleware Services
  7. Email: middleware@vt.edu
  8. Version: $Revision: 268 $
  9. Updated: $Date: 2009-05-28 16:21:40 +0200 (Thu, 28 May 2009) $
  10. */
  11. package edu.vt.middleware.ldap.servlets.session;
  12. import java.io.File;
  13. import com.meterware.httpunit.GetMethodWebRequest;
  14. import com.meterware.httpunit.PostMethodWebRequest;
  15. import com.meterware.httpunit.WebRequest;
  16. import com.meterware.httpunit.WebResponse;
  17. import com.meterware.servletunit.ServletRunner;
  18. import com.meterware.servletunit.ServletUnitClient;
  19. import edu.vt.middleware.ldap.Ldap;
  20. import edu.vt.middleware.ldap.TestUtil;
  21. import edu.vt.middleware.ldap.bean.LdapEntry;
  22. import org.testng.AssertJUnit;
  23. import org.testng.annotations.AfterClass;
  24. import org.testng.annotations.BeforeClass;
  25. import org.testng.annotations.Parameters;
  26. import org.testng.annotations.Test;
  27. /**
  28. * Unit test for {@link SessionManager}.
  29. *
  30. * @author Middleware Services
  31. * @version $Revision: 268 $
  32. */
  33. public class SessionManagerTest
  34. {
  35. /** Entry created for tests. */
  36. private static LdapEntry testLdapEntry;
  37. /** To test servlets with. */
  38. private ServletRunner servletRunner;
  39. /**
  40. * @param ldifFile to create.
  41. * @param webXml web.xml for queries
  42. *
  43. * @throws Exception On test failure.
  44. */
  45. @Parameters({ "createEntry10", "webXml" })
  46. @BeforeClass(groups = {"servlettest"})
  47. public void createLdapEntry(final String ldifFile, final String webXml)
  48. throws Exception
  49. {
  50. final String ldif = TestUtil.readFileIntoString(ldifFile);
  51. testLdapEntry = TestUtil.convertLdifToEntry(ldif);
  52. Ldap ldap = TestUtil.createSetupLdap();
  53. ldap.create(
  54. testLdapEntry.getDn(),
  55. testLdapEntry.getLdapAttributes().toAttributes());
  56. ldap.close();
  57. ldap = TestUtil.createLdap();
  58. while (
  59. !ldap.compare(
  60. testLdapEntry.getDn(),
  61. testLdapEntry.getDn().split(",")[0])) {
  62. Thread.sleep(100);
  63. }
  64. ldap.close();
  65. this.servletRunner = new ServletRunner(new File(webXml));
  66. }
  67. /** @throws Exception On test failure. */
  68. @AfterClass(groups = {"servlettest"})
  69. public void deleteLdapEntry()
  70. throws Exception
  71. {
  72. final Ldap ldap = TestUtil.createSetupLdap();
  73. ldap.delete(testLdapEntry.getDn());
  74. ldap.close();
  75. }
  76. /**
  77. * @param user to authenticate
  78. * @param password to authenticate with
  79. *
  80. * @throws Exception On test failure.
  81. */
  82. @Parameters({
  83. "sessionManagerUser",
  84. "sessionManagerPassword"
  85. })
  86. @Test(groups = {"servlettest"})
  87. public void login(final String user, final String password)
  88. throws Exception
  89. {
  90. System.setProperty(
  91. "javax.net.ssl.trustStore",
  92. "src/test/resources/ed.truststore");
  93. System.setProperty("javax.net.ssl.trustStoreType", "BKS");
  94. System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
  95. final ServletUnitClient sc = this.servletRunner.newClient();
  96. // login
  97. WebRequest request = new PostMethodWebRequest(
  98. "http://servlets.ldap.middleware.vt.edu/Login");
  99. request.setParameter("user", user);
  100. request.setParameter("credential", password);
  101. request.setParameter(
  102. "url",
  103. "http://servlets.ldap.middleware.vt.edu/SessionCheck");
  104. WebResponse response = sc.getResponse(request);
  105. AssertJUnit.assertNotNull(response);
  106. final String[] sessionData = response.getText().trim().split(":");
  107. AssertJUnit.assertEquals(user, sessionData[1]);
  108. // logout
  109. request = new GetMethodWebRequest(
  110. "http://servlets.ldap.middleware.vt.edu/Logout");
  111. request.setParameter(
  112. "url",
  113. "http://servlets.ldap.middleware.vt.edu/SessionCheck");
  114. response = sc.getResponse(request);
  115. AssertJUnit.assertNotNull(response);
  116. AssertJUnit.assertEquals("", response.getText());
  117. System.clearProperty("javax.net.ssl.trustStore");
  118. System.clearProperty("javax.net.ssl.trustStoreType");
  119. System.clearProperty("javax.net.ssl.trustStorePassword");
  120. }
  121. }