001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.util.parse.metapattern;
018:
019: import java.util.regex.Matcher;
020:
021: import junit.framework.TestCase;
022:
023: import org.apache.wicket.util.parse.metapattern.parsers.CommaSeparatedVariableParser;
024: import org.apache.wicket.util.parse.metapattern.parsers.IntegerVariableAssignmentParser;
025: import org.apache.wicket.util.parse.metapattern.parsers.TagNameParser;
026: import org.apache.wicket.util.parse.metapattern.parsers.VariableAssignmentParser;
027:
028: /**
029: * Test cases for this object
030: * @author Jonathan Locke
031: */
032: public final class MetaPatternTest extends TestCase {
033: /**
034: *
035: */
036: public void testSimple() {
037: // Parse "variable = <number>"
038: final Group variable = new Group(MetaPattern.VARIABLE_NAME);
039: final IntegerGroup value = new IntegerGroup(MetaPattern.INTEGER);
040: final MetaPattern variableAssignment = new MetaPattern(
041: new MetaPattern[] { variable,
042: MetaPattern.OPTIONAL_WHITESPACE,
043: MetaPattern.EQUALS,
044: MetaPattern.OPTIONAL_WHITESPACE, value });
045:
046: final Matcher matcher = variableAssignment.matcher("foo = 9");
047: assertTrue(matcher.matches());
048: assertEquals("foo", matcher.group(1));
049: assertEquals("9", matcher.group(2));
050: }
051:
052: /**
053: * Test assignment of variables.
054: */
055: public void testVariableAssignmentParser() {
056: VariableAssignmentParser parser = new VariableAssignmentParser(
057: "foo = 9");
058: assertTrue(parser.matches());
059: assertEquals("foo", parser.getKey());
060: assertEquals("9", parser.getValue());
061:
062: parser = new VariableAssignmentParser("foo=9");
063: assertTrue(parser.matches());
064: assertEquals("foo", parser.getKey());
065: assertEquals("9", parser.getValue());
066: }
067:
068: /**
069: * Test assignment of integers.
070: */
071: public void testIntegerVariableAssignmentParser() {
072: IntegerVariableAssignmentParser parser = new IntegerVariableAssignmentParser(
073: "foo = 9");
074: assertTrue(parser.matches());
075: assertEquals("foo", parser.getVariable());
076: assertEquals(9, parser.getIntValue());
077: assertEquals(9, parser.getLongValue());
078:
079: parser = new IntegerVariableAssignmentParser("foo=9");
080: assertTrue(parser.matches());
081: assertEquals("foo", parser.getVariable());
082: assertEquals(9, parser.getIntValue());
083: assertEquals(9, parser.getLongValue());
084:
085: parser = new IntegerVariableAssignmentParser("foo=a");
086: assertFalse(parser.matches());
087: }
088:
089: /**
090: * Test parsing of comma separated variables.
091: */
092: public void testCommaSeparatedVariableParser() {
093: CommaSeparatedVariableParser parser = new CommaSeparatedVariableParser(
094: "a,b,c");
095: assertTrue(parser.matches());
096: assertEquals(3, parser.getValues().size());
097: assertEquals("a", parser.getValues().get(0));
098: assertEquals("b", parser.getValues().get(1));
099: assertEquals("c", parser.getValues().get(2));
100:
101: // no whitespaces will be removed
102: parser = new CommaSeparatedVariableParser(" a ,b, c , d ");
103: assertTrue(parser.matches());
104: assertEquals(4, parser.getValues().size());
105: assertEquals(" a ", parser.getValues().get(0));
106: assertEquals("b", parser.getValues().get(1));
107: assertEquals(" c ", parser.getValues().get(2));
108: assertEquals(" d ", parser.getValues().get(3));
109:
110: // It'll care for "" and '' but it'll not remove them
111: parser = new CommaSeparatedVariableParser("'a ',\" b\",'c,d'");
112: assertTrue(parser.matches());
113: assertEquals(3, parser.getValues().size());
114: assertEquals("'a '", parser.getValues().get(0));
115: assertEquals("\" b\"", parser.getValues().get(1));
116: assertEquals("'c,d'", parser.getValues().get(2));
117:
118: // But no escapes. Because no separator is following the 2nd "'",
119: // it'll stop parsing the string.
120: parser = new CommaSeparatedVariableParser("'a\'b, c");
121: assertTrue(parser.matches());
122: assertEquals(1, parser.getValues().size());
123: assertEquals("'a'", parser.getValues().get(0));
124:
125: parser = new CommaSeparatedVariableParser("a");
126: assertTrue(parser.matches());
127: assertEquals(1, parser.getValues().size());
128: assertEquals("a", parser.getValues().get(0));
129:
130: // Empty elements are not supported
131: parser = new CommaSeparatedVariableParser("a,,");
132: assertTrue(parser.matches());
133: assertEquals(1, parser.getValues().size());
134: assertEquals("a", parser.getValues().get(0));
135: }
136:
137: /**
138: * Test the tag parser.
139: */
140: public void testTagParser() {
141: String tag = "name";
142: TagNameParser parser = new TagNameParser(tag);
143: assertEquals(true, parser.matcher().matches());
144: assertEquals("name", parser.getName());
145: assertNull(parser.getNamespace());
146:
147: tag = "namespace:name";
148: parser = new TagNameParser(tag);
149: assertEquals(true, parser.matcher().matches());
150: assertEquals("name", parser.getName());
151: assertEquals("namespace", parser.getNamespace());
152:
153: tag = "namespace:";
154: parser = new TagNameParser(tag);
155: assertEquals(false, parser.matcher().matches());
156:
157: tag = ":names";
158: parser = new TagNameParser(tag);
159: assertEquals(false, parser.matcher().matches());
160: }
161: }
|