001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.junit.script;
016    
017    import org.apache.hivemind.Resource;
018    import org.apache.tapestry.IComponent;
019    import org.apache.tapestry.IScriptProcessor;
020    import org.apache.tapestry.util.IdAllocator;
021    import org.apache.tapestry.util.PageRenderSupportImpl;
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    /**
027     * Used by {@link org.apache.tapestry.junit.script.TestScript}.
028     *
029     * @author Howard Lewis Ship
030     * @since 3.0
031     */
032    public class MockScriptProcessor implements IScriptProcessor
033    {
034        private StringBuffer _body;
035    
036        private StringBuffer _initialization;
037    
038        private StringBuffer _postInitialization;
039    
040        private List _externalScripts;
041    
042        private IdAllocator _idAllocator = new IdAllocator();
043    
044        public void reset()
045        {
046            if (_body != null)
047                _body.delete(0, _body.length());
048            if (_initialization != null)
049                _initialization.delete(0, _initialization.length());
050            if (_externalScripts != null)
051                _externalScripts.clear();
052            _idAllocator.clear();
053        }
054    
055        public void addBodyScript(String script)
056        {
057            addBodyScript(null, script);
058        }
059    
060        public void addBodyScript(IComponent target, String script)
061        {
062            if (_body == null)
063                _body = new StringBuffer();
064    
065            _body.append(script);
066        }
067    
068        public String getBody()
069        {
070            if (_body == null)
071                return null;
072    
073            return _body.toString();
074        }
075    
076        public void addInitializationScript(String script)
077        {
078            addInitializationScript(null, script);
079        }
080    
081        public void addInitializationScript(IComponent target, String script)
082        {
083            if (_initialization == null)
084                _initialization = new StringBuffer();
085    
086            _initialization.append(script);
087        }
088    
089        public void addScriptAfterInitialization(IComponent target, String script)
090        {
091            if (_postInitialization == null)
092                _postInitialization = new StringBuffer();
093    
094            _postInitialization.append(script);
095        }
096    
097        public String getInitialization()
098        {
099            if (_initialization == null && _postInitialization == null)
100                return null;
101    
102            return PageRenderSupportImpl.getContent(_initialization)
103                   + PageRenderSupportImpl.getContent(_postInitialization);
104        }
105    
106        public void addExternalScript(Resource scriptResource)
107        {
108            addExternalScript(null, scriptResource);
109        }
110    
111        /**
112         * {@inheritDoc}
113         */
114        public boolean isBodyScriptAllowed(IComponent target)
115        {
116            return true;
117        }
118    
119        /**
120         * {@inheritDoc}
121         */
122        public boolean isExternalScriptAllowed(IComponent target)
123        {
124            return true;
125        }
126    
127        /**
128         * {@inheritDoc}
129         */
130        public boolean isInitializationScriptAllowed(IComponent target)
131        {
132            return true;
133        }
134    
135        public void addExternalScript(IComponent target, Resource scriptResource)
136        {
137            if (_externalScripts == null)
138                _externalScripts = new ArrayList();
139    
140            _externalScripts.add(scriptResource);
141        }
142    
143        public Resource[] getExternalScripts()
144        {
145            if (_externalScripts == null)
146                return null;
147    
148            int count = _externalScripts.size();
149    
150            return (Resource[]) _externalScripts.toArray(new Resource[count]);
151        }
152    
153        public String getUniqueString(String baseValue)
154        {
155            return _idAllocator.allocateId(baseValue);
156        }
157    
158    }