Skip to content

Commit

Permalink
consistent output for test report and logging identifying device with…
Browse files Browse the repository at this point in the history
… serial number, avdname, manufacturer and model

support for avdname and emulator-xxxx for android.device parameter
  • Loading branch information
mosabua committed Sep 2, 2011
1 parent 9547c22 commit 1060458
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.android.ddmlib.InstallException;
import com.jayway.maven.plugins.android.common.AetherHelper;
import com.jayway.maven.plugins.android.common.AndroidExtension;
import com.jayway.maven.plugins.android.common.DeviceHelper;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathNotFoundException;
import org.apache.commons.jxpath.xml.DocumentContainer;
Expand Down Expand Up @@ -541,8 +542,7 @@ public void doWithDevice(final IDevice device) throws MojoExecutionException {
device.installPackage(apkFile.getAbsolutePath(), true);
getLog().info("Successfully installed "
+ apkFile.getAbsolutePath() + " to "
+ device.getSerialNumber() + " (avdName="
+ device.getAvdName() + ")");
+ DeviceHelper.getDescriptiveName(device));
} catch (InstallException e) {
throw new MojoExecutionException("Install of "
+ apkFile.getAbsolutePath() + "failed.", e);
Expand Down Expand Up @@ -570,9 +570,19 @@ protected void doWithDevices(final DeviceCallback deviceCallback) throws MojoExe
getLog().info("android.device parameter set to " + device);
for (IDevice idevice : devices) {
// use specified device or all emulators or all devices
if (("emulator".equals(device) && idevice.isEmulator())
|| ("usb".equals(device) && !idevice.isEmulator())
|| (idevice.getAvdName() != null && idevice.getAvdName().equals(device))) {
if ("emulator".equals(device) && idevice.isEmulator()) {
getLog().info("Emulator " + DeviceHelper.getDescriptiveName(idevice) + " found.");
deviceCallback.doWithDevice(idevice);
} else if ("usb".equals(device) && !idevice.isEmulator()) {
getLog().info("Device " + DeviceHelper.getDescriptiveName(idevice) + " found.");
deviceCallback.doWithDevice(idevice);
} else if (idevice.isEmulator()
&& (idevice.getAvdName().equalsIgnoreCase(device)
|| idevice.getSerialNumber().equalsIgnoreCase(device))) {
getLog().info("Emulator " + DeviceHelper.getDescriptiveName(idevice) + " found.");
deviceCallback.doWithDevice(idevice);
} else if (!idevice.isEmulator() && idevice.getSerialNumber().equals(device)) {
getLog().info("Device " + DeviceHelper.getDescriptiveName(idevice) + " found.");
deviceCallback.doWithDevice(idevice);
}
}
Expand All @@ -590,17 +600,6 @@ protected void doWithDevices(final DeviceCallback deviceCallback) throws MojoExe
}
}

/**
* Adds relevant parameter to the parameters list for chosen device.
*
* @param commands the parameters to be used with the {@code adb} command
* @param device the device to be used
*/
protected void addDeviceParameter(List<String> commands, IDevice device) {
commands.add("-s");
commands.add(device.getSerialNumber());
}

