001    /*
002     * Copyright 1999-2001,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    
017    package org.apache.commons.latka.jelly;
018    
019    import java.io.IOException;
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    import org.apache.commons.jelly.JellyTagException;
024    import org.apache.commons.jelly.TagSupport;
025    import org.apache.commons.jelly.XMLOutput;
026    
027    import org.apache.commons.latka.DefaultLatkaEventInfo;
028    import org.apache.commons.latka.Latka;
029    import org.apache.commons.latka.LatkaException;
030    import org.apache.commons.latka.XMLReporter;
031    import org.apache.commons.latka.event.LatkaEventInfo;
032    import org.apache.commons.latka.event.LatkaEventListener;
033    import org.apache.commons.latka.event.SuiteCompletedEvent;
034    
035    /**
036     *
037     * @author  Morgan Delagrange
038     */
039    public class SuiteTag extends TagSupport {
040            
041        protected String _defaultHost = null;
042        protected int _defaultPort = -1;
043        protected String _defaultProxyHost = null;
044        protected int _defaultProxyPort = -1;
045        protected String _label = null;
046    
047        protected SuiteSettings _settings = null;
048        protected Map _sessionCache = new HashMap();
049    
050        /**
051         *  Wraps Latka tests, provides some defaults for host, port etc.
052         *
053         * @param xmlOutput a place to write output
054         * @throws JellyTagException if the suite fails, either as a result of test failures
055         *    or from problems executing tags, generating reports, etc.
056         */
057        public void doTag(XMLOutput xmlOutput) throws JellyTagException {
058            // if an enclosing tag does not specify a listener, provide a default
059            boolean defaultListener = false;
060            LatkaEventListener listener = null;
061            LatkaEventInfo eventInfo = null;
062            {
063                listener = 
064                    JellyUtils.getInstance().getLatkaEventListener(getContext());
065                if (listener == null) {
066                    listener = new XMLReporter();
067                    defaultListener = true;
068                }
069    
070                eventInfo = new DefaultLatkaEventInfo(listener);
071                JellyUtils.getInstance().setLatkaEventInfo(getContext(), eventInfo);
072            }
073    
074    
075            _settings = 
076                new SuiteSettings(_defaultHost, _defaultPort, _defaultProxyHost, 
077                                  _defaultProxyPort);
078            invokeBody(xmlOutput);
079    
080            eventInfo.suiteCompleted(new SuiteCompletedEvent());
081            JellyUtils.getInstance().removeLatkaEventInfo(getContext());
082    
083            if (defaultListener == true) {
084                
085                try {
086                    Latka latka = new Latka();
087                    String transformedReport = 
088                      latka.transformXML(((XMLReporter) listener).getDocumentAsString());
089    
090                    System.out.println(transformedReport);
091                } catch (LatkaException e) {
092                    throw new JellyTagException("could not generate latka report",e);
093                } catch (IOException e) {
094                    throw new JellyTagException("could not generate latka report",e);
095                }
096    
097                if (eventInfo.didSuiteSucceed() == false) {
098                    throw new JellyTagException("SUITE FAILED");
099                }
100            }
101    
102    
103        }
104    
105        public SuiteSettings getSuiteSettings() {
106            return _settings;
107        }
108    
109        public Map getSessionCache() {
110            return _sessionCache;
111        }
112    
113        /**
114         * Setter for defaultHost
115         * 
116         * @param defaultHost
117         *               defaultHost for all requests
118         */
119        public void setDefaultHost(String defaultHost) {
120            _defaultHost = defaultHost;
121        }
122        
123        /**
124         * Setter for defaultPort
125         * 
126         * @param defaultPort
127         *               defaultPort for all requests
128         */
129        public void setDefaultPort(int defaultPort) {
130            _defaultPort = defaultPort;
131        }
132    
133    
134        /**
135         * Setter for defaultProxyHost
136         * 
137         * @param defaultHost
138         *               defaultProxyHost for all requests
139         */
140        public void setDefaultProxyHost(String defaultHost) {
141            _defaultProxyHost = defaultHost;
142        }
143    
144        /**
145         * Setter for defaultProxyPort
146         * 
147         * @param defaultPort
148         *               defaultProxyPort for all requests
149         */
150        public void setDefaultProxyPort(int defaultPort) {
151            _defaultProxyPort = defaultPort;
152        }
153    
154        /**
155         * Set the label for this suite
156         * 
157         * @param label  suite label
158         */
159        public void setLabel(String label) {
160            _label = label;
161        }
162    
163    }