001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.tests.embedded.structure;
034:
035: import com.flexive.shared.CacheAdmin;
036: import com.flexive.shared.EJBLookup;
037: import com.flexive.shared.FxLanguage;
038: import com.flexive.shared.FxSharedUtils;
039: import com.flexive.shared.exceptions.FxApplicationException;
040: import com.flexive.shared.exceptions.FxLogoutFailedException;
041: import com.flexive.shared.interfaces.SelectListEngine;
042: import com.flexive.shared.security.ACL;
043: import com.flexive.shared.structure.FxSelectList;
044: import com.flexive.shared.structure.FxSelectListEdit;
045: import com.flexive.shared.structure.FxSelectListItem;
046: import com.flexive.shared.structure.FxSelectListItemEdit;
047: import com.flexive.shared.value.FxSelectMany;
048: import com.flexive.shared.value.FxSelectOne;
049: import com.flexive.shared.value.FxString;
050: import com.flexive.shared.value.SelectMany;
051: import com.flexive.shared.value.renderer.FxValueRenderer;
052: import com.flexive.shared.value.renderer.FxValueRendererFactory;
053: import com.flexive.tests.embedded.FxTestUtils;
054: import static com.flexive.tests.embedded.FxTestUtils.login;
055: import static com.flexive.tests.embedded.FxTestUtils.logout;
056: import com.flexive.tests.embedded.TestUsers;
057: import org.apache.commons.lang.RandomStringUtils;
058: import org.testng.annotations.AfterClass;
059: import org.testng.annotations.BeforeClass;
060: import org.testng.annotations.Test;
061:
062: import java.util.Iterator;
063: import java.util.List;
064: import java.util.Collections;
065:
066: /**
067: * FxSelectList and FxSelectListItem tests
068: *
069: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
070: */
071: @Test(groups={"ejb","structure"})
072: public class SelectListTest {
073:
074: private SelectListEngine le;
075: private ACL aclList, aclItem;
076:
077: /**
078: * setup...
079: *
080: * @throws Exception on errors
081: */
082: @BeforeClass
083: public void beforeClass() throws Exception {
084: login(TestUsers.SUPERVISOR);
085: le = EJBLookup.getSelectListEngine();
086: //create the base type
087: ACL[] tmp = FxTestUtils.createACLs(new String[] {
088: "LIST_" + RandomStringUtils.random(16, true, true),
089: "ITEM_" + RandomStringUtils.random(16, true, true),
090:
091: }, new ACL.Category[] { ACL.Category.SELECTLIST,
092: ACL.Category.SELECTLISTITEM }, TestUsers
093: .getTestMandator());
094: aclList = tmp[0];
095: aclItem = tmp[1];
096: }
097:
098: @AfterClass
099: public void afterClass() throws FxLogoutFailedException,
100: FxApplicationException {
101: FxTestUtils.removeACL(aclList, aclItem);
102: logout();
103: }
104:
105: @Test
106: public void selectListManipulation() throws Exception {
107: final String TEST_NAME1 = "List1_"
108: + RandomStringUtils.random(16, true, true);
109: final String TEST_NAME2 = "List2_"
110: + RandomStringUtils.random(16, true, true);
111: FxSelectListEdit list = FxSelectListEdit.createNew(TEST_NAME1,
112: new FxString("label"), new FxString("description"),
113: false, aclList, aclItem);
114: FxSelectListItem item1_1 = new FxSelectListItemEdit(aclItem,
115: list, new FxString("item1.label"), "item1.data",
116: "item1.color");
117: new FxSelectListItemEdit(aclItem, list, new FxString(
118: "item2.label"), "item2.data", "item2.color");
119: long list1Id = le.save(list);
120: FxSelectList list1_load = CacheAdmin.getEnvironment()
121: .getSelectList(list1Id);
122: new FxSelectListItemEdit(aclItem, list1_load, new FxString(
123: "item3.label"), "item3.data", "item3.color");
124: le.save(list1_load.asEditable());
125: assert !CacheAdmin.getEnvironment().getSelectList(list1Id)
126: .hasDefaultItem() : "No default item expected for list 1!";
127: boolean found = false;
128: for (FxSelectListItem item : CacheAdmin.getEnvironment()
129: .getSelectList(list1Id).getItems()) {
130: found = found || item.getData().equals("item3.data");
131: }
132: assert found : "item3 was not saved!";
133: assert TEST_NAME1.equals(list1_load.getName()) : "Wrong name! Got ["
134: + list1_load.getName()
135: + "] but expected ["
136: + TEST_NAME1 + "]!";
137: assert list1_load.getItems().size() == 3 : "Wrong number of items. Expected 3 but got "
138: + list1_load.getItems().size();
139:
140: FxSelectListEdit list2 = FxSelectListEdit.createNew(list1_load,
141: TEST_NAME2, new FxString("label2"), new FxString(
142: "description2"), false, aclList, aclItem);
143: FxSelectListItem defItem = new FxSelectListItemEdit(aclItem,
144: list2, new FxString("list2.item1.label"),
145: "list2.item1.data", "list2.item1.color")
146: // .setParentItem(item1_1)
147: .setDefaultItem();
148: long list2Id = le.save(list2);
149: FxSelectList _list2 = CacheAdmin.getEnvironment()
150: .getSelectList(list2Id);
151: assert _list2.hasDefaultItem() : "list 2 is expected to have a default item";
152: assert _list2.getDefaultItem().getList().getId() == _list2
153: .getId() : "Wrong list id for default item!";
154: assert _list2.hasParentList() : "list 2 is expected to have a parent list!";
155: le.save(_list2.asEditable().setDefaultItem(null));
156: _list2 = CacheAdmin.getEnvironment().getSelectList(list2Id);
157: assert !_list2.hasDefaultItem() : "list 2 should no longer have a default item!";
158: le.remove(list1_load);
159: assert !CacheAdmin.getEnvironment().getSelectList(list2Id)
160: .hasParentList() : "list 2 is not expected to have a parent list anymore!";
161: le.remove(CacheAdmin.getEnvironment().getSelectList(list2Id));
162: }
163:
164: @Test
165: public void selectListFormatter() throws FxApplicationException {
166: FxSelectListEdit list = FxSelectListEdit.createNew(
167: "selectListValueTest", new FxString("label"),
168: new FxString("description"), false, aclList, aclItem);
169: new FxSelectListItemEdit(aclItem, list, new FxString(
170: "default label").setTranslation(FxLanguage.ENGLISH,
171: "english item").setTranslation(FxLanguage.GERMAN,
172: "deutscher eintrag"), "item1.data", "item1.color");
173: new FxSelectListItemEdit(aclItem, list, new FxString(
174: "default label").setTranslation(FxLanguage.ENGLISH,
175: "english item 2").setTranslation(FxLanguage.GERMAN,
176: "deutscher eintrag 2"), "item2.data", "item2.color");
177: final long selectListId = le.save(list);
178: final FxSelectList loadedList = CacheAdmin.getEnvironment()
179: .getSelectList(selectListId);
180: try {
181: final Iterator<FxSelectListItem> iterator = loadedList
182: .getItems().iterator();
183: final FxSelectListItem item1 = iterator.next();
184: final FxSelectListItem item2 = iterator.next();
185: final String label1En = item1.getLabel().getTranslation(
186: FxLanguage.ENGLISH);
187: final String label1De = item1.getLabel().getTranslation(
188: FxLanguage.GERMAN);
189: final String label2En = item2.getLabel().getTranslation(
190: FxLanguage.ENGLISH);
191: final String label2De = item2.getLabel().getTranslation(
192: FxLanguage.GERMAN);
193: final FxValueRenderer formatEn = FxValueRendererFactory
194: .getInstance(new FxLanguage("en"));
195: final FxValueRenderer formatDe = FxValueRendererFactory
196: .getInstance(new FxLanguage("de"));
197:
198: final FxSelectOne selectOne = new FxSelectOne(item1);
199: final String labelEn = formatEn.format(selectOne);
200: final String labelDe = formatDe.format(selectOne);
201: assert labelEn.equals(label1En) : "Expected select list label '"
202: + label1En + "', got: " + labelEn;
203: assert labelDe.equals(label1De) : "Expected select list label '"
204: + label1De + "', got: " + labelDe;
205:
206: final SelectMany many = new SelectMany(loadedList);
207: many.selectItem(item1);
208: many.selectItem(item2);
209: final FxSelectMany selectMany = new FxSelectMany(many);
210: final String labelManyEn = formatEn.format(selectMany);
211: final String labelManyDe = formatDe.format(selectMany);
212: assert labelManyEn
213: .equals(label1En.compareTo(label2En) < 0 ? label1En
214: + ", " + label2En : label2En + ", "
215: + label1En) : "Unexpected label: "
216: + labelManyEn;
217: assert labelManyDe
218: .equals(label1De.compareTo(label2De) < 0 ? label1De
219: + ", " + label2De : label2De + ", "
220: + label1De) : "Unexpected label: "
221: + labelManyDe;
222: } finally {
223: le.remove(loadedList);
224: }
225: }
226: }
|