KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > test > AbstractEmailSubscriptionTest


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.test;
17
18 import org.outerj.daisy.repository.testsupport.AbstractDaisyTestCase;
19 import org.outerj.daisy.repository.*;
20 import org.outerj.daisy.repository.schema.DocumentType;
21 import org.outerj.daisy.repository.user.Role;
22 import org.outerj.daisy.repository.user.User;
23 import org.outerj.daisy.emailnotifier.EmailSubscriptionManager;
24 import org.outerj.daisy.emailnotifier.Subscription;
25 import org.outerj.daisy.emailnotifier.CollectionSubscriptionKey;
26
27 import java.util.HashSet JavaDoc;
28 import java.util.Arrays JavaDoc;
29
30 public abstract class AbstractEmailSubscriptionTest extends AbstractDaisyTestCase {
31     protected boolean resetDataStores() {
32         return true;
33     }
34
35     protected abstract RepositoryManager getRepositoryManager() throws Exception JavaDoc;
36
37     public void testSubscriptions() throws Exception JavaDoc {
38         RepositoryManager repositoryManager = getRepositoryManager();
39         Repository repository = repositoryManager.getRepository(new Credentials("testuser", "testuser"));
40         repository.switchRole(Role.ADMINISTRATOR);
41
42         // create some content
43
CollectionManager collectionManager = repository.getCollectionManager();
44         DocumentCollection collection1 = collectionManager.createCollection("collection1");
45         collection1.save();
46         DocumentCollection collection2 = collectionManager.createCollection("collection2");
47         collection2.save();
48
49         DocumentType documentType = repository.getRepositorySchema().createDocumentType("testdoctype");
50         documentType.save();
51
52         Document document1 = repository.createDocument("doc1", "testdoctype");
53         document1.addToCollection(collection1);
54         document1.addToCollection(collection2);
55         document1.save();
56
57         Document document2 = repository.createDocument("doc2", "testdoctype");
58         document2.addToCollection(collection1);
59         document2.addToCollection(collection2);
60         document2.save();
61
62         EmailSubscriptionManager emailSubscriptionManager = (EmailSubscriptionManager)repository.getExtension("EmailSubscriptionManager");
63
64         Subscription subscription = emailSubscriptionManager.getSubscription();
65         subscription.setReceiveDocumentEvents(true);
66         subscription.save();
67
68         assertEquals(1, emailSubscriptionManager.getSubscriptions().getArray().length);
69
70         // Create a non-admin role and user
71
Role userRole = repository.getUserManager().getRole("user", false);
72
73         User jules = repository.getUserManager().createUser("jules");
74         jules.addToRole(userRole);
75         jules.setDefaultRole(userRole);
76         jules.setPassword("jules");
77         jules.save();
78
79         Repository julesRepository = repositoryManager.getRepository(new Credentials("jules", "jules"));
80         EmailSubscriptionManager julesSM = (EmailSubscriptionManager)julesRepository.getExtension("EmailSubscriptionManager");
81         Subscription julesSubscription = julesSM.getSubscription();
82         julesSubscription.setReceiveDocumentEvents(true);
83         julesSubscription.setReceiveSchemaEvents(true);
84         julesSubscription.setReceiveCommentEvents(true);
85         julesSubscription.save();
86
87         julesSubscription = julesSM.getSubscription();
88         assertEquals(julesSubscription.getUserId(), jules.getId());
89         assertEquals(julesSubscription.getReceiveDocumentEvents(), true);
90         assertEquals(julesSubscription.getReceiveSchemaEvents(), true);
91         assertEquals(julesSubscription.getReceiveUserEvents(), false);
92         assertEquals(julesSubscription.getReceiveCollectionEvents(), false);
93         assertEquals(julesSubscription.getReceiveAclEvents(), false);
94         assertEquals(julesSubscription.getReceiveCommentEvents(), true);
95
96         try {
97             julesSM.getSubscription(repository.getUserId());
98             fail("User jules shouldn't be able to retrieve subscription of other user.");
99         } catch (Exception JavaDoc e) {
100         }
101
102         try {
103             julesSM.deleteSubscription(repository.getUserId());
104             fail("User jules shouldn't be able to delete subscription of other user.");
105         } catch (Exception JavaDoc e) {
106         }
107
108         try {
109             julesSM.getSubscriptions();
110             fail("User jules shouldn't be able to get list of all subscriptions.");
111         } catch (Exception JavaDoc e) {
112         }
113
114         try {
115             julesSM.deleteAllSubscriptionsForDocument(1);
116             fail("User jules shouldn't be able to delete all subscriptions of a document.");
117         } catch (Exception JavaDoc e) {
118         }
119
120         try {
121             julesSM.deleteAllSubscriptionsForCollection(1);
122             fail("User jules shouldn't be able to delete all subscriptions of a collection.");
123         } catch (Exception JavaDoc e) {
124         }
125
126         assertEquals(0, emailSubscriptionManager.getAllCollectionEventSubscribers().getArray().length);
127         assertEquals(1, emailSubscriptionManager.getAllSchemaEventSubscribers().getArray().length);
128         assertEquals(0, emailSubscriptionManager.getAllUserEventSubscribers().getArray().length);
129
130         julesSubscription.setSubscribedVariantKeys(new VariantKey[] { new VariantKey(document1.getId(), -1, -1) });
131         julesSubscription.save();
132
133         julesSM.addDocumentSubscription(new VariantKey(document2.getId(), -1, -1));
134
135         julesSubscription = julesSM.getSubscription();
136         assertEquals(2, julesSubscription.getSubscribedVariantKeys().length);
137
138         assertEquals(1, emailSubscriptionManager.getAllDocumentEventSubscribers(document1.getId(), -1, -1, new long[] {}).getArray().length);
139         assertEquals(1, emailSubscriptionManager.getAllCommentEventSubscribers(document1.getId(), -1, -1, new long[] {}).getArray().length);
140
141         emailSubscriptionManager.addDocumentSubscription(new VariantKey(document1.getId(), -1, -1));
142         assertEquals(2, emailSubscriptionManager.getAllDocumentEventSubscribers(document1.getId(), -1, -1, new long[] {}).getArray().length);
143
144         julesSubscription.setSubscribedVariantKeys(new VariantKey[] {});
145         julesSubscription.setSubscribedCollectionKeys(new CollectionSubscriptionKey[] { new CollectionSubscriptionKey(collection1.getId(), -1, -1), new CollectionSubscriptionKey(collection2.getId(), -1, -1)});
146         julesSubscription.save();
147
148         assertEquals(2, emailSubscriptionManager.getAllDocumentEventSubscribers(document1.getId(), -1, -1, new long[] {collection1.getId(), collection2.getId()}).getArray().length);
149
150         julesSM.addDocumentSubscription(new VariantKey(document1.getId(), -1, -1));
151         julesSM.deleteDocumentSubscription(new VariantKey(document1.getId(), -1, -1));
152
153         julesSubscription = julesSM.getSubscription();
154         assertEquals(0, julesSubscription.getSubscribedVariantKeys().length);
155
156         emailSubscriptionManager.addDocumentSubscription(jules.getId(), new VariantKey(document1.getId(), -1, -1));
157         emailSubscriptionManager.addDocumentSubscription(jules.getId(), new VariantKey(document2.getId(), -1, -1));
158         julesSubscription = julesSM.getSubscription();
159         assertEquals(2, julesSubscription.getSubscribedVariantKeys().length);
160
161         emailSubscriptionManager.deleteAllSubscriptionsForDocument(document1.getId());
162         julesSubscription = julesSM.getSubscription();
163         assertEquals(1, julesSubscription.getSubscribedVariantKeys().length);
164
165         assertTrue(julesSM.isSubsribed(new VariantKey(document2.getId(), -1, -1)));
166
167         julesSM.deleteSubscription();
168
169         // user with admin privileges should be able to manipulate other users' subscriptions
170
julesSubscription = emailSubscriptionManager.getSubscription(jules.getId());
171         julesSubscription.setReceiveSchemaEvents(false);
172         julesSubscription.save();
173
174         // try subscription deletion
175
julesSM.deleteSubscription();
176
177
178         // try implicit subscription creation when calling addDocumentSubscription
179
User jef = repository.getUserManager().createUser("jef");
180         jef.addToRole(userRole);
181         jef.setDefaultRole(userRole);
182         jef.setPassword("jef");
183         jef.save();
184
185         Repository jefRepository = repositoryManager.getRepository(new Credentials("jef", "jef"));
186         EmailSubscriptionManager jefSM = (EmailSubscriptionManager)jefRepository.getExtension("EmailSubscriptionManager");
187         jefSM.addDocumentSubscription(new VariantKey(document1.getId(), -1, -1));
188
189         Subscription jefSubscription = jefSM.getSubscription();
190         assertEquals(1, jefSubscription.getSubscribedVariantKeys().length);
191
192         jefSM.deleteSubscription();
193         emailSubscriptionManager.deleteSubscription();
194
195
196         //
197
// Test variant-specific subscription things
198
//
199

200         // Note: for the subscription manager it doesn't matter whether branches & languages
201
// actually exist (nor documents for that matter), so we just use fake numbers here
202

203         // first reset the subscription
204
julesSubscription = julesSM.getSubscription();
205         julesSubscription.setSubscribedVariantKeys(new VariantKey[] {});
206         julesSubscription.setSubscribedCollectionKeys(new CollectionSubscriptionKey[] {});
207         julesSubscription.save();
208
209         // add subscription for different languages & branches
210
julesSM.addDocumentSubscription(new VariantKey(document1.getId(), 1, 1));
211         julesSM.addDocumentSubscription(new VariantKey(document1.getId(), 2, 2));
212         julesSM.addDocumentSubscription(new VariantKey(document2.getId(), 1, -1));
213
214         julesSubscription = julesSM.getSubscription();
215         HashSet JavaDoc keys = new HashSet JavaDoc(Arrays.asList(julesSubscription.getSubscribedVariantKeys()));
216         assertEquals(3, keys.size());
217         assertTrue(keys.contains(new VariantKey(document1.getId(), 1, 1)));
218         assertTrue(keys.contains(new VariantKey(document1.getId(), 2, 2)));
219         assertTrue(keys.contains(new VariantKey(document2.getId(), 1, -1)));
220
221         assertTrue(julesSM.isSubsribed(new VariantKey(document2.getId(), 1, -1)));
222         assertTrue(julesSM.isSubsribed(new VariantKey(document1.getId(), 1, 1)));
223         assertFalse(julesSM.isSubsribed(new VariantKey(document1.getId(), 1, 2)));
224
225         julesSM.deleteDocumentSubscription(new VariantKey(document1.getId(), 1, 1));
226         julesSM.deleteDocumentSubscription(new VariantKey(document1.getId(), 2, 2));
227         julesSM.deleteDocumentSubscription(new VariantKey(document2.getId(), 1, -1));
228
229         assertEquals(0, julesSM.getSubscription().getSubscribedVariantKeys().length);
230
231         julesSubscription = julesSM.getSubscription();
232         julesSubscription.setReceiveDocumentEvents(true);
233         julesSubscription.setSubscribedCollectionKeys(new CollectionSubscriptionKey[] {
234             new CollectionSubscriptionKey(collection1.getId(), 1, -1)
235         });
236         julesSubscription.save();
237
238         assertEquals(1, emailSubscriptionManager.getAllDocumentEventSubscribers(document1.getId(), 1, 123, new long[] {collection1.getId()}).getArray().length);
239         assertEquals(0, emailSubscriptionManager.getAllDocumentEventSubscribers(document1.getId(), 2, 1, new long[] {collection1.getId()}).getArray().length);
240     }
241 }
242
Popular Tags