/*
 * Copyright 2012-2014 Thomas Bouffard (redfish4ktc)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.ktc.soapui.maven.extension;

import com.eviware.soapui.tools.AbstractSoapUIRunner;
import java.util.Properties;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

public abstract class AbstractSoapuiRunnerMojo extends AbstractSoapuiMojo {
    // already in smartbear implementation
    private String[] globalProperties;
    protected String projectFile;
    private String projectPassword;
    private String[] projectProperties;
    protected boolean saveAfterRun;
    private String settingsFile;
    private String settingsPassword;
    private boolean skip;
    private Properties soapuiProperties;

    // custom maven-soapui-extension-plugin
    protected String runnerType;

    @Override
    protected void performExecute() throws MojoExecutionException, MojoFailureException {
        if (skip || (System.getProperty("maven.test.skip", "false").equals("true"))) {
            getLog().info("SoapUI execution is skipped.");
            return;
        }
        if (projectFile == null) {
            throw new MojoExecutionException("soapui-project-file setting is required");
        }
        performRunnerExecute();
    }

    protected abstract void performRunnerExecute() throws MojoExecutionException, MojoFailureException;

    protected void configureWithSharedParameters(AbstractSoapUIRunner runner) {
        configureWithSharedParameters(runner, projectFile);
    }

    protected void configureWithSharedParameters(AbstractSoapUIRunner runner, String currentProjectFile) {
        runner.setProjectFile(currentProjectFile);
        if (projectPassword != null) {
            runner.setProjectPassword(projectPassword);
        }
        if (settingsFile != null) {
            runner.setSettingsFile(settingsFile);
        }
        if (settingsPassword != null) {
            runner.setSoapUISettingsPassword(settingsPassword);
        }
        if (globalProperties != null) {
            runner.setGlobalProperties(globalProperties);
        }
        if (projectProperties != null) {
            runner.setProjectProperties(projectProperties);
        }
        if (soapuiProperties != null && !soapuiProperties.isEmpty()) {
            for (Object keyObject : soapuiProperties.keySet()) {
                String key = (String) keyObject;
                getLog().info("Setting " + key + " value " + soapuiProperties.getProperty(key));
                System.setProperty(key, soapuiProperties.getProperty(key));
            }
        }
    }
    
}

@@ -656,16 +656,80 @@
 nsion;%0A%0A
+import static org.apache.commons.lang3.ArrayUtils.nullToEmpty;%0A%0A
 import c
@@ -774,16 +774,16 @@
 Runner;%0A
-
 import j
@@ -1703,32 +1703,120 @@
 eturn;%0A        %7D
+%0A%0A        // TODO this is managed by the plugin.xml (default value) + all soapui runners
 %0A        if (pro
@@ -2387,51 +2387,8 @@
 e);%0A
-        if (projectPassword != null) %7B%0A    
     
@@ -2447,150 +2447,46 @@
     
-%7D%0A        if (settingsFile != null) %7B%0A            runner.setSettingsFile(settingsFile);%0A        %7D%0A        if (settingsPassword != null) %7B%0A    
+runner.setSettingsFile(settingsFile);%0A
     
@@ -2545,62 +2545,8 @@
 d);%0A
-        %7D%0A        if (globalProperties != null) %7B%0A    
     
@@ -2576,16 +2576,28 @@
 perties(
+nullToEmpty(
 globalPr
@@ -2609,65 +2609,11 @@
 ies)
+)
 ;%0A
-        %7D%0A        if (projectProperties != null) %7B%0A    
     
@@ -2644,16 +2644,28 @@
 perties(
+nullToEmpty(
 projectP
@@ -2674,17 +2674,19 @@
 perties)
-;
+);%0A
 %0A       
@@ -2690,51 +2690,120 @@
     
-%7D
+initializeSystemProperties();
 %0A    
+%7D%0A%0A
     
-if (soapuiProperties != null && !
+private void initializeSystemProperties() %7B%0A        if (!isNullOrEmpty(
 soap
@@ -2818,17 +2818,8 @@
 ties
-.isEmpty(
 )) %7B
@@ -3136,15 +3136,128 @@
 %7D%0A    %7D%0A
+%0A
     
+private static boolean isNullOrEmpty(Properties props) %7B%0A        return props == null %7C%7C props.isEmpty();%0A    %7D%0A
 %0A%7D%0A

Remove extra null check in the Abstract mojo #118