DmsIntegrationTests.java 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import org.springframework.security.context.SecurityContextHolder;
  2. import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
  3. import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
  4. import sample.dms.AbstractElement;
  5. import sample.dms.Directory;
  6. import sample.dms.DocumentDao;
  7. /**
  8. * Basic integration test for DMS sample.
  9. *
  10. * @author Ben Alex
  11. * @version $Id$
  12. *
  13. */
  14. public class DmsIntegrationTests extends AbstractTransactionalDataSourceSpringContextTests {
  15. protected DocumentDao documentDao;
  16. protected String[] getConfigLocations() {
  17. return new String[] {"classpath:applicationContext-dms-shared.xml", "classpath:applicationContext-dms-insecure.xml"};
  18. }
  19. protected void onTearDown() throws Exception {
  20. SecurityContextHolder.clearContext();
  21. }
  22. public void setDocumentDao(DocumentDao documentDao) {
  23. this.documentDao = documentDao;
  24. }
  25. public void testBasePopulation() {
  26. assertEquals(9, jdbcTemplate.queryForInt("select count(id) from DIRECTORY"));
  27. assertEquals(90, jdbcTemplate.queryForInt("select count(id) from FILE"));
  28. assertEquals(3, documentDao.findElements(Directory.ROOT_DIRECTORY).length);
  29. }
  30. public void testMarissaRetrieval() {
  31. process("rod", "koala", false);
  32. }
  33. public void testScottRetrieval() {
  34. process("scott", "wombat", false);
  35. }
  36. public void testDianneRetrieval() {
  37. process("dianne", "emu", false);
  38. }
  39. protected void process(String username, String password, boolean shouldBeFiltered) {
  40. SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(username, password));
  41. System.out.println("------ Test for username: " + username + " ------");
  42. AbstractElement[] rootElements = documentDao.findElements(Directory.ROOT_DIRECTORY);
  43. assertEquals(3, rootElements.length);
  44. Directory homeDir = null;
  45. Directory nonHomeDir = null;
  46. for (int i = 0; i < rootElements.length; i++) {
  47. if (rootElements[i].getName().equals(username)) {
  48. homeDir = (Directory) rootElements[i];
  49. } else {
  50. nonHomeDir = (Directory) rootElements[i];
  51. }
  52. }
  53. System.out.println("Home directory......: " + homeDir.getFullName());
  54. System.out.println("Non-home directory..: " + nonHomeDir.getFullName());
  55. AbstractElement[] homeElements = documentDao.findElements(homeDir);
  56. assertEquals(12, homeElements.length); // confidential and shared directories, plus 10 files
  57. AbstractElement[] nonHomeElements = documentDao.findElements(nonHomeDir);
  58. assertEquals(shouldBeFiltered ? 11 : 12, nonHomeElements.length); // cannot see the user's "confidential" sub-directory when filtering
  59. // Attempt to read the other user's confidential directory from the returned results
  60. // Of course, we shouldn't find a "confidential" directory in the results if we're filtering
  61. Directory nonHomeConfidentialDir = null;
  62. for (int i = 0; i < nonHomeElements.length; i++) {
  63. if (nonHomeElements[i].getName().equals("confidential")) {
  64. nonHomeConfidentialDir = (Directory) nonHomeElements[i];
  65. }
  66. }
  67. if (shouldBeFiltered) {
  68. assertNull("Found confidential directory when we should not have", nonHomeConfidentialDir);
  69. } else {
  70. System.out.println("Inaccessible dir....: " + nonHomeConfidentialDir.getFullName());
  71. assertEquals(10, documentDao.findElements(nonHomeConfidentialDir).length); // 10 files (no sub-directories)
  72. }
  73. SecurityContextHolder.clearContext();
  74. }
  75. }