View Javadoc

1   /*
2    * Copyright 2011 SOFTEC sa. All rights reserved.
3    *
4    * This source code is licensed under the Creative Commons
5    * Attribution-NonCommercial-NoDerivs 3.0 Luxembourg
6    * License.
7    *
8    * To view a copy of this license, visit
9    * http://creativecommons.org/licenses/by-nc-nd/3.0/lu/
10   * or send a letter to Creative Commons, 171 Second Street,
11   * Suite 300, San Francisco, California, 94105, USA.
12   */
13  
14  package org.codehaus.mojo.javascript;
15  
16  import org.apache.maven.plugin.AbstractMojo;
17  import org.apache.maven.plugin.MojoExecutionException;
18  import org.apache.maven.plugin.MojoFailureException;
19  import org.codehaus.mojo.javascript.titanium.TitaniumBuilder;
20  import org.codehaus.mojo.javascript.titanium.TitaniumUtils;
21  
22  import java.io.File;
23  import java.util.UUID;
24  
25  /**
26   * @phase test
27   * @goal titanium-jasmine
28   * @plexus.component role="org.codehaus.mojo.javascript.TitaniumJasmineMojo" role-hint="titanium"
29   */
30  public class TitaniumJasmineMojo extends AbstractTitaniumPackageMojo {
31  
32      /**
33       * <p>The test package execution mode.</p>
34       * <p>Allow the execution of the package on an emulator/device.</p>
35       * <p>Values are:</p>
36       * <dl>
37       *   <dt>none</dt>
38       *   <dd>Do not execute. (Default value)</dd>
39       *   <dt>emulator</dt>
40       *   <dd>Execute on an emulator whose settings are specified in {@link VirtualDevice}.</dd>
41       *   <dt>device</dt>
42       *   <dd>Execute on a connected device.</dd>
43       * </dl>
44       *
45       * If not specified, the value of {@link #executeMode} will be taken.
46       * @parameter expression="${testExecuteMode}"
47       */
48      private String testExecuteMode;
49  
50      /**
51       * @parameter default-value="${project.basedir}${file.separator}src${file.separator}test${file.separator}javascript" expression="${jsunitTestSourceDirectory}"
52       */
53      protected File jasmineTestSourceDirectory;
54  
55      /**
56       * The output directory of the test files.
57       * @parameter default-value="${project.build.testOutputDirectory}"
58       */
59      protected File testOutputDirectory;
60  
61      protected File getPlatformTestOutputDirectory() {
62          return new File(testOutputDirectory, platform);
63      }
64  
65      /**
66       * Set this to 'true' to bypass unit tests entirely. Its use is NOT
67       * RECOMMENDED, but quite convenient on occasion.
68       *
69       * @parameter expression="${maven.test.skip}"
70       */
71      protected boolean skipTests;
72  
73      protected String getAppId() {
74          return project.getGroupId() + "." + project.getArtifactId() + ".test";
75      }
76  
77      public void execute() throws MojoExecutionException, MojoFailureException {
78          if(skipTests || !jasmineTestSourceDirectory.exists() || !testOutputDirectory.exists()) {
79              getLog().info("No Jasmine tests, skipping Jasmine tests preparation.");
80              return;
81          }
82  
83          TitaniumUtils.checkVirtualDevice(platform, getTitaniumSettings(), getVirtualDevice());
84  
85          if (!checkPomSettings()) {
86              return;
87          }
88  
89          if (testExecuteMode == null) {
90              testExecuteMode = executeMode;
91          }
92  
93          if (testExecuteMode.equals("none")) {
94              getLog().info("Skipping jasmine test as testExecuteMode is none");
95              return;
96          }
97  
98          if (platform.compareToIgnoreCase("android") == 0) {
99              File androidBuilder = resolveAndroidBuilder();
100             TitaniumBuilder tiBuilder = new TitaniumBuilder(androidBuilder,
101                     null,
102                     titaniumSettings.getAndroidSdk());
103             packageTestAndroid(tiBuilder);
104         } else if (platform.compareToIgnoreCase("iphone") == 0
105                 || platform.compareToIgnoreCase("ipad") == 0
106                 || platform.compareToIgnoreCase("universal") == 0) {
107             File iosBuilder = resolveIOSBuilder();
108             TitaniumBuilder tiBuilder = new TitaniumBuilder(null,
109                     iosBuilder,
110                     null);
111             packageTestIphone(tiBuilder);
112         } else {
113             throw new MojoExecutionException("Unsupported platform: " + platform);
114         }
115     }
116 
117     private void packageTestAndroid(TitaniumBuilder tiBuilder)
118             throws MojoExecutionException, MojoFailureException {
119         try {
120             if (testExecuteMode.equals("virtual")) {
121                 tiBuilder.launchOnAndroidEmulator(project.getName(),
122                                                 getPlatformTestOutputDirectory(),
123                                                 getAppId(),
124                                                 getAndroidAPI(),
125                                                 getVirtualDevice().getAndroidAPI(),
126                                                 getVirtualDevice().getSkin(),
127                                                 getVirtualDevice().getWait(),
128                                                 getLog());
129 
130             } else if (testExecuteMode.equals("device")) {
131                 ProcessBuilder pb = tiBuilder.createAndroidBuilderProcess("install",
132                         project.getName() + " Test",
133                         getPlatformTestOutputDirectory().getAbsolutePath(),
134                         getAppId(),
135                         getVirtualDevice().getAndroidAPI(),
136                         getVirtualDevice().getSkin());
137                 Process deviceProcess = pb.start();
138                 getLog().info("Deploying on device ");
139                 TitaniumBuilder.logProcess(deviceProcess, getLog());
140                 getLog().info("done");
141             }
142         } catch (MojoFailureException e) {
143             throw e;
144         } catch (Throwable t) {
145             throw new MojoExecutionException("Error while executing android builder", t);
146         }
147     }
148 
149     private void packageTestIphone(TitaniumBuilder tiBuilder) throws MojoExecutionException, MojoFailureException {
150         if (testExecuteMode.equals("virtual")) {
151             tiBuilder.launchIphoneEmulator(getIosVersion(),
152                     getPlatformTestOutputDirectory().getAbsolutePath(),
153                     project.getName() + " Test",
154                     platform,
155                     getVirtualDevice().getFamily(),
156                     getLog());
157         } else if (testExecuteMode.equals("device")) {
158             File tiAppFile = new File(getPlatformTestOutputDirectory(), "tiapp.xml");
159             try {
160                 if (tiAppFile.exists()) {
161                     Tiapp tiApp = getTitaniumSettings().getTiappFromXML(tiAppFile);
162                 }  else {
163                     getLog().warn("Unable to find " + tiAppFile.getAbsolutePath()
164                     + ". uuid will be random.");
165                 }
166 
167             } catch (Throwable t) {
168                 getLog().error("Error while parsing " + tiAppFile.getAbsolutePath()
169                 + ". The application uuid will be random ");
170             }
171             tiBuilder.launchIphoneDevice(getIosVersion(),
172                     getPlatformTestOutputDirectory(),
173                     getAppId(),
174                     project.getName() + " Test",
175                     getTitaniumSettings().getIosDevelopmentProvisioningProfile(),
176                     getTitaniumSettings().getIosDevelopmentCertificate(),
177                     getVirtualDevice().getFamily(),
178                     getLog());
179         }
180     }
181 }