Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/main/java/io/appium/java_client/android/Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import lombok.Data;
import lombok.experimental.Accessors;

import static io.appium.java_client.internal.Preconditions.checkArgument;
import static io.appium.java_client.internal.Strings.isNullOrEmpty;

/**
* This is a simple POJO class to support the {@link StartsActivity}.
*/
Expand All @@ -29,10 +26,12 @@ public class Activity {
* @param appActivity The value for the app activity.
*/
public Activity(String appPackage, String appActivity) {
checkArgument(!isNullOrEmpty(appPackage),
"App package should be defined as not empty or null string");
checkArgument(!isNullOrEmpty(appActivity),
"App activity should be defined as not empty or null string");
if (appPackage == null || appPackage.isEmpty()) {
throw new IllegalArgumentException("App package should be defined as not empty or null string");
}
if (appActivity == null || appActivity.isEmpty()) {
throw new IllegalArgumentException("App activity should be defined as not empty or null string");
}
this.appPackage = appPackage;
this.appActivity = appActivity;
this.stopApp = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.HashMap;
import java.util.Map;

import static io.appium.java_client.internal.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import static java.util.Optional.ofNullable;

Expand Down Expand Up @@ -72,7 +71,9 @@ public AndroidInstallApplicationOptions withReplaceDisabled() {
* @return self instance for chaining.
*/
public AndroidInstallApplicationOptions withTimeout(Duration timeout) {
checkArgument(!requireNonNull(timeout).isNegative(), "The timeout value cannot be negative");
if (requireNonNull(timeout).isNegative()) {
throw new IllegalArgumentException("The timeout value cannot be negative");
}
this.timeout = timeout;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.HashMap;
import java.util.Map;

import static io.appium.java_client.internal.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import static java.util.Optional.ofNullable;

Expand All @@ -47,8 +46,9 @@ public AndroidRemoveApplicationOptions() {
* @return self instance for chaining.
*/
public AndroidRemoveApplicationOptions withTimeout(Duration timeout) {
checkArgument(!requireNonNull(timeout).isNegative(),
"The timeout value cannot be negative");
if (requireNonNull(timeout).isNegative()) {
throw new IllegalArgumentException("The timeout value cannot be negative");
}
this.timeout = timeout;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.HashMap;
import java.util.Map;

import static io.appium.java_client.internal.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import static java.util.Optional.ofNullable;

Expand All @@ -46,7 +45,9 @@ public AndroidTerminateApplicationOptions() {
* @return self instance for chaining.
*/
public AndroidTerminateApplicationOptions withTimeout(Duration timeout) {
checkArgument(!requireNonNull(timeout).isNegative(), "The timeout value cannot be negative");
if (requireNonNull(timeout).isNegative()) {
throw new IllegalArgumentException("The timeout value cannot be negative");
}
this.timeout = timeout;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.util.EnumMap;
import java.util.Map;

import static io.appium.java_client.internal.Preconditions.checkArgument;
import static io.appium.java_client.internal.Strings.isNullOrEmpty;
import static java.util.Objects.requireNonNull;

/**
Expand Down Expand Up @@ -93,7 +91,9 @@ private Builder() {
* @return this builder
*/
public Builder matchThreshold(double threshold) {
checkArgument(threshold >= 0 && threshold <= 1, "The threshold must be between 0 and 1");
if (!(threshold >= 0 && threshold <= 1)) {
throw new IllegalArgumentException("The threshold must be between 0 and 1");
}
settings.put(ImagesSetting.IMAGE_MATCH_THRESHOLD, threshold);
return this;
}
Expand All @@ -105,7 +105,9 @@ public Builder matchThreshold(double threshold) {
* @return this builder
*/
public Builder matchMethod(String method) {
checkArgument(!isNullOrEmpty(method), "The match method must not be empty");
if (method == null || method.isEmpty()) {
throw new IllegalArgumentException("The match method must not be empty");
}
settings.put(ImagesSetting.IMAGE_MATCH_METHOD, method);
return this;
}
Expand Down Expand Up @@ -150,7 +152,9 @@ public Builder fixTemplateScale(boolean enabled) {
* @return this builder
*/
public Builder defaultTemplateScale(double scale) {
checkArgument(Double.isFinite(scale) && scale > 0, "The scale must be a finite positive number");
if (!Double.isFinite(scale) || scale <= 0) {
throw new IllegalArgumentException("The scale must be a finite positive number");
}
settings.put(ImagesSetting.DEFAULT_IMAGE_TEMPLATE_SCALE, scale);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.HashMap;
import java.util.Map;

import static io.appium.java_client.internal.Preconditions.checkArgument;
import static java.util.Optional.ofNullable;

/**
Expand Down Expand Up @@ -70,7 +69,9 @@ public FeaturesMatchingOptions withMatchFunc(MatchingFunction name) {
* @return self instance for chaining.
*/
public FeaturesMatchingOptions withGoodMatchesFactor(int factor) {
checkArgument(factor > 1);
if (factor <= 1) {
throw new IllegalArgumentException("The good matches factor must be greater than 1");
}
this.goodMatchesFactor = factor;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* 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 io.appium.java_client.architecture;

import com.tngtech.archunit.core.domain.Dependency;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.ImportOption;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.StringJoiner;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Driver and plugin packages become separate modules, so they may only use the documented API of core:
* the private {@code io.appium.java_client.internal} package is off limits for them.
*/
class InternalApiGuardrailTest {
private static final String INTERNAL = Packages.ROOT + ".internal.";
private static final String PLUGINS = Packages.ROOT + ".plugins.";
private static final Pattern IMPORT = Pattern.compile("^import\\s+(?:static\\s+)?([\\w.]+(?:\\.\\*)?);");

@Test
void driversAndPluginsDoNotUseInternalPackages() throws IOException {
Set<String> violations = new TreeSet<>();
for (JavaClass origin : new ClassFileImporter()
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
.importPackages(Packages.ROOT)) {
if (!isModuleCode(origin.getName())) {
continue;
}
for (Dependency dependency : origin.getDirectDependenciesFromSelf()) {
if (dependency.getTargetClass().getName().startsWith(INTERNAL)) {
violations.add(origin.getName() + " -> " + dependency.getTargetClass().getName());
}
}
}
violations.addAll(importViolations());
assertTrue(violations.isEmpty(), () -> "Drivers and plugins must not use io.appium.java_client.internal; "
+ "promote what they need to io.appium.java_client.extension or avoid the dependency:\n "
+ String.join("\n ", violations));
}

private static boolean isModuleCode(String className) {
return !Packages.owner(className).equals("core") || className.startsWith(PLUGINS);
}

/** Bytecode misses imports used only in Javadoc or for inlined constants; read them from sources. */
private static Set<String> importViolations() throws IOException {
Path root = Paths.get("src", "main", "java");
Set<String> result = new TreeSet<>();
try (Stream<Path> files = Files.walk(root)) {
for (Path file : (Iterable<Path>) files.filter(x -> x.toString().endsWith(".java"))::iterator) {
StringJoiner joiner = new StringJoiner(".");
root.relativize(file).forEach(x -> joiner.add(x.toString()));
String name = joiner.toString().replaceAll("\\.java$", "");
if (!isModuleCode(name)) {
continue;
}
for (String line : Files.readAllLines(file)) {
Matcher matcher = IMPORT.matcher(line);
if (matcher.find() && matcher.group(1).startsWith(INTERNAL)) {
result.add(name + " -> " + matcher.group(1));
}
}
}
}
return result;
}
}
Loading