ContactManagerTests.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package sample.contact;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.fail;
  18. import java.util.List;
  19. import org.junit.After;
  20. import org.junit.Test;
  21. import org.junit.runner.RunWith;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.security.acls.domain.BasePermission;
  24. import org.springframework.security.acls.sid.PrincipalSid;
  25. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  26. import org.springframework.security.core.Authentication;
  27. import org.springframework.security.core.context.SecurityContextHolder;
  28. import org.springframework.test.context.ContextConfiguration;
  29. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  30. /**
  31. * Tests {@link ContactManager}.
  32. *
  33. * @author David Leal
  34. * @author Ben Alex
  35. * @Author Luke Taylor
  36. */
  37. @ContextConfiguration(locations={
  38. "/applicationContext-common-authorization.xml",
  39. "/applicationContext-common-business.xml",
  40. "/applicationContext-contacts-test.xml"})
  41. @RunWith(SpringJUnit4ClassRunner.class)
  42. public class ContactManagerTests {
  43. //~ Instance fields ================================================================================================
  44. @Autowired
  45. protected ContactManager contactManager;
  46. //~ Methods ========================================================================================================
  47. void assertContainsContact(long id, List<Contact> contacts) {
  48. for(Contact contact : contacts) {
  49. if (contact.getId().equals(Long.valueOf(id))) {
  50. return;
  51. }
  52. }
  53. fail("List of contacts should have contained: " + id);
  54. }
  55. void assertDoestNotContainContact(long id, List<Contact> contacts) {
  56. for(Contact contact : contacts) {
  57. if (contact.getId().equals(Long.valueOf(id))) {
  58. fail("List of contact should NOT (but did) contain: " + id);
  59. }
  60. }
  61. }
  62. /**
  63. * Locates the first <code>Contact</code> of the exact name specified.
  64. * <p>
  65. * Uses the {@link ContactManager#getAll()} method.
  66. *
  67. * @param id Identify of the contact to locate (must be an exact match)
  68. *
  69. * @return the domain or <code>null</code> if not found
  70. */
  71. Contact getContact(String id) {
  72. for(Contact contact : contactManager.getAll()) {
  73. if (contact.getId().equals(id)) {
  74. return contact;
  75. }
  76. }
  77. return null;
  78. }
  79. private void makeActiveUser(String username) {
  80. String password = "";
  81. if ("rod".equals(username)) {
  82. password = "koala";
  83. } else if ("dianne".equals(username)) {
  84. password = "emu";
  85. } else if ("scott".equals(username)) {
  86. password = "wombat";
  87. } else if ("peter".equals(username)) {
  88. password = "opal";
  89. }
  90. Authentication authRequest = new UsernamePasswordAuthenticationToken(username, password);
  91. SecurityContextHolder.getContext().setAuthentication(authRequest);
  92. }
  93. @After
  94. public void clearContext() {
  95. SecurityContextHolder.clearContext();
  96. }
  97. @Test
  98. public void testDianne() {
  99. makeActiveUser("dianne"); // has ROLE_USER
  100. List<Contact> contacts = contactManager.getAll();
  101. assertEquals(4, contacts.size());
  102. assertContainsContact(4, contacts);
  103. assertContainsContact(5, contacts);
  104. assertContainsContact(6, contacts);
  105. assertContainsContact(8, contacts);
  106. assertDoestNotContainContact(1, contacts);
  107. assertDoestNotContainContact(2, contacts);
  108. assertDoestNotContainContact(3, contacts);
  109. }
  110. @Test
  111. public void testrod() {
  112. makeActiveUser("rod"); // has ROLE_SUPERVISOR
  113. List<Contact> contacts = contactManager.getAll();
  114. assertEquals(4, contacts.size());
  115. assertContainsContact(1, contacts);
  116. assertContainsContact(2, contacts);
  117. assertContainsContact(3, contacts);
  118. assertContainsContact(4, contacts);
  119. assertDoestNotContainContact(5, contacts);
  120. Contact c1 = contactManager.getById(new Long(4));
  121. contactManager.deletePermission(c1, new PrincipalSid("bob"), BasePermission.ADMINISTRATION);
  122. contactManager.addPermission(c1, new PrincipalSid("bob"), BasePermission.ADMINISTRATION);
  123. }
  124. @Test
  125. public void testScott() {
  126. makeActiveUser("scott"); // has ROLE_USER
  127. List<Contact> contacts = contactManager.getAll();
  128. assertEquals(5, contacts.size());
  129. assertContainsContact(4, contacts);
  130. assertContainsContact(6, contacts);
  131. assertContainsContact(7, contacts);
  132. assertContainsContact(8, contacts);
  133. assertContainsContact(9, contacts);
  134. assertDoestNotContainContact(1, contacts);
  135. }
  136. }