/**
* Undeploys an apk from a connected emulator or usb device. Also deletes the application's data and cache
* directories on the device.
Expand Down Expand Up @@ -632,8 +631,7 @@ public void doWithDevice(final IDevice device) throws MojoExecutionException {
try {
device.uninstallPackage(packageName);
getLog().info("Successfully uninstalled " + packageName +
" from " + device.getSerialNumber() + " (avdName="
+ device.getAvdName() + ")");
" from " + DeviceHelper.getDescriptiveName(device));
result.set(true);
} catch (InstallException e) {
result.set(false);
Expand Down Expand Up @@ -834,7 +832,10 @@ protected AndroidSdk getAndroidSdk() throws MojoExecutionException {
private String getAndroidHomeOrThrow() throws MojoExecutionException {
final String androidHome = System.getenv(ENV_ANDROID_HOME);
if (isBlank(androidHome)) {
throw new MojoExecutionException("No Android SDK path could be found. You may configure it in the pom using <sdk><path>...</path></sdk> or <properties><android.sdk.path>...</android.sdk.path></properties> or on command-line using -Dandroid.sdk.path=... or by setting environment variable " + ENV_ANDROID_HOME);
throw new MojoExecutionException("No Android SDK path could be found. You may configure it in the " +
"plugin configuration section in the pom file using <sdk><path>...</path></sdk> or " +
"<properties><android.sdk.path>...</android.sdk.path></properties> or on command-line " +
"using -Dandroid.sdk.path=... or by setting environment variable " + ENV_ANDROID_HOME);
}
return androidHome;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.jayway.maven.plugins.android.common;

import com.android.ddmlib.IDevice;
import org.apache.commons.lang.StringUtils;

/**
* A bunch of helper methods for dealing with IDevice instances.
* @author Manfred Moser <[email protected]>
*/
public class DeviceHelper {

private static final String MANUFACTURER_PROPERTY = "ro.product.manufacturer";
private static final String MODEL_PROPERTY = "ro.product.model";

/**
* Get a device identifier string that is suitable for filenames as well as log messages.
* This means it is human readable and contains no spaces.
* Used for instrumentation test report file names so see more at
* AbstractInstrumentationMojo#testCreateReport javadoc since
* that is the public documentation.
*/
public static String getDescriptiveName(IDevice device) {
// if any of this logic changes update javadoc for
// AbstractInstrumentationMojo#testCreateReport
String SEPARATOR = "_";
StringBuilder identfier = new StringBuilder()
.append(device.getSerialNumber());
if (device.getAvdName() != null) {
identfier.append(SEPARATOR).append(device.getAvdName());
}
String manufacturer = getManufacturer(device);
if (StringUtils.isNotBlank(manufacturer)) {
identfier.append(SEPARATOR).append(manufacturer);
}
String model = getModel(device);
if (StringUtils.isNotBlank(model)) {
identfier.append(SEPARATOR).append(model);
}
return identfier.toString();
}

/**
* @return the manufacturer of the device as set in #MANUFACTURER_PROPERTY, typically "unknown" for emulators
*/
public static String getManufacturer(IDevice device) {
return StringUtils.deleteWhitespace(device.getProperty(MANUFACTURER_PROPERTY));
}

/**
* @return the model of the device as set in #MODEL_PROPERTY, typically "sdk" for emulators
*/
public static String getModel(IDevice device) {
return StringUtils.deleteWhitespace(device.getProperty(MODEL_PROPERTY));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.jayway.maven.plugins.android.AbstractIntegrationtestMojo;
import com.jayway.maven.plugins.android.DeviceCallback;

import com.jayway.maven.plugins.android.common.DeviceHelper;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -133,9 +134,10 @@ public abstract class AbstractInstrumentationMojo extends AbstractIntegrationtes
* on.
* <br /><br />
* The files are stored in target/surefire-reports and named TEST-deviceid.xml.
* The deviceid for an emulator is deviceSerialNumber_avdName. The
* serial number is commonly emulator-5554 for the first emulator
* started with numbers increasing.
* The deviceid for an emulator is deviceSerialNumber_avdName_manufacturer_model.
* The serial number is commonly emulator-5554 for the first emulator started
* with numbers increasing. avdName is as defined in the SDK tool. The
* manufacturer is typically "unknown" and the model is typically "sdk".
* The deviceid for an actual devices is
* deviceSerialNumber_manufacturer_model.
* <br /><br />
Expand Down Expand Up @@ -335,7 +337,7 @@ public void testRunStarted(String runName, int testCount) {
testSuiteAttributes.setNamedItem(nameAttr);

Attr hostnameAttr = junitReport.createAttribute(ATTR_TESTSUITE_HOSTNAME);
hostnameAttr.setValue(getDeviceIdentifier());
hostnameAttr.setValue(DeviceHelper.getDescriptiveName(device));
testSuiteAttributes.setNamedItem(hostnameAttr);

Node propertiesNode = junitReport.createElement(TAG_PROPERTIES);
Expand Down Expand Up @@ -564,7 +566,7 @@ private void writeJunitReportToFile() {
String fileName = new StringBuilder()
.append(directory)
.append("/TEST-")
.append(getDeviceIdentifier())
.append(DeviceHelper.getDescriptiveName(device))
.append(".xml")
.toString();
File reportFile = new File(fileName);
Expand Down Expand Up @@ -630,33 +632,5 @@ public boolean threwException() {
public String getExceptionMessages() {
return exceptionMessages.toString();
}

/**
* Get a device identifier string. More documentation at the
* AbstractInstrumentationMojo#testCreateReport javadoc since
* that is the public documentation.
*
x * @return
*/
private String getDeviceIdentifier() {
// if any of this logic changes update javadoc for
// AbstractInstrumentationMojo#testCreateReport
String SEPARATOR = "_";
StringBuilder identfier = new StringBuilder()
.append(device.getSerialNumber());
if (device.getAvdName() != null) {
identfier.append(SEPARATOR).append(device.getAvdName());
} else {
String manufacturer = StringUtils.deleteWhitespace(device.getProperty("ro.product.manufacturer"));
if (StringUtils.isNotBlank(manufacturer)) {
identfier.append(SEPARATOR).append(manufacturer);
}
String model = StringUtils.deleteWhitespace(device.getProperty("ro.product.model"));
if (StringUtils.isNotBlank(model)) {
identfier.append(SEPARATOR).append(model);
}
}
return identfier.toString();
}
}
}

0 comments on commit 1060458

Please sign in to comment.