1 package com.atlassian.core.ofbiz.test;
2
3 import com.atlassian.core.ofbiz.CoreFactory;
4 import com.atlassian.core.ofbiz.test.mock.MockSequenceUtil;
5 import com.atlassian.core.ofbiz.util.EntityUtils;
6 import com.atlassian.core.action.DefaultActionDispatcher;
7
8 import org.ofbiz.core.entity.GenericEntityException;
9 import org.ofbiz.core.entity.GenericValue;
10 import org.ofbiz.core.entity.MemoryHelper;
11 import org.ofbiz.core.entity.model.ModelEntity;
12
13 import webwork.action.ActionContext;
14
15 import com.opensymphony.user.DuplicateEntityException;
16 import com.opensymphony.user.Group;
17 import com.opensymphony.user.ImmutableException;
18 import com.opensymphony.user.User;
19 import com.opensymphony.user.UserManager;
20
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.Map;
25
26 import junit.framework.Assert;
27 import junit.framework.TestCase;
28
29 public class UtilsForTests
30 {
31
32
33
34
35
36
37
38 public static GenericValue getTestEntity(final String entity, final Map fields)
39 {
40 try
41 {
42 return EntityUtils.createValue(entity, fields);
43 }
44 catch (final GenericEntityException e)
45 {
46 throw new EntityException(e);
47 }
48 }
49
50
51
52
53
54
55
56
57 public static User getTestUser(final String username)
58 {
59 try
60 {
61 return UserManager.getInstance().createUser(username);
62 }
63 catch (final DuplicateEntityException e)
64 {
65 throw new DuplicateException(e);
66 }
67 catch (final ImmutableException e)
68 {
69 throw new UnchangeableException(e);
70 }
71 }
72
73 public static Group getTestGroup(final String groupname)
74 {
75 try
76 {
77 return UserManager.getInstance().createGroup(groupname);
78 }
79 catch (final DuplicateEntityException e)
80 {
81 throw new DuplicateException(e);
82 }
83 catch (final ImmutableException e)
84 {
85 throw new UnchangeableException(e);
86 }
87 }
88
89 public static GenericValue getTestConstant(final String entity, Map params)
90 {
91 try
92 {
93 if (params == null)
94 {
95 params = new HashMap();
96 }
97
98 if (params.get("id") == null)
99 {
100 final String id = EntityUtils.getNextStringId(entity);
101 params.put("id", id);
102 }
103
104 GenericValue v = CoreFactory.getGenericDelegator().makeValue(entity, params);
105 v = v.create();
106 return v;
107 }
108 catch (final GenericEntityException e)
109 {
110 throw new EntityException(e);
111 }
112 }
113
114
115
116
117 public static void cleanUsers() throws Exception
118 {
119 for (final Iterator iterator = UserManager.getInstance().getUsers().iterator(); iterator.hasNext();)
120 {
121 ((User) iterator.next()).remove();
122 }
123
124 for (final Iterator iterator = UserManager.getInstance().getGroups().iterator(); iterator.hasNext();)
125 {
126 ((Group) iterator.next()).remove();
127 }
128 }
129
130 public static void cleanWebWork()
131 {
132 ActionContext.setContext(new ActionContext());
133 CoreFactory.setActionDispatcher(new DefaultActionDispatcher());
134 }
135
136 public static void cleanOFBiz()
137 {
138 MemoryHelper.clearCache();
139
140 final String helperName = CoreFactory.getGenericDelegator().getEntityHelperName("SequenceValueItem");
141 final ModelEntity seqEntity = CoreFactory.getGenericDelegator().getModelEntity("SequenceValueItem");
142
143 CoreFactory.getGenericDelegator().setSequencer(new MockSequenceUtil(helperName, seqEntity, "seqName", "seqId"));
144 }
145
146
147
148
149 public static void checkSingleElementCollection(final TestCase test, final Collection collection, final Object expected)
150 {
151 Assert.assertEquals(1, collection.size());
152 Assert.assertTrue(collection.contains(expected));
153 }
154
155
156
157
158
159 public static class DuplicateException extends RuntimeException
160 {
161 DuplicateException(final DuplicateEntityException ex)
162 {
163 super(ex);
164 }
165 }
166
167 public static class EntityException extends RuntimeException
168 {
169 EntityException(final GenericEntityException ex)
170 {
171 super(ex);
172 }
173 }
174
175 public static class UnchangeableException extends RuntimeException
176 {
177 UnchangeableException(final ImmutableException ex)
178 {
179 super(ex);
180 }
181 }
182 }