Skip to content

Commit

Permalink
Merge pull request #2271 from zebrunner/develop
Browse files Browse the repository at this point in the history
 1.2.9
  • Loading branch information
vdelendik authored Apr 10, 2024
2 parents efb72da + e61444b commit d79a99a
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>11</java.version>
<carina-webdriver.version>1.2.10</carina-webdriver.version>
<carina-webdriver.version>1.2.11</carina-webdriver.version>
<!-- Logging -->
<slf4j.version>1.7.30</slf4j.version>
<log4j.version>2.17.1</log4j.version>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/zebrunner/carina/core/IAbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*******************************************************************************/
package com.zebrunner.carina.core;

import com.zebrunner.carina.core.filter.v2.TestRunFilterListener;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
Expand All @@ -41,7 +42,7 @@

// on start order is FilterTestsListener, TestRunListener and CarinaListener
// on finish reverse order, i.e. CarinaListener, TestRunListener and FilterTestsListener
@LinkedListeners({ CarinaListener.class, TestRunListener.class, FilterTestsListener.class })
@LinkedListeners({ CarinaListener.class, TestRunListener.class, FilterTestsListener.class, TestRunFilterListener.class})
public interface IAbstractTest extends ICustomTypePageFactory, ITestCases {

long EXPLICIT_TIMEOUT = Configuration.getRequired(WebDriverConfiguration.Parameter.EXPLICIT_TIMEOUT, Long.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public enum Parameter implements IParameter {
/**
* Executing rules logic
*/
TEST_RUN_RULES("test_run_rules");
TEST_RUN_RULES("test_run_rules"),

/**
* todo add description
*/
FILTER_METHOD_PATTERN("filter_method_pattern");

private final String key;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.zebrunner.carina.core.filter;
package com.zebrunner.carina.core.filter.v1;

import java.lang.invoke.MethodHandles;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zebrunner.carina.core.filter.impl.OwnerFilter;
import com.zebrunner.carina.core.filter.impl.PriorityFilter;
import com.zebrunner.carina.core.filter.impl.TagFilter;
import com.zebrunner.carina.core.filter.v1.impl.OwnerFilter;
import com.zebrunner.carina.core.filter.v1.impl.PriorityFilter;
import com.zebrunner.carina.core.filter.v1.impl.TagFilter;
/**
* Enum to store rules (name and implementation of the rule)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.zebrunner.carina.core.filter;
package com.zebrunner.carina.core.filter.v1;

import java.util.Arrays;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.zebrunner.carina.core.filter;
package com.zebrunner.carina.core.filter.v1;

public class IncorrectFilterException extends RuntimeException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
* *****************************************************************************/

package com.zebrunner.carina.core.filter.impl;
package com.zebrunner.carina.core.filter.v1.impl;

import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
Expand All @@ -24,7 +24,7 @@
import org.slf4j.LoggerFactory;
import org.testng.ITestNGMethod;

import com.zebrunner.carina.core.filter.IFilter;
import com.zebrunner.carina.core.filter.v1.IFilter;
import com.zebrunner.carina.core.registrar.ownership.MethodOwner;

public class OwnerFilter implements IFilter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.zebrunner.carina.core.filter.impl;
package com.zebrunner.carina.core.filter.v1.impl;

import java.lang.invoke.MethodHandles;
import java.util.List;
Expand All @@ -22,7 +22,7 @@
import org.slf4j.LoggerFactory;
import org.testng.ITestNGMethod;

import com.zebrunner.carina.core.filter.IFilter;
import com.zebrunner.carina.core.filter.v1.IFilter;
import com.zebrunner.carina.core.registrar.tag.Priority;
import com.zebrunner.carina.core.registrar.tag.TestPriority;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.zebrunner.carina.core.filter.impl;
package com.zebrunner.carina.core.filter.v1.impl;

import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
Expand All @@ -23,7 +23,7 @@
import org.slf4j.LoggerFactory;
import org.testng.ITestNGMethod;

import com.zebrunner.carina.core.filter.IFilter;
import com.zebrunner.carina.core.filter.v1.IFilter;
import com.zebrunner.carina.core.registrar.tag.TestTag;

public class TagFilter implements IFilter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.zebrunner.carina.core.filter.rule;
package com.zebrunner.carina.core.filter.v1.rule;

import java.util.List;

import com.zebrunner.carina.core.filter.IFilter;
import com.zebrunner.carina.core.filter.v1.IFilter;

/**
* Java bean for the rule that can be used for suite limit
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.zebrunner.carina.core.filter.v2;

import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.concurrent.ConcurrentException;
import org.apache.commons.lang3.concurrent.LazyInitializer;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.testng.ITestNGMethod;

import com.zebrunner.carina.webdriver.config.WebDriverConfiguration;

public class CountryFilter implements ITestFilter {

private static final LazyInitializer<String> COUNTRY = new LazyInitializer<>() {
@Override
protected String initialize() throws ConcurrentException {
return WebDriverConfiguration.getLocale()
.getCountry();
}
};

@Override
public String key() {
return "country";
}

@Override
public boolean isPerform(ITestNGMethod testMethod) {
try {
String country = COUNTRY.get();
return testMethod != null && testMethod.getGroups().length > 0 &&
Arrays.stream(testMethod.getGroups())
.anyMatch(group -> StringUtils.equals(country, group));
} catch (ConcurrentException e) {
return ExceptionUtils.rethrow(e);
}
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/zebrunner/carina/core/filter/v2/ITestFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.zebrunner.carina.core.filter.v2;

import org.testng.ITestNGMethod;

import com.zebrunner.carina.utils.exception.InvalidConfigurationException;

public interface ITestFilter {

boolean isPerform(ITestNGMethod testMethod);

default String key() {
throw new InvalidConfigurationException("Filter should have key.");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.zebrunner.carina.core.filter.v2;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.zebrunner.carina.core.config.TestConfiguration;
import org.apache.commons.lang3.concurrent.ConcurrentException;
import org.apache.commons.lang3.concurrent.LazyInitializer;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.testng.ITestNGMethod;

import com.zebrunner.carina.utils.config.Configuration;

public class MethodsFilter implements ITestFilter {

private static final LazyInitializer<Boolean> IS_PERFORM = new LazyInitializer<>() {
@Override
protected Boolean initialize() throws ConcurrentException {
return Configuration.get(TestConfiguration.Parameter.FILTER_METHOD_PATTERN)
.isPresent();
}
};

private static final LazyInitializer<List<Pattern>> PATTERN = new LazyInitializer<>() {
@Override
protected List<Pattern> initialize() throws ConcurrentException {
return Arrays.stream(Configuration.getRequired(TestConfiguration.Parameter.FILTER_METHOD_PATTERN).split(","))
.map(Pattern::compile)
.collect(Collectors.toList());
}
};

@Override
public String key() {
return "methods";
}

@Override
public boolean isPerform(ITestNGMethod testMethod) {
try {
if (!IS_PERFORM.get()) {
return true;
}
for (Pattern pattern : PATTERN.get()) {
if (testMethod != null && pattern.matcher(testMethod.getRealClass().getSimpleName() + "#" + testMethod.getMethodName())
.find()) {
return true;
}
}
return false;
} catch (ConcurrentException e) {
return ExceptionUtils.rethrow(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*******************************************************************************
* Copyright 2020-2023 Zebrunner Inc (https://www.zebrunner.com).
*
* 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 com.zebrunner.carina.core.filter.v2;

import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.concurrent.ConcurrentException;
import org.apache.commons.lang3.concurrent.LazyInitializer;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.ISuite;
import org.testng.ISuiteListener;
import org.testng.ITestNGMethod;

import com.zebrunner.carina.utils.config.Configuration;

public class TestRunFilterListener implements ISuiteListener {
private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final LazyInitializer<List<ITestFilter>> FILTERS = new LazyInitializer<>() {
@Override
protected List<ITestFilter> initialize() throws ConcurrentException {
List<ITestFilter> filters = List.of(new CountryFilter(), new MethodsFilter());
List<String> usedFilters = Configuration.get("test_run_filters")
.map(StringUtils::lowerCase)
.map(p -> StringUtils.split(p, ","))
.map(Arrays::asList)
.orElse(List.of());

if (usedFilters.isEmpty()) {
return List.of();
}
return filters.stream()
.filter(filter -> usedFilters.contains(filter.key()))
.collect(Collectors.toList());
}
};

@Override
public void onStart(ISuite suite) {
try {
List<ITestFilter> filters = FILTERS.get();
if (filters.isEmpty()) {
return;
}

for (ITestNGMethod testMethod : suite.getAllMethods().stream().filter(ITestNGMethod::isTest)
.collect(Collectors.toList())) {
boolean isPerform = filters.stream()
.allMatch(filter -> filter.isPerform(testMethod));

if (!isPerform) {
LOGGER.info("Disable test: [{}]", testMethod.getMethodName());
testMethod.setInvocationCount(0);
}
}
} catch (ConcurrentException e) {
ExceptionUtils.rethrow(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import org.testng.ITestNGMethod;

import com.zebrunner.carina.core.config.TestConfiguration;
import com.zebrunner.carina.core.filter.Filter;
import com.zebrunner.carina.core.filter.IFilter;
import com.zebrunner.carina.core.filter.rule.Rule;
import com.zebrunner.carina.core.filter.v1.Filter;
import com.zebrunner.carina.core.filter.v1.IFilter;
import com.zebrunner.carina.core.filter.v1.rule.Rule;
import com.zebrunner.carina.utils.commons.SpecialKeywords;
import com.zebrunner.carina.utils.config.Configuration;

Expand Down

0 comments on commit d79a99a

Please sign in to comment.