1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.mailboxmanager.util;
21  
22  import java.util.Iterator;
23  
24  import javax.mail.Flags;
25  
26  import org.apache.james.mailboxmanager.MailboxListener;
27  import org.apache.james.mailboxmanager.MockMailboxListenerAdded;
28  import org.apache.james.mailboxmanager.MockMailboxListenerFlagsUpdate;
29  import org.jmock.Mock;
30  import org.jmock.MockObjectTestCase;
31  
32  public class MailboxEventAnalyserTest extends MockObjectTestCase {
33  
34      private static final long BASE_SESSION_ID = 99;
35  
36      MailboxEventAnalyser analyser;
37  
38      protected void setUp() throws Exception {
39          super.setUp();
40          analyser = new MailboxEventAnalyser(BASE_SESSION_ID);
41      }
42  
43      protected void tearDown() throws Exception {
44          super.tearDown();
45      }
46  
47      public void testShouldBeNoSizeChangeOnOtherEvent() throws Exception {
48          final Mock mock = mock(MailboxListener.Event.class);
49          mock.expects(atLeastOnce()).method("getSessionId").will(
50                  returnValue(11L));
51          analyser.event((MailboxListener.Event) mock.proxy());
52          assertFalse(analyser.isSizeChanged());
53      }
54  
55      public void testShouldBeNoSizeChangeOnAdded() throws Exception {
56          analyser.event(new MockMailboxListenerAdded(78, 11));
57          assertTrue(analyser.isSizeChanged());
58      }
59  
60      public void testShouldNoSizeChangeAfterReset() throws Exception {
61          analyser.event(new MockMailboxListenerAdded(99, 11));
62          analyser.reset();
63          assertFalse(analyser.isSizeChanged());
64      }
65  
66      public void testShouldNotSetUidWhenNoSystemFlagChange() throws Exception {
67          final MockMailboxListenerFlagsUpdate update = new MockMailboxListenerFlagsUpdate(
68                  90, new Flags(), 11);
69          analyser.event(update);
70          assertNotNull(analyser.flagUpdateUids());
71          assertFalse(analyser.flagUpdateUids().hasNext());
72      }
73  
74      public void testShouldSetUidWhenSystemFlagChange() throws Exception {
75          final long uid = 900L;
76          final MockMailboxListenerFlagsUpdate update = new MockMailboxListenerFlagsUpdate(
77                  uid, new Flags(), 11);
78          update.flags.add(Flags.Flag.ANSWERED);
79          analyser.event(update);
80          final Iterator iterator = analyser.flagUpdateUids();
81          assertNotNull(iterator);
82          assertTrue(iterator.hasNext());
83          assertEquals(new Long(uid), iterator.next());
84          assertFalse(iterator.hasNext());
85      }
86  
87      public void testShouldClearFlagUidsUponReset() throws Exception {
88          final long uid = 900L;
89          final MockMailboxListenerFlagsUpdate update = new MockMailboxListenerFlagsUpdate(
90                  uid, new Flags(), 11);
91          update.flags.add(Flags.Flag.ANSWERED);
92          analyser.event(update);
93          analyser.reset();
94          assertNotNull(analyser.flagUpdateUids());
95          assertFalse(analyser.flagUpdateUids().hasNext());
96      }
97  
98      public void testShouldNotSetUidWhenSystemFlagChangeDifferentSessionInSilentMode()
99              throws Exception {
100         final long uid = 900L;
101         final MockMailboxListenerFlagsUpdate update = new MockMailboxListenerFlagsUpdate(
102                 uid, new Flags(), 11);
103         update.flags.add(Flags.Flag.ANSWERED);
104         analyser.setSilentFlagChanges(true);
105         analyser.event(update);
106         final Iterator iterator = analyser.flagUpdateUids();
107         assertNotNull(iterator);
108         assertTrue(iterator.hasNext());
109         assertEquals(new Long(uid), iterator.next());
110         assertFalse(iterator.hasNext());
111     }
112 
113     public void testShouldNotSetUidWhenSystemFlagChangeSameSessionInSilentMode()
114             throws Exception {
115         final MockMailboxListenerFlagsUpdate update = new MockMailboxListenerFlagsUpdate(
116                 345, new Flags(), BASE_SESSION_ID);
117         update.flags.add(Flags.Flag.ANSWERED);
118         analyser.setSilentFlagChanges(true);
119         analyser.event(update);
120         final Iterator iterator = analyser.flagUpdateUids();
121         assertNotNull(iterator);
122         assertFalse(iterator.hasNext());
123     }
124 
125     public void testShouldNotSetUidWhenOnlyRecentFlagUpdated() throws Exception {
126         final MockMailboxListenerFlagsUpdate update = new MockMailboxListenerFlagsUpdate(
127                 886, new Flags(), BASE_SESSION_ID);
128         update.flags.add(Flags.Flag.RECENT);
129         analyser.event(update);
130         final Iterator iterator = analyser.flagUpdateUids();
131         assertNotNull(iterator);
132         assertFalse(iterator.hasNext());
133     }
134 }