001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.util.test;
023:
024: import java.io.File;
025:
026: import org.jboss.util.StringPropertyReplacer;
027: import junit.framework.TestCase;
028:
029: /**
030: * Unit tests for the custom JBoss property editors
031: *
032: * @see org.jboss.util.StringPropertyReplacer
033: * @author <a href="Adrian.Brock@HappeningTimes.com">Adrian.Brock</a>
034: * @version $Revision: 57211 $
035: */
036: public class PropertyPatternUnitTestCase extends TestCase {
037: static final String simpleKey = "org.jboss.test.util.test.Simple";
038: static final String simple = "AProperty";
039: static final String anotherKey = "org.jboss.test.util.test.Another";
040: static final String another = "BProperty";
041: static final String doesNotExist = "org.jboss.test.util.test.DoesNotExist";
042: static final String before = "Before";
043: static final String between = "Between";
044: static final String after = "After";
045: static final String fileSeparatorKey = "/";
046: static final String pathSeparatorKey = ":";
047:
048: String longWithNoProperties = new String(
049: "\n"
050: + " BLOB_TYPE=OBJECT_BLOB\n"
051: + " INSERT_TX = INSERT INTO JMS_TRANSACTIONS (TXID) values(?)\n"
052: + " INSERT_MESSAGE = INSERT INTO JMS_MESSAGES (MESSAGEID, DESTINATION, MESSAGEBLOB, TXID, TXOP) VALUES(?,?,?,?,?)\n"
053: + " SELECT_ALL_UNCOMMITED_TXS = SELECT TXID FROM JMS_TRANSACTIONS\n"
054: + " SELECT_MAX_TX = SELECT MAX(TXID) FROM JMS_MESSAGES\n"
055: + " SELECT_MESSAGES_IN_DEST = SELECT MESSAGEID, MESSAGEBLOB FROM JMS_MESSAGES WHERE DESTINATION=?\n"
056: + " SELECT_MESSAGE = SELECT MESSAGEID, MESSAGEBLOB FROM JMS_MESSAGES WHERE MESSAGEID=? AND DESTINATION=?\n"
057: + " MARK_MESSAGE = UPDATE JMS_MESSAGES SET TXID=?, TXOP=? WHERE MESSAGEID=? AND DESTINATION=?\n"
058: + " UPDATE_MESSAGE = UPDATE JMS_MESSAGES SET MESSAGEBLOB=? WHERE MESSAGEID=? AND DESTINATION=?\n"
059: + " UPDATE_MARKED_MESSAGES = UPDATE JMS_MESSAGES SET TXID=?, TXOP=? WHERE TXOP=?\n"
060: + " UPDATE_MARKED_MESSAGES_WITH_TX = UPDATE JMS_MESSAGES SET TXID=?, TXOP=? WHERE TXOP=? AND TXID=?\n"
061: + " DELETE_MARKED_MESSAGES_WITH_TX = DELETE FROM JMS_MESSAGES WHERE TXID IS NOT NULL AND TXOP=?\n"
062: + " DELETE_TX = DELETE FROM JMS_TRANSACTIONS WHERE TXID = ?\n"
063: + " DELETE_MARKED_MESSAGES = DELETE FROM JMS_MESSAGES WHERE TXID=? AND TXOP=?\n"
064: + " DELETE_MESSAGE = DELETE FROM JMS_MESSAGES WHERE MESSAGEID=? AND DESTINATION=?\n"
065: + " CREATE_MESSAGE_TABLE = CREATE TABLE JMS_MESSAGES ( MESSAGEID INTEGER NOT NULL, \\\n"
066: + " DESTINATION VARCHAR(255) NOT NULL, TXID INTEGER, TXOP CHAR(1), \\\n"
067: + " MESSAGEBLOB OBJECT, PRIMARY KEY (MESSAGEID, DESTINATION) )\n"
068: + " CREATE_TX_TABLE = CREATE TABLE JMS_TRANSACTIONS ( TXID INTEGER )\n");
069:
070: static {
071: System.setProperty(simpleKey, simple);
072: System.setProperty(anotherKey, another);
073: }
074:
075: public PropertyPatternUnitTestCase(String name) {
076: super (name);
077: }
078:
079: public void testEmptyPattern() throws Exception {
080: assertEquals("Empty pattern", "", StringPropertyReplacer
081: .replaceProperties(""));
082: }
083:
084: public void testNoPattern() throws Exception {
085: assertEquals("No pattern", "xxx", StringPropertyReplacer
086: .replaceProperties("xxx"));
087: }
088:
089: public void testNoProperty() throws Exception {
090: assertEquals("No pattern", "${xxx}", StringPropertyReplacer
091: .replaceProperties("${xxx}"));
092: }
093:
094: public void testNoPropertyWithDefault() throws Exception {
095: assertEquals("No pattern", "xxx-default",
096: StringPropertyReplacer
097: .replaceProperties("${xxx:xxx-default}"));
098: }
099:
100: public void testPropertyWithDefault() throws Exception {
101: assertEquals("Simple pattern", simple,
102: StringPropertyReplacer.replaceProperties("${"
103: + simpleKey + ":simpleDefault}"));
104: }
105:
106: public void testSimpleProperty() throws Exception {
107: assertEquals("Simple pattern", simple, StringPropertyReplacer
108: .replaceProperties("${" + simpleKey + "}"));
109: }
110:
111: public void testFileSeparatorProperty() throws Exception {
112: assertEquals("File Separator", before + File.separator + after,
113: StringPropertyReplacer.replaceProperties(before + "${"
114: + fileSeparatorKey + "}" + after));
115: }
116:
117: public void testPathSeparatorProperty() throws Exception {
118: assertEquals("Path Separator", before + File.pathSeparator
119: + after, StringPropertyReplacer
120: .replaceProperties(before + "${" + pathSeparatorKey
121: + "}" + after));
122: }
123:
124: public void testStringBeforeProperty() throws Exception {
125: assertEquals("String before pattern", before + simple,
126: StringPropertyReplacer.replaceProperties(before + "${"
127: + simpleKey + "}"));
128: }
129:
130: public void testStringAfterProperty() throws Exception {
131: assertEquals("String after pattern", simple + after,
132: StringPropertyReplacer.replaceProperties("${"
133: + simpleKey + "}" + after));
134: }
135:
136: public void testStringBeforeAfterProperty() throws Exception {
137: assertEquals("String before and after pattern", before + simple
138: + after, StringPropertyReplacer
139: .replaceProperties(before + "${" + simpleKey + "}"
140: + after));
141: }
142:
143: public void testStringBeforeBetweenProperty() throws Exception {
144: assertEquals("String before and between pattern", before
145: + simple + between + another, StringPropertyReplacer
146: .replaceProperties(before + "${" + simpleKey + "}"
147: + between + "${" + anotherKey + "}"));
148: }
149:
150: public void testStringAfterBetweenProperty() throws Exception {
151: assertEquals("String after and between pattern", simple
152: + between + another + after, StringPropertyReplacer
153: .replaceProperties("${" + simpleKey + "}" + between
154: + "${" + anotherKey + "}" + after));
155: }
156:
157: public void testStringBeforeAfterBetweenProperty() throws Exception {
158: assertEquals("String before, after and between pattern", before
159: + simple + between + another + after,
160: StringPropertyReplacer.replaceProperties(before + "${"
161: + simpleKey + "}" + between + "${" + anotherKey
162: + "}" + after));
163: }
164:
165: public void testDollarBeforeProperty() throws Exception {
166: assertEquals("Dollar before pattern", "$" + simple,
167: StringPropertyReplacer.replaceProperties("$${"
168: + simpleKey + "}"));
169: }
170:
171: public void testSpaceBetweenDollarAndProperty() throws Exception {
172: assertEquals("Dollar before pattern", "$ {" + simpleKey + "}",
173: StringPropertyReplacer.replaceProperties("$ {"
174: + simpleKey + "}"));
175: }
176:
177: public void testPropertyDoesNotExist() throws Exception {
178: assertEquals("Property does not exist", "${" + doesNotExist
179: + "}", StringPropertyReplacer.replaceProperties("${"
180: + doesNotExist + "}"));
181: }
182:
183: public void testPathologicalProperties() throws Exception {
184: assertEquals("$", StringPropertyReplacer.replaceProperties("$"));
185: assertEquals("{", StringPropertyReplacer.replaceProperties("{"));
186: assertEquals("}", StringPropertyReplacer.replaceProperties("}"));
187: assertEquals("${", StringPropertyReplacer
188: .replaceProperties("${"));
189: assertEquals("$}", StringPropertyReplacer
190: .replaceProperties("$}"));
191: assertEquals("{$", StringPropertyReplacer
192: .replaceProperties("{$"));
193: assertEquals("{}", StringPropertyReplacer
194: .replaceProperties("{}"));
195: assertEquals("{{", StringPropertyReplacer
196: .replaceProperties("{{"));
197: assertEquals("}$", StringPropertyReplacer
198: .replaceProperties("}$"));
199: assertEquals("}{", StringPropertyReplacer
200: .replaceProperties("}{"));
201: assertEquals("}}", StringPropertyReplacer
202: .replaceProperties("}}"));
203: assertEquals("}}", StringPropertyReplacer
204: .replaceProperties("}}"));
205: assertEquals("${}", StringPropertyReplacer
206: .replaceProperties("${}"));
207: assertEquals("$}{", StringPropertyReplacer
208: .replaceProperties("$}{"));
209: assertEquals("}${", StringPropertyReplacer
210: .replaceProperties("}${"));
211: assertEquals("}{$", StringPropertyReplacer
212: .replaceProperties("}{$"));
213: assertEquals("{$}", StringPropertyReplacer
214: .replaceProperties("{$}"));
215: assertEquals("{}$", StringPropertyReplacer
216: .replaceProperties("{}$"));
217: }
218:
219: public void testLongWithNoProperties() throws Exception {
220: long start = System.currentTimeMillis();
221: assertEquals("No properties in long string",
222: longWithNoProperties, StringPropertyReplacer
223: .replaceProperties(longWithNoProperties));
224: long end = System.currentTimeMillis();
225: assertTrue("Shouldn't take very long", end - start < 1000);
226: }
227:
228: public void testUnixPathProperty() {
229: String unixPath = "/disk1/somepath/somefile.xml";
230: String replaced = StringPropertyReplacer
231: .replaceProperties(unixPath);
232: assertTrue(replaced.equals(unixPath));
233: replaced = StringPropertyReplacer.replaceProperties(replaced);
234: assertTrue(replaced.equals(unixPath));
235: }
236:
237: public void testWin32PathProperty() {
238: String win32Path = "C:\\disk1\\somepath\\somefile.xml";
239: String replaced = StringPropertyReplacer
240: .replaceProperties(win32Path);
241: assertTrue(replaced.equals(win32Path));
242: replaced = StringPropertyReplacer.replaceProperties(replaced);
243: assertTrue(replaced.equals(win32Path));
244: }
245: }
|