1
2
3
4
5
6
7
8
9
10
11
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
27
28
29
30 public class TitaniumJasmineMojo extends AbstractTitaniumPackageMojo {
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 private String testExecuteMode;
49
50
51
52
53 protected File jasmineTestSourceDirectory;
54
55
56
57
58
59 protected File testOutputDirectory;
60
61 protected File getPlatformTestOutputDirectory() {
62 return new File(testOutputDirectory, platform);
63 }
64
65
66
67
68
69
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 }