1 package org.itracker.model;
2
3 import org.itracker.core.resources.ITrackerResources;
4 import org.itracker.model.util.CustomFieldUtilities;
5 import org.itracker.services.ConfigurationService;
6 import org.junit.Test;
7 import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
8
9 import static org.itracker.Assert.assertEntityComparator;
10 import static org.itracker.Assert.assertEntityComparatorEquals;
11
12
13 public class CustomFieldValueIT extends AbstractDependencyInjectionSpringContextTests {
14 private CustomFieldValue cust;
15 private ConfigurationService configurationService;
16
17 @Test
18 public void testSetCustomField() {
19 try {
20 cust.setCustomField(null);
21 fail("did not throw IllegalArgumentException ");
22 } catch (IllegalArgumentException e) {
23 assertTrue(true);
24 }
25 }
26
27 @Test
28 public void testSetValue() {
29 try {
30 cust.setValue(null);
31 fail("did not throw IllegalArgumentException ");
32 } catch (IllegalArgumentException e) {
33 assertTrue(true);
34 }
35 }
36
37 @Test
38 public void testSortOrderComparator() throws Exception {
39 CustomFieldValue valueA, valueB;
40
41 CustomField fieldA = new CustomField();
42 CustomField fieldB = new CustomField();
43
44 fieldA.setId(0);
45 fieldB.setId(1);
46
47 valueA = new CustomFieldValue(fieldA, "1");
48 valueA.setSortOrder(1);
49 valueB = new CustomFieldValue(fieldB, "2");
50 valueB.setSortOrder(2);
51 valueA.setId(1);
52 valueB.setId(2);
53
54
55 assertEntityComparator("name comparator", CustomFieldValue.SORT_ORDER_COMPARATOR,
56 valueA, valueB);
57 assertEntityComparator("name comparator", CustomFieldValue.SORT_ORDER_COMPARATOR,
58 valueA, null);
59
60 valueA.setSortOrder(valueB.getSortOrder());
61 assertEntityComparatorEquals("name comparator",
62 CustomFieldValue.SORT_ORDER_COMPARATOR, valueA, valueB);
63 assertEntityComparatorEquals("name comparator",
64 CustomFieldValue.SORT_ORDER_COMPARATOR, valueA, valueB);
65
66 }
67
68 @Test
69 public void testNameComparator() throws Exception {
70 CustomFieldValue valueA, valueB;
71
72 CustomField fieldA = new CustomField();
73 CustomField fieldB = new CustomField();
74
75 fieldA.setId(0);
76 fieldB.setId(1);
77
78 valueA = new CustomFieldValue(fieldA, "1");
79 valueB = new CustomFieldValue(fieldB, "2");
80 valueA.setId(1);
81 valueB.setId(2);
82
83 Language langA = new Language(ITrackerResources.getDefaultLocale(), CustomFieldUtilities.getCustomFieldOptionLabelKey(fieldA.getId(), valueA.getId()));
84 langA.setResourceValue("a");
85
86 Language langB = new Language(ITrackerResources.getDefaultLocale(), CustomFieldUtilities.getCustomFieldOptionLabelKey(fieldB.getId(), valueB.getId()));
87 langB.setResourceValue("b");
88
89 this.configurationService.updateLanguageItem(langA);
90 this.configurationService.updateLanguageItem(langB);
91
92
93 assertEntityComparator("name comparator",
94 new CustomFieldUtilities.CustomFieldValueByNameComparator(null),
95 valueA, valueB);
96 assertEntityComparator("name comparator",
97 new CustomFieldUtilities.CustomFieldValueByNameComparator(null),
98 valueA, null);
99
100 langA.setResourceValue(langB.getResourceValue());
101 this.configurationService.updateLanguageItem(langA);
102 this.configurationService.updateLanguageItem(langB);
103 ITrackerResources.clearKeyFromBundles(langA.getResourceKey(), true);
104 ITrackerResources.clearKeyFromBundles(langB.getResourceKey(), true);
105
106
107
108
109 assertEntityComparatorEquals("name comparator",
110 new CustomFieldUtilities.CustomFieldValueByNameComparator(null),
111 valueA, valueA);
112 }
113
114 @Test
115 public void testToString() {
116 assertNotNull(cust.toString());
117 }
118
119
120 public void onSetUp() throws Exception {
121 cust = new CustomFieldValue();
122 configurationService = (ConfigurationService) applicationContext.getBean("configurationService");
123 }
124
125 public void onTearDown() throws Exception {
126 cust = null;
127 }
128
129 @Override
130 protected String[] getConfigLocations() {
131 return new String[]{"application-context.xml"};
132 }
133
134 }