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.model;
018
019import java.util.List;
020
021import org.apache.commons.scxml2.SCXMLExecutor;
022import org.apache.commons.scxml2.SCXMLTestHelper;
023import org.junit.Assert;
024import org.junit.Test;
025
026public class StateTest {
027
028    @Test
029    public void testGetTransitionsListNull() {
030        State state = new State();
031        Assert.assertNull(state.getTransitionsList("event"));
032    }
033        
034    @Test
035    public void testGetTransitionsList() {
036        State state = new State();
037        state.getTransitionsList().add(new Transition());
038        Assert.assertNotNull(state.getTransitionsList(null));
039    }
040        
041    @Test
042    public void testAddTransitionDoesNotContainKey() {
043        Transition transition = new Transition();
044        transition.setEvent("event");
045
046        State state = new State();
047        state.addTransition(transition);
048        
049        List<Transition> events = state.getTransitionsList("event");
050        
051        Assert.assertEquals(1, events.size());
052        Assert.assertEquals("event", events.get(0).getEvent());
053    }
054        
055    @Test
056    public void testAddTransitionContainKey() {
057        Transition transition1 = new Transition();
058        transition1.setEvent("event");
059
060        Transition transition2 = new Transition();
061        transition2.setEvent("event");
062
063        State state = new State();
064        state.addTransition(transition1);
065        state.addTransition(transition2);
066        
067        List<Transition> events = state.getTransitionsList("event");
068        
069        Assert.assertEquals(2, events.size());
070    }
071        
072    @Test
073    public void testGetTransitionList() {
074        Transition transition1 = new Transition();
075        transition1.setEvent("event");
076
077        Transition transition2 = new Transition();
078        transition2.setEvent("event");
079
080        State state = new State();
081        state.addTransition(transition1);
082        state.addTransition(transition2);
083        
084        List<Transition> events = state.getTransitionsList();
085        
086        Assert.assertEquals(2, events.size());
087    }
088        
089    @Test
090    public void testHasHistoryEmpty() {
091        State state = new State();
092        Assert.assertFalse(state.hasHistory());
093    }
094    
095    @Test
096    public void testHasHistory() {
097        History history = new History();
098
099        State state = new State();
100        state.addHistory(history);
101        
102        Assert.assertTrue(state.hasHistory());
103    }
104        
105    @Test
106    public void testIsSimple() {
107        State state = new State();
108        Assert.assertTrue(state.isSimple());
109    }
110        
111    @Test
112    public void testIsSimpleHasChildren() {
113        State state1 = new State();
114        
115        State state = new State();
116        state.addChild(state1);
117        
118        Assert.assertFalse(state.isSimple());
119    }
120        
121    @Test
122    public void testIsCompositeFalse() {
123        State state = new State();
124        Assert.assertFalse(state.isComposite());
125    }
126        
127    @Test
128    public void testIsCompositeParallel() {
129        State child = new State();
130
131        State state = new State();
132        state.addChild(child);
133        
134        Assert.assertTrue(state.isComposite());
135    }
136        
137    @Test
138    public void testIsCompositeHasChildren() {
139        State state1 = new State();
140
141        State state = new State();
142        state.addChild(state1);
143        
144        Assert.assertTrue(state.isComposite());
145    }
146        
147    @Test
148    public void testIsRegion() {
149        State state = new State();
150        state.setParent(new Parallel());
151        
152        Assert.assertTrue(state.isRegion());
153    }
154        
155    @Test
156    public void testIsRegionNotParallel() {
157        State state = new State();
158        state.setParent(new State());
159        
160        Assert.assertFalse(state.isRegion());
161    }
162    
163    @Test
164    public void testInitialAttribute() throws Exception {
165        SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/model/state-01.xml");
166        exec.go();
167        Assert.assertEquals("s11", exec.getStatus().getStates().iterator().next().getId());
168    }
169}