1 package org.itracker.services;
2
3 import org.apache.log4j.Logger;
4 import org.hibernate.ObjectNotFoundException;
5 import org.itracker.TestInitialContextFactory;
6 import org.itracker.core.resources.ITrackerResources;
7 import org.itracker.model.*;
8 import org.itracker.model.CustomField.Type;
9 import org.itracker.model.util.CustomFieldUtilities;
10 import org.itracker.model.util.IssueUtilities;
11 import org.itracker.model.util.UserUtilities;
12 import org.itracker.persistence.dao.*;
13 import org.itracker.services.implementations.ConfigurationServiceImpl;
14 import org.junit.Test;
15 import org.springframework.mock.jndi.SimpleNamingContextBuilder;
16
17 import javax.naming.Context;
18 import javax.naming.InitialContext;
19 import java.text.DateFormat;
20 import java.text.ParseException;
21 import java.text.SimpleDateFormat;
22 import java.util.*;
23
24 import static org.hamcrest.CoreMatchers.*;
25 import static org.itracker.Assert.*;
26 import static org.itracker.model.Configuration.Type.*;
27
28 public class ConfigurationServiceIT extends
29 AbstractServicesIntegrationTest {
30
31 private static final Logger log = Logger
32 .getLogger(ConfigurationServiceIT.class);
33
34 private ConfigurationDAO configurationDAO;
35 private ProjectDAO projectDAO;
36 private ProjectScriptDAO projectScriptDAO;
37 private WorkflowScriptDAO workflowScriptDAO;
38 private CustomFieldDAO customFieldDAO;
39 private CustomFieldValueDAO customFieldValueDAO;
40 private LanguageDAO languageDAO;
41 Properties configurationProperties;
42
43
44
45
46 ConfigurationService configurationService;
47
48
49 @Test
50 public void testConfigurationServiceImplConstructor() {
51 try {
52 configurationService = new ConfigurationServiceImpl(null, configurationDAO, customFieldDAO, customFieldValueDAO,
53 languageDAO, projectScriptDAO, workflowScriptDAO);
54 fail("argument Properties configurationProperties is null, did not throw IllegalArgumentException");
55 } catch (IllegalArgumentException e) {
56 assertTrue(true);
57 }
58 }
59
60 @Test
61 public void testLookupConfigurationItemById() {
62
63 Configuration config = configurationService.getConfigurationItem(2000);
64
65 assertNotNull("configuration (id:2000)", config);
66 assertEquals("id", 2000, config.getId().intValue());
67 DateFormat format = new SimpleDateFormat("M-d-y");
68 try {
69 assertEquals("create date", format.parse("1-1-2008"), config
70 .getCreateDate());
71 assertEquals("modified date", format.parse("1-1-2008"), config
72 .getLastModifiedDate());
73 } catch (ParseException e) {
74 log
75 .error(
76 "testLookupConfigurationItemById: failed to parse date for assertion",
77 e);
78 fail("failed to parse date for assertion: " + e.getMessage());
79 }
80 assertEquals("value", "Test Value", config.getValue());
81 assertEquals("version", "Version 1.0", config.getVersion());
82 assertEquals("order", 1, config.getOrder());
83 assertEquals("type", (Object) 1, config.getType().getCode());
84 assertEquals("type", locale, config.getType());
85
86 }
87
88 @Test
89 public void testFailGetConfigurationItemById() {
90
91
92 Configuration config = configurationService
93 .getConfigurationItem(999999);
94
95 assertNull("non existing configuration item", config);
96
97 }
98
99
100
101
102 @Test
103 public void testGetJndiOverriddenProperty() throws Exception {
104 final SimpleNamingContextBuilder namingContext = TestInitialContextFactory.getNamingContext();
105 try {
106 final String web_session_timeout = "web_session_timeout";
107 namingContext.bind("java:comp/env/itracker/" + web_session_timeout, "300");
108
109
110 Context ctx = new InitialContext();
111
112
113 assertEquals("java:comp/env/itracker/web_session_timeout", "300", ctx.lookup("java:comp/env/itracker/" + web_session_timeout));
114
115 String val = configurationProperties.getProperty(web_session_timeout);
116 assertEquals("configurationProperties#web_session_timeout", "30", val);
117
118
119 val = configurationService.getProperty(web_session_timeout);
120 assertEquals("configurationService.web_session_timeout", "300", val);
121
122 } finally {
123 namingContext.deactivate();
124 }
125 }
126
127
128 @Test
129 public void testGetBooleanProperty() {
130 boolean prop = configurationService.getBooleanProperty("non_existent_property_name", false);
131 assertFalse(prop);
132
133 prop = configurationService.getBooleanProperty("non_existent_property_name", true);
134 assertTrue(prop);
135 }
136
137 @Test
138 public void testGetIntegerProperty() {
139 int prop = configurationService.getIntegerProperty("non_existent_property_name", 123456);
140 assertEquals(123456, prop);
141
142 prop = configurationService.getIntegerProperty("non_existent_property_name", 123456);
143 assertEquals(123456, prop);
144
145
146 prop = configurationService.getIntegerProperty("project", 123456);
147 assertEquals(123456, prop);
148 }
149
150 @Test
151 public void testGetLongProperty() {
152 long prop = configurationService.getLongProperty("non_existent_property_name", 123456L);
153 assertEquals(123456, prop);
154
155 prop = configurationService.getLongProperty("non_existent_property_name", 123456L);
156 assertEquals(123456, prop);
157
158
159 prop = configurationService.getLongProperty("project", 123456);
160 assertEquals(123456, prop);
161 }
162
163
164 @Test
165 public void testProperty() {
166 assertEquals("project property", configurationProperties
167 .getProperty("default_locale"), configurationService
168 .getProperty("default_locale"));
169
170 }
171
172 @Test
173 public void testResetConfigurationCache() {
174
175 configurationService.resetConfigurationCache(resolution);
176 assertEquals(1, IssueUtilities.getResolutions(Locale.ENGLISH).size());
177
178
179 configurationService.resetConfigurationCache(severity);
180 assertEquals(1, IssueUtilities.getSeverities(Locale.ENGLISH).size());
181
182
183 configurationService.resetConfigurationCache(status);
184 assertEquals(1, IssueUtilities.getStatuses(Locale.ENGLISH).size());
185
186
187 configurationService.resetConfigurationCache(customfield);
188 assertEquals(4, IssueUtilities.getCustomFields().size());
189 }
190
191 @Test
192 public void testGetConfigurationItemsByType() {
193 List<Configuration> configs = configurationService.getConfigurationItemsByType(locale);
194 assertNotNull(configs);
195 assertEquals("configs(locale)", 3, configs.size());
196 assertEquals("configs(locale)[0].type", locale, configs.get(0).getType());
197
198 configs = configurationService.getConfigurationItemsByType(status);
199 assertNotNull(configs);
200 assertEquals("configs(status)", 1, configs.size());
201 assertEquals("configs(status)[0].type", status, configs.get(0).getType());
202
203 configs = configurationService.getConfigurationItemsByType(severity);
204 assertNotNull(configs);
205 assertEquals("configs(severity)", 1, configs.size());
206 assertEquals("configs(severity)[0].type", severity, configs.get(0).getType());
207
208 configs = configurationService.getConfigurationItemsByType(resolution);
209 assertNotNull(configs);
210 assertEquals("configs(resolution)", 1, configs.size());
211 assertEquals("configs(resolution)[0].type", resolution, configs.get(0).getType());
212
213
214 configs = configurationService.getConfigurationItemsByType(locale, Locale.UK);
215 assertNotNull(configs);
216 assertEquals("configs(1, en_UK).size", 3, configs.size());
217
218 configs = configurationService.getConfigurationItemsByType(status, Locale.UK);
219 assertNotNull(configs);
220 assertEquals("configs(2, en_UK).size", 1, configs.size());
221
222 configs = configurationService.getConfigurationItemsByType(severity, Locale.UK);
223 assertNotNull(configs);
224 assertEquals("configs(3, en_UK).size", 1, configs.size());
225
226 configs = configurationService.getConfigurationItemsByType(resolution, Locale.UK);
227 assertNotNull(configs);
228 assertEquals("configs(4, en_UK).size", 1, configs.size());
229
230
231
232 configs = configurationService.getConfigurationItemsByType(2);
233 assertNotNull(configs);
234 assertEquals("configs(2)", 1, configs.size());
235 assertEquals("configs(2)[0].type", status, configs.get(0).getType());
236
237
238 configs = configurationService.getConfigurationItemsByType(1, Locale.UK);
239 assertNotNull(configs);
240 assertEquals("configs(1, en_UK).size", 3, configs.size());
241
242
243 configs = configurationService.getConfigurationItemsByType(2, Locale.UK);
244 assertNotNull(configs);
245 assertEquals("configs(2, en_UK).size", 1, configs.size());
246
247
248 configs = configurationService.getConfigurationItemsByType(3, Locale.UK);
249 assertNotNull(configs);
250 assertEquals("configs(3, en_UK).size", 1, configs.size());
251
252
253 configs = configurationService.getConfigurationItemsByType(4, Locale.UK);
254 assertNotNull(configs);
255 assertEquals("configs(4, en_UK).size", 1, configs.size());
256
257 }
258
259 @Test
260 public void testUpdateConfigurationItem() {
261 Configuration conf = configurationDAO.findByPrimaryKey(2000);
262 assertFalse(conf.getOrder() == 789);
263 conf.setOrder(789);
264 configurationService.updateConfigurationItem(conf);
265 conf = configurationDAO.findByPrimaryKey(2000);
266 assertEquals("new order value", 789, conf.getOrder());
267
268
269 }
270
271 @Test
272 public void testUpdateConfigurationItems() {
273 Configuration conf = configurationDAO.findByPrimaryKey(2000);
274 assertFalse(conf.getOrder() == 987);
275 conf.setOrder(987);
276 List<Configuration> items = new ArrayList<Configuration>();
277 items.add(conf);
278
279
280 configurationService.updateConfigurationItems(items, locale);
281 conf = configurationDAO.findByPrimaryKey(2000);
282 assertEquals("new order value", 987, conf.getOrder());
283
284 }
285
286 @Test
287 public void testRemoveConfigurationItem() {
288 Configuration conf = new Configuration(status, "1", "1", 1);
289 configurationDAO.save(conf);
290 Integer id = conf.getId();
291 assertNotNull(id);
292 assertNotNull(configurationDAO.findByPrimaryKey(id));
293
294 configurationService.removeConfigurationItem(id);
295 conf = configurationDAO.findByPrimaryKey(id);
296 assertNull("removed item", conf);
297
298 }
299
300 @Test
301 public void testRemoveConfigurationItems() {
302
303 Configuration conf = new Configuration(status, "1", "1", 123);
304 configurationDAO.save(conf);
305 Integer id = conf.getId();
306 assertNotNull(id);
307 assertNotNull(configurationDAO.findByPrimaryKey(id));
308
309 configurationService.removeConfigurationItems(status);
310 conf = configurationDAO.findByPrimaryKey(id);
311 assertNull("removed item", conf);
312
313 Configuration conf1 = new Configuration(status, "1", "1", 234);
314 configurationDAO.save(conf1);
315 Integer id1 = conf1.getId();
316 assertNotNull(id1);
317 assertNotNull(configurationDAO.findByPrimaryKey(id1));
318
319 configurationService.removeConfigurationItems(conf1);
320 conf1 = configurationDAO.findByPrimaryKey(id1);
321 assertNull("removed item", conf1);
322 }
323
324 @Test
325 public void testRemoveCustomFieldValues() {
326 final Integer id = 4;
327 CustomField customField = customFieldDAO.findByPrimaryKey(id);
328
329 CustomFieldValue customFieldValue = new CustomFieldValue(
330 customField, "my_value"
331 );
332
333 customFieldValue = configurationService.createCustomFieldValue(customFieldValue);
334 assertNotNull(customFieldValue);
335 assertNotNull(customFieldValue.getId());
336 assertEquals("custom field value", "my_value", customFieldValue.getValue());
337
338 assertNotNull("CustomField#" + id, customField);
339 assertEquals("customField.size", 4, customField.getOptions().size());
340
341 CustomFieldValue cfv = customField.getOptions().get(2);
342 Integer cfvId = cfv.getId();
343 assertNotNull("CustomFieldValue#id", cfvId);
344
345 configurationService.removeCustomFieldValues(id);
346
347 try {
348 fail("delete #" + customFieldValueDAO.findByPrimaryKey(cfvId));
349 customField = customFieldDAO.findByPrimaryKey(4);
350 assertEquals("customField.size", 3, customField.getOptions().size());
351 } catch (Exception e) {
352 assertEquals(ObjectNotFoundException.class, e.getClass());
353 }
354 }
355
356 @Test
357 public void testConfigurationItemExists() {
358
359 Configuration conf = new Configuration(locale, "Test Value", "1");
360 assertTrue("conf type: 1, value: Test Value", configurationService.configurationItemExists(conf));
361
362 conf = new Configuration(locale, "Unknown Value", "1");
363 assertFalse("conf type: 1, value: Unknown Value", configurationService.configurationItemExists(conf));
364 }
365
366 @Test
367 public void testConfigurationItemUpToDate() {
368
369 Configuration configuration = new Configuration(locale, "Test Value", "Version 1.0", 1);
370 assertTrue(configurationService.isConfigurationItemUpToDate(configuration));
371
372 }
373
374 @Test
375 public void testGetProjectScript() {
376 ProjectScript projectScript = configurationService.getProjectScript(1);
377 assertNotNull("project script #1", projectScript);
378 assertEquals("project script id", new Integer(1), projectScript.getId());
379 }
380
381 @Test
382 public void testGetProjectScripts() {
383 List<ProjectScript> projectScripts = configurationService.getProjectScripts();
384 assertNotNull(projectScripts);
385 assertEquals("total project scripts", 1, projectScripts.size());
386 }
387
388 @Test
389 public void testCreateProjectScript() {
390 ProjectScript projectScript = new ProjectScript();
391 projectScript.setFieldId(1);
392 projectScript.setFieldType(customfield);
393 projectScript.setPriority(1);
394 projectScript.setProject(projectDAO.findByPrimaryKey(3));
395 projectScript.setScript(workflowScriptDAO.findByPrimaryKey(1));
396
397
398 ProjectScript newProjectScript = configurationService.createProjectScript(projectScript);
399 assertNotNull(newProjectScript);
400 assertNotNull(newProjectScript.getId());
401 assertEquals("priority", projectScript.getPriority(), newProjectScript.getPriority());
402 assertEquals("script", projectScript.getScript(), newProjectScript.getScript());
403 assertEquals("field id", projectScript.getFieldId(), newProjectScript.getFieldId());
404 assertEquals("project", projectScript.getProject(), newProjectScript.getProject());
405
406
407 projectScriptDAO.delete(projectScript);
408 }
409
410 @Test
411 public void testUpdateProjectScript() {
412 ProjectScript projectScript = projectScriptDAO.findByPrimaryKey(1);
413 projectScript.setPriority(999);
414
415 ProjectScript newProjectScript = configurationService.updateProjectScript(projectScript);
416 assertNotNull(newProjectScript);
417 assertEquals("priority", 999, newProjectScript.getPriority());
418
419 projectScript = projectScriptDAO.findByPrimaryKey(1);
420 assertEquals("priority", 999, projectScript.getPriority());
421 }
422
423 @Test
424 public void testRemoveProjectScript() {
425
426 ProjectScript projectScript = new ProjectScript();
427 projectScript.setFieldId(1);
428 projectScript.setFieldType(customfield);
429 projectScript.setPriority(1);
430 projectScript.setProject(projectDAO.findByPrimaryKey(3));
431 projectScript.setScript(workflowScriptDAO.findByPrimaryKey(1));
432 projectScriptDAO.save(projectScript);
433 Integer id = projectScript.getId();
434 assertNotNull(id);
435 configurationService.removeProjectScript(id);
436 assertNull("removed project script", projectScriptDAO.findByPrimaryKey(id));
437 }
438
439 @Test
440 public void testGetWorkflowScript() {
441 WorkflowScript workflowScript = configurationService.getWorkflowScript(1);
442 assertNotNull(workflowScript);
443 assertEquals(new Integer(1), workflowScript.getId());
444 }
445
446 @Test
447 public void testGetWorkflowScripts() {
448 List<WorkflowScript> workflowScripts = configurationService.getWorkflowScripts();
449 assertNotNull(workflowScripts);
450 assertEquals("total worflow scripts", 2, workflowScripts.size());
451 }
452
453 @Test
454 public void testCreateWorkflowScript() {
455 WorkflowScript workflowScript = new WorkflowScript();
456 workflowScript.setName("script");
457 workflowScript.setScript("my_script");
458 WorkflowScript newWorkflowScript = configurationService.createWorkflowScript(workflowScript);
459
460 assertNotNull(newWorkflowScript);
461 assertNotNull(newWorkflowScript.getId());
462 assertEquals("new workflow script", "my_script", newWorkflowScript.getScript());
463 }
464
465 @Test
466 public void testUpdateWorkflowScript() {
467 WorkflowScript workflowScript = workflowScriptDAO.findByPrimaryKey(1);
468 workflowScript.setScript("my_brand_new_script");
469 configurationService.updateWorkflowScript(workflowScript);
470
471 workflowScript = workflowScriptDAO.findByPrimaryKey(1);
472 assertNotNull(workflowScript);
473 assertEquals("new workflow script", "my_brand_new_script", workflowScript.getScript());
474 }
475
476 @Test
477 public void testRemoveWorkflowScript() {
478 WorkflowScript workflowScript = new WorkflowScript();
479 workflowScript.setName("script");
480 workflowScript.setScript("my_script");
481 workflowScriptDAO.save(workflowScript);
482 Integer id = workflowScript.getId();
483 assertNotNull(id);
484
485 configurationService.removeWorkflowScript(id);
486
487 workflowScript = workflowScriptDAO.findByPrimaryKey(id);
488 assertNull("removed script", workflowScript);
489
490 }
491
492 @Test
493 public void testGetCustomField() {
494 CustomField customField = configurationService.getCustomField(1);
495 assertNotNull(customField);
496 assertEquals(new Integer(1), customField.getId());
497 assertTrue(customField.isRequired());
498 }
499
500 @Test
501 public void testGetCustomFields() {
502 List<CustomField> fields = configurationService.getCustomFields();
503 assertNotNull("fields[]", fields);
504 assertEquals("total custom fields", 4, fields.size());
505 }
506
507 @Test
508 public void testCreateCustomField() {
509 CustomField customField = new CustomField();
510 customField.setFieldType(Type.STRING);
511 customField = configurationService.createCustomField(customField);
512 assertNotNull(customField);
513 assertNotNull(customField.getId());
514 assertNotNull(customField.getId());
515 assertEquals("field type", Type.STRING, customField.getFieldType());
516 }
517
518 @Test
519 public void testUpdateCustomField() {
520 CustomField customField = customFieldDAO.findByPrimaryKey(1);
521 assertTrue("required", customField.isRequired());
522 customField.setRequired(false);
523 configurationService.updateCustomField(customField);
524
525 customField = customFieldDAO.findByPrimaryKey(1);
526 assertFalse("required", customField.isRequired());
527
528 }
529
530 @Test
531 public void testRemoveCustomField() throws Exception {
532
533 CustomField customField = new CustomField();
534 customField.setFieldType(Type.STRING);
535 customFieldDAO.save(customField);
536 Integer id = customField.getId();
537 assertNotNull("id", id);
538 assertNotNull("customFieldDAO.findByPrimaryKey(id)", customFieldDAO.findByPrimaryKey(id));
539
540 configurationService.removeCustomField(id);
541 assertNull("customFieldDAO.findByPrimaryKey(removed id)", customFieldDAO.findByPrimaryKey(id));
542
543
544
545 CustomField customField1 = new CustomField();
546 customField1.setFieldType(Type.LIST);
547 customFieldDAO.save(customField1);
548 Integer id1 = customField1.getId();
549 assertNotNull("id1", id1);
550 assertNotNull("customFieldDAO.findByPrimaryKey(id1)", customFieldDAO.findByPrimaryKey(id1));
551
552 configurationService.removeCustomField(id1);
553 assertNull("customFieldDAO.findByPrimaryKey(id1)", customFieldDAO.findByPrimaryKey(id1));
554 }
555
556 @Test
557 public void testGetCustomFieldValue() {
558 CustomFieldValue customFieldValue = configurationService.getCustomFieldValue(1);
559 assertEquals("id", new Integer(1), customFieldValue.getId());
560 assertEquals("value", "2", customFieldValue.getValue());
561
562 }
563
564 @Test
565 public void testCreateCustomFieldValue() {
566 CustomFieldValue customFieldValue = new CustomFieldValue(
567 customFieldDAO.findByPrimaryKey(1), "my_value"
568 );
569
570 customFieldValue = configurationService.createCustomFieldValue(customFieldValue);
571 assertNotNull("customFieldValue", customFieldValue);
572 assertNotNull("customFieldValue.id", customFieldValue.getId());
573 assertEquals("custom field value", "my_value", customFieldValue.getValue());
574
575 }
576
577 @Test
578 public void testUpdateCustomFieldValue() {
579 CustomFieldValue customFieldValue = customFieldValueDAO.findByPrimaryKey(1);
580 assertEquals("value", "2", customFieldValue.getValue());
581
582 customFieldValue.setValue("brand_new_value");
583 configurationService.updateCustomFieldValue(customFieldValue);
584
585 customFieldValue = customFieldValueDAO.findByPrimaryKey(1);
586 assertEquals("new value", "brand_new_value", customFieldValue.getValue());
587
588 }
589
590 @Test
591 public void testUpdateCustomFieldValues() {
592 CustomFieldValue customFieldValue2 = customFieldValueDAO.findByPrimaryKey(2);
593 CustomFieldValue customFieldValue3 = customFieldValueDAO.findByPrimaryKey(3);
594 List<CustomFieldValue> customFieldValues = new ArrayList<CustomFieldValue>();
595 customFieldValues.add(customFieldValue2);
596 customFieldValues.add(customFieldValue3);
597
598 assertEquals("value#2", "1", customFieldValue2.getValue());
599 assertEquals("value#3", "4", customFieldValue3.getValue());
600
601 customFieldValue2.setValue("22");
602 customFieldValue3.setValue("33");
603
604 configurationService.updateCustomFieldValues(1, customFieldValues);
605
606 customFieldValue2 = customFieldValueDAO.findByPrimaryKey(2);
607 customFieldValue3 = customFieldValueDAO.findByPrimaryKey(3);
608 assertEquals("new value#2", "22", customFieldValue2.getValue());
609 assertEquals("new value#3", "33", customFieldValue3.getValue());
610
611 }
612
613 @Test
614 public void testRemoveCustomFieldValue() {
615 CustomFieldValue customFieldValue = new CustomFieldValue(
616 customFieldDAO.findByPrimaryKey(1), "my_value");
617 customFieldValueDAO.save(customFieldValue);
618 Integer id = customFieldValue.getId();
619 assertNotNull("id", id);
620 assertNotNull("customFieldValueDAO.findByPrimaryKey(id)", customFieldValueDAO.findByPrimaryKey(id));
621
622 configurationService.removeCustomFieldValue(id);
623 try {
624 CustomFieldValue value = customFieldValueDAO.findByPrimaryKey(id);
625 fail("custom field value: " + value);
626 } catch (Exception e) {
627 assertEquals(ObjectNotFoundException.class, e.getClass());
628 }
629
630
631 }
632
633 @Test
634 public void testGetLanguageItemByKey() {
635 Language languageItem = configurationService.getLanguageItemByKey("test_key", null);
636 assertNotNull("Language", languageItem);
637 assertEquals("id", new Integer(9999971), languageItem.getId());
638 assertEquals("resource_value", "test_value", languageItem.getResourceValue());
639
640 }
641
642 @Test
643 public void testGetLanguageItemsByKey() {
644 List<Language> languages = configurationService.getLanguageItemsByKey("test_key");
645 assertNotNull(languages);
646 assertEquals("total languaes with test_key key", 2, languages.size());
647
648 languages = configurationService.getLanguageItemsByKey("non_existent_key");
649 assertNotNull(languages);
650 assertTrue("total languaes with test_key key", languages.isEmpty());
651 }
652
653 @Test
654 public void testUpdateLanguageItem() {
655 Language language = languageDAO.findById(999999);
656 language.setLocale("de");
657 configurationService.updateLanguageItem(language);
658 language = languageDAO.findById(999999);
659 assertEquals("locale", "de", language.getLocale());
660 }
661
662 @Test
663 public void testRemoveLanguageKey() {
664 Language language1 = new Language("ua", "key1", "value1");
665 Language language2 = new Language("ru", "key1", "value2");
666 languageDAO.save(language1);
667 languageDAO.save(language2);
668 assertNotNull(language1.getId());
669 assertNotNull(language2.getId());
670
671 configurationService.removeLanguageKey("key1");
672 language1 = languageDAO.findById(language1.getId());
673 language2 = languageDAO.findById(language2.getId());
674
675 assertNull("removed language", language1);
676 assertNull("removed language", language2);
677 }
678
679 @Test
680 public void testRemoveLanguageItem() {
681 Language language = new Language("ua", "my_key", "my_value");
682 languageDAO.save(language);
683 Integer id = language.getId();
684 assertNotNull("id", id);
685
686 configurationService.removeLanguageItem(language);
687 language = languageDAO.findById(id);
688 assertNull("removed language", language);
689 }
690
691 @Test
692 public void testRemoveLocale() {
693
694
695
696
697 final List<Configuration> localeConfigs = configurationService.getConfigurationItemsByType(locale);
698
699 final String va = "va";
700 Configuration removed = null;
701 for (Configuration configuration : localeConfigs) {
702 if (configuration.getValue().equals(va)) {
703 configurationService.removeConfigurationItem(configuration.getId());
704 removed = configuration;
705 ITrackerResources.clearBundles();
706 break;
707 }
708 }
709 assertThat(removed, notNullValue());
710 assertThat(
711 configurationService.getConfigurationItemsByType(locale),
712 not(hasItem(removed)));
713 }
714 @Test
715 public void testAddLocale() {
716
717 Configuration localeVb = new Configuration(locale, new NameValuePair("Test", "vb"));
718 Configuration configurationItem = configurationService.createConfigurationItem(localeVb);
719 assertThat(configurationItem, notNullValue());
720 assertThat(configurationItem.getId(), notNullValue());
721 assertThat(configurationItem.isNew(), is(false));
722
723 configurationService.updateLanguageItem(new Language("vb", "itracker.locale.name", "Test"));
724 ITrackerResources.clearBundles();
725
726 List<String> vb = configurationService.getAvailableLanguages().get("vb");
727 assertThat(vb, notNullValue());
728
729
730 }
731 @Test
732 public void testGetSortedKeys() {
733 String[] keys = configurationService.getSortedKeys();
734 assertNotNull("keys", keys);
735 assertEquals("total keys", 2, keys.length);
736 }
737
738 @Test
739 public void testGetDefinedKeys() {
740 Map<String, String> keyMap = configurationService.getDefinedKeys("test_locale");
741 assertNotNull("keyMap", keyMap);
742 assertEquals("total keys", 1, keyMap.size());
743
744 keyMap = configurationService.getDefinedKeys("undefined_locale");
745 assertNotNull("keyMap (undefined_locale)", keyMap);
746 assertEquals("total keys", 0, keyMap.size());
747
748 }
749
750 @Test
751 public void testGetDefinedKeysAsArray() {
752 List<NameValuePair> keyMap = configurationService.getDefinedKeysAsArray("test_locale");
753 assertNotNull("keyMap", keyMap);
754 assertEquals("total keys", 1, keyMap.size());
755
756 keyMap = configurationService.getDefinedKeysAsArray("undefined_locale");
757 assertNotNull(keyMap);
758 assertEquals("total keys", 0, keyMap.size());
759
760 }
761
762 @Test
763 public void testGetNumberDefinedKeys() {
764 int totalKeys = configurationService.getNumberDefinedKeys("test_locale");
765 assertEquals("total keys", 1, totalKeys);
766
767 totalKeys = configurationService.getNumberDefinedKeys("undefined_locale");
768 assertEquals("total keys", 0, totalKeys);
769
770 }
771
772 @Test
773 public void testGetAvailableLanguages() {
774 Map<String, List<String>> availableLanguages = configurationService.getAvailableLanguages();
775 assertNotNull(availableLanguages);
776
777
778 assertEquals("available languages", 1, availableLanguages.size());
779
780 }
781
782 @Test
783 public void testGetNumberAvailableLanguages() {
784 int availableLanguages = configurationService.getNumberAvailableLanguages();
785 assertEquals("available languages", 1, availableLanguages);
786
787 }
788
789 @Test
790 public void testUpdateLanguage() {
791 Language language = languageDAO.findById(999999);
792 language.setResourceValue("brand_new_value");
793
794 List<Language> items = new ArrayList<Language>();
795 items.add(language);
796
797
798 configurationService.updateLanguage(Locale.ENGLISH, items);
799
800 language = languageDAO.findById(999999);
801 assertNotNull(language);
802 assertEquals("resource value", "brand_new_value", language.getResourceValue());
803
804 }
805
806 private void doTestGetPermissionName(final Locale locale,
807 final int permissionId, final String expected) {
808 final String actual =
809 UserUtilities.getPermissionName(permissionId, locale);
810 assertEquals("UserUtilities.getPermissionName(" + permissionId + ", " +
811 locale + ")", expected, actual);
812 }
813
814 @Test
815 public void testInitializeConfiguration()
816 throws Exception
817
818 {
819
820 doTestGetPermissionName(new Locale("test"),
821 UserUtilities.PERMISSION_ASSIGN_OTHERS,
822 "Assign Issues to Others");
823
824 doTestGetPermissionName(new Locale("test"),
825 UserUtilities.PERMISSION_ASSIGN_SELF,
826 "Assign Issues to Self");
827 doTestGetPermissionName(new Locale("test"),
828 UserUtilities.PERMISSION_CLOSE, "Close Issues");
829
830 doTestGetPermissionName(new Locale("test"),
831 UserUtilities.PERMISSION_EDIT, "Edit All Issues");
832 doTestGetPermissionName(new Locale("test"),
833 UserUtilities.PERMISSION_CREATE, "Create Issues");
834
835 doTestGetPermissionName(new Locale("test"),
836 UserUtilities.PERMISSION_PRODUCT_ADMIN, "Project Admin");
837
838 doTestGetPermissionName(new Locale("test"),
839 UserUtilities.PERMISSION_USER_ADMIN, "test_value");
840 }
841
842 @Test
843 public void testNameComparator() throws Exception {
844 CustomFieldValue valueA, valueB;
845
846 CustomField fieldA = new CustomField();
847 CustomField fieldB = new CustomField();
848
849 fieldA.setId(0);
850 fieldB.setId(1);
851
852 valueA = new CustomFieldValue(fieldA, "1");
853 valueB = new CustomFieldValue(fieldB, "2");
854 valueA.setId(1);
855 valueB.setId(2);
856
857 Language langA = new Language(ITrackerResources.getDefaultLocale(), CustomFieldUtilities.getCustomFieldOptionLabelKey(fieldA.getId(), valueA.getId()));
858 langA.setResourceValue("a");
859
860 Language langB = new Language(ITrackerResources.getDefaultLocale(), CustomFieldUtilities.getCustomFieldOptionLabelKey(fieldB.getId(), valueB.getId()));
861 langB.setResourceValue("b");
862
863 this.configurationService.updateLanguageItem(langA);
864 this.configurationService.updateLanguageItem(langB);
865
866
867 assertEntityComparator("name comparator",
868 new CustomFieldUtilities.CustomFieldValueByNameComparator(null),
869 valueA, valueB);
870 assertEntityComparator("name comparator",
871 new CustomFieldUtilities.CustomFieldValueByNameComparator(null),
872 valueA, null);
873
874 langA.setResourceValue(langB.getResourceValue());
875 this.configurationService.updateLanguageItem(langA);
876 this.configurationService.updateLanguageItem(langB);
877 ITrackerResources.clearKeyFromBundles(langA.getResourceKey(), true);
878 ITrackerResources.clearKeyFromBundles(langB.getResourceKey(), true);
879
880
881
882
883 assertEntityComparatorEquals("name comparator",
884 new CustomFieldUtilities.CustomFieldValueByNameComparator(null),
885 valueA, valueA);
886 }
887
888 @Override
889 public void onTearDown() throws Exception {
890 super.onTearDown();
891
892
893 }
894
895 @Override
896 public void onSetUp() throws Exception {
897
898 super.onSetUp();
899 configurationService = (ConfigurationService) applicationContext
900 .getBean("configurationService");
901 configurationProperties = (Properties) applicationContext
902 .getBean("configurationProperties");
903
904 this.configurationDAO = (ConfigurationDAO) applicationContext.getBean("configurationDAO");
905 this.projectDAO = (ProjectDAO) applicationContext.getBean("projectDAO");
906 this.projectScriptDAO = (ProjectScriptDAO) applicationContext.getBean("projectScriptDAO");
907 this.workflowScriptDAO = (WorkflowScriptDAO) applicationContext.getBean("workflowScriptDAO");
908 this.customFieldDAO = (CustomFieldDAO) applicationContext.getBean("customFieldDAO");
909 this.customFieldValueDAO = (CustomFieldValueDAO) applicationContext.getBean("customFieldValueDAO");
910 this.languageDAO = (LanguageDAO) applicationContext.getBean("languageDAO");
911
912
913 }
914
915 protected String[] getDataSetFiles() {
916 return new String[]{
917 "dataset/configurationbean_dataset.xml",
918 "dataset/workflowscriptbean_dataset.xml",
919 "dataset/projectbean_dataset.xml",
920 "dataset/projectscriptbean_dataset.xml",
921 "dataset/languagebean_dataset.xml",
922 "dataset/customfieldbean_dataset.xml",
923 "dataset/customfieldvaluebean_dataset.xml",
924
925 };
926 }
927
928 }