e1f96f477530777a55c2bbf5b2058e10e703a370
[cdmlib.git] / cdmlib-model / src / test / java / eu / etaxonomy / cdm / strategy / cache / agent / TeamDefaultCacheStrategyTest.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.strategy.cache.agent;
10
11 import static org.junit.Assert.assertNotNull;
12
13 import org.apache.commons.lang3.StringUtils;
14 import org.apache.logging.log4j.LogManager;
15 import org.apache.logging.log4j.Logger;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19
20 import eu.etaxonomy.cdm.model.agent.Person;
21 import eu.etaxonomy.cdm.model.agent.Team;
22
23 /**
24 * @author a.mueller
25 * @since 29.09.2009
26 */
27 public class TeamDefaultCacheStrategyTest {
28
29 @SuppressWarnings("unused")
30 private static final Logger logger = LogManager.getLogger();
31
32 private static Team team1;
33 private static Team team2;
34 private static Team team3;
35
36 private static Person person1;
37 private static Person person2;
38 private static Person person3;
39 private static Person person4;
40
41 @Before
42 public void setUp() throws Exception {
43 team1 = Team.NewInstance();
44 team2 = Team.NewInstance();
45 team3 = Team.NewInstance(); //empty team
46
47 person1 = Person.NewInstance();
48
49 person1.setGivenName("P1GN");
50 person1.setFamilyName("P1FN");
51 person1.setPrefix("Dr1.");
52 person1.setSuffix("Suff1");
53 person1.setCollectorTitle("P1CT");
54
55 person2 = Person.NewInstance();
56 person2.setNomenclaturalTitle("P2NomT");
57 person2.setFamilyName("P2FN");
58 person2.setGivenName("P2GN");
59 person2.setSuffix("P2Suff");
60
61 person3 = Person.NewInstance();
62 person3.setNomenclaturalTitle("P3NomT");
63
64 person4 = Person.NewInstance();
65 person4.setNomenclaturalTitle("P4NomT");
66 person4.setFamilyName("P4FN");
67
68 team1.addTeamMember(person1);
69
70 team2.addTeamMember(person2);
71 team2.addTeamMember(person1);
72 team2.addTeamMember(person3);
73 team2.addTeamMember(person4);
74 }
75
76 //**************************************** TESTS **************************************
77
78 @Test
79 public final void testNewInstance() {
80 TeamDefaultCacheStrategy cacheStrategy = TeamDefaultCacheStrategy.NewInstance();
81 assertNotNull(cacheStrategy);
82 }
83
84 @Test
85 public final void testGetNomenclaturalTitleCache(){
86 Assert.assertNotNull("team1 nomenclatural title must not to be null",
87 team1.getNomenclaturalTitleCache());
88 Assert.assertEquals("team1 nomenclatural title should be created by elements",
89 "P1FN, P.", team1.getNomenclaturalTitleCache());
90 person1.setSuffix(null);
91 Assert.assertEquals("team1 nomenclatural title should be P1FN, P.",
92 "P1FN, P.", team1.getNomenclaturalTitleCache());
93
94 //team2
95 Assert.assertEquals("team2 nomenclatural title should be 'P2NomT, P1FN, P., P3NomT & P4NomT'",
96 "P2NomT, P1FN, P., P3NomT & P4NomT", team2.getNomenclaturalTitleCache());
97 //more
98 team2.setHasMoreMembers(true);
99 Assert.assertEquals("team2 nomenclatural title should be 'P2NomT, P1FN, P., P3NomT, P4NomT & al.'",
100 "P2NomT, P1FN, P., P3NomT, P4NomT & al.", team2.getNomenclaturalTitleCache());
101 team2.setHasMoreMembers(false);
102 //3 members
103 team2.setCacheStrategy(TeamDefaultCacheStrategy.NewInstanceNomEtAl(3));
104 team2.setTitleCache(null, false);
105 Assert.assertEquals("team2 nomenclatural title should still be 'P2NomT, P1FN, P. & al.' now.",
106 "P2NomT, P1FN, P. & al.", team2.getNomenclaturalTitleCache());
107 //4 members
108 team2.setCacheStrategy(TeamDefaultCacheStrategy.NewInstanceNomEtAl(4));
109 team2.setTitleCache(null, false);
110 Assert.assertEquals("team2 nomenclatural title should still be 'P2NomT, P1FN, P., P3NomT & P4NomT' now.",
111 "P2NomT, P1FN, P., P3NomT & P4NomT", team2.getNomenclaturalTitleCache());
112
113
114 //team3/empty team
115 Assert.assertNotNull("team3 nomenclatural title must not to be null",
116 team3.getNomenclaturalTitleCache());
117 Assert.assertTrue("team3 nomenclatural title must not be empty",
118 StringUtils.isNotBlank(team3.getNomenclaturalTitleCache()));
119
120 //don't take next test to serious, may be also something different, but not empty
121 Assert.assertEquals("team3 nomenclatural title should be empty team replacement string", TeamDefaultCacheStrategy.EMPTY_TEAM, team3.getNomenclaturalTitleCache());
122 }
123
124 @Test
125 public final void testGetTitleCache(){
126 Assert.assertNotNull("team1 title cache must not to be null",
127 team1.getTitleCache());
128 Assert.assertEquals("team1 title cache should be created by members titleCache",
129 "P1FN, P.", team1.getTitleCache());
130 person1.setSuffix(null);
131 Assert.assertEquals("team1 title cache should be P1FN, P.",
132 "P1FN, P.", team1.getTitleCache());
133 //peson2
134 Assert.assertEquals("team2 title cache should be 'P2FN, P., P1FN, P., P3NomT & P4FN'",
135 "P2FN, P., P1FN, P., P3NomT & P4FN", team2.getTitleCache());
136 team2.setHasMoreMembers(true);
137
138 Assert.assertEquals("team2 title cache should be 'P2FN, P., P1FN, P., P3NomT, P4FN & al.'",
139 "P2FN, P., P1FN, P., P3NomT, P4FN & al.", team2.getTitleCache());
140 team2.setHasMoreMembers(false);
141
142 team2.setCacheStrategy(TeamDefaultCacheStrategy.NewInstanceTitleEtAl(3));
143 team2.setTitleCache(null, false);
144 Assert.assertEquals("team2 nomenclatural title should still be 'P2FN, P., P1FN, P. & al.' now.",
145 "P2FN, P., P1FN, P. & al.", team2.getTitleCache());
146
147 team2.setCacheStrategy(TeamDefaultCacheStrategy.NewInstanceTitleEtAl(4));
148 team2.setTitleCache(null, false);
149 Assert.assertEquals("team2 nomenclatural title should still be 'P2FN, P., P1FN, P., P3NomT & P4FN' now.",
150 "P2FN, P., P1FN, P., P3NomT & P4FN", team2.getTitleCache());
151 team2.setHasMoreMembers(true);
152 team2.setTitleCache(null, false);
153 Assert.assertEquals("team2 nomenclatural title should still be 'P2FN, P., P1FN, P., P3NomT & al.' now.",
154 "P2FN, P., P1FN, P., P3NomT & al.", team2.getTitleCache());
155
156
157 //person3
158 Assert.assertNotNull("team3 title cache must not to be null",
159 team3.getTitleCache());
160 Assert.assertTrue("team3 title cache must not be empty",
161 StringUtils.isNotBlank(team3.getTitleCache()));
162
163 //don't take to exact, may be also something different, but not empty
164 Assert.assertEquals("team3 title cache should be empty team replacement string",
165 TeamDefaultCacheStrategy.EMPTY_TEAM, team3.getTitleCache());
166 }
167
168 @Test
169 public final void testFullTitle(){
170 Assert.assertNotNull("team1 full title must not to be null", team1.getFullTitle());
171 Assert.assertEquals("team1 full title should be created by elements",
172 "Dr1. P1GN P1FN Suff1", team1.getFullTitle());
173 person1.setSuffix(null);
174 Assert.assertEquals("team1 full title should be Dr1. P1GN P1FN", "Dr1. P1GN P1FN",
175 team1.getFullTitle());
176 //team2
177 Assert.assertEquals("team2 full title should be 'P2GN P2FN P2Suff, Dr1. P1GN P1FN, P3NomT & P4FN'",
178 "P2GN P2FN P2Suff, Dr1. P1GN P1FN, P3NomT & P4FN", team2.getFullTitle());
179 team2.setHasMoreMembers(true);
180 Assert.assertEquals("team2 full title should be 'P2GN P2FN P2Suff, Dr1. P1GN P1FN, P3NomT, P4FN & al.'",
181 "P2GN P2FN P2Suff, Dr1. P1GN P1FN, P3NomT, P4FN & al.", team2.getFullTitle());
182
183 //team3
184 Assert.assertNotNull("team3 full title must not to be null",
185 team3.getFullTitle());
186 Assert.assertTrue("team3 full title must not be empty",
187 StringUtils.isNotBlank(team3.getFullTitle()));
188
189 //don't take to serious, may be also something different, but not empty
190 Assert.assertEquals("team3 full title should should be empty team replacement string",
191 TeamDefaultCacheStrategy.EMPTY_TEAM, team3.getFullTitle());
192 }
193
194 @Test
195 public final void testGetCollectorTitleCache(){
196 Assert.assertNotNull("team1 collector title cache must not to be null",
197 team1.getCollectorTitleCache());
198 Assert.assertEquals("team1 collector title cache title should be created by elements",
199 "P1CT", team1.getCollectorTitleCache());
200 person1.setSuffix(null);
201 Assert.assertEquals("team1 collector title cache should be P1FN, P.",
202 "P1CT", team1.getCollectorTitleCache());
203
204 //team2
205 Assert.assertEquals("team2 collector title cache should be 'P. P2FN, P1CT, P3NomT & P4FN'",
206 "P. P2FN, P1CT, P3NomT & P4FN", team2.getCollectorTitleCache());
207 //more
208 team2.setHasMoreMembers(true);
209 Assert.assertEquals("team2 collector title cache should be 'P. P2FN, P1CT, P3NomT, P4FN & al.'",
210 "P. P2FN, P1CT, P3NomT, P4FN & al.", team2.getCollectorTitleCache());
211 team2.setHasMoreMembers(false);
212 //3 members
213 team2.setCacheStrategy(TeamDefaultCacheStrategy.NewInstance(3));
214 team2.resetCaches();
215 Assert.assertEquals("team2 collector title cache should still be 'P. P2FN, P1CT & al.' now.",
216 "P. P2FN, P1CT & al.", team2.getCollectorTitleCache());
217 //4 members
218 team2.setCacheStrategy(TeamDefaultCacheStrategy.NewInstance(4));
219 team2.resetCaches();
220 Assert.assertEquals("team2 collector title cache should still be 'P. P2FN, P1CT, P3NomT & P4FN' now.",
221 "P. P2FN, P1CT, P3NomT & P4FN", team2.getCollectorTitleCache());
222
223
224 //team3/empty team
225 Assert.assertNotNull("team3 collector title cache must not to be null",
226 team3.getCollectorTitleCache());
227 Assert.assertTrue("team3 collector title cache must not be empty",
228 StringUtils.isNotBlank(team3.getCollectorTitleCache()));
229
230 //don't take next test to serious, may be also something different, but not empty
231 Assert.assertEquals("team3 collector title cache should be empty team replacement string", TeamDefaultCacheStrategy.EMPTY_TEAM, team3.getCollectorTitleCache());
232 }
233
234 @Test
235 public final void testListenersOnMembers(){
236 Assert.assertNotNull("team1 title cache must not to be null", team1.getTitleCache());
237 Assert.assertEquals("team1 title cache should be created by elements",
238 "P1FN, P.", team1.getTitleCache());
239 person1.setGivenName("O.");
240 Assert.assertEquals("team1 title cache should be P1FN, O.", "P1FN, O.", team1.getTitleCache());
241 }
242 }