001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package sample.contact;
016:
017: import org.acegisecurity.Authentication;
018:
019: import org.acegisecurity.acls.domain.BasePermission;
020: import org.acegisecurity.acls.sid.PrincipalSid;
021:
022: import org.acegisecurity.context.SecurityContextHolder;
023:
024: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
025:
026: import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
027:
028: import org.springframework.test.AbstractTransactionalSpringContextTests;
029:
030: import java.util.Iterator;
031: import java.util.List;
032:
033: /**
034: * Tests {@link ContactManager}.
035: *
036: * @author David Leal
037: * @author Ben Alex
038: */
039: public class GetAllContactsTests extends
040: AbstractTransactionalSpringContextTests {
041: //~ Instance fields ================================================================================================
042:
043: protected ContactManager contactManager;
044:
045: //~ Methods ========================================================================================================
046:
047: protected void assertContainsContact(String id, List contacts) {
048: Iterator iter = contacts.iterator();
049: System.out.println(contacts);
050:
051: while (iter.hasNext()) {
052: Contact contact = (Contact) iter.next();
053:
054: if (contact.getId().toString().equals(id)) {
055: return;
056: }
057: }
058:
059: fail("List of contacts should have contained: " + id);
060: }
061:
062: protected void assertNotContainsContact(String id, List contacts) {
063: Iterator iter = contacts.iterator();
064:
065: while (iter.hasNext()) {
066: Contact domain = (Contact) iter.next();
067:
068: if (domain.getId().toString().equals(id)) {
069: fail("List of contact should NOT (but did) contain: "
070: + id);
071: }
072: }
073: }
074:
075: private static void destroySecureContext() {
076: SecurityContextHolder.clearContext();
077: }
078:
079: protected String[] getConfigLocations() {
080: setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
081:
082: return new String[] {
083: "applicationContext-common-authorization.xml",
084: "applicationContext-common-business.xml",
085: "applicationContext-contacts-test.xml" };
086: }
087:
088: /**
089: * Locates the first <code>Contact</code> of the exact name specified.<p>Uses the {@link
090: * ContactManager#getAll()} method.</p>
091: *
092: * @param id Identify of the contact to locate (must be an exact match)
093: *
094: * @return the domain or <code>null</code> if not found
095: */
096: protected Contact getContact(String id) {
097: List contacts = contactManager.getAll();
098: Iterator iter = contacts.iterator();
099:
100: while (iter.hasNext()) {
101: Contact contact = (Contact) iter.next();
102:
103: if (contact.getId().equals(id)) {
104: return contact;
105: }
106: }
107:
108: return null;
109: }
110:
111: protected void makeActiveUser(String username) {
112: String password = "";
113:
114: if ("marissa".equals(username)) {
115: password = "koala";
116: } else if ("dianne".equals(username)) {
117: password = "emu";
118: } else if ("scott".equals(username)) {
119: password = "wombat";
120: } else if ("peter".equals(username)) {
121: password = "opal";
122: }
123:
124: Authentication authRequest = new UsernamePasswordAuthenticationToken(
125: username, password);
126: SecurityContextHolder.getContext().setAuthentication(
127: authRequest);
128: }
129:
130: protected void onTearDownInTransaction() {
131: destroySecureContext();
132: }
133:
134: public void setContactManager(ContactManager contactManager) {
135: this .contactManager = contactManager;
136: }
137:
138: public void testDianne() {
139: makeActiveUser("dianne"); // has ROLE_USER
140:
141: List contacts = contactManager.getAll();
142: assertEquals(4, contacts.size());
143:
144: assertContainsContact(Long.toString(4), contacts);
145: assertContainsContact(Long.toString(5), contacts);
146: assertContainsContact(Long.toString(6), contacts);
147: assertContainsContact(Long.toString(8), contacts);
148:
149: assertNotContainsContact(Long.toString(1), contacts);
150: assertNotContainsContact(Long.toString(2), contacts);
151: assertNotContainsContact(Long.toString(3), contacts);
152: }
153:
154: public void testMarissa() {
155: makeActiveUser("marissa"); // has ROLE_SUPERVISOR
156:
157: List contacts = contactManager.getAll();
158:
159: assertEquals(4, contacts.size());
160:
161: assertContainsContact(Long.toString(1), contacts);
162: assertContainsContact(Long.toString(2), contacts);
163: assertContainsContact(Long.toString(3), contacts);
164: assertContainsContact(Long.toString(4), contacts);
165:
166: assertNotContainsContact(Long.toString(5), contacts);
167:
168: Contact c1 = contactManager.getById(new Long(4));
169:
170: contactManager.deletePermission(c1, new PrincipalSid("bob"),
171: BasePermission.ADMINISTRATION);
172: }
173:
174: public void testScott() {
175: makeActiveUser("scott"); // has ROLE_USER
176:
177: List contacts = contactManager.getAll();
178:
179: assertEquals(5, contacts.size());
180:
181: assertContainsContact(Long.toString(4), contacts);
182: assertContainsContact(Long.toString(6), contacts);
183: assertContainsContact(Long.toString(7), contacts);
184: assertContainsContact(Long.toString(8), contacts);
185: assertContainsContact(Long.toString(9), contacts);
186:
187: assertNotContainsContact(Long.toString(1), contacts);
188: }
189: }
|