001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.extensions;
021    
022    import java.text.ParseException;
023    import java.text.SimpleDateFormat;
024    import java.util.Date;
025    import java.util.List;
026    
027    import org.mactor.framework.ConfigException;
028    import org.mactor.framework.MactorException;
029    import org.mactor.framework.TestContext;
030    import org.mactor.framework.extensioninterface.ActionCommand;
031    
032    /**
033     * Suspend execution until the time specifed by the first parameter (on the
034     * format yyyy-MM-dd'T'HH:mm:ss.SSSZ, i.e: 2008-07-04T12:08:56.235+0100)
035     * 
036     * @author Lars Ivar Almli
037     */
038    public class DelayUntil implements ActionCommand {
039            public void perform(TestContext context, List<String> params) throws MactorException {
040                    if (params.size() != 1)
041                            throw new ConfigException("Invalid testspec. One parameter expected: [<the data-time on the form yyyy-MM-dd'T'HH:mm:ss.SSSZ the action should delay until>]>");
042                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
043                    Date d = null;
044                    try {
045                            d = sdf.parse(params.get(0));
046                    } catch (ParseException pe) {
047                            throw new ConfigException("The parameter must be on the format yyyy-MM-dd'T'HH:mm:ss.SSSZ (i.e. 2008-07-04T12:08:56.235+0100) -  '" + params.get(0) + "' is  not");
048                    }
049                    long time2sleep = d.getTime() - System.currentTimeMillis();
050                    if (time2sleep > 0) {
051                            try {
052                                    Thread.sleep(time2sleep);
053                            } catch (InterruptedException ie) {
054                                    throw new MactorException("The sleep was interrupted");
055                            }
056                    }
057            }
058    }