001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.test.impl;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.StringReader;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025: import junit.textui.TestRunner;
026:
027: import org.apache.commons.jelly.JellyContext;
028: import org.apache.commons.jelly.XMLOutput;
029: import org.apache.commons.jelly.impl.Embedded;
030: import org.xml.sax.InputSource;
031:
032: /**
033: * Unit case of Embedded
034: *
035: * @author <a href="mailto:vinayc@apache.org">Vinay Chandran</a>
036: */
037: public class TestEmbedded extends TestCase {
038:
039: public static void main(String[] args) {
040: TestRunner.run(suite());
041: }
042:
043: public static Test suite() {
044: return new TestSuite(TestEmbedded.class);
045: }
046:
047: public TestEmbedded(String testName) {
048: super (testName);
049: }
050:
051: /**
052: * test Script input as a java.lang.String object
053: */
054: public void testStringAsScript() {
055: Embedded embedded = new Embedded();
056: String jellyScript = "<?xml version=\"1.0\"?>"
057: + " <j:jelly xmlns:j=\"jelly:core\">"
058: + "jelly-test-case" + " </j:jelly>";
059: embedded.setScript(jellyScript);
060: ByteArrayOutputStream baos = new ByteArrayOutputStream();
061: embedded.setOutputStream(baos);
062: boolean status = embedded.execute();
063: //executed properly without script errors
064: assertTrue("Emebedded execution failed", status);
065: //check that the output confirms the exepected
066: assertEquals("jelly-test-case", new String(baos.toByteArray()));
067: //test generation of error
068: embedded.setScript(jellyScript + "obnoxious-part");
069: status = embedded.execute();
070: //test failure of execution
071: assertFalse("A script with bad XML was executed successfully",
072: status);
073: //Asserting the parser generated a errorMsg
074: assertNotNull(
075: "A script with bad XML didn't generate an error message",
076: embedded.getErrorMsg());
077: }
078:
079: /**
080: * test Script input as a InputStream
081: */
082: public void testInputStreamAsScript() {
083: Embedded embedded = new Embedded();
084: String jellyScript = "<?xml version=\"1.0\"?>"
085: + " <j:jelly xmlns:j=\"jelly:core\">"
086: + "jelly-test-case" + " </j:jelly>";
087: embedded.setScript(new ByteArrayInputStream(jellyScript
088: .getBytes()));
089: ByteArrayOutputStream baos = new ByteArrayOutputStream();
090: embedded.setOutputStream(baos);
091: boolean status = embedded.execute();
092: //executed properly without script errors
093: assertEquals(status, true);
094: //check that the output confirms the expected
095: assertEquals("jelly-test-case", new String(baos.toByteArray()));
096: }
097:
098: /**
099: * Test simple 'raw' execution of a string. See JELLY-189.
100: */
101: public void testRawExecuteAsString() throws Exception {
102: String message = "<?xml version=\"1.0\"?>"
103: + " <j:jelly xmlns:j=\"jelly:core\">"
104: + "jelly-test-case" + " </j:jelly>";
105: ByteArrayOutputStream output = new ByteArrayOutputStream();
106: XMLOutput xmlOutput = XMLOutput.createXMLOutput(output);
107: InputSource script = new InputSource(new StringReader(message
108: .toString()));
109: JellyContext context = new JellyContext();
110: context.runScript(script, xmlOutput);
111: output.close();
112: //check that the output confirms the expected
113: assertEquals("jelly-test-case",
114: new String(output.toByteArray()));
115: }
116: }
|