// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hivemind.test.sdl;
import java.net.URL;
import java.util.List;
import javax.swing.border.LineBorder;
import org.apache.hivemind.Element;
import org.apache.hivemind.Resource;
import org.apache.hivemind.test.HiveMindTestCase;
import org.apache.hivemind.util.URLResource;
import org.xml.sax.SAXParseException;
/**
* Tests the {@link org.apache.hivemind.sdl.SDLResourceParser} indirectly, by
* using {@link hivemind.test.sdl.ElementAssembler} to
*
* @author Howard Lewis Ship
*/
public class TestSDLResourceParser extends HiveMindTestCase
{
private static final String LINEBREAK = System.getProperty("line.separator");
private Element parse(String file) throws Exception
{
URL fileURL = getClass().getResource(file);
Resource r = new URLResource(fileURL);
ElementAssembler a = new ElementAssembler();
return a.parse(r);
}
private void assertElement(
String expectedName,
int expectedLine,
int expectedColumn,
Element e)
{
assertEquals(expectedName, e.getElementName());
assertEquals(expectedLine, e.getLocation().getLineNumber());
assertEquals(expectedColumn, e.getLocation().getColumnNumber());
}
public void testMinimal() throws Exception
{
Element root = parse("minimal.sdl");
assertElement("module", 15, 1, root);
assertEquals(0, root.getAttributes().size());
assertEquals(true, root.isEmpty());
assertEquals("", root.getContent());
}
public void testElementLiteralContent() throws Exception
{
Element root = parse("elementLiteralContent.sdl");
List l = root.getElements();
assertEquals(6, l.size());
Element e = (Element) l.get(0);
assertElement("string", 17, 3, e);
assertEquals("Now is the time", e.getContent());
e = (Element) l.get(1);
assertEquals(" For all good men ", e.getContent());
e = (Element) l.get(2);
// The parser runs the individual numeric values together!
assertEquals("3.1459", e.getContent());
e = (Element) l.get(3);
assertEquals("org.apache.hivemind", e.getContent());
e = (Element) l.get(4);
assertEquals("The value of PI is 3.1459, give or take.", e.getContent());
e = (Element) l.get(5);
assertEquals("${env.current.dir}", e.getContent());
}
private void checkAttributes(Element e, String[] attributesAndValues)
{
for (int i = 0; i < attributesAndValues.length; i += 2)
{
String name = attributesAndValues[i];
assertEquals(
"Attribute " + name,
attributesAndValues[i + 1],
e.getAttributeValue(name));
}
assertEquals("Attribute count", attributesAndValues.length / 2, e.getAttributes().size());
}
public void testAttributes() throws Exception
{
Element root = parse("attributes.sdl");
List l = root.getElements();
assertEquals(7, l.size());
Element e = (Element) l.get(0);
checkAttributes(e, new String[] { "a", "b", "c", "d" });
e = (Element) l.get(1);
checkAttributes(e, new String[] { "user", "fred.flintstone", "friend", "barney.rubble" });
e = (Element) l.get(2);
checkAttributes(e, new String[] { "pi", "3.14159", "sunday", "0", "profit", "-1.34" });
e = (Element) l.get(3);
checkAttributes(
e,
new String[] { "user", "Fred Flintstone", "long", "Barney" + LINEBREAK + "Rubble" });
e = (Element) l.get(4);
checkAttributes(
e,
new String[] { "user", "Wilma Flintstone", "long", "Howard \"The Tank\" Lewis Ship" });
e = (Element) l.get(5);
checkAttributes(
e,
new String[] { "quoted", "Tab:\tSlash:\\Quote:\"Newline:\nCR:\rOther:\\?", "extended",
// Slash has no meaning in an extended literal.
"Tab:\\tSlash:\\\\Quote:\\\"Newline:\\nCR:\\rOther:\\?" });
e = (Element) l.get(6);
checkAttributes(
e,
new String[] { "fred", "${fred.flintstone}", "barney", "${barney.rubble}" });
}
public void testNoRootElement() throws Exception
{
try
{
parse("missing-root.sdl");
unreachable();
}
catch (SAXParseException ex)
{
// The column may be 35 or 36 depending on line encoding.
assertExceptionRegexp(
ex,
"sdl/missing\\-root\\.sdl: Encountered \"<EOF>\" at line 15, column 3(5|6)\\.");
}
}
public void testErrorInvalidElement() throws Exception
{
try
{
parse("ErrorInvalidElement.sdl");
unreachable();
}
catch (SAXParseException ex)
{
assertExceptionSubstring(ex, "Encountered: \"%\"");
}
}
public void testColonValue() throws Exception
{
Element root = parse("colonvalue.sdl");
assertEquals("element", root.getElementName());
assertEquals("prefix:suffix", root.getAttributeValue("attribute"));
}
}