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.shared;
034:
035: import com.flexive.shared.FxFormatUtils;
036: import com.flexive.shared.FxSharedUtils;
037: import com.flexive.shared.value.FxString;
038: import org.apache.commons.lang.StringUtils;
039: import org.testng.Assert;
040: import org.testng.annotations.Test;
041:
042: import java.util.Arrays;
043: import java.util.HashMap;
044: import java.util.Map;
045:
046: /**
047: * Unit tests for com.flexive.shared.FxSharedUtils.
048: *
049: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
050: * @see com.flexive.shared.FxSharedUtils
051: */
052: @Test(groups={"shared"})
053: public class FxSharedUtilTest {
054:
055: /**
056: * Unit test for com.flexive.shared.FxSharedUtils.formatResource.
057: */
058: @Test
059: public void formatResource() {
060: assert "ABC".equals(FxFormatUtils.formatResource("{0}", -1,
061: "ABC")) : "Did not replace single argument correctly";
062: assert "Test:-ABC-".equals(FxFormatUtils.formatResource(
063: "Test:-{0}-", -1, "ABC")) : "Did not replace single argument correctly";
064: assert "Test:-ABC-".equals(FxFormatUtils.formatResource(
065: "Test:-{0}{1}C{2}", -1, "A", "B", "-")) : "Did not replace multiple arguments correctly";
066: Map<Long, String> translations = new HashMap<Long, String>();
067: translations.put(1L, "translation1");
068: translations.put(2L, "translation2");
069: FxString translatedString = new FxString(translations);
070: assert "Test: translation1".equals(FxFormatUtils
071: .formatResource("Test: {0}", 1, translatedString)) : "Did not replace FxString argument correctly";
072: assert "Test: translation2".equals(FxFormatUtils
073: .formatResource("Test: {0}", 2, translatedString)) : "Did not replace FxString argument correctly";
074: assert "Test: translation2==translation2".equals(FxFormatUtils
075: .formatResource("Test: {0}=={1}", 2, translatedString,
076: translatedString)) : "Did not replace multiple FxString arguments correctly";
077: assert "".equals(FxFormatUtils.formatResource("", -1)) : "Did not treat empty string value correctly.";
078: assert "".equals(FxFormatUtils.formatResource(null, -1)) : "Did not treat null string value correctly.";
079: assert "\t \t \n".equals(FxFormatUtils.formatResource(
080: "\t{0}\t \n", -1, " ")) : "Did not preserve whitespace correctly.";
081: }
082:
083: /**
084: * Tests for the filter method
085: */
086: @Test
087: public void filter() {
088: String newline = "abc\ndef";
089: String htmlFilter = "1 > 2 < 3";
090: Assert.assertEquals(FxFormatUtils.escapeForJavaScript(newline,
091: false), newline.replace('\n', ' '),
092: "Did not preserve newlines correctly.");
093: Assert.assertEquals(FxFormatUtils.escapeForJavaScript(newline,
094: true), StringUtils.replace(newline, "\n", "<br/>"),
095: "Did not replaces newlines correctly.");
096: Assert.assertEquals(FxFormatUtils.escapeForJavaScript(
097: htmlFilter, false, true), StringUtils.replace(
098: StringUtils.replace(htmlFilter, "<", "<"), ">",
099: ">"), "Did not escape HTML code correctly.");
100: Assert.assertEquals(FxFormatUtils.escapeForJavaScript(null),
101: "", "Did not treat null value correctly.");
102: Assert.assertEquals(FxFormatUtils.escapeForJavaScript(""), "",
103: "Did not treat empty string correctly.");
104: }
105:
106: @Test
107: public void splitLiterals() {
108: assert FxSharedUtils.splitLiterals(null).length == 0 : "Null parameters should return an empty array.";
109: assertSplitResult(FxSharedUtils.splitLiterals("literal value"),
110: new String[] { "literal value" });
111: assertSplitResult(FxSharedUtils
112: .splitLiterals("value 1, value 2"), new String[] {
113: "value 1", "value 2" });
114: assertSplitResult(FxSharedUtils.splitLiterals(","),
115: new String[] { "", "" });
116: assertSplitResult(FxSharedUtils.splitLiterals(",,"),
117: new String[] { "", "", "" });
118: assertSplitResult(FxSharedUtils
119: .splitLiterals("some,value,list"), new String[] {
120: "some", "value", "list" });
121: assertSplitResult(FxSharedUtils
122: .splitLiterals("'some,value,list'"),
123: new String[] { "some,value,list" });
124: assertSplitResult(FxSharedUtils
125: .splitLiterals("'something','has to','change'"),
126: new String[] { "something", "has to", "change" });
127: assertSplitResult(FxSharedUtils.splitLiterals("'',''"),
128: new String[] { "", "" });
129: assertSplitResult(
130: FxSharedUtils
131: .splitLiterals("'\"quoted,string\"',\"'another quoted,string'\""),
132: new String[] { "\"quoted,string\"",
133: "'another quoted,string'" });
134: }
135:
136: @Test
137: public void getColumnIndex() {
138: final String[] columns = { "co.@pk", "co.name",
139: "co.list.@value" };
140: Assert.assertEquals(FxSharedUtils
141: .getColumnIndex(columns, "@pk"), 1);
142: Assert.assertEquals(FxSharedUtils.getColumnIndex(columns,
143: "co.@pk"), 1);
144: Assert.assertEquals(
145: FxSharedUtils.getColumnIndex(columns, "pk"), -1);
146: Assert.assertEquals(FxSharedUtils.getColumnIndex(columns,
147: "@value"), 3);
148: Assert.assertEquals(FxSharedUtils.getColumnIndex(columns,
149: "co.list.@value"), 3);
150: }
151:
152: private void assertSplitResult(String[] result, String[] expected) {
153: assert Arrays.asList(result).equals(Arrays.asList(expected)) : "Expected split result: "
154: + Arrays.asList(expected)
155: + ", got: "
156: + Arrays.asList(result);
157: }
158: }
|