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 */
017package org.apache.commons.scxml2.io;
018
019import java.util.Set;
020
021import org.apache.commons.scxml2.SCXMLExecutor;
022import org.apache.commons.scxml2.SCXMLTestHelper;
023import org.apache.commons.scxml2.model.EnterableState;
024import org.apache.commons.scxml2.model.ModelException;
025import org.junit.Assert;
026import org.junit.Test;
027/**
028 * Test white box nature of <state> element "src" attribute.
029 */
030public class StateSrcTest {
031
032    @Test
033    public void testRecursiveSrcInclude() throws Exception {
034        SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/io/src-test-1.xml");
035        exec.go();
036        Set<EnterableState> states = exec.getStatus().getStates();
037        Assert.assertEquals(1, states.size());
038        Assert.assertEquals("srctest3", states.iterator().next().getId());
039        states = SCXMLTestHelper.fireEvent(exec, "src.test");
040        Assert.assertEquals(1, states.size());
041        Assert.assertEquals("srctest1end", states.iterator().next().getId());
042        Assert.assertTrue(exec.getStatus().isFinal());
043    }
044    
045    @Test
046    public void testBadSrcInclude() throws Exception {
047        try {
048            SCXMLReader.read(SCXMLTestHelper.getResource("org/apache/commons/scxml2/io/src-test-4.xml"));
049            Assert.fail("Document with bad <state> src attribute shouldn't be parsed!");
050        } catch (ModelException me) {
051            Assert.assertTrue("Unexpected error message for bad <state> 'src' URI",
052                me.getMessage() != null && me.getMessage().contains("Source attribute in <state src="));
053        }
054    }
055    
056    @Test
057    public void testBadSrcFragmentInclude() throws Exception {
058        try {
059            SCXMLReader.read(SCXMLTestHelper.getResource("org/apache/commons/scxml2/io/src-test-5.xml"));
060            Assert.fail("Document with bad <state> src attribute shouldn't be parsed!");
061        } catch (ModelException me) {
062            Assert.assertTrue("Unexpected error message for bad <state> 'src' URI fragment",
063                me.getMessage() != null && me.getMessage().contains("URI Fragment in <state src="));
064        }
065    }
066}
067