1   /*
2    * Created by IntelliJ IDEA.
3    * User: Administrator
4    * Date: 4/03/2002
5    * Time: 15:15:27
6    * To change template
7    * for new class use
8    * Code Style | Class Templates options (Tools | IDE Options).
9    */
10  package com.atlassian.core.user;
11  
12  import com.opensymphony.user.DuplicateEntityException;
13  import com.opensymphony.user.Group;
14  import com.opensymphony.user.ImmutableException;
15  import com.opensymphony.user.User;
16  import com.opensymphony.user.UserManager;
17  import junit.framework.TestCase;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  /**
25   * Tests to test UserUtils and OSUser integration into JIRA
26   */
27  public class TestGroupUtils extends TestCase
28  {
29      private static final String groupName1 = "abc";
30      private static final String groupName2 = "ACb";
31      private static final String groupName3 = "xa";
32      private static final String groupName4 = "XB";
33      private static final String groupName5 = "XC";
34  
35      public TestGroupUtils(String s)
36      {
37          super(s);
38      }
39  
40      protected void setUp() throws Exception
41      {
42          super.setUp();
43      }
44  
45      protected void tearDown() throws Exception
46      {
47          for (Iterator iterator = UserManager.getInstance().getUsers().iterator(); iterator.hasNext();)
48          {
49              ((User) iterator.next()).remove();
50          }
51  
52          for (Iterator iterator = UserManager.getInstance().getGroups().iterator(); iterator.hasNext();)
53          {
54              ((Group) iterator.next()).remove();
55          }
56          super.tearDown();
57      }
58  
59      public void testGroupMethods() throws Exception
60      {
61          assertTrue(!GroupUtils.existsGroup("foo"));
62          assertNull(GroupUtils.getGroup("foo"));
63          assertEquals(0, GroupUtils.getGroups().size());
64  
65          Group foo = UserManager.getInstance().createGroup("foo");
66          assertEquals(1, GroupUtils.getGroups().size());
67          assertTrue(GroupUtils.getGroups().contains(foo));
68  
69          assertTrue(GroupUtils.existsGroup("foo"));
70          assertEquals(foo, GroupUtils.getGroup("foo"));
71  
72          assertTrue(!GroupUtils.existsGroup("bar"));
73          Group group = GroupUtils.getGroupSafely("bar");
74          assertTrue(GroupUtils.existsGroup("bar"));
75          assertEquals(group, GroupUtils.getGroup("bar"));
76      }
77  
78      public void testGetNullGroup()
79      {
80          final Group group = GroupUtils.getGroup(null);
81          assertNull(group);
82      }
83  
84      public void testRemoveGroup() throws Exception
85      {
86          Group foogroup = UserManager.getInstance().createGroup("foogroup");
87          User foo = UserManager.getInstance().createUser("foo");
88          foo.addToGroup(foogroup);
89  
90          assertEquals(1, foo.getGroups().size());
91          GroupUtils.removeGroup(foogroup);
92          assertEquals(0, foo.getGroups().size());
93      }
94  
95      public void testSortGroups() throws Exception
96      {
97          List alphabeticalGroupsNames = createAlphaGroupNameList();
98          List groups = createGroups(createUnalphaGroupNames());
99          GroupUtils.sortGroups(groups);
100 
101         assertAlphaOrder(alphabeticalGroupsNames, groups);
102     }
103 
104     private List createGroups(List groupNames) throws ImmutableException, DuplicateEntityException
105     {
106         List groups = new ArrayList();
107         for (Iterator iter = groupNames.iterator(); iter.hasNext();)
108         {
109             String groupName = (String) iter.next();
110             groups.add(UserManager.getInstance().createGroup(groupName));
111         }
112         return groups;
113     }
114 
115     private List createUnalphaGroupNames() throws DuplicateEntityException, ImmutableException
116     {
117         List groups = new ArrayList();
118         groups.add(groupName2);
119         groups.add(groupName1);
120         groups.add(groupName5);
121         groups.add(groupName4);
122         groups.add(groupName3);
123         return groups;
124     }
125 
126     public void testGetGroups() throws Exception
127     {
128         List alphabeticalGroups = createAlphaGroupNameList();
129 
130         UserManager.getInstance().createGroup(groupName5);
131         UserManager.getInstance().createGroup(groupName1);
132         UserManager.getInstance().createGroup(groupName4);
133         UserManager.getInstance().createGroup(groupName2);
134         UserManager.getInstance().createGroup(groupName3);
135 
136         Collection getGroups = GroupUtils.getGroups();
137 
138         assertAlphaOrder(alphabeticalGroups, getGroups);
139     }
140 
141     private List createAlphaGroupNameList()
142     {
143         List alphabeticalGroups = new ArrayList();
144         alphabeticalGroups.add(groupName1);
145         alphabeticalGroups.add(groupName2);
146         alphabeticalGroups.add(groupName3);
147         alphabeticalGroups.add(groupName4);
148         alphabeticalGroups.add(groupName5);
149         return alphabeticalGroups;
150     }
151 
152     private void assertAlphaOrder(List alphabeticalGroups, Collection getGroups)
153     {
154         Iterator alphaIter = alphabeticalGroups.iterator();
155         for (Iterator iter = getGroups.iterator(); iter.hasNext();)
156         {
157             Group group = (Group) iter.next();
158             String alphaName = (String) alphaIter.next();
159             assertTrue(alphaName.equals(group.getName()));
160         }
161     }
162 
163     public void testSortGroupNames() throws Exception
164     {
165         List alphabeticalGroupNames = createAlphaGroupNameList();
166         List unalphabeticalGroupNames = new ArrayList();
167         unalphabeticalGroupNames.add(groupName4);
168         unalphabeticalGroupNames.add(groupName1);
169         unalphabeticalGroupNames.add(groupName3);
170         unalphabeticalGroupNames.add(groupName5);
171         unalphabeticalGroupNames.add(groupName2);
172 
173         GroupUtils.sortGroupNames(unalphabeticalGroupNames);
174 
175         Iterator alphaIter = alphabeticalGroupNames.iterator();
176         for (Iterator iter = unalphabeticalGroupNames.iterator(); iter.hasNext();)
177         {
178             String groupName = (String) iter.next();
179             String alphaGroupName = (String) alphaIter.next();
180 
181             assertTrue("Asserting that the element of the previously unalphabetic list is now in the right position", groupName.equals(alphaGroupName));
182         }
183     }
184 }