diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..e1d2299cc5 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,10 @@ +* text=auto +*.java text +*.md text +*.sql text +*.sh text eol=lf +*.xml text +*.yaml text +*.yml text +*.bat text eol=crlf +/CHANGES.txt merge=union diff --git a/bin/junitconverter.bat b/bin/junitconverter.bat index 0abcd0bda2..44911dbed5 100644 --- a/bin/junitconverter.bat +++ b/bin/junitconverter.bat @@ -1,6 +1,6 @@ -set ROOT=c:\java\TestNG -set JAR=%ROOT%\testng-4.5-jdk15.jar;%ROOT%\test\build - rem set JAR=%ROOT%\testng-4.5-jdk14.jar;%ROOT%\test-14\build - -java -ea -classpath %ROOT%\3rdparty\junit.jar;%JAVA_HOME%\lib\tools.jar;%JAR%;%CLASSPATH% org.testng.JUnitConverter -restore -overwrite -annotation -srcdir src - +set ROOT=c:\java\TestNG +set JAR=%ROOT%\testng-4.5-jdk15.jar;%ROOT%\test\build + rem set JAR=%ROOT%\testng-4.5-jdk14.jar;%ROOT%\test-14\build + +java -ea -classpath %ROOT%\3rdparty\junit.jar;%JAVA_HOME%\lib\tools.jar;%JAR%;%CLASSPATH% org.testng.JUnitConverter -restore -overwrite -annotation -srcdir src + diff --git a/bin/slave.bat b/bin/slave.bat index 24d2fb1b50..13a5972bf8 100644 --- a/bin/slave.bat +++ b/bin/slave.bat @@ -1 +1 @@ -testng -d client-output -slave %1 %2 %3 +testng -d client-output -slave %1 %2 %3 diff --git a/bin/testng.bat b/bin/testng.bat index 8ea1edff95..42cb89de22 100644 --- a/bin/testng.bat +++ b/bin/testng.bat @@ -1,5 +1,5 @@ -set ROOT=c:\java\TestNG -set JAR=%ROOT%\testng-5.2beta-jdk15.jar;%ROOT%\test\build - rem set JAR=%ROOT%\testng-4.5-jdk14.jar;%ROOT%\test-14\build - -java -ea -classpath %ROOT%\3rdparty\junit.jar;%JAVA_HOME%\lib\tools.jar;%JAR%;%CLASSPATH% org.testng.TestNG %1 %2 %3 %4 %5 %6 %7 %8 %9 +set ROOT=c:\java\TestNG +set JAR=%ROOT%\testng-5.2beta-jdk15.jar;%ROOT%\test\build + rem set JAR=%ROOT%\testng-4.5-jdk14.jar;%ROOT%\test-14\build + +java -ea -classpath %ROOT%\3rdparty\junit.jar;%JAVA_HOME%\lib\tools.jar;%JAR%;%CLASSPATH% org.testng.TestNG %1 %2 %3 %4 %5 %6 %7 %8 %9 diff --git a/gradlew.bat b/gradlew.bat index ac1b06f938..107acd32c4 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,89 +1,89 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem - -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto execute - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto execute - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/testng-asserts/src/test/java/org/testng/AssertTest.java b/testng-asserts/src/test/java/org/testng/AssertTest.java index e71c469fa9..5102a60f14 100644 --- a/testng-asserts/src/test/java/org/testng/AssertTest.java +++ b/testng-asserts/src/test/java/org/testng/AssertTest.java @@ -1,606 +1,606 @@ -package org.testng; - -import java.util.*; - -import org.testng.annotations.Test; -import org.testng.collections.Lists; -import org.testng.collections.Maps; -import org.testng.collections.Sets; - -import com.google.common.collect.ImmutableMap; - -import testhelper.PerformanceUtils; - -public class AssertTest { - @Test - public void nullObjectArrayAssertEquals() { - Object[] expected= null; - Object[] actual= null; - Assert.assertEquals(actual, expected); - } - - @Test - public void nullObjectArrayAssertNoOrder() { - Object[] expected= null; - Object[] actual= null; - Assert.assertEqualsNoOrder(actual, expected); - } - - @Test - public void nullCollectionAssertEquals() { - Collection expected = null; - Collection actual = null; - Assert.assertEquals(actual, expected); - } - - @Test - public void nullSetAssertEquals() { - Set expected = null; - Set actual = null; - Assert.assertEquals(actual, expected); - } - - @Test - public void testCollectionAssertEquals() { - final Collection expected = Sets.newHashSet( - new Asymmetric(10, 'a'), - new Asymmetric(11, 'b') - ); - final Collection actual = Sets.newHashSet( - new Asymmetric(10, 'a'), - new Asymmetric(11, 'b') - ); - Assert.assertEquals(actual, expected); - } - - @Test(description = "GITHUB-2483", expectedExceptions = AssertionError.class) - public void testAsymetricNotEquals(){ - AsymetricEquals equalsSame = AsymetricEquals.equalsSame(); - AsymetricEquals equalsAll = AsymetricEquals.equalsAll(); - //sanity - Assert.assertFalse(equalsSame.equals(equalsAll)); - Assert.assertTrue(equalsAll.equals(equalsSame)); - //actual check - Assert.assertNotEquals(equalsSame, equalsAll); - } - - @Test - public void testListAssertNotEquals() { - final Collection expected = Lists.newArrayList(new Asymmetric(10, 'a'), new Asymmetric(11, 'b')); - final Collection actual = Lists.newArrayList(new Asymmetric(11, 'b'), new Asymmetric(10, 'a')); - Assert.assertNotEquals(actual, expected); - } - - @Test - public void testSetAssertEquals() { - final Set expected = Sets.newHashSet( - new Asymmetric(10, 'a'), - new Asymmetric(11, 'b') - ); - final Set actual = Sets.newHashSet( - new Asymmetric(10, 'a'), - new Asymmetric(11, 'b') - ); - Assert.assertEquals(actual, expected); - } - - @Test - public void nullMapAssertEquals() { - Map expected = null; - Map actual = null; - Assert.assertEquals(actual, expected); - } - - @Test - public void setAssertEquals() { - Set expected = Sets.newHashSet("a", 1); - Set actual = Sets.newHashSet("a", 1); - - Assert.assertEquals(actual, expected); - } - - @Test - public void mapAssertEquals() { - Map expected = Maps.newHashMap(); - Map actual = Maps.newHashMap(); - - expected.put(null, "a"); - expected.put("a", "a"); - expected.put("b", "c"); - actual.put("b", "c"); - actual.put(null, "a"); - actual.put("a", "a"); - - Assert.assertEquals(actual, expected); - } - - @Test(description = "GITHUB-2211") - public void mapAssertEqualsWithMessage() { - String expected = "User provided message"; - String actual = ""; - try { - Assert.assertEquals(ImmutableMap.of("k", "v"), ImmutableMap.of(), expected); - } catch (AssertionError error) { - actual = error.getMessage(); - } - Assert.assertEquals(actual, expected); - } - - @Test - public void oneNullMapAssertEquals() { - Map expected = Maps.newHashMap(); - Map actual = null; - try { - Assert.assertEquals(actual, expected); - Assert.fail("AssertEquals didn't fail"); - } - catch (AssertionError error) { - //do nothing - } - } - - @Test - public void oneNullSetAssertEquals() { - Set expected = null; - Set actual = Sets.newHashSet(); - try { - Assert.assertEquals(actual, expected); - Assert.fail("AssertEquals didn't fail"); - } - catch (AssertionError error) { - //do nothing - } - } - - /** - * Testing comparison algorithm using big arrays. - * - * @see Issue #1384 – Huge performance issue between 6.5.2 - * and 6.11 - */ - @Test - public void compareLargeArrays() { - int length = 1024 * 100; - byte[] first = new byte[length]; - byte[] second = new byte[length]; - - Random rnd = new Random(); - rnd.nextBytes(first); - System.arraycopy(first, 0, second, 0, length); - - long before = PerformanceUtils.measureAllocatedMemory(); - Assert.assertEquals(first, second); - long memoryUsage = PerformanceUtils.measureAllocatedMemory() - before; - - // assertEquals() with primitive type arrays requires ~65Kb of memory - // assertEquals() with Object-type requires ~3Mb of memory when comparing 100Kb arrays. - // choosing 100Kb as a threshold - Assert.assertTrue(memoryUsage < 100 * 1024, "Amount of used memory should be approximately 65Kb"); - } - - @Test - public void compareShortArrays() { - short[] actual = {Short.MIN_VALUE, 0, Short.MAX_VALUE}; - short[] expected = {Short.MIN_VALUE, 0, Short.MAX_VALUE}; - Assert.assertEquals(actual, expected); - } - - @Test - public void compareIntArrays() { - int[] actual = {Integer.MIN_VALUE, 0, Integer.MAX_VALUE}; - int[] expected = {Integer.MIN_VALUE, 0, Integer.MAX_VALUE}; - Assert.assertEquals(actual, expected); - } - - @Test - public void compareLongArrays() { - long[] actual = {Long.MIN_VALUE, 0, Long.MAX_VALUE}; - long[] expected = {Long.MIN_VALUE, 0, Long.MAX_VALUE}; - Assert.assertEquals(actual, expected); - } - - @Test - public void compareBooleanArrays() { - boolean[] actual = {true, false}; - boolean[] expected = {true, false}; - Assert.assertEquals(actual, expected); - } - - @Test - public void compareCharacterArrays() { - char[] actual = {'a', '1', '#'}; - char[] expected = {'a', '1', '#'}; - Assert.assertEquals(actual, expected); - } - - @Test - public void compareFloatArrays() { - float[] actual = {(float) Math.PI, (float) Math.E, Float.MIN_VALUE, Float.MIN_NORMAL, Float.MAX_VALUE, - Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY}; - float[] expected = {(float) Math.PI, (float) Math.E, Float.MIN_VALUE, Float.MIN_NORMAL, Float.MAX_VALUE, - Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY}; - Assert.assertEquals(actual, expected); - } - - @Test - public void compareFloatArraysWithDelta() { - float[] actual = {0.1f, 0.2f, 0.3f, 0.4f}; - float[] expected = {0.5f, 0.7f, 0.1f, 0.2f}; - Assert.assertEquals(actual, expected, 0.5f); - } - - @Test(expectedExceptions = AssertionError.class) - public void compareUnEqualFloatArraysWithDelta() { - float[] actual = {0.1f, 0.2f, 0.3f, 0.4f}; - float[] expected = {0.5f, 0.7f, 0.1f, 0.2f}; - Assert.assertEquals(actual, expected, 0.1f); - } - - @Test - public void compareFloatArraysWithNaNValues() { - Assert.assertEquals(new float[] { Float.NaN }, new float[] { Float.NaN }); - } - - @Test - public void compareFloatWithNaNValues() { - Assert.assertEquals(Float.NaN, Float.NaN); - } - - @Test - public void compareDoubleArrays() { - double[] actual = {Math.PI, Math.E, Double.MIN_VALUE, Double.MIN_NORMAL, Double.MAX_VALUE, - Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY}; - double[] expected = {Math.PI, Math.E, Double.MIN_VALUE, Double.MIN_NORMAL, Double.MAX_VALUE, - Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY}; - Assert.assertEquals(actual, expected); - } - - @Test - public void compareDoubleArraysWithNaNValues() { - Assert.assertEquals(new double[] { Double.NaN }, new double[] { Double.NaN }); - } - - @Test - public void compareDoubleWithNaNValues() { - Assert.assertEquals(Double.NaN, Double.NaN); - } - - @Test - public void compareDoubleArraysWithDelta() { - double[] actual = {0.1d, 0.2d, 0.3d, 0.4d}; - double[] expected = {0.5d, 0.7d, 0.1d, 0.2d}; - Assert.assertEquals(actual, expected, 0.5d); - } - - @Test(expectedExceptions = AssertionError.class) - public void compareUnEqualDoubleArraysWithDelta() { - double[] actual = {0.1d, 0.2d, 0.3d, 0.4d}; - double[] expected = {0.5d, 0.7d, 0.1d, 0.2d}; - Assert.assertEquals(actual, expected, 0.1d); - } - - @SuppressWarnings("serial") - @Test(expectedExceptions = AssertionError.class) - public void assertEqualsMapShouldFail() { - Map mapActual = new HashMap() {{ - put("a","1"); - }}; - Map mapExpected = new HashMap() {{ - put("a","1"); - put("b","2"); - }}; - - Assert.assertEquals(mapActual, mapExpected); - } - - @Test(expectedExceptions = AssertionError.class) - public void assertEqualsSymmetricScalar() { - Assert.assertEquals(new Asymmetric(42, 'd'), new Contrived(42)); - } - - @Test(expectedExceptions = AssertionError.class) - public void assertEqualsSymmetricArrays() { - Object[] actual = {1, new Asymmetric(42, 'd'), "inDay"}; - Object[] expected = {1, new Contrived(42), "inDay"}; - Assert.assertEquals(actual, expected); - } - - @Test(expectedExceptions = AssertionError.class) - public void testAssertEqualsWithActualBrokenEqualsTrue() { - // BrokenEqualsTrue.equals(Object) always returns true, even for null - // However, Assert is not supposed to check equals(Object) implementation, so assertEquals should fail - Assert.assertEquals(new BrokenEqualsTrue(), null); - } - - @Test(expectedExceptions = AssertionError.class) - public void testAssertEqualsWithExpectedBrokenEqualsTrue() { - // BrokenEqualsTrue.equals(Object) always returns true, even for null - // However, Assert is not supposed to check equals(Object) implementation, so assertEquals should fail - Assert.assertEquals(null, new BrokenEqualsTrue()); - } - - @Test - public void testAssertEqualsWithNull() { - Object obj = null; - Assert.assertEquals(obj, obj); - } - - @Test(description = "GITHUB-1935", expectedExceptions = AssertionError.class, - expectedExceptionsMessageRegExp = "expected \\[y\\] but found \\[x\\]") - public void testInequalityMessage() { - Assert.assertEquals("x", "y"); - } - - @Test - public void testAssertEqualsNoOrder() { - String[] actual = {"a", "b"}; - String[] expected = {"b", "a"}; - Assert.assertEqualsNoOrder(actual, expected); - } - - @Test - public void testAssertEqualsNoOrderWithEmpty() { - Assert.assertEqualsNoOrder(new String[0], new String[0]); - } - - @Test - public void testAssertEqualsNoOrderWithDifferentElementType() { - Assert.assertEqualsNoOrder(new String[0], new Object[0]); - Object[] actual = {"a"}; - String[] expected = {"a"}; - Assert.assertEqualsNoOrder(actual, expected); - } - - @Test(expectedExceptions = AssertionError.class) - public void testAssertEqualsNoOrderWithDuplicate() { - String[] actual = {"a"}; - String[] expected = {"a", "a"}; - Assert.assertEqualsNoOrder(actual, expected); - } - - @Test - public void testAssertEqualsNoOrderWithBothNull() { - Assert.assertEqualsNoOrder((Object[]) null, (Object[]) null); - } - - @Test(expectedExceptions = AssertionError.class) - public void testAssertEqualsNoOrderWithActualNull() { - Assert.assertEqualsNoOrder(null, new String[0]); - } - - @Test(expectedExceptions = AssertionError.class) - public void testAssertEqualsNoOrderWithExpectedNull() { - Assert.assertEqualsNoOrder(new String[0], null); - } - - @Test(description = "GITHUB-2500", expectedExceptions = AssertionError.class) - public void testAssertEqualsNoOrderNotDeep() { - // assertEqualsNoOrder does not compare arrays deeply, so elements which are - // arrays as well are only compared for reference equality - String[][] actual = {{}}; - String[][] expected = {{}}; - Assert.assertEqualsNoOrder(actual, expected); - } - - @Test(description = "GITHUB-2080", expectedExceptions = AssertionError.class, - expectedExceptionsMessageRegExp = "test expected \\[true\\] but found \\[false\\]") - public void testAssertTrueMessage() { - Assert.assertTrue(false, "test"); - } - - @Test(description = "GITHUB-2080", expectedExceptions = AssertionError.class, - expectedExceptionsMessageRegExp = "test expected \\[false\\] but found \\[true\\]") - public void testAssertFalseMessage() { - Assert.assertFalse(true, "test"); - } - - @Test(description = "GITHUB-2080", expectedExceptions = AssertionError.class, - expectedExceptionsMessageRegExp = "did not expect \\[x\\] but found \\[x\\]") - public void testEqualityMessage() { - Assert.assertNotEquals("x", "x"); - } - - @Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class, - expectedExceptionsMessageRegExp = "did not expect \\[1.5\\] but found \\[1.5\\]") - public void testAssertNotEqualsFloat() { - Assert.assertNotEquals(1.5f, 1.5f, 0.5f); - } - - @Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class, - expectedExceptionsMessageRegExp = "did not expect \\[1.5\\] but found \\[1.5\\]") - public void testAssertNotEqualsDouble() { - Assert.assertNotEquals(1.5, 1.5, 0.5); - } - - @Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class, - expectedExceptionsMessageRegExp = "did not expect \\[\\[test\\]\\] but found \\[\\[test\\]\\]") - public void testAssertNotEqualsSet() { - Set set = new HashSet<>(); - set.add("test"); - Assert.assertNotEquals(set, set); - } - - @Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class, - expectedExceptionsMessageRegExp = "did not expect \\[\\[test\\]\\] but found \\[\\[test\\]\\]") - public void testAssertNotEqualsDeepSet() { - Set set = new HashSet<>(); - set.add("test"); - Assert.assertNotEqualsDeep(set, set); - } - - @Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class, - expectedExceptionsMessageRegExp = "did not expect \\[\\{key=value\\}\\] but found \\[\\{key=value\\}\\]") - public void testAssertNotEqualsMap() { - Map map = new HashMap<>(); - map.put("key", "value"); - Assert.assertNotEquals(map, map); - } - - @Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class, - expectedExceptionsMessageRegExp = "did not expect \\[\\{key=value\\}\\] but found \\[\\{key=value\\}\\]") - public void testAssertNotEqualsDeepMap() { - Map map = new HashMap<>(); - map.put("key", "value"); - Assert.assertNotEqualsDeep(map, map); - } - - @Test(description = "GITHUB-2486") - public void testAssertNotSame() { - Contrived object = new Contrived(2); - Contrived object2 = new Contrived(2); - Assert.assertNotSame(object2, object); - } - - @Test(description = "GITHUB-2486", expectedExceptions = AssertionError.class) - public void testAssertSame() { - Contrived object = new Contrived(2); - Contrived object2 = new Contrived(2); - Assert.assertSame(object2, object); - } - - @Test(description = "GITHUB-2490") - public void testAssertNotEqualsWithActualBrokenEqualsTrue() { - // BrokenEqualsTrue.equals(Object) always returns true, even for null - // However, Assert is not supposed to check equals(Object) implementation, so assertNotEquals should succeed - Assert.assertNotEquals(new BrokenEqualsTrue(), null); - } - - @Test(description = "GITHUB-2490") - public void testAssertNotEqualsWithExpectedBrokenEqualsTrue() { - // BrokenEqualsTrue.equals(Object) always returns true, even for null - // However, Assert is not supposed to check equals(Object) implementation, so assertNotEquals should succeed - Assert.assertNotEquals(null, new BrokenEqualsTrue()); - } - - @Test(description = "GITHUB-2490") - public void testAssertNotEqualsWithActualBrokenEqualsFalse() { - // BrokenEqualsFalse.equals(Object) always returns false; assertNotEquals should fail - Assert.assertNotEquals(new BrokenEqualsFalse(), null); - } - - @Test(description = "GITHUB-2490") - public void testAssertNotEqualsWithExpectedBrokenEqualsFalse() { - // BrokenEqualsFalse.equals(Object) always returns false; assertNotEquals should fail - Assert.assertNotEquals(null, new BrokenEqualsFalse()); - } - - @Test(description = "GITHUB-2490") - public void testAssertNotEqualsWithExpectedNullAndNewObject() { - Assert.assertNotEquals(new Object(), null); - } - - @Test - public void testAssertNotEqualsWithActualNullExceptedEmptyString() { - Assert.assertNotEquals(null, ""); - } - - @Test - public void testAssertNotEqualsWithActualEmptyStringExceptedNull() { - Assert.assertNotEquals("", null); - } - - @Test(expectedExceptions = AssertionError.class) - public void testAssertNotEqualsWithNull() { - Object obj = null; - Assert.assertNotEquals(obj, obj); - } - - class Contrived { - - int integer; - - Contrived(int integer){ - this.integer = integer; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof Contrived)) return false; - - Contrived contrived = (Contrived) o; - - return integer == contrived.integer; - } - - @Override - public int hashCode() { - return integer; - } - } - - class Asymmetric extends Contrived { - - char character; - - Asymmetric(int integer, char character) { - super(integer); - this.character = character; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof Asymmetric)) return false; - if (!super.equals(o)) return false; - - Asymmetric that = (Asymmetric) o; - - return character == that.character; - } - - @Override - public int hashCode() { - int result = super.hashCode(); - result = 31 * result + (int) character; - return result; - } - } - - static class AsymetricEquals { - - static AsymetricEquals equalsAll(){ - return new AsymetricEquals(null); - } - - static AsymetricEquals equalsSame(){ - return new AsymetricEquals(new Object()); - } - - Object value; - - private AsymetricEquals(Object value) { - this.value = value; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof AsymetricEquals)) return false; - - AsymetricEquals that = (AsymetricEquals) o; - if (value == null) - return true; - return value.equals(that.value); - } - - @Override - public int hashCode() { - return Objects.hashCode(value); - } - } - - static class BrokenEqualsTrue { - @Override - public boolean equals(Object o) { - return true; // broken implementation - } - } - - static class BrokenEqualsFalse { - @Override - public boolean equals(Object o) { - return false; // broken implementation - } - } -} +package org.testng; + +import java.util.*; + +import org.testng.annotations.Test; +import org.testng.collections.Lists; +import org.testng.collections.Maps; +import org.testng.collections.Sets; + +import com.google.common.collect.ImmutableMap; + +import testhelper.PerformanceUtils; + +public class AssertTest { + @Test + public void nullObjectArrayAssertEquals() { + Object[] expected= null; + Object[] actual= null; + Assert.assertEquals(actual, expected); + } + + @Test + public void nullObjectArrayAssertNoOrder() { + Object[] expected= null; + Object[] actual= null; + Assert.assertEqualsNoOrder(actual, expected); + } + + @Test + public void nullCollectionAssertEquals() { + Collection expected = null; + Collection actual = null; + Assert.assertEquals(actual, expected); + } + + @Test + public void nullSetAssertEquals() { + Set expected = null; + Set actual = null; + Assert.assertEquals(actual, expected); + } + + @Test + public void testCollectionAssertEquals() { + final Collection expected = Sets.newHashSet( + new Asymmetric(10, 'a'), + new Asymmetric(11, 'b') + ); + final Collection actual = Sets.newHashSet( + new Asymmetric(10, 'a'), + new Asymmetric(11, 'b') + ); + Assert.assertEquals(actual, expected); + } + + @Test(description = "GITHUB-2483", expectedExceptions = AssertionError.class) + public void testAsymetricNotEquals(){ + AsymetricEquals equalsSame = AsymetricEquals.equalsSame(); + AsymetricEquals equalsAll = AsymetricEquals.equalsAll(); + //sanity + Assert.assertFalse(equalsSame.equals(equalsAll)); + Assert.assertTrue(equalsAll.equals(equalsSame)); + //actual check + Assert.assertNotEquals(equalsSame, equalsAll); + } + + @Test + public void testListAssertNotEquals() { + final Collection expected = Lists.newArrayList(new Asymmetric(10, 'a'), new Asymmetric(11, 'b')); + final Collection actual = Lists.newArrayList(new Asymmetric(11, 'b'), new Asymmetric(10, 'a')); + Assert.assertNotEquals(actual, expected); + } + + @Test + public void testSetAssertEquals() { + final Set expected = Sets.newHashSet( + new Asymmetric(10, 'a'), + new Asymmetric(11, 'b') + ); + final Set actual = Sets.newHashSet( + new Asymmetric(10, 'a'), + new Asymmetric(11, 'b') + ); + Assert.assertEquals(actual, expected); + } + + @Test + public void nullMapAssertEquals() { + Map expected = null; + Map actual = null; + Assert.assertEquals(actual, expected); + } + + @Test + public void setAssertEquals() { + Set expected = Sets.newHashSet("a", 1); + Set actual = Sets.newHashSet("a", 1); + + Assert.assertEquals(actual, expected); + } + + @Test + public void mapAssertEquals() { + Map expected = Maps.newHashMap(); + Map actual = Maps.newHashMap(); + + expected.put(null, "a"); + expected.put("a", "a"); + expected.put("b", "c"); + actual.put("b", "c"); + actual.put(null, "a"); + actual.put("a", "a"); + + Assert.assertEquals(actual, expected); + } + + @Test(description = "GITHUB-2211") + public void mapAssertEqualsWithMessage() { + String expected = "User provided message"; + String actual = ""; + try { + Assert.assertEquals(ImmutableMap.of("k", "v"), ImmutableMap.of(), expected); + } catch (AssertionError error) { + actual = error.getMessage(); + } + Assert.assertEquals(actual, expected); + } + + @Test + public void oneNullMapAssertEquals() { + Map expected = Maps.newHashMap(); + Map actual = null; + try { + Assert.assertEquals(actual, expected); + Assert.fail("AssertEquals didn't fail"); + } + catch (AssertionError error) { + //do nothing + } + } + + @Test + public void oneNullSetAssertEquals() { + Set expected = null; + Set actual = Sets.newHashSet(); + try { + Assert.assertEquals(actual, expected); + Assert.fail("AssertEquals didn't fail"); + } + catch (AssertionError error) { + //do nothing + } + } + + /** + * Testing comparison algorithm using big arrays. + * + * @see Issue #1384 – Huge performance issue between 6.5.2 + * and 6.11 + */ + @Test + public void compareLargeArrays() { + int length = 1024 * 100; + byte[] first = new byte[length]; + byte[] second = new byte[length]; + + Random rnd = new Random(); + rnd.nextBytes(first); + System.arraycopy(first, 0, second, 0, length); + + long before = PerformanceUtils.measureAllocatedMemory(); + Assert.assertEquals(first, second); + long memoryUsage = PerformanceUtils.measureAllocatedMemory() - before; + + // assertEquals() with primitive type arrays requires ~65Kb of memory + // assertEquals() with Object-type requires ~3Mb of memory when comparing 100Kb arrays. + // choosing 100Kb as a threshold + Assert.assertTrue(memoryUsage < 100 * 1024, "Amount of used memory should be approximately 65Kb"); + } + + @Test + public void compareShortArrays() { + short[] actual = {Short.MIN_VALUE, 0, Short.MAX_VALUE}; + short[] expected = {Short.MIN_VALUE, 0, Short.MAX_VALUE}; + Assert.assertEquals(actual, expected); + } + + @Test + public void compareIntArrays() { + int[] actual = {Integer.MIN_VALUE, 0, Integer.MAX_VALUE}; + int[] expected = {Integer.MIN_VALUE, 0, Integer.MAX_VALUE}; + Assert.assertEquals(actual, expected); + } + + @Test + public void compareLongArrays() { + long[] actual = {Long.MIN_VALUE, 0, Long.MAX_VALUE}; + long[] expected = {Long.MIN_VALUE, 0, Long.MAX_VALUE}; + Assert.assertEquals(actual, expected); + } + + @Test + public void compareBooleanArrays() { + boolean[] actual = {true, false}; + boolean[] expected = {true, false}; + Assert.assertEquals(actual, expected); + } + + @Test + public void compareCharacterArrays() { + char[] actual = {'a', '1', '#'}; + char[] expected = {'a', '1', '#'}; + Assert.assertEquals(actual, expected); + } + + @Test + public void compareFloatArrays() { + float[] actual = {(float) Math.PI, (float) Math.E, Float.MIN_VALUE, Float.MIN_NORMAL, Float.MAX_VALUE, + Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY}; + float[] expected = {(float) Math.PI, (float) Math.E, Float.MIN_VALUE, Float.MIN_NORMAL, Float.MAX_VALUE, + Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY}; + Assert.assertEquals(actual, expected); + } + + @Test + public void compareFloatArraysWithDelta() { + float[] actual = {0.1f, 0.2f, 0.3f, 0.4f}; + float[] expected = {0.5f, 0.7f, 0.1f, 0.2f}; + Assert.assertEquals(actual, expected, 0.5f); + } + + @Test(expectedExceptions = AssertionError.class) + public void compareUnEqualFloatArraysWithDelta() { + float[] actual = {0.1f, 0.2f, 0.3f, 0.4f}; + float[] expected = {0.5f, 0.7f, 0.1f, 0.2f}; + Assert.assertEquals(actual, expected, 0.1f); + } + + @Test + public void compareFloatArraysWithNaNValues() { + Assert.assertEquals(new float[] { Float.NaN }, new float[] { Float.NaN }); + } + + @Test + public void compareFloatWithNaNValues() { + Assert.assertEquals(Float.NaN, Float.NaN); + } + + @Test + public void compareDoubleArrays() { + double[] actual = {Math.PI, Math.E, Double.MIN_VALUE, Double.MIN_NORMAL, Double.MAX_VALUE, + Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY}; + double[] expected = {Math.PI, Math.E, Double.MIN_VALUE, Double.MIN_NORMAL, Double.MAX_VALUE, + Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY}; + Assert.assertEquals(actual, expected); + } + + @Test + public void compareDoubleArraysWithNaNValues() { + Assert.assertEquals(new double[] { Double.NaN }, new double[] { Double.NaN }); + } + + @Test + public void compareDoubleWithNaNValues() { + Assert.assertEquals(Double.NaN, Double.NaN); + } + + @Test + public void compareDoubleArraysWithDelta() { + double[] actual = {0.1d, 0.2d, 0.3d, 0.4d}; + double[] expected = {0.5d, 0.7d, 0.1d, 0.2d}; + Assert.assertEquals(actual, expected, 0.5d); + } + + @Test(expectedExceptions = AssertionError.class) + public void compareUnEqualDoubleArraysWithDelta() { + double[] actual = {0.1d, 0.2d, 0.3d, 0.4d}; + double[] expected = {0.5d, 0.7d, 0.1d, 0.2d}; + Assert.assertEquals(actual, expected, 0.1d); + } + + @SuppressWarnings("serial") + @Test(expectedExceptions = AssertionError.class) + public void assertEqualsMapShouldFail() { + Map mapActual = new HashMap() {{ + put("a","1"); + }}; + Map mapExpected = new HashMap() {{ + put("a","1"); + put("b","2"); + }}; + + Assert.assertEquals(mapActual, mapExpected); + } + + @Test(expectedExceptions = AssertionError.class) + public void assertEqualsSymmetricScalar() { + Assert.assertEquals(new Asymmetric(42, 'd'), new Contrived(42)); + } + + @Test(expectedExceptions = AssertionError.class) + public void assertEqualsSymmetricArrays() { + Object[] actual = {1, new Asymmetric(42, 'd'), "inDay"}; + Object[] expected = {1, new Contrived(42), "inDay"}; + Assert.assertEquals(actual, expected); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsWithActualBrokenEqualsTrue() { + // BrokenEqualsTrue.equals(Object) always returns true, even for null + // However, Assert is not supposed to check equals(Object) implementation, so assertEquals should fail + Assert.assertEquals(new BrokenEqualsTrue(), null); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsWithExpectedBrokenEqualsTrue() { + // BrokenEqualsTrue.equals(Object) always returns true, even for null + // However, Assert is not supposed to check equals(Object) implementation, so assertEquals should fail + Assert.assertEquals(null, new BrokenEqualsTrue()); + } + + @Test + public void testAssertEqualsWithNull() { + Object obj = null; + Assert.assertEquals(obj, obj); + } + + @Test(description = "GITHUB-1935", expectedExceptions = AssertionError.class, + expectedExceptionsMessageRegExp = "expected \\[y\\] but found \\[x\\]") + public void testInequalityMessage() { + Assert.assertEquals("x", "y"); + } + + @Test + public void testAssertEqualsNoOrder() { + String[] actual = {"a", "b"}; + String[] expected = {"b", "a"}; + Assert.assertEqualsNoOrder(actual, expected); + } + + @Test + public void testAssertEqualsNoOrderWithEmpty() { + Assert.assertEqualsNoOrder(new String[0], new String[0]); + } + + @Test + public void testAssertEqualsNoOrderWithDifferentElementType() { + Assert.assertEqualsNoOrder(new String[0], new Object[0]); + Object[] actual = {"a"}; + String[] expected = {"a"}; + Assert.assertEqualsNoOrder(actual, expected); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsNoOrderWithDuplicate() { + String[] actual = {"a"}; + String[] expected = {"a", "a"}; + Assert.assertEqualsNoOrder(actual, expected); + } + + @Test + public void testAssertEqualsNoOrderWithBothNull() { + Assert.assertEqualsNoOrder((Object[]) null, (Object[]) null); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsNoOrderWithActualNull() { + Assert.assertEqualsNoOrder(null, new String[0]); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertEqualsNoOrderWithExpectedNull() { + Assert.assertEqualsNoOrder(new String[0], null); + } + + @Test(description = "GITHUB-2500", expectedExceptions = AssertionError.class) + public void testAssertEqualsNoOrderNotDeep() { + // assertEqualsNoOrder does not compare arrays deeply, so elements which are + // arrays as well are only compared for reference equality + String[][] actual = {{}}; + String[][] expected = {{}}; + Assert.assertEqualsNoOrder(actual, expected); + } + + @Test(description = "GITHUB-2080", expectedExceptions = AssertionError.class, + expectedExceptionsMessageRegExp = "test expected \\[true\\] but found \\[false\\]") + public void testAssertTrueMessage() { + Assert.assertTrue(false, "test"); + } + + @Test(description = "GITHUB-2080", expectedExceptions = AssertionError.class, + expectedExceptionsMessageRegExp = "test expected \\[false\\] but found \\[true\\]") + public void testAssertFalseMessage() { + Assert.assertFalse(true, "test"); + } + + @Test(description = "GITHUB-2080", expectedExceptions = AssertionError.class, + expectedExceptionsMessageRegExp = "did not expect \\[x\\] but found \\[x\\]") + public void testEqualityMessage() { + Assert.assertNotEquals("x", "x"); + } + + @Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class, + expectedExceptionsMessageRegExp = "did not expect \\[1.5\\] but found \\[1.5\\]") + public void testAssertNotEqualsFloat() { + Assert.assertNotEquals(1.5f, 1.5f, 0.5f); + } + + @Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class, + expectedExceptionsMessageRegExp = "did not expect \\[1.5\\] but found \\[1.5\\]") + public void testAssertNotEqualsDouble() { + Assert.assertNotEquals(1.5, 1.5, 0.5); + } + + @Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class, + expectedExceptionsMessageRegExp = "did not expect \\[\\[test\\]\\] but found \\[\\[test\\]\\]") + public void testAssertNotEqualsSet() { + Set set = new HashSet<>(); + set.add("test"); + Assert.assertNotEquals(set, set); + } + + @Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class, + expectedExceptionsMessageRegExp = "did not expect \\[\\[test\\]\\] but found \\[\\[test\\]\\]") + public void testAssertNotEqualsDeepSet() { + Set set = new HashSet<>(); + set.add("test"); + Assert.assertNotEqualsDeep(set, set); + } + + @Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class, + expectedExceptionsMessageRegExp = "did not expect \\[\\{key=value\\}\\] but found \\[\\{key=value\\}\\]") + public void testAssertNotEqualsMap() { + Map map = new HashMap<>(); + map.put("key", "value"); + Assert.assertNotEquals(map, map); + } + + @Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class, + expectedExceptionsMessageRegExp = "did not expect \\[\\{key=value\\}\\] but found \\[\\{key=value\\}\\]") + public void testAssertNotEqualsDeepMap() { + Map map = new HashMap<>(); + map.put("key", "value"); + Assert.assertNotEqualsDeep(map, map); + } + + @Test(description = "GITHUB-2486") + public void testAssertNotSame() { + Contrived object = new Contrived(2); + Contrived object2 = new Contrived(2); + Assert.assertNotSame(object2, object); + } + + @Test(description = "GITHUB-2486", expectedExceptions = AssertionError.class) + public void testAssertSame() { + Contrived object = new Contrived(2); + Contrived object2 = new Contrived(2); + Assert.assertSame(object2, object); + } + + @Test(description = "GITHUB-2490") + public void testAssertNotEqualsWithActualBrokenEqualsTrue() { + // BrokenEqualsTrue.equals(Object) always returns true, even for null + // However, Assert is not supposed to check equals(Object) implementation, so assertNotEquals should succeed + Assert.assertNotEquals(new BrokenEqualsTrue(), null); + } + + @Test(description = "GITHUB-2490") + public void testAssertNotEqualsWithExpectedBrokenEqualsTrue() { + // BrokenEqualsTrue.equals(Object) always returns true, even for null + // However, Assert is not supposed to check equals(Object) implementation, so assertNotEquals should succeed + Assert.assertNotEquals(null, new BrokenEqualsTrue()); + } + + @Test(description = "GITHUB-2490") + public void testAssertNotEqualsWithActualBrokenEqualsFalse() { + // BrokenEqualsFalse.equals(Object) always returns false; assertNotEquals should fail + Assert.assertNotEquals(new BrokenEqualsFalse(), null); + } + + @Test(description = "GITHUB-2490") + public void testAssertNotEqualsWithExpectedBrokenEqualsFalse() { + // BrokenEqualsFalse.equals(Object) always returns false; assertNotEquals should fail + Assert.assertNotEquals(null, new BrokenEqualsFalse()); + } + + @Test(description = "GITHUB-2490") + public void testAssertNotEqualsWithExpectedNullAndNewObject() { + Assert.assertNotEquals(new Object(), null); + } + + @Test + public void testAssertNotEqualsWithActualNullExceptedEmptyString() { + Assert.assertNotEquals(null, ""); + } + + @Test + public void testAssertNotEqualsWithActualEmptyStringExceptedNull() { + Assert.assertNotEquals("", null); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertNotEqualsWithNull() { + Object obj = null; + Assert.assertNotEquals(obj, obj); + } + + class Contrived { + + int integer; + + Contrived(int integer){ + this.integer = integer; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Contrived)) return false; + + Contrived contrived = (Contrived) o; + + return integer == contrived.integer; + } + + @Override + public int hashCode() { + return integer; + } + } + + class Asymmetric extends Contrived { + + char character; + + Asymmetric(int integer, char character) { + super(integer); + this.character = character; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Asymmetric)) return false; + if (!super.equals(o)) return false; + + Asymmetric that = (Asymmetric) o; + + return character == that.character; + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + (int) character; + return result; + } + } + + static class AsymetricEquals { + + static AsymetricEquals equalsAll(){ + return new AsymetricEquals(null); + } + + static AsymetricEquals equalsSame(){ + return new AsymetricEquals(new Object()); + } + + Object value; + + private AsymetricEquals(Object value) { + this.value = value; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof AsymetricEquals)) return false; + + AsymetricEquals that = (AsymetricEquals) o; + if (value == null) + return true; + return value.equals(that.value); + } + + @Override + public int hashCode() { + return Objects.hashCode(value); + } + } + + static class BrokenEqualsTrue { + @Override + public boolean equals(Object o) { + return true; // broken implementation + } + } + + static class BrokenEqualsFalse { + @Override + public boolean equals(Object o) { + return false; // broken implementation + } + } +} diff --git a/testng-asserts/src/test/java/test/asserttests/ArrayEqualityAssertTest.java b/testng-asserts/src/test/java/test/asserttests/ArrayEqualityAssertTest.java index 5dd9ae8b56..3912a9ef7a 100644 --- a/testng-asserts/src/test/java/test/asserttests/ArrayEqualityAssertTest.java +++ b/testng-asserts/src/test/java/test/asserttests/ArrayEqualityAssertTest.java @@ -1,336 +1,336 @@ -package test.asserttests; - -import org.testng.annotations.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertEqualsDeep; -import static org.testng.Assert.assertNotEquals; -import static org.testng.Assert.assertNotEqualsDeep; - -/** Tests different equality cases for nested collections and arrays. */ -public class ArrayEqualityAssertTest { - - @Test - public void arrayAssertEquals() { - assertEquals( - new int[] {42}, - new int[] {42}, - "arrays of primitives are compared by value in assertEquals"); - } - - @Test(expectedExceptions = AssertionError.class) - public void arrayAssertNotEquals() { - assertNotEquals( - new int[] {42}, - new int[] {42}, - "arrays of primitives are compared by value in assertNotEquals"); - } - - @Test - public void boxedArrayAssertEquals() { - assertEquals( - new Integer[] {42}, - new Integer[] {42}, - "arrays of wrapped values are compared by value in assertEquals"); - } - - @Test(expectedExceptions = AssertionError.class) - public void boxedArrayAssertNotEquals() { - assertNotEquals( - new Integer[] {42}, - new Integer[] {42}, - "arrays of wrapped values are compared by value in assertNotEquals"); - } - - @Test - public void mixedArraysAssertEquals() { - assertEquals( - new int[] {42}, - new Integer[] {42}, - "arrays of wrapped values are compared by value in assertEquals"); - assertEquals( - new Integer[] {42}, - new int[] {42}, - "arrays of wrapped values are compared by value in assertEquals"); - } - - @Test(expectedExceptions = AssertionError.class) - public void mixedArraysAssertNotEquals() { - assertNotEquals( - new int[] {42}, - new Integer[] {42}, - "arrays of wrapped values are compared by value in assertNotEquals"); - assertNotEquals( - new Integer[] {42}, - new int[] {42}, - "arrays of wrapped values are compared by value in assertNotEquals"); - } - - @Test(expectedExceptions = AssertionError.class) - public void arrayInsideListAssertEquals() { - List list = Arrays.asList(new int[] {42}); - List listCopy = Arrays.asList(new int[] {42}); - assertEquals(list, listCopy, "arrays inside lists are compared by reference in assertEquals"); - } - - @Test - public void arrayInsideListAssertNotEquals() { - List list = Arrays.asList(new int[] {42}); - List listCopy = Arrays.asList(new int[] {42}); - assertNotEquals( - list, listCopy, "arrays inside lists are compared by reference in assertNotEquals"); - } - - @Test(expectedExceptions = AssertionError.class) - public void arrayInsideMapAssertEquals() { - Map map = new HashMap<>(); - map.put("array", new int[] {42}); - Map mapCopy = new HashMap<>(); - mapCopy.put("array", new int[] {42}); - - // arrays inside maps are compared by reference in assertEquals(Map,Map) - assertEquals(map, mapCopy); - } - - @Test(expectedExceptions = AssertionError.class) - public void arrayInsideMapAssertEqualsWithMessage() { - Map map = new HashMap<>(); - map.put("array", new int[] {42}); - Map mapCopy = new HashMap<>(); - mapCopy.put("array", new int[] {42}); - - assertEquals( - map, - mapCopy, - "arrays inside maps are compared by reference in assertEquals(Map,Map,String)"); - } - - @Test - public void arrayInsideMapAssertNotEquals() { - Map map = new HashMap<>(); - map.put("array", new int[] {42}); - Map mapCopy = new HashMap<>(); - mapCopy.put("array", new int[] {42}); - - assertNotEquals( - map, mapCopy, "arrays inside maps are compared by reference in assertNotEquals"); - } - - @Test - public void arrayInsideMapAssertEqualsDeep() { - Map map = new HashMap<>(); - map.put("array", new int[] {42}); - Map mapCopy = new HashMap<>(); - mapCopy.put("array", new int[] {42}); - - // arrays inside maps are compared by value in assertEqualsDeep(Map,Map) - assertEqualsDeep(map, mapCopy); - } - - @Test - public void arrayInsideMapAssertEqualsDeepWithMessage() { - Map map = new HashMap<>(); - map.put("array", new int[] {42}); - Map mapCopy = new HashMap<>(); - mapCopy.put("array", new int[] {42}); - - assertEqualsDeep( - map, - mapCopy, - "arrays inside maps are compared by value in assertEqualsDeep(Map,Map,String)"); - } - - @Test(expectedExceptions = AssertionError.class) - public void arrayInsideMapAssertNotEqualsDeep() { - Map map = new HashMap<>(); - map.put("array", new int[] {42}); - Map mapCopy = new HashMap<>(); - mapCopy.put("array", new int[] {42}); - - assertNotEqualsDeep( - map, mapCopy, "arrays inside maps are compared by value in assertNotEqualsDeep"); - } - - @Test(expectedExceptions = AssertionError.class) - public void arrayInsideSetAssertEquals() { - Set set = new HashSet<>(); - set.add(new int[] {42}); - Set setCopy = new HashSet<>(); - setCopy.add(new int[] {42}); - - assertEquals(set, setCopy, "arrays inside sets are compared by reference in assertNotEquals"); - } - - @Test - public void arrayInsideSetAssertNotEquals() { - Set set = new HashSet<>(); - set.add(new int[] {42}); - Set setCopy = new HashSet<>(); - setCopy.add(new int[] {42}); - - assertNotEquals( - set, setCopy, "arrays inside sets are compared by reference in assertNotEquals"); - } - - @Test - public void arrayInsideSetAssertEqualsDeep() { - Set set = new HashSet<>(); - set.add(new int[] {42}); - Set setCopy = new HashSet<>(); - setCopy.add(new int[] {42}); - - assertEqualsDeep(set, setCopy, "arrays inside sets are compared by value in assertEqualsDeep"); - } - - @Test(expectedExceptions = AssertionError.class) - public void arrayDeepInSetsAssertEqualsForExpectedLessThanActual() { - Set actual = new HashSet<>(); - actual.add("42"); - actual.add("43"); - Set expected = new HashSet<>(); - expected.add("42"); - - assertEqualsDeep(actual, expected, "Sets not equal:"); - } - - @Test(expectedExceptions = AssertionError.class) - public void arrayDeepInSetsAssertEqualsForActualLessThanExpected() { - Set actual = new HashSet<>(); - actual.add("42"); - Set expected = new HashSet<>(); - expected.add("42"); - expected.add("43"); - - assertEqualsDeep(actual, expected, "Sets not equal:"); - } - - @Test(expectedExceptions = AssertionError.class) - public void arrayInsideSetAssertNotEqualsDeep() { - Set set = new HashSet<>(); - set.add(new int[] {42}); - Set setCopy = new HashSet<>(); - setCopy.add(new int[] {42}); - - assertNotEqualsDeep( - set, setCopy, "arrays inside sets are compared by value in assertNotEqualsDeep"); - } - - @Test(expectedExceptions = AssertionError.class) - public void arrayDeepInListsAssertEquals() { - List> list = Collections.singletonList(Arrays.asList(new int[] {42})); - List> listCopy = Collections.singletonList(Arrays.asList(new int[] {42})); - - assertEquals( - list, - listCopy, - "arrays inside lists which are inside lists themselves are compared by reference in assertEquals"); - } - - @Test(expectedExceptions = AssertionError.class) - public void arrayDeepInMapsAssertEquals() { - Map> map = new HashMap<>(); - Map innerMap = new HashMap<>(); - innerMap.put("array", new int[] {42}); - map.put("map", innerMap); - Map> mapCopy = new HashMap<>(); - Map innerMapCopy = new HashMap<>(); - innerMapCopy.put("array", new int[] {42}); - mapCopy.put("map", innerMapCopy); - - assertEquals( - map, - mapCopy, - "arrays inside maps which are inside maps themselves are compared by reference in assertEquals"); - } - - @Test(expectedExceptions = AssertionError.class) - public void arrayDeepInListAndMapAssertEquals() { - List> list = new ArrayList<>(); - Map innerMap = new HashMap<>(); - innerMap.put("array", new int[] {42}); - list.add(innerMap); - List> listCopy = new ArrayList<>(); - Map innerMapCopy = new HashMap<>(); - innerMapCopy.put("array", new int[] {42}); - list.add(innerMapCopy); - - assertEquals( - list, - listCopy, - "arrays inside maps which are inside lists themselves are compared by reference in assertEquals"); - } - - @Test(expectedExceptions = AssertionError.class) - public void arrayDeepInMapAndListAssertEquals() { - Map> map = new HashMap<>(); - map.put("list", Arrays.asList(new int[] {42})); - Map> mapCopy = new HashMap<>(); - mapCopy.put("list", Arrays.asList(new int[] {42})); - - assertEquals( - map, - mapCopy, - "arrays inside maps which are inside lists themselves are compared by reference in assertEquals"); - } - - @Test(expectedExceptions = AssertionError.class) - public void arrayInsideIterableAssertEquals() { - Iterable iterable = Arrays.asList(new int[] {42}); - Iterable iterableCopy = Arrays.asList(new int[] {42}); - assertEquals( - iterable, - iterableCopy, - "arrays inside Iterables are compared by reference in assertEquals"); - } - - @Test(expectedExceptions = AssertionError.class) - public void arrayDeepInIterablesAssertEquals() { - List> iterable = Collections.singletonList(Arrays.asList(new int[] {42})); - List> iterableCopy = Collections.singletonList(Arrays.asList(new int[] {42})); - - assertEquals( - iterable, - iterableCopy, - "arrays inside Iterables which are inside Iterables themselves are compared by reference in assertEquals"); - } - - @Test - public void arrayInsideArrayAssertEquals() { - int[][] array = new int[][] {new int[] {42}}; - int[][] arrayCopy = new int[][] {new int[] {42}}; - assertEquals( - array, arrayCopy, "arrays inside arrays are compared by value in assertEquals"); - } - - @Test - public void arrayDeepInArraysAssertEquals() { - int[][][] array = new int[][][] {new int[][] {new int[] {42}}}; - int[][][] arrayCopy = new int[][][] {new int[][] {new int[] {42}}}; - - assertEquals( - array, - arrayCopy, - "arrays inside arrays which are inside arrays themselves are compared by value in assertEquals"); - } - - @SuppressWarnings("unchecked") - @Test(expectedExceptions = AssertionError.class) - public void arrayDeepInArrayAndListAssertEquals() { - List[] array = new List[] {Arrays.asList(new int[] {42})}; - List[] arrayCopy = new List[] {Arrays.asList(new int[] {42})}; - - assertEquals( - array, - arrayCopy, - "arrays inside arrays which are inside arrays themselves are compared by reference in assertEquals"); - } -} +package test.asserttests; + +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertEqualsDeep; +import static org.testng.Assert.assertNotEquals; +import static org.testng.Assert.assertNotEqualsDeep; + +/** Tests different equality cases for nested collections and arrays. */ +public class ArrayEqualityAssertTest { + + @Test + public void arrayAssertEquals() { + assertEquals( + new int[] {42}, + new int[] {42}, + "arrays of primitives are compared by value in assertEquals"); + } + + @Test(expectedExceptions = AssertionError.class) + public void arrayAssertNotEquals() { + assertNotEquals( + new int[] {42}, + new int[] {42}, + "arrays of primitives are compared by value in assertNotEquals"); + } + + @Test + public void boxedArrayAssertEquals() { + assertEquals( + new Integer[] {42}, + new Integer[] {42}, + "arrays of wrapped values are compared by value in assertEquals"); + } + + @Test(expectedExceptions = AssertionError.class) + public void boxedArrayAssertNotEquals() { + assertNotEquals( + new Integer[] {42}, + new Integer[] {42}, + "arrays of wrapped values are compared by value in assertNotEquals"); + } + + @Test + public void mixedArraysAssertEquals() { + assertEquals( + new int[] {42}, + new Integer[] {42}, + "arrays of wrapped values are compared by value in assertEquals"); + assertEquals( + new Integer[] {42}, + new int[] {42}, + "arrays of wrapped values are compared by value in assertEquals"); + } + + @Test(expectedExceptions = AssertionError.class) + public void mixedArraysAssertNotEquals() { + assertNotEquals( + new int[] {42}, + new Integer[] {42}, + "arrays of wrapped values are compared by value in assertNotEquals"); + assertNotEquals( + new Integer[] {42}, + new int[] {42}, + "arrays of wrapped values are compared by value in assertNotEquals"); + } + + @Test(expectedExceptions = AssertionError.class) + public void arrayInsideListAssertEquals() { + List list = Arrays.asList(new int[] {42}); + List listCopy = Arrays.asList(new int[] {42}); + assertEquals(list, listCopy, "arrays inside lists are compared by reference in assertEquals"); + } + + @Test + public void arrayInsideListAssertNotEquals() { + List list = Arrays.asList(new int[] {42}); + List listCopy = Arrays.asList(new int[] {42}); + assertNotEquals( + list, listCopy, "arrays inside lists are compared by reference in assertNotEquals"); + } + + @Test(expectedExceptions = AssertionError.class) + public void arrayInsideMapAssertEquals() { + Map map = new HashMap<>(); + map.put("array", new int[] {42}); + Map mapCopy = new HashMap<>(); + mapCopy.put("array", new int[] {42}); + + // arrays inside maps are compared by reference in assertEquals(Map,Map) + assertEquals(map, mapCopy); + } + + @Test(expectedExceptions = AssertionError.class) + public void arrayInsideMapAssertEqualsWithMessage() { + Map map = new HashMap<>(); + map.put("array", new int[] {42}); + Map mapCopy = new HashMap<>(); + mapCopy.put("array", new int[] {42}); + + assertEquals( + map, + mapCopy, + "arrays inside maps are compared by reference in assertEquals(Map,Map,String)"); + } + + @Test + public void arrayInsideMapAssertNotEquals() { + Map map = new HashMap<>(); + map.put("array", new int[] {42}); + Map mapCopy = new HashMap<>(); + mapCopy.put("array", new int[] {42}); + + assertNotEquals( + map, mapCopy, "arrays inside maps are compared by reference in assertNotEquals"); + } + + @Test + public void arrayInsideMapAssertEqualsDeep() { + Map map = new HashMap<>(); + map.put("array", new int[] {42}); + Map mapCopy = new HashMap<>(); + mapCopy.put("array", new int[] {42}); + + // arrays inside maps are compared by value in assertEqualsDeep(Map,Map) + assertEqualsDeep(map, mapCopy); + } + + @Test + public void arrayInsideMapAssertEqualsDeepWithMessage() { + Map map = new HashMap<>(); + map.put("array", new int[] {42}); + Map mapCopy = new HashMap<>(); + mapCopy.put("array", new int[] {42}); + + assertEqualsDeep( + map, + mapCopy, + "arrays inside maps are compared by value in assertEqualsDeep(Map,Map,String)"); + } + + @Test(expectedExceptions = AssertionError.class) + public void arrayInsideMapAssertNotEqualsDeep() { + Map map = new HashMap<>(); + map.put("array", new int[] {42}); + Map mapCopy = new HashMap<>(); + mapCopy.put("array", new int[] {42}); + + assertNotEqualsDeep( + map, mapCopy, "arrays inside maps are compared by value in assertNotEqualsDeep"); + } + + @Test(expectedExceptions = AssertionError.class) + public void arrayInsideSetAssertEquals() { + Set set = new HashSet<>(); + set.add(new int[] {42}); + Set setCopy = new HashSet<>(); + setCopy.add(new int[] {42}); + + assertEquals(set, setCopy, "arrays inside sets are compared by reference in assertNotEquals"); + } + + @Test + public void arrayInsideSetAssertNotEquals() { + Set set = new HashSet<>(); + set.add(new int[] {42}); + Set setCopy = new HashSet<>(); + setCopy.add(new int[] {42}); + + assertNotEquals( + set, setCopy, "arrays inside sets are compared by reference in assertNotEquals"); + } + + @Test + public void arrayInsideSetAssertEqualsDeep() { + Set set = new HashSet<>(); + set.add(new int[] {42}); + Set setCopy = new HashSet<>(); + setCopy.add(new int[] {42}); + + assertEqualsDeep(set, setCopy, "arrays inside sets are compared by value in assertEqualsDeep"); + } + + @Test(expectedExceptions = AssertionError.class) + public void arrayDeepInSetsAssertEqualsForExpectedLessThanActual() { + Set actual = new HashSet<>(); + actual.add("42"); + actual.add("43"); + Set expected = new HashSet<>(); + expected.add("42"); + + assertEqualsDeep(actual, expected, "Sets not equal:"); + } + + @Test(expectedExceptions = AssertionError.class) + public void arrayDeepInSetsAssertEqualsForActualLessThanExpected() { + Set actual = new HashSet<>(); + actual.add("42"); + Set expected = new HashSet<>(); + expected.add("42"); + expected.add("43"); + + assertEqualsDeep(actual, expected, "Sets not equal:"); + } + + @Test(expectedExceptions = AssertionError.class) + public void arrayInsideSetAssertNotEqualsDeep() { + Set set = new HashSet<>(); + set.add(new int[] {42}); + Set setCopy = new HashSet<>(); + setCopy.add(new int[] {42}); + + assertNotEqualsDeep( + set, setCopy, "arrays inside sets are compared by value in assertNotEqualsDeep"); + } + + @Test(expectedExceptions = AssertionError.class) + public void arrayDeepInListsAssertEquals() { + List> list = Collections.singletonList(Arrays.asList(new int[] {42})); + List> listCopy = Collections.singletonList(Arrays.asList(new int[] {42})); + + assertEquals( + list, + listCopy, + "arrays inside lists which are inside lists themselves are compared by reference in assertEquals"); + } + + @Test(expectedExceptions = AssertionError.class) + public void arrayDeepInMapsAssertEquals() { + Map> map = new HashMap<>(); + Map innerMap = new HashMap<>(); + innerMap.put("array", new int[] {42}); + map.put("map", innerMap); + Map> mapCopy = new HashMap<>(); + Map innerMapCopy = new HashMap<>(); + innerMapCopy.put("array", new int[] {42}); + mapCopy.put("map", innerMapCopy); + + assertEquals( + map, + mapCopy, + "arrays inside maps which are inside maps themselves are compared by reference in assertEquals"); + } + + @Test(expectedExceptions = AssertionError.class) + public void arrayDeepInListAndMapAssertEquals() { + List> list = new ArrayList<>(); + Map innerMap = new HashMap<>(); + innerMap.put("array", new int[] {42}); + list.add(innerMap); + List> listCopy = new ArrayList<>(); + Map innerMapCopy = new HashMap<>(); + innerMapCopy.put("array", new int[] {42}); + list.add(innerMapCopy); + + assertEquals( + list, + listCopy, + "arrays inside maps which are inside lists themselves are compared by reference in assertEquals"); + } + + @Test(expectedExceptions = AssertionError.class) + public void arrayDeepInMapAndListAssertEquals() { + Map> map = new HashMap<>(); + map.put("list", Arrays.asList(new int[] {42})); + Map> mapCopy = new HashMap<>(); + mapCopy.put("list", Arrays.asList(new int[] {42})); + + assertEquals( + map, + mapCopy, + "arrays inside maps which are inside lists themselves are compared by reference in assertEquals"); + } + + @Test(expectedExceptions = AssertionError.class) + public void arrayInsideIterableAssertEquals() { + Iterable iterable = Arrays.asList(new int[] {42}); + Iterable iterableCopy = Arrays.asList(new int[] {42}); + assertEquals( + iterable, + iterableCopy, + "arrays inside Iterables are compared by reference in assertEquals"); + } + + @Test(expectedExceptions = AssertionError.class) + public void arrayDeepInIterablesAssertEquals() { + List> iterable = Collections.singletonList(Arrays.asList(new int[] {42})); + List> iterableCopy = Collections.singletonList(Arrays.asList(new int[] {42})); + + assertEquals( + iterable, + iterableCopy, + "arrays inside Iterables which are inside Iterables themselves are compared by reference in assertEquals"); + } + + @Test + public void arrayInsideArrayAssertEquals() { + int[][] array = new int[][] {new int[] {42}}; + int[][] arrayCopy = new int[][] {new int[] {42}}; + assertEquals( + array, arrayCopy, "arrays inside arrays are compared by value in assertEquals"); + } + + @Test + public void arrayDeepInArraysAssertEquals() { + int[][][] array = new int[][][] {new int[][] {new int[] {42}}}; + int[][][] arrayCopy = new int[][][] {new int[][] {new int[] {42}}}; + + assertEquals( + array, + arrayCopy, + "arrays inside arrays which are inside arrays themselves are compared by value in assertEquals"); + } + + @SuppressWarnings("unchecked") + @Test(expectedExceptions = AssertionError.class) + public void arrayDeepInArrayAndListAssertEquals() { + List[] array = new List[] {Arrays.asList(new int[] {42})}; + List[] arrayCopy = new List[] {Arrays.asList(new int[] {42})}; + + assertEquals( + array, + arrayCopy, + "arrays inside arrays which are inside arrays themselves are compared by reference in assertEquals"); + } +} diff --git a/testng-core-api/src/main/java/org/testng/IConfigurationListener.java b/testng-core-api/src/main/java/org/testng/IConfigurationListener.java index b242b561c1..9d755de79d 100644 --- a/testng-core-api/src/main/java/org/testng/IConfigurationListener.java +++ b/testng-core-api/src/main/java/org/testng/IConfigurationListener.java @@ -1,81 +1,81 @@ -package org.testng; - -/** Listener interface for events related to configuration methods. */ -public interface IConfigurationListener extends ITestNGListener { - - /** - * Invoked whenever a configuration method succeeded. - * - * @param tr The test result - */ - default void onConfigurationSuccess(ITestResult tr) { - // not implemented - } - - /** - * Invoked whenever a configuration method succeeded. - * - * @param tr The test result - * @param tm The test method - */ - default void onConfigurationSuccess(ITestResult tr, ITestNGMethod tm) { - // not implemented - } - - /** - * Invoked whenever a configuration method failed. - * - * @param tr The test result - */ - default void onConfigurationFailure(ITestResult tr) { - // not implemented - } - - /** - * Invoked whenever a configuration method failed. - * - * @param tr The test result - * @param tm The test method - */ - default void onConfigurationFailure(ITestResult tr, ITestNGMethod tm) { - // not implemented - } - - /** - * Invoked whenever a configuration method was skipped. - * - * @param tr The test result - */ - default void onConfigurationSkip(ITestResult tr) { - // not implemented - } - - /** - * Invoked whenever a configuration method was skipped. - * - * @param tr The test result - * @param tm The test method - */ - default void onConfigurationSkip(ITestResult tr, ITestNGMethod tm) { - // not implemented - } - - /** - * Invoked before a configuration method is invoked. - * - * @param tr The test result - */ - default void beforeConfiguration(ITestResult tr) { - // not implemented - } - - /** - * Invoked before a configuration method is invoked. - * - * @param tr The test result - * @param tm The test method - */ - default void beforeConfiguration(ITestResult tr, ITestNGMethod tm) { - // not implemented - } -} +package org.testng; + +/** Listener interface for events related to configuration methods. */ +public interface IConfigurationListener extends ITestNGListener { + + /** + * Invoked whenever a configuration method succeeded. + * + * @param tr The test result + */ + default void onConfigurationSuccess(ITestResult tr) { + // not implemented + } + + /** + * Invoked whenever a configuration method succeeded. + * + * @param tr The test result + * @param tm The test method + */ + default void onConfigurationSuccess(ITestResult tr, ITestNGMethod tm) { + // not implemented + } + + /** + * Invoked whenever a configuration method failed. + * + * @param tr The test result + */ + default void onConfigurationFailure(ITestResult tr) { + // not implemented + } + + /** + * Invoked whenever a configuration method failed. + * + * @param tr The test result + * @param tm The test method + */ + default void onConfigurationFailure(ITestResult tr, ITestNGMethod tm) { + // not implemented + } + + /** + * Invoked whenever a configuration method was skipped. + * + * @param tr The test result + */ + default void onConfigurationSkip(ITestResult tr) { + // not implemented + } + + /** + * Invoked whenever a configuration method was skipped. + * + * @param tr The test result + * @param tm The test method + */ + default void onConfigurationSkip(ITestResult tr, ITestNGMethod tm) { + // not implemented + } + + /** + * Invoked before a configuration method is invoked. + * + * @param tr The test result + */ + default void beforeConfiguration(ITestResult tr) { + // not implemented + } + + /** + * Invoked before a configuration method is invoked. + * + * @param tr The test result + * @param tm The test method + */ + default void beforeConfiguration(ITestResult tr, ITestNGMethod tm) { + // not implemented + } +} diff --git a/testng-core-api/src/main/java/org/testng/IMethodSelectorContext.java b/testng-core-api/src/main/java/org/testng/IMethodSelectorContext.java index 53d7d71ea2..81095f9ed6 100644 --- a/testng-core-api/src/main/java/org/testng/IMethodSelectorContext.java +++ b/testng-core-api/src/main/java/org/testng/IMethodSelectorContext.java @@ -1,31 +1,31 @@ -package org.testng; - -import java.util.Map; - -/** - * An implementation of this interface is passed to all the Method Selectors when their - * includeMethod() is invoked. Method selectors can invoke any method of this context at that time. - * - *

Created on Jan 3, 2007 - * - * @author Cedric Beust - */ -public interface IMethodSelectorContext { - - /** @return true if no more Method Selectors should be invoked after the current one. */ - boolean isStopped(); - - /** - * Indicate that no other Method Selectors should be invoked after the current one if stopped is - * false. - * - * @param stopped The value - */ - void setStopped(boolean stopped); - - /** - * @return a Map that can be freely manipulated by the Method Selector. This can be used to share - * information among several Method Selectors. - */ - Map getUserData(); -} +package org.testng; + +import java.util.Map; + +/** + * An implementation of this interface is passed to all the Method Selectors when their + * includeMethod() is invoked. Method selectors can invoke any method of this context at that time. + * + *

Created on Jan 3, 2007 + * + * @author Cedric Beust + */ +public interface IMethodSelectorContext { + + /** @return true if no more Method Selectors should be invoked after the current one. */ + boolean isStopped(); + + /** + * Indicate that no other Method Selectors should be invoked after the current one if stopped is + * false. + * + * @param stopped The value + */ + void setStopped(boolean stopped); + + /** + * @return a Map that can be freely manipulated by the Method Selector. This can be used to share + * information among several Method Selectors. + */ + Map getUserData(); +} diff --git a/testng-core-api/src/main/java/org/testng/ISuite.java b/testng-core-api/src/main/java/org/testng/ISuite.java index c6e9e9729f..d9842a6209 100644 --- a/testng-core-api/src/main/java/org/testng/ISuite.java +++ b/testng-core-api/src/main/java/org/testng/ISuite.java @@ -1,98 +1,98 @@ -package org.testng; - -import java.util.Collection; -import java.util.List; -import java.util.Map; - -import org.testng.internal.annotations.IAnnotationFinder; -import org.testng.xml.XmlSuite; - -import com.google.inject.Injector; - -/** - * Interface defining a Test Suite. - * - * @author Cedric Beust, Aug 6, 2004 - */ -public interface ISuite extends IAttributes { - - /** @return the name of this suite. */ - String getName(); - - /** @return The results for this suite. */ - Map getResults(); - - /** @return The object factory used to create all test instances. */ - ITestObjectFactory getObjectFactory(); - - @Deprecated - /** - * @deprecated - This interface stands deprecated as of TestNG 7.5.0 - */ - default IObjectFactory2 getObjectFactory2() { - return null; - } - - /** @return The output directory used for the reports. */ - String getOutputDirectory(); - - /** @return true if the tests must be run in parallel. */ - String getParallel(); - - String getParentModule(); - - String getGuiceStage(); - - /** - * @param parameterName The name of the parameter - * @return The value of this parameter, or null if none was specified. - */ - String getParameter(String parameterName); - - /** - * Retrieves the map of groups and their associated test methods. - * - * @return A map where the key is the group and the value is a list of methods used by this group. - */ - Map> getMethodsByGroups(); - - /** @return a list of all the methods that were invoked in this suite. */ - List getAllInvokedMethods(); - - /** @return All the methods that were not included in this test run. */ - Collection getExcludedMethods(); - - /** Triggers the start of running tests included in the suite. */ - void run(); - - /** - * @return The host where this suite was run, or null if it was run locally. The returned string - * has the form: host:port - */ - String getHost(); - - /** - * Retrieves the shared state for a suite. - * - * @return the share state of the current suite. - */ - SuiteRunState getSuiteState(); - - /** @return the annotation finder used for the specified type (JDK5 or javadoc) */ - IAnnotationFinder getAnnotationFinder(); - - /** @return The representation of the current XML suite file. */ - XmlSuite getXmlSuite(); - - void addListener(ITestNGListener listener); - - Injector getParentInjector(); - - void setParentInjector(Injector injector); - - /** - * @return the total number of methods found in this suite. The presence of factories or data - * providers might cause the actual number of test methods run be bigger than this list. - */ - List getAllMethods(); -} +package org.testng; + +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import org.testng.internal.annotations.IAnnotationFinder; +import org.testng.xml.XmlSuite; + +import com.google.inject.Injector; + +/** + * Interface defining a Test Suite. + * + * @author Cedric Beust, Aug 6, 2004 + */ +public interface ISuite extends IAttributes { + + /** @return the name of this suite. */ + String getName(); + + /** @return The results for this suite. */ + Map getResults(); + + /** @return The object factory used to create all test instances. */ + ITestObjectFactory getObjectFactory(); + + @Deprecated + /** + * @deprecated - This interface stands deprecated as of TestNG 7.5.0 + */ + default IObjectFactory2 getObjectFactory2() { + return null; + } + + /** @return The output directory used for the reports. */ + String getOutputDirectory(); + + /** @return true if the tests must be run in parallel. */ + String getParallel(); + + String getParentModule(); + + String getGuiceStage(); + + /** + * @param parameterName The name of the parameter + * @return The value of this parameter, or null if none was specified. + */ + String getParameter(String parameterName); + + /** + * Retrieves the map of groups and their associated test methods. + * + * @return A map where the key is the group and the value is a list of methods used by this group. + */ + Map> getMethodsByGroups(); + + /** @return a list of all the methods that were invoked in this suite. */ + List getAllInvokedMethods(); + + /** @return All the methods that were not included in this test run. */ + Collection getExcludedMethods(); + + /** Triggers the start of running tests included in the suite. */ + void run(); + + /** + * @return The host where this suite was run, or null if it was run locally. The returned string + * has the form: host:port + */ + String getHost(); + + /** + * Retrieves the shared state for a suite. + * + * @return the share state of the current suite. + */ + SuiteRunState getSuiteState(); + + /** @return the annotation finder used for the specified type (JDK5 or javadoc) */ + IAnnotationFinder getAnnotationFinder(); + + /** @return The representation of the current XML suite file. */ + XmlSuite getXmlSuite(); + + void addListener(ITestNGListener listener); + + Injector getParentInjector(); + + void setParentInjector(Injector injector); + + /** + * @return the total number of methods found in this suite. The presence of factories or data + * providers might cause the actual number of test methods run be bigger than this list. + */ + List getAllMethods(); +} diff --git a/testng-core-api/src/main/java/org/testng/ITestNGMethod.java b/testng-core-api/src/main/java/org/testng/ITestNGMethod.java index 45b18c8503..14a7987470 100644 --- a/testng-core-api/src/main/java/org/testng/ITestNGMethod.java +++ b/testng-core-api/src/main/java/org/testng/ITestNGMethod.java @@ -1,273 +1,273 @@ -package org.testng; - -import org.testng.annotations.CustomAttribute; -import org.testng.internal.ConstructorOrMethod; -import org.testng.internal.IParameterInfo; -import org.testng.xml.XmlTest; - -import java.util.List; -import java.util.Map; -import java.util.concurrent.Callable; - -/** - * Describes a TestNG annotated method and the instance on which it will be invoked. - * - *

This interface is not meant to be implemented by users. - */ -public interface ITestNGMethod extends Cloneable { - - /** - * @return The real class on which this method was declared (can be different from - * getMethod().getDeclaringClass() if the test method was defined in a superclass). - */ - Class getRealClass(); - - ITestClass getTestClass(); - - /** - * Sets the test class having this method. This is not necessarily the declaring class. - * - * @param cls The test class having this method. - */ - void setTestClass(ITestClass cls); - - /** - * Returns the method name. This is needed for serialization because methods are not Serializable. - * - * @return the method name. - */ - String getMethodName(); - - Object getInstance(); - - /** - * Needed for serialization. - * - * @return The hashcode of instances - */ - long[] getInstanceHashCodes(); - - /** - * @return The groups this method belongs to, possibly added to the groups declared on the class. - */ - String[] getGroups(); - - /** - * @return The groups this method depends on, possibly added to the groups declared on the class. - */ - String[] getGroupsDependedUpon(); - - /** @return If a group was not found. */ - String getMissingGroup(); - - void setMissingGroup(String group); - - String[] getBeforeGroups(); - - String[] getAfterGroups(); - - /** - * @return The methods this method depends on, possibly added to the methods declared on the - * class. - */ - String[] getMethodsDependedUpon(); - - void addMethodDependedUpon(String methodName); - - /** @return true if this method was annotated with @Test */ - boolean isTest(); - - /** @return true if this method was annotated with @Configuration and beforeTestMethod = true */ - boolean isBeforeMethodConfiguration(); - - /** @return true if this method was annotated with @Configuration and beforeTestMethod = false */ - boolean isAfterMethodConfiguration(); - - /** @return true if this method was annotated with @Configuration and beforeClassMethod = true */ - boolean isBeforeClassConfiguration(); - - /** @return true if this method was annotated with @Configuration and beforeClassMethod = false */ - boolean isAfterClassConfiguration(); - - /** @return true if this method was annotated with @Configuration and beforeSuite = true */ - boolean isBeforeSuiteConfiguration(); - - /** @return true if this method was annotated with @Configuration and afterSuite = true */ - boolean isAfterSuiteConfiguration(); - - /** @return true if this method is a @BeforeTest (@Configuration beforeTest=true) */ - boolean isBeforeTestConfiguration(); - - /** @return true if this method is an @AfterTest (@Configuration afterTest=true) */ - boolean isAfterTestConfiguration(); - - boolean isBeforeGroupsConfiguration(); - - boolean isAfterGroupsConfiguration(); - - default boolean hasBeforeGroupsConfiguration() { - return false; - } - - default boolean hasAfterGroupsConfiguration() { - return false; - } - - - /** @return The timeout in milliseconds. */ - long getTimeOut(); - - void setTimeOut(long timeOut); - - /** @return the number of times this method needs to be invoked. */ - int getInvocationCount(); - - void setInvocationCount(int count); - - /** @return the success percentage for this method (between 0 and 100). */ - int getSuccessPercentage(); - - /** @return The id of the thread this method was run in. */ - String getId(); - - void setId(String id); - - long getDate(); - - void setDate(long date); - - /** - * @param testClass The test class - * @return true if this ITestNGMethod can be invoked from within IClass. - */ - boolean canRunFromClass(IClass testClass); - - /** @return true if this method is alwaysRun=true */ - boolean isAlwaysRun(); - - /** @return the number of threads to be used when invoking the method on parallel */ - int getThreadPoolSize(); - - void setThreadPoolSize(int threadPoolSize); - - boolean getEnabled(); - - String getDescription(); - - void setDescription(String description); - - void incrementCurrentInvocationCount(); - - int getCurrentInvocationCount(); - - void setParameterInvocationCount(int n); - - int getParameterInvocationCount(); - - void setMoreInvocationChecker(Callable moreInvocationChecker); - - boolean hasMoreInvocation(); - - ITestNGMethod clone(); - - IRetryAnalyzer getRetryAnalyzer(ITestResult result); - - void setRetryAnalyzerClass(Class clazz); - - Class getRetryAnalyzerClass(); - - boolean skipFailedInvocations(); - - void setSkipFailedInvocations(boolean skip); - - /** @return The time under which all invocationCount methods need to complete by. */ - long getInvocationTimeOut(); - - boolean ignoreMissingDependencies(); - - void setIgnoreMissingDependencies(boolean ignore); - - /** - * Which invocation numbers of this method should be used (only applicable if it uses a data - * provider). If this value is an empty list, use all the values returned from the data provider. - * These values are read from the XML file in the <include invocationNumbers="..."> tag. - * - * @return The list of invocation numbers - */ - List getInvocationNumbers(); - - void setInvocationNumbers(List numbers); - - /** - * The list of invocation numbers that failed, which is only applicable for methods that have a - * data provider. - * - * @param number The invocation number that failed - */ - void addFailedInvocationNumber(int number); - - List getFailedInvocationNumbers(); - - /** - * The scheduling priority. Lower priorities get scheduled first. - * - * @return The priority value - */ - int getPriority(); - - void setPriority(int priority); - - int getInterceptedPriority(); - - void setInterceptedPriority(int priority); - - /** @return the XmlTest this method belongs to. */ - XmlTest getXmlTest(); - - ConstructorOrMethod getConstructorOrMethod(); - - /** - * @param test - The {@link XmlTest} object. - * @return the parameters found in the include tag, if any - */ - Map findMethodParameters(XmlTest test); - - /** - * getRealClass().getName() + "." + getMethodName() - * - * @return qualified name for this method - */ - String getQualifiedName(); - - default boolean isDataDriven() { - return false; - } - - /** - * @return - A {@link IParameterInfo} object that represents details about the parameters - * associated with the factory method. - */ - default IParameterInfo getFactoryMethodParamsInfo() { - return null; - } - - /** - * @return - An array of {@link CustomAttribute} that represents the custom attributes associated - * with a test. - */ - default CustomAttribute[] getAttributes() { - return new CustomAttribute[]{}; - } - - /** - * @return - An {@link IDataProviderMethod} for a data provider powered test method and - * null otherwise. - */ - default IDataProviderMethod getDataProviderMethod() { - return null; - } - - default Class[] getParameterTypes() { - return new Class[] {}; - } -} +package org.testng; + +import org.testng.annotations.CustomAttribute; +import org.testng.internal.ConstructorOrMethod; +import org.testng.internal.IParameterInfo; +import org.testng.xml.XmlTest; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.Callable; + +/** + * Describes a TestNG annotated method and the instance on which it will be invoked. + * + *

This interface is not meant to be implemented by users. + */ +public interface ITestNGMethod extends Cloneable { + + /** + * @return The real class on which this method was declared (can be different from + * getMethod().getDeclaringClass() if the test method was defined in a superclass). + */ + Class getRealClass(); + + ITestClass getTestClass(); + + /** + * Sets the test class having this method. This is not necessarily the declaring class. + * + * @param cls The test class having this method. + */ + void setTestClass(ITestClass cls); + + /** + * Returns the method name. This is needed for serialization because methods are not Serializable. + * + * @return the method name. + */ + String getMethodName(); + + Object getInstance(); + + /** + * Needed for serialization. + * + * @return The hashcode of instances + */ + long[] getInstanceHashCodes(); + + /** + * @return The groups this method belongs to, possibly added to the groups declared on the class. + */ + String[] getGroups(); + + /** + * @return The groups this method depends on, possibly added to the groups declared on the class. + */ + String[] getGroupsDependedUpon(); + + /** @return If a group was not found. */ + String getMissingGroup(); + + void setMissingGroup(String group); + + String[] getBeforeGroups(); + + String[] getAfterGroups(); + + /** + * @return The methods this method depends on, possibly added to the methods declared on the + * class. + */ + String[] getMethodsDependedUpon(); + + void addMethodDependedUpon(String methodName); + + /** @return true if this method was annotated with @Test */ + boolean isTest(); + + /** @return true if this method was annotated with @Configuration and beforeTestMethod = true */ + boolean isBeforeMethodConfiguration(); + + /** @return true if this method was annotated with @Configuration and beforeTestMethod = false */ + boolean isAfterMethodConfiguration(); + + /** @return true if this method was annotated with @Configuration and beforeClassMethod = true */ + boolean isBeforeClassConfiguration(); + + /** @return true if this method was annotated with @Configuration and beforeClassMethod = false */ + boolean isAfterClassConfiguration(); + + /** @return true if this method was annotated with @Configuration and beforeSuite = true */ + boolean isBeforeSuiteConfiguration(); + + /** @return true if this method was annotated with @Configuration and afterSuite = true */ + boolean isAfterSuiteConfiguration(); + + /** @return true if this method is a @BeforeTest (@Configuration beforeTest=true) */ + boolean isBeforeTestConfiguration(); + + /** @return true if this method is an @AfterTest (@Configuration afterTest=true) */ + boolean isAfterTestConfiguration(); + + boolean isBeforeGroupsConfiguration(); + + boolean isAfterGroupsConfiguration(); + + default boolean hasBeforeGroupsConfiguration() { + return false; + } + + default boolean hasAfterGroupsConfiguration() { + return false; + } + + + /** @return The timeout in milliseconds. */ + long getTimeOut(); + + void setTimeOut(long timeOut); + + /** @return the number of times this method needs to be invoked. */ + int getInvocationCount(); + + void setInvocationCount(int count); + + /** @return the success percentage for this method (between 0 and 100). */ + int getSuccessPercentage(); + + /** @return The id of the thread this method was run in. */ + String getId(); + + void setId(String id); + + long getDate(); + + void setDate(long date); + + /** + * @param testClass The test class + * @return true if this ITestNGMethod can be invoked from within IClass. + */ + boolean canRunFromClass(IClass testClass); + + /** @return true if this method is alwaysRun=true */ + boolean isAlwaysRun(); + + /** @return the number of threads to be used when invoking the method on parallel */ + int getThreadPoolSize(); + + void setThreadPoolSize(int threadPoolSize); + + boolean getEnabled(); + + String getDescription(); + + void setDescription(String description); + + void incrementCurrentInvocationCount(); + + int getCurrentInvocationCount(); + + void setParameterInvocationCount(int n); + + int getParameterInvocationCount(); + + void setMoreInvocationChecker(Callable moreInvocationChecker); + + boolean hasMoreInvocation(); + + ITestNGMethod clone(); + + IRetryAnalyzer getRetryAnalyzer(ITestResult result); + + void setRetryAnalyzerClass(Class clazz); + + Class getRetryAnalyzerClass(); + + boolean skipFailedInvocations(); + + void setSkipFailedInvocations(boolean skip); + + /** @return The time under which all invocationCount methods need to complete by. */ + long getInvocationTimeOut(); + + boolean ignoreMissingDependencies(); + + void setIgnoreMissingDependencies(boolean ignore); + + /** + * Which invocation numbers of this method should be used (only applicable if it uses a data + * provider). If this value is an empty list, use all the values returned from the data provider. + * These values are read from the XML file in the <include invocationNumbers="..."> tag. + * + * @return The list of invocation numbers + */ + List getInvocationNumbers(); + + void setInvocationNumbers(List numbers); + + /** + * The list of invocation numbers that failed, which is only applicable for methods that have a + * data provider. + * + * @param number The invocation number that failed + */ + void addFailedInvocationNumber(int number); + + List getFailedInvocationNumbers(); + + /** + * The scheduling priority. Lower priorities get scheduled first. + * + * @return The priority value + */ + int getPriority(); + + void setPriority(int priority); + + int getInterceptedPriority(); + + void setInterceptedPriority(int priority); + + /** @return the XmlTest this method belongs to. */ + XmlTest getXmlTest(); + + ConstructorOrMethod getConstructorOrMethod(); + + /** + * @param test - The {@link XmlTest} object. + * @return the parameters found in the include tag, if any + */ + Map findMethodParameters(XmlTest test); + + /** + * getRealClass().getName() + "." + getMethodName() + * + * @return qualified name for this method + */ + String getQualifiedName(); + + default boolean isDataDriven() { + return false; + } + + /** + * @return - A {@link IParameterInfo} object that represents details about the parameters + * associated with the factory method. + */ + default IParameterInfo getFactoryMethodParamsInfo() { + return null; + } + + /** + * @return - An array of {@link CustomAttribute} that represents the custom attributes associated + * with a test. + */ + default CustomAttribute[] getAttributes() { + return new CustomAttribute[]{}; + } + + /** + * @return - An {@link IDataProviderMethod} for a data provider powered test method and + * null otherwise. + */ + default IDataProviderMethod getDataProviderMethod() { + return null; + } + + default Class[] getParameterTypes() { + return new Class[] {}; + } +} diff --git a/testng-core-api/src/main/java/org/testng/SuiteRunState.java b/testng-core-api/src/main/java/org/testng/SuiteRunState.java index c53a8ef4a2..d4258a54e6 100644 --- a/testng-core-api/src/main/java/org/testng/SuiteRunState.java +++ b/testng-core-api/src/main/java/org/testng/SuiteRunState.java @@ -1,20 +1,20 @@ -package org.testng; - -import java.util.concurrent.atomic.AtomicBoolean; - -/** - * A state object that records the status of the suite run. Mainly used to figure out if there are - * any @BeforeSuite failures. - */ -public class SuiteRunState { - - private final AtomicBoolean m_hasFailures = new AtomicBoolean(); - - public void failed() { - m_hasFailures.set(true); - } - - public boolean isFailed() { - return m_hasFailures.get(); - } -} +package org.testng; + +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * A state object that records the status of the suite run. Mainly used to figure out if there are + * any @BeforeSuite failures. + */ +public class SuiteRunState { + + private final AtomicBoolean m_hasFailures = new AtomicBoolean(); + + public void failed() { + m_hasFailures.set(true); + } + + public boolean isFailed() { + return m_hasFailures.get(); + } +} diff --git a/testng-core-api/src/main/java/org/testng/log4testng/Logger.java b/testng-core-api/src/main/java/org/testng/log4testng/Logger.java index b68080caa2..a527eef5c2 100644 --- a/testng-core-api/src/main/java/org/testng/log4testng/Logger.java +++ b/testng-core-api/src/main/java/org/testng/log4testng/Logger.java @@ -1,467 +1,467 @@ -package org.testng.log4testng; - -import java.io.IOException; -import java.io.InputStream; -import java.io.PrintStream; -import java.util.Iterator; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Properties; - -import org.testng.collections.Maps; - -/** - * TestNG support logging via a custom logging framework similar to Log4j. To control logging, add a resource named - * "log4testng.properties" to your classpath. The logging levels are TRACE, DEBUG, INFO, WARN, ERROR - * and FATAL. The Logging framework has the following characteristics: - * - *

    - *
  • All logging is done using System.out (for levels < ERROR) or System.err. There is no way - * to specify Appenders. - *
  • There is no way to control logging programmatically. - *
  • The log4testng.properties resource is searched in the classpath on the first call to the - * logging API. If it is not present, logging defaults to the WARN level. - *
- * - * The property file contains lines in the following format: - * - *

- * # log4testng will log its own behavior (generally used for debugging this package only).
- * log4testng.debug=true
- *
- * # Specifies the root Loggers logging level. Will log DEBUG level and above
- * log4testng.rootLogger=DEBUG
- *
- * # The org.testng.reporters.EmailableReporter Logger will log TRACE level and above
- * log4testng.logger.org.testng.reporters.EmailableReporter=TRACE
- *
- * # All Logger in packages below org.testng will log WARN level and above
- * log4testng.logger.org.testng=WARN
- * 
- * - * In your source files you will typically instantiate and use loggers this ways: - * - *

- * import org.testng.log4testng.Logger;
- *
- * class ThisClass {
- *     private static final Logger LOGGER = Logger.getLogger(ThisClass.class);
- *
- *     ...
- *     LOGGER.debug("entering myMethod()");
- *     ...
- *     LOGGER.warn("unknown file: " + filename);
- *     ...
- *     LOGGER.error("Unexpected error", exception);
- * 
- */ -public class Logger { - - // Attribute an hierarchical integer value to all levels. - private static int i = 0; - private static final int TRACE = i++; - private static final int DEBUG = i++; - private static final int INFO = i++; - static final int WARN = i++; - private static final int ERROR = i++; - private static final int FATAL = i++; - private static final int LEVEL_COUNT = i; - - /** Standard prefix of all property names in log4testng.properties. */ - private static final String PREFIX = "log4testng."; - - /** Standard prefix of all logger names in log4testng.properties. */ - private static final String LOGGER_PREFIX = PREFIX + "logger."; - - /** Root logger name in log4testng.properties. */ - private static final String ROOT_LOGGER = PREFIX + "rootLogger"; - - /** Debug property name in log4testng.properties. */ - private static final String DEBUG_PROPERTY = PREFIX + "debug"; - - /** The standard error stream (this is allways System.err except for unit tests) */ - static PrintStream err = System.err; - - /** The standard output stream (this is allways System.out except for unit tests) */ - static PrintStream out = System.out; - - /** An ordered list of level names. */ - private static final String[] levelNames = new String[LEVEL_COUNT]; - - static { - levelNames[TRACE] = "TRACE"; - levelNames[DEBUG] = "DEBUG"; - levelNames[INFO] = "INFO"; - levelNames[WARN] = "WARN"; - levelNames[ERROR] = "ERROR"; - levelNames[FATAL] = "FATAL"; - } - - /** A map from level name to level integer index (TRACE->0, DEBUG->1 ...) */ - private static final Map levelMap = Maps.newHashMap(); - - static { - for (i = 0; i < LEVEL_COUNT; ++i) { - levelMap.put(levelNames[i], i); - } - } - - /** true if the Logging system has been initialized. */ - static boolean initialized; - - /** Map from Logger names to level index (as specified in log4testng.properties) */ - private static final Map loggerLevels = Maps.newHashMap(); - - /** Map of all known loggers. */ - static final Map loggers = Maps.newHashMap(); - - /** The logging level of the root logger (defaults to warn). */ - static int rootLoggerLevel = WARN; - - /** Should log4testng log what it is doing (defaults to false). */ - static boolean debug = false; - - /** The logger's level */ - private final int level; - - /** The logger's name. */ - private final Class klass; - - private final String m_className; - - /** - * Retrieve a logger named according to the value of the pClass.getName() parameter. If the named - * logger already exists, then the existing instance will be returned. Otherwise, a new instance - * is created. By default, loggers do not have a set level but inherit it from their nearest - * ancestor with a set level. - * - * @param pClass The class' logger to retrieve. - * @return a logger named according to the value of the pClass.getName(). - */ - public static synchronized Logger getLogger(Class pClass) { - initialize(); - Logger logger = loggers.get(pClass); - if (logger != null) { - return logger; - } - int level = getLevel(pClass); - logger = new Logger(pClass, level); - loggers.put(pClass, logger); - - return logger; - } - - /** - * Check whether this logger is enabled for the TRACE Level. - * - * @return true if this logger is enabled for level TRACE, false otherwise. - */ - public boolean isTraceEnabled() { - return isLevelEnabled(TRACE); - } - - /** - * Log a message object with the TRACE level. This method first checks if this logger is TRACE - * enabled. If this logger is TRACE enabled, then it converts the message object (passed as - * parameter) to a string by invoking toString(). WARNING Note that passing a Throwable to this - * method will print the name of the Throwable but no stack trace. To print a stack trace use the - * trace(Object, Throwable) form instead. - * - * @param message the message object to log. - */ - public void trace(Object message) { - log(TRACE, message, null); - } - - /** - * Log a message object with the TRACE level including the stack trace of the Throwable t passed - * as parameter. See Logger.trace(Object) form for more detailed information. - * - * @param message the message object to log. - * @param t the exception to log, including its stack trace. - */ - public void trace(Object message, Throwable t) { - log(TRACE, message, t); - } - - /** - * Check whether this logger is enabled for the DEBUG Level. - * - * @return true if this logger is enabled for level DEBUG, false otherwise. - */ - public boolean isDebugEnabled() { - return isLevelEnabled(DEBUG); - } - - /** - * Log a message object with the DEBUG level. See Logger.trace(Object) form for more detailed - * information. - * - * @param message the message object to log. - */ - public void debug(Object message) { - log(DEBUG, message, null); - } - - /** - * Log a message object with the DEBUG level including the stack trace of the Throwable t passed - * as parameter. See Logger.trace(Object, Throwable) form for more detailed information. - * - * @param message the message object to log. - * @param t the exception to log, including its stack trace. - */ - public void debug(Object message, Throwable t) { - log(DEBUG, message, t); - } - - /** - * Check whether this logger is enabled for the INFO Level. - * - * @return true if this logger is enabled for level INFO, false otherwise. - */ - public boolean isInfoEnabled() { - return isLevelEnabled(INFO); - } - - /** - * Log a message object with the INFO level. See Logger.trace(Object) form for more detailed - * information. - * - * @param message the message object to log. - */ - public void info(Object message) { - log(INFO, message, null); - } - - /** - * Log a message object with the WARN level including the stack trace of the Throwable t passed as - * parameter. See Logger.trace(Object, Throwable) form for more detailed information. - * - * @param message the message object to log. - * @param t the exception to log, including its stack trace. - */ - public void info(Object message, Throwable t) { - log(INFO, message, t); - } - - /** - * Log a message object with the WARN level. See Logger.trace(Object) form for more detailed - * information. - * - * @param message the message object to log. - */ - public void warn(Object message) { - log(WARN, message, null); - } - - /** - * Log a message object with the ERROR level including the stack trace of the Throwable t passed - * as parameter. See Logger.trace(Object, Throwable) form for more detailed information. - * - * @param message the message object to log. - * @param t the exception to log, including its stack trace. - */ - public void warn(Object message, Throwable t) { - log(WARN, message, t); - } - - /** - * Log a message object with the ERROR level. See Logger.trace(Object) form for more detailed - * information. - * - * @param message the message object to log. - */ - public void error(Object message) { - log(ERROR, message, null); - } - - /** - * Log a message object with the DEBUG level including the stack trace of the Throwable t passed - * as parameter. See Logger.trace(Object, Throwable) form for more detailed information. - * - * @param message the message object to log. - * @param t the exception to log, including its stack trace. - */ - public void error(Object message, Throwable t) { - log(ERROR, message, t); - } - - /** - * Log a message object with the FATAL level. See Logger.trace(Object) form for more detailed - * information. - * - * @param message the message object to log. - */ - public void fatal(Object message) { - log(FATAL, message, null); - } - - /** - * Log a message object with the FATAL level including the stack trace of the Throwable t passed - * as parameter. See Logger.trace(Object, Throwable) form for more detailed information. - * - * @param message the message object to log. - * @param t the exception to log, including its stack trace. - */ - public void fatal(Object message, Throwable t) { - log(FATAL, message, t); - } - - private Logger(Class pClass, int pLevel) { - level = pLevel; - klass = pClass; - m_className = pClass.getName().substring(pClass.getName().lastIndexOf('.') + 1); - } - - private static synchronized void initialize() { - if (initialized) { - return; - } - - // We flag as initialized right away because if anything goes wrong - // We still consider it initialized. TODO Is this OK? - initialized = true; - - InputStream is = - Thread.currentThread().getContextClassLoader().getResourceAsStream("log4testng.properties"); - if (is == null) { - return; - } - Properties properties = new Properties(); - try { - properties.load(is); - } catch (IOException e) { - throw new RuntimeException(e); - } - - checkProperties(properties); - } - - static void checkProperties(Properties pProperties) { - { - // See if we want to debug log4testng - String debugStr = pProperties.getProperty(DEBUG_PROPERTY); - if (debugStr != null) { - if (debugStr.equalsIgnoreCase("true")) { - debug = true; - } else if (debugStr.equalsIgnoreCase("false")) { - debug = false; - } else { - throw new IllegalArgumentException("Unknown " + DEBUG_PROPERTY + " value " + debugStr); - } - } - loglog4testng("log4testng.debug set to " + debug); - } - - { - // Set the value of the root logger (if any). - String rootLevelStr = pProperties.getProperty(ROOT_LOGGER); - if (rootLevelStr != null) { - Integer ilevel = levelMap.get(rootLevelStr.toUpperCase()); - if (ilevel == null) { - throw new IllegalArgumentException( - "Unknown level for log4testng.rootLogger " - + rootLevelStr - + " in log4testng.properties"); - } - rootLoggerLevel = ilevel; - loglog4testng("Root level logger set to " + rootLevelStr + " level."); - } - } - - Iterator it = pProperties.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry entry = (Entry) it.next(); - String logger = (String) entry.getKey(); - String level = (String) entry.getValue(); - - if (!logger.startsWith(PREFIX)) { - throw new IllegalArgumentException("Illegal property value: " + logger); - } - if (logger.equals(DEBUG_PROPERTY)) { - // Already handled - } else if (logger.equals(ROOT_LOGGER)) { - // Already handled - } else { - if (!logger.startsWith(LOGGER_PREFIX)) { - throw new IllegalArgumentException("Illegal property value: " + logger); - } - - Integer ilevel = levelMap.get(level.toUpperCase()); - if (ilevel == null) { - throw new IllegalArgumentException( - "Unknown level " + level + " for logger " + logger + " in log4testng.properties"); - } - - loggerLevels.put(logger.substring(LOGGER_PREFIX.length()), ilevel); - loglog4testng("logger " + logger + " set to " + ilevel + " level."); - } - } - } - - /** - * Returns the level associated to the current class. The level is obtain by searching for a - * logger in the "testng-logging.properties" resource. For example, if class is - * "org.testng.TestNG" the the following loggers are searched in this order: - * - *
    - *
  1. "org.testng.TestNG" - *
  2. "org.testng" - *
  3. "org" - *
  4. The root level - *
- * - * @param pClass the class name used for logger name. - * @return the level associated to the current class. - */ - private static int getLevel(Class pClass) { - String name = pClass.getName(); - loglog4testng("Getting level for logger " + name); - while (true) { - Integer level = loggerLevels.get(name); - if (level != null) { - loglog4testng("Found level " + level + " for logger " + name); - - return level; - } - int dot = name.lastIndexOf('.'); - if (dot == -1) { - loglog4testng("Found level " + rootLoggerLevel + " for root logger"); - - // Logger name not found. Defaults to root logger level. - return rootLoggerLevel; - } - name = name.substring(0, dot); - } - } - - private boolean isLevelEnabled(int pLevel) { - return level <= pLevel; - } - - private void log(int pLevel, Object pMessage, Throwable pT) { - if (isLevelEnabled(pLevel)) { - PrintStream ps = (pLevel >= ERROR) ? err : out; - if (null != pT) { - synchronized (ps) { - ps.println("[" + m_className + "] [" + levelNames[pLevel] + "] " + pMessage); - pT.printStackTrace(ps); - } - } else { - ps.println("[" + m_className + "] [" + levelNames[pLevel] + "] " + pMessage); - } - } - } - - /** - * Logs the message to System.out of debug is on. - * - * @param pmessage the message to log to the console - */ - private static void loglog4testng(String pmessage) { - if (debug) { - out.println("[log4testng] [debug] " + pmessage); - } - } -} +package org.testng.log4testng; + +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintStream; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; + +import org.testng.collections.Maps; + +/** + * TestNG support logging via a custom logging framework similar to Log4j. To control logging, add a resource named + * "log4testng.properties" to your classpath. The logging levels are TRACE, DEBUG, INFO, WARN, ERROR + * and FATAL. The Logging framework has the following characteristics: + * + *
    + *
  • All logging is done using System.out (for levels < ERROR) or System.err. There is no way + * to specify Appenders. + *
  • There is no way to control logging programmatically. + *
  • The log4testng.properties resource is searched in the classpath on the first call to the + * logging API. If it is not present, logging defaults to the WARN level. + *
+ * + * The property file contains lines in the following format: + * + *

+ * # log4testng will log its own behavior (generally used for debugging this package only).
+ * log4testng.debug=true
+ *
+ * # Specifies the root Loggers logging level. Will log DEBUG level and above
+ * log4testng.rootLogger=DEBUG
+ *
+ * # The org.testng.reporters.EmailableReporter Logger will log TRACE level and above
+ * log4testng.logger.org.testng.reporters.EmailableReporter=TRACE
+ *
+ * # All Logger in packages below org.testng will log WARN level and above
+ * log4testng.logger.org.testng=WARN
+ * 
+ * + * In your source files you will typically instantiate and use loggers this ways: + * + *

+ * import org.testng.log4testng.Logger;
+ *
+ * class ThisClass {
+ *     private static final Logger LOGGER = Logger.getLogger(ThisClass.class);
+ *
+ *     ...
+ *     LOGGER.debug("entering myMethod()");
+ *     ...
+ *     LOGGER.warn("unknown file: " + filename);
+ *     ...
+ *     LOGGER.error("Unexpected error", exception);
+ * 
+ */ +public class Logger { + + // Attribute an hierarchical integer value to all levels. + private static int i = 0; + private static final int TRACE = i++; + private static final int DEBUG = i++; + private static final int INFO = i++; + static final int WARN = i++; + private static final int ERROR = i++; + private static final int FATAL = i++; + private static final int LEVEL_COUNT = i; + + /** Standard prefix of all property names in log4testng.properties. */ + private static final String PREFIX = "log4testng."; + + /** Standard prefix of all logger names in log4testng.properties. */ + private static final String LOGGER_PREFIX = PREFIX + "logger."; + + /** Root logger name in log4testng.properties. */ + private static final String ROOT_LOGGER = PREFIX + "rootLogger"; + + /** Debug property name in log4testng.properties. */ + private static final String DEBUG_PROPERTY = PREFIX + "debug"; + + /** The standard error stream (this is allways System.err except for unit tests) */ + static PrintStream err = System.err; + + /** The standard output stream (this is allways System.out except for unit tests) */ + static PrintStream out = System.out; + + /** An ordered list of level names. */ + private static final String[] levelNames = new String[LEVEL_COUNT]; + + static { + levelNames[TRACE] = "TRACE"; + levelNames[DEBUG] = "DEBUG"; + levelNames[INFO] = "INFO"; + levelNames[WARN] = "WARN"; + levelNames[ERROR] = "ERROR"; + levelNames[FATAL] = "FATAL"; + } + + /** A map from level name to level integer index (TRACE->0, DEBUG->1 ...) */ + private static final Map levelMap = Maps.newHashMap(); + + static { + for (i = 0; i < LEVEL_COUNT; ++i) { + levelMap.put(levelNames[i], i); + } + } + + /** true if the Logging system has been initialized. */ + static boolean initialized; + + /** Map from Logger names to level index (as specified in log4testng.properties) */ + private static final Map loggerLevels = Maps.newHashMap(); + + /** Map of all known loggers. */ + static final Map loggers = Maps.newHashMap(); + + /** The logging level of the root logger (defaults to warn). */ + static int rootLoggerLevel = WARN; + + /** Should log4testng log what it is doing (defaults to false). */ + static boolean debug = false; + + /** The logger's level */ + private final int level; + + /** The logger's name. */ + private final Class klass; + + private final String m_className; + + /** + * Retrieve a logger named according to the value of the pClass.getName() parameter. If the named + * logger already exists, then the existing instance will be returned. Otherwise, a new instance + * is created. By default, loggers do not have a set level but inherit it from their nearest + * ancestor with a set level. + * + * @param pClass The class' logger to retrieve. + * @return a logger named according to the value of the pClass.getName(). + */ + public static synchronized Logger getLogger(Class pClass) { + initialize(); + Logger logger = loggers.get(pClass); + if (logger != null) { + return logger; + } + int level = getLevel(pClass); + logger = new Logger(pClass, level); + loggers.put(pClass, logger); + + return logger; + } + + /** + * Check whether this logger is enabled for the TRACE Level. + * + * @return true if this logger is enabled for level TRACE, false otherwise. + */ + public boolean isTraceEnabled() { + return isLevelEnabled(TRACE); + } + + /** + * Log a message object with the TRACE level. This method first checks if this logger is TRACE + * enabled. If this logger is TRACE enabled, then it converts the message object (passed as + * parameter) to a string by invoking toString(). WARNING Note that passing a Throwable to this + * method will print the name of the Throwable but no stack trace. To print a stack trace use the + * trace(Object, Throwable) form instead. + * + * @param message the message object to log. + */ + public void trace(Object message) { + log(TRACE, message, null); + } + + /** + * Log a message object with the TRACE level including the stack trace of the Throwable t passed + * as parameter. See Logger.trace(Object) form for more detailed information. + * + * @param message the message object to log. + * @param t the exception to log, including its stack trace. + */ + public void trace(Object message, Throwable t) { + log(TRACE, message, t); + } + + /** + * Check whether this logger is enabled for the DEBUG Level. + * + * @return true if this logger is enabled for level DEBUG, false otherwise. + */ + public boolean isDebugEnabled() { + return isLevelEnabled(DEBUG); + } + + /** + * Log a message object with the DEBUG level. See Logger.trace(Object) form for more detailed + * information. + * + * @param message the message object to log. + */ + public void debug(Object message) { + log(DEBUG, message, null); + } + + /** + * Log a message object with the DEBUG level including the stack trace of the Throwable t passed + * as parameter. See Logger.trace(Object, Throwable) form for more detailed information. + * + * @param message the message object to log. + * @param t the exception to log, including its stack trace. + */ + public void debug(Object message, Throwable t) { + log(DEBUG, message, t); + } + + /** + * Check whether this logger is enabled for the INFO Level. + * + * @return true if this logger is enabled for level INFO, false otherwise. + */ + public boolean isInfoEnabled() { + return isLevelEnabled(INFO); + } + + /** + * Log a message object with the INFO level. See Logger.trace(Object) form for more detailed + * information. + * + * @param message the message object to log. + */ + public void info(Object message) { + log(INFO, message, null); + } + + /** + * Log a message object with the WARN level including the stack trace of the Throwable t passed as + * parameter. See Logger.trace(Object, Throwable) form for more detailed information. + * + * @param message the message object to log. + * @param t the exception to log, including its stack trace. + */ + public void info(Object message, Throwable t) { + log(INFO, message, t); + } + + /** + * Log a message object with the WARN level. See Logger.trace(Object) form for more detailed + * information. + * + * @param message the message object to log. + */ + public void warn(Object message) { + log(WARN, message, null); + } + + /** + * Log a message object with the ERROR level including the stack trace of the Throwable t passed + * as parameter. See Logger.trace(Object, Throwable) form for more detailed information. + * + * @param message the message object to log. + * @param t the exception to log, including its stack trace. + */ + public void warn(Object message, Throwable t) { + log(WARN, message, t); + } + + /** + * Log a message object with the ERROR level. See Logger.trace(Object) form for more detailed + * information. + * + * @param message the message object to log. + */ + public void error(Object message) { + log(ERROR, message, null); + } + + /** + * Log a message object with the DEBUG level including the stack trace of the Throwable t passed + * as parameter. See Logger.trace(Object, Throwable) form for more detailed information. + * + * @param message the message object to log. + * @param t the exception to log, including its stack trace. + */ + public void error(Object message, Throwable t) { + log(ERROR, message, t); + } + + /** + * Log a message object with the FATAL level. See Logger.trace(Object) form for more detailed + * information. + * + * @param message the message object to log. + */ + public void fatal(Object message) { + log(FATAL, message, null); + } + + /** + * Log a message object with the FATAL level including the stack trace of the Throwable t passed + * as parameter. See Logger.trace(Object, Throwable) form for more detailed information. + * + * @param message the message object to log. + * @param t the exception to log, including its stack trace. + */ + public void fatal(Object message, Throwable t) { + log(FATAL, message, t); + } + + private Logger(Class pClass, int pLevel) { + level = pLevel; + klass = pClass; + m_className = pClass.getName().substring(pClass.getName().lastIndexOf('.') + 1); + } + + private static synchronized void initialize() { + if (initialized) { + return; + } + + // We flag as initialized right away because if anything goes wrong + // We still consider it initialized. TODO Is this OK? + initialized = true; + + InputStream is = + Thread.currentThread().getContextClassLoader().getResourceAsStream("log4testng.properties"); + if (is == null) { + return; + } + Properties properties = new Properties(); + try { + properties.load(is); + } catch (IOException e) { + throw new RuntimeException(e); + } + + checkProperties(properties); + } + + static void checkProperties(Properties pProperties) { + { + // See if we want to debug log4testng + String debugStr = pProperties.getProperty(DEBUG_PROPERTY); + if (debugStr != null) { + if (debugStr.equalsIgnoreCase("true")) { + debug = true; + } else if (debugStr.equalsIgnoreCase("false")) { + debug = false; + } else { + throw new IllegalArgumentException("Unknown " + DEBUG_PROPERTY + " value " + debugStr); + } + } + loglog4testng("log4testng.debug set to " + debug); + } + + { + // Set the value of the root logger (if any). + String rootLevelStr = pProperties.getProperty(ROOT_LOGGER); + if (rootLevelStr != null) { + Integer ilevel = levelMap.get(rootLevelStr.toUpperCase()); + if (ilevel == null) { + throw new IllegalArgumentException( + "Unknown level for log4testng.rootLogger " + + rootLevelStr + + " in log4testng.properties"); + } + rootLoggerLevel = ilevel; + loglog4testng("Root level logger set to " + rootLevelStr + " level."); + } + } + + Iterator it = pProperties.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry entry = (Entry) it.next(); + String logger = (String) entry.getKey(); + String level = (String) entry.getValue(); + + if (!logger.startsWith(PREFIX)) { + throw new IllegalArgumentException("Illegal property value: " + logger); + } + if (logger.equals(DEBUG_PROPERTY)) { + // Already handled + } else if (logger.equals(ROOT_LOGGER)) { + // Already handled + } else { + if (!logger.startsWith(LOGGER_PREFIX)) { + throw new IllegalArgumentException("Illegal property value: " + logger); + } + + Integer ilevel = levelMap.get(level.toUpperCase()); + if (ilevel == null) { + throw new IllegalArgumentException( + "Unknown level " + level + " for logger " + logger + " in log4testng.properties"); + } + + loggerLevels.put(logger.substring(LOGGER_PREFIX.length()), ilevel); + loglog4testng("logger " + logger + " set to " + ilevel + " level."); + } + } + } + + /** + * Returns the level associated to the current class. The level is obtain by searching for a + * logger in the "testng-logging.properties" resource. For example, if class is + * "org.testng.TestNG" the the following loggers are searched in this order: + * + *
    + *
  1. "org.testng.TestNG" + *
  2. "org.testng" + *
  3. "org" + *
  4. The root level + *
+ * + * @param pClass the class name used for logger name. + * @return the level associated to the current class. + */ + private static int getLevel(Class pClass) { + String name = pClass.getName(); + loglog4testng("Getting level for logger " + name); + while (true) { + Integer level = loggerLevels.get(name); + if (level != null) { + loglog4testng("Found level " + level + " for logger " + name); + + return level; + } + int dot = name.lastIndexOf('.'); + if (dot == -1) { + loglog4testng("Found level " + rootLoggerLevel + " for root logger"); + + // Logger name not found. Defaults to root logger level. + return rootLoggerLevel; + } + name = name.substring(0, dot); + } + } + + private boolean isLevelEnabled(int pLevel) { + return level <= pLevel; + } + + private void log(int pLevel, Object pMessage, Throwable pT) { + if (isLevelEnabled(pLevel)) { + PrintStream ps = (pLevel >= ERROR) ? err : out; + if (null != pT) { + synchronized (ps) { + ps.println("[" + m_className + "] [" + levelNames[pLevel] + "] " + pMessage); + pT.printStackTrace(ps); + } + } else { + ps.println("[" + m_className + "] [" + levelNames[pLevel] + "] " + pMessage); + } + } + } + + /** + * Logs the message to System.out of debug is on. + * + * @param pmessage the message to log to the console + */ + private static void loglog4testng(String pmessage) { + if (debug) { + out.println("[log4testng] [debug] " + pmessage); + } + } +} diff --git a/testng-core/src/main/java/org/testng/internal/BaseTestMethod.java b/testng-core/src/main/java/org/testng/internal/BaseTestMethod.java index 5818fdd0a9..73292ecbc9 100644 --- a/testng-core/src/main/java/org/testng/internal/BaseTestMethod.java +++ b/testng-core/src/main/java/org/testng/internal/BaseTestMethod.java @@ -1,773 +1,773 @@ -package org.testng.internal; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.Callable; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.regex.Pattern; - -import org.testng.IClass; -import org.testng.IRetryAnalyzer; -import org.testng.ITestClass; -import org.testng.ITestNGMethod; -import org.testng.ITestObjectFactory; -import org.testng.ITestResult; -import org.testng.annotations.ITestOrConfiguration; -import org.testng.collections.Lists; -import org.testng.collections.Maps; -import org.testng.collections.Sets; -import org.testng.internal.annotations.DisabledRetryAnalyzer; -import org.testng.internal.annotations.IAnnotationFinder; -import org.testng.internal.invokers.IInvocationStatus; -import org.testng.xml.XmlTest; - -/** Superclass to represent both @Test and @Configuration methods. */ -public abstract class BaseTestMethod implements ITestNGMethod, IInvocationStatus { - - private static final Pattern SPACE_SEPARATOR_PATTERN = Pattern.compile(" +"); - - /** - * The test class on which the test method was found. Note that this is not necessarily the - * declaring class. - */ - protected ITestClass m_testClass; - - protected final Class m_methodClass; - protected final ConstructorOrMethod m_method; - private String m_signature; - protected String m_id = ""; - protected long m_date = -1; - protected final IAnnotationFinder m_annotationFinder; - protected String[] m_groups = {}; - protected String[] m_groupsDependedUpon = {}; - protected String[] m_methodsDependedUpon = {}; - protected String[] m_beforeGroups = {}; - protected String[] m_afterGroups = {}; - private boolean m_isAlwaysRun; - private boolean m_enabled; - - private final String m_methodName; - // If a depended group is not found - private String m_missingGroup; - private String m_description = null; - protected AtomicInteger m_currentInvocationCount = new AtomicInteger(0); - private int m_parameterInvocationCount = 1; - private Callable m_moreInvocationChecker; - private IRetryAnalyzer m_retryAnalyzer = null; - private Class m_retryAnalyzerClass = null; - private boolean m_skipFailedInvocations = true; - private long m_invocationTimeOut = 0L; - - private List m_invocationNumbers = Lists.newArrayList(); - private final Collection m_failedInvocationNumbers = new ConcurrentLinkedQueue<>(); - private long m_timeOut = 0; - - private boolean m_ignoreMissingDependencies; - private int m_priority; - private int m_interceptedPriority; - - private XmlTest m_xmlTest; - private final Object m_instance; - - private final Map m_testMethodToRetryAnalyzer = Maps.newConcurrentMap(); - protected final ITestObjectFactory m_objectFactory; - - public BaseTestMethod( - ITestObjectFactory objectFactory, - String methodName, - ConstructorOrMethod com, - IAnnotationFinder annotationFinder, - Object instance) { - m_objectFactory = objectFactory; - m_methodClass = com.getDeclaringClass(); - m_method = com; - m_methodName = methodName; - m_annotationFinder = annotationFinder; - m_instance = instance; - } - - /** {@inheritDoc} */ - @Override - public boolean isAlwaysRun() { - return m_isAlwaysRun; - } - - protected void setAlwaysRun(boolean alwaysRun) { - m_isAlwaysRun = alwaysRun; - } - - /** {@inheritDoc} */ - @Override - public Class getRealClass() { - return m_methodClass; - } - - /** {@inheritDoc} */ - @Override - public ITestClass getTestClass() { - return m_testClass; - } - - /** {@inheritDoc} */ - @Override - public void setTestClass(ITestClass tc) { - if (tc == null) { - throw new IllegalArgumentException("test class cannot be null"); - } - boolean assignable = m_method.getDeclaringClass().isAssignableFrom(tc.getRealClass()); - if (!assignable) { - throw new IllegalArgumentException( - "mismatch in classes between " - + tc.getName() - + " and " - + m_method.getDeclaringClass().getName()); - } - - m_testClass = tc; - } - - /** {@inheritDoc} */ - @Override - public String getMethodName() { - return m_methodName; - } - - @Override - public Object getInstance() { - return IParameterInfo.embeddedInstance(m_instance); - } - - /** {@inheritDoc} */ - @Override - public long[] getInstanceHashCodes() { - return m_testClass.getInstanceHashCodes(); - } - - /** - * {@inheritDoc} - * - * @return the addition of groups defined on the class and on this method. - */ - @Override - public String[] getGroups() { - return m_groups; - } - - /** {@inheritDoc} */ - @Override - public String[] getGroupsDependedUpon() { - return m_groupsDependedUpon; - } - - /** {@inheritDoc} */ - @Override - public String[] getMethodsDependedUpon() { - return m_methodsDependedUpon; - } - - /** {@inheritDoc} */ - @Override - public boolean isTest() { - return false; - } - - /** {@inheritDoc} */ - @Override - public boolean isBeforeSuiteConfiguration() { - return false; - } - - /** {@inheritDoc} */ - @Override - public boolean isAfterSuiteConfiguration() { - return false; - } - - /** {@inheritDoc} */ - @Override - public boolean isBeforeTestConfiguration() { - return false; - } - - /** {@inheritDoc} */ - @Override - public boolean isAfterTestConfiguration() { - return false; - } - - /** {@inheritDoc} */ - @Override - public boolean isBeforeGroupsConfiguration() { - return false; - } - - /** {@inheritDoc} */ - @Override - public boolean isAfterGroupsConfiguration() { - return false; - } - - /** {@inheritDoc} */ - @Override - public boolean isBeforeClassConfiguration() { - return false; - } - - /** {@inheritDoc} */ - @Override - public boolean isAfterClassConfiguration() { - return false; - } - - /** {@inheritDoc} */ - @Override - public boolean isBeforeMethodConfiguration() { - return false; - } - - /** {@inheritDoc} */ - @Override - public boolean isAfterMethodConfiguration() { - return false; - } - - /** {@inheritDoc} */ - @Override - public long getTimeOut() { - return m_timeOut != 0 ? m_timeOut : (m_xmlTest != null ? m_xmlTest.getTimeOut(0) : 0); - } - - @Override - public void setTimeOut(long timeOut) { - m_timeOut = timeOut; - } - - /** - * {@inheritDoc} - * - * @return the number of times this method needs to be invoked. - */ - @Override - public int getInvocationCount() { - return 1; - } - - /** No-op. */ - @Override - public void setInvocationCount(int counter) {} - - /** {@inheritDoc} Default value for successPercentage. */ - @Override - public int getSuccessPercentage() { - return 100; - } - - /** {@inheritDoc} */ - @Override - public String getId() { - return m_id; - } - - /** {@inheritDoc} */ - @Override - public void setId(String id) { - m_id = id; - } - - /** - * {@inheritDoc} - * - * @return Returns the date. - */ - @Override - public long getDate() { - return m_date; - } - - /** - * {@inheritDoc} - * - * @param date The date to set. - */ - @Override - public void setDate(long date) { - m_date = date; - } - - /** {@inheritDoc} */ - @Override - public boolean canRunFromClass(IClass testClass) { - return m_methodClass.isAssignableFrom(testClass.getRealClass()); - } - - /** - * {@inheritDoc} Compares two BaseTestMethod using the test class then the associated Java Method. - */ - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - - BaseTestMethod other = (BaseTestMethod) obj; - - boolean isEqual = - m_testClass == null - ? other.m_testClass == null - : other.m_testClass != null - && m_testClass.getRealClass().equals(other.m_testClass.getRealClass()) - && getInstance() == other.getInstance(); - - return isEqual && getConstructorOrMethod().equals(other.getConstructorOrMethod()); - } - - /** - * {@inheritDoc} This implementation returns the associated Java Method's hash code. - * - * @return the associated Java Method's hash code. - */ - @Override - public int hashCode() { - return m_method.hashCode(); - } - - protected void initGroups(Class annotationClass) { - ITestOrConfiguration annotation = - getAnnotationFinder().findAnnotation(getConstructorOrMethod(), annotationClass); - Object object = getInstance(); - Class clazz = getConstructorOrMethod().getDeclaringClass(); - if (object != null) { - clazz = object.getClass(); - } - ITestOrConfiguration classAnnotation = getAnnotationFinder().findAnnotation(clazz, annotationClass); - - setGroups( - getStringArray( - null != annotation ? annotation.getGroups() : null, - null != classAnnotation ? classAnnotation.getGroups() : null)); - - initRestOfGroupDependencies(annotationClass); - } - - protected void initBeforeAfterGroups( - Class annotationClass, String[] groups) { - String[] groupsAtMethodLevel = - calculateGroupsTouseConsideringValuesAndGroupValues(annotationClass, groups); - // @BeforeGroups and @AfterGroups annotation cannot be used at Class level. So its always null - setGroups(getStringArray(groupsAtMethodLevel, null)); - initRestOfGroupDependencies(annotationClass); - } - - private String[] calculateGroupsTouseConsideringValuesAndGroupValues( - Class annotationClass, String[] groups) { - if (groups == null || groups.length == 0) { - ITestOrConfiguration annotation = - getAnnotationFinder().findAnnotation(getConstructorOrMethod(), annotationClass); - groups = null != annotation ? annotation.getGroups() : null; - } - return groups; - } - - private void initRestOfGroupDependencies(Class annotationClass) { - // - // Init groups depended upon - // - ITestOrConfiguration annotation = - getAnnotationFinder().findAnnotation(getConstructorOrMethod(), annotationClass); - ITestOrConfiguration classAnnotation = - getAnnotationFinder() - .findAnnotation(getConstructorOrMethod().getDeclaringClass(), annotationClass); - - Map> xgd = calculateXmlGroupDependencies(m_xmlTest); - List xmlGroupDependencies = Lists.newArrayList(); - for (String g : getGroups()) { - Set gdu = xgd.get(g); - if (gdu != null) { - xmlGroupDependencies.addAll(gdu); - } - } - setGroupsDependedUpon( - getStringArray( - null != annotation ? annotation.getDependsOnGroups() : null, - null != classAnnotation ? classAnnotation.getDependsOnGroups() : null), - xmlGroupDependencies); - - String[] methodsDependedUpon = - getStringArray( - null != annotation ? annotation.getDependsOnMethods() : null, - null != classAnnotation ? classAnnotation.getDependsOnMethods() : null); - // Qualify these methods if they don't have a package - for (int i = 0; i < methodsDependedUpon.length; i++) { - String m = methodsDependedUpon[i]; - if (!m.contains(".")) { - m = MethodHelper.calculateMethodCanonicalName(m_methodClass, methodsDependedUpon[i]); - methodsDependedUpon[i] = m != null ? m : methodsDependedUpon[i]; - } - } - setMethodsDependedUpon(methodsDependedUpon); - } - - private static Map> calculateXmlGroupDependencies(XmlTest xmlTest) { - Map> result = Maps.newHashMap(); - if (xmlTest == null) { - return result; - } - - for (Map.Entry e : xmlTest.getXmlDependencyGroups().entrySet()) { - String name = e.getKey(); - String dependsOn = e.getValue(); - Set set = result.computeIfAbsent(name, s -> Sets.newHashSet()); - set.addAll(Arrays.asList(SPACE_SEPARATOR_PATTERN.split(dependsOn))); - } - - return result; - } - - protected IAnnotationFinder getAnnotationFinder() { - return m_annotationFinder; - } - - protected IClass getIClass() { - return m_testClass; - } - - static StringBuilder stringify(String cls, ConstructorOrMethod method) { - StringBuilder result = - new StringBuilder(cls).append(".").append(method.getName()).append("("); - return result.append(method.stringifyParameterTypes()).append(")"); - } - - private String computeSignature() { - String classLong = m_method.getDeclaringClass().getName(); - String cls = classLong.substring(classLong.lastIndexOf(".") + 1); - StringBuilder result = stringify(cls, m_method); - result - .append("[pri:") - .append(getPriority()) - .append(", instance:") - .append(getInstance()) - .append(instanceParameters()) - .append("]"); - - return result.toString(); - } - - public String getSimpleName() { - return m_method.getDeclaringClass().getSimpleName() + "." + m_method.getName(); - } - - private String instanceParameters() { - IParameterInfo instance = getFactoryMethodParamsInfo(); - if (instance != null ) { - return ", instance params:" + Arrays.toString(instance.getParameters()); - } - return ""; - } - - protected String getSignature() { - if (m_signature == null) { - m_signature = computeSignature(); - } - return m_signature; - } - - /** {@inheritDoc} */ - @Override - public String toString() { - return getSignature(); - } - - protected String[] getStringArray(String[] methodArray, String[] classArray) { - final Set vResult = Sets.newHashSet(); - if (null != methodArray) { - Collections.addAll(vResult, methodArray); - } - if (null != classArray) { - Collections.addAll(vResult, classArray); - } - return vResult.toArray(new String[0]); - } - - protected void setGroups(String[] groups) { - m_groups = groups; - } - - protected void setGroupsDependedUpon(String[] groups, Collection xmlGroupDependencies) { - List l = Lists.newArrayList(); - l.addAll(Arrays.asList(groups)); - l.addAll(xmlGroupDependencies); - m_groupsDependedUpon = l.toArray(new String[0]); - } - - protected void setMethodsDependedUpon(String[] methods) { - m_methodsDependedUpon = methods; - } - - /** {@inheritDoc} */ - @Override - public void addMethodDependedUpon(String method) { - String[] newMethods = new String[m_methodsDependedUpon.length + 1]; - newMethods[0] = method; - System.arraycopy(m_methodsDependedUpon, 0, newMethods, 1, m_methodsDependedUpon.length); - m_methodsDependedUpon = newMethods; - } - - /** {@inheritDoc} */ - @Override - public String getMissingGroup() { - return m_missingGroup; - } - - /** {@inheritDoc} */ - @Override - public void setMissingGroup(String group) { - m_missingGroup = group; - } - - /** {@inheritDoc} */ - @Override - public int getThreadPoolSize() { - return 0; - } - - /** No-op. */ - @Override - public void setThreadPoolSize(int threadPoolSize) {} - - @Override - public void setDescription(String description) { - m_description = description; - } - - /** {@inheritDoc} */ - @Override - public String getDescription() { - return m_description; - } - - public void setEnabled(boolean enabled) { - m_enabled = enabled; - } - - @Override - public boolean getEnabled() { - return m_enabled; - } - - /** {@inheritDoc} */ - @Override - public String[] getBeforeGroups() { - return m_beforeGroups; - } - - /** {@inheritDoc} */ - @Override - public String[] getAfterGroups() { - return m_afterGroups; - } - - @Override - public void incrementCurrentInvocationCount() { - m_currentInvocationCount.incrementAndGet(); - } - - @Override - public int getCurrentInvocationCount() { - return m_currentInvocationCount.get(); - } - - @Override - public void setParameterInvocationCount(int n) { - m_parameterInvocationCount = n; - } - - @Override - public int getParameterInvocationCount() { - return m_parameterInvocationCount; - } - - @Override - public void setMoreInvocationChecker(Callable moreInvocationChecker) { - m_moreInvocationChecker = moreInvocationChecker; - } - - @Override - public boolean hasMoreInvocation() { - if (m_moreInvocationChecker != null) { - try { - return m_moreInvocationChecker.call(); - } catch (Exception e) { - // Should never append - throw new RuntimeException(e); - } - } - return getCurrentInvocationCount() < getInvocationCount() * getParameterInvocationCount(); - } - - @Override - public abstract ITestNGMethod clone(); - - @Override - public IRetryAnalyzer getRetryAnalyzer(ITestResult result) { - return getRetryAnalyzerConsideringMethodParameters(result); - } - - @Override - public void setRetryAnalyzerClass(Class clazz) { - m_retryAnalyzerClass = clazz == null ? DisabledRetryAnalyzer.class : clazz; - m_retryAnalyzer = m_objectFactory.newInstance(m_retryAnalyzerClass); - } - - @Override - public Class getRetryAnalyzerClass() { - return m_retryAnalyzerClass; - } - - @Override - public boolean skipFailedInvocations() { - return m_skipFailedInvocations; - } - - @Override - public void setSkipFailedInvocations(boolean s) { - m_skipFailedInvocations = s; - } - - public void setInvocationTimeOut(long timeOut) { - m_invocationTimeOut = timeOut; - } - - @Override - public long getInvocationTimeOut() { - return m_invocationTimeOut; - } - - @Override - public boolean ignoreMissingDependencies() { - return m_ignoreMissingDependencies; - } - - @Override - public void setIgnoreMissingDependencies(boolean i) { - m_ignoreMissingDependencies = i; - } - - @Override - public List getInvocationNumbers() { - return m_invocationNumbers; - } - - @Override - public void setInvocationNumbers(List numbers) { - m_invocationNumbers = numbers; - } - - @Override - public List getFailedInvocationNumbers() { - return new ArrayList<>(m_failedInvocationNumbers); - } - - @Override - public void addFailedInvocationNumber(int number) { - m_failedInvocationNumbers.add(number); - } - - @Override - public int getPriority() { - return m_priority; - } - - @Override - public void setPriority(int priority) { - m_priority = priority; - } - - @Override - public int getInterceptedPriority() { - return m_interceptedPriority; - } - - @Override - public void setInterceptedPriority(int priority) { - m_interceptedPriority = priority; - } - - @Override - public XmlTest getXmlTest() { - return m_xmlTest; - } - - public void setXmlTest(XmlTest xmlTest) { - m_xmlTest = xmlTest; - } - - @Override - public ConstructorOrMethod getConstructorOrMethod() { - return m_method; - } - - @Override - public Class[] getParameterTypes() { - return m_method.getParameterTypes(); - } - - @Override - public Map findMethodParameters(XmlTest test) { - return XmlTestUtils.findMethodParameters(test, getTestClass().getName(), getMethodName()); - } - - @Override - public String getQualifiedName() { - return getRealClass().getName() + "." + getMethodName(); - } - - @Override - public IParameterInfo getFactoryMethodParamsInfo() { - if (m_instance instanceof IParameterInfo) { - return (IParameterInfo) m_instance; - } - return null; - } - - private long invocationTime; - - @Override - public void setInvokedAt(long date) { - this.invocationTime = date; - } - - @Override - public long getInvocationTime() { - return invocationTime; - } - - private IRetryAnalyzer getRetryAnalyzerConsideringMethodParameters(ITestResult tr) { - Object[] key = tr.getParameters(); - IRetryAnalyzer retryAnalyzer = this.m_retryAnalyzer; - if (key != null && key.length != 0 && retryAnalyzer != null) { - final String keyAsString = getSimpleName() + "#" + getParameterInvocationCount(); - final IRetryAnalyzer currentRetryAnalyzerInMap = m_testMethodToRetryAnalyzer.get(keyAsString); - if (currentRetryAnalyzerInMap == null || currentRetryAnalyzerInMap.getClass() != retryAnalyzer.getClass()) { - retryAnalyzer = m_testMethodToRetryAnalyzer.compute( - keyAsString, - (s, ra) -> m_objectFactory.newInstance(this.m_retryAnalyzer.getClass())); - } else { - retryAnalyzer = currentRetryAnalyzerInMap; - } - } - return retryAnalyzer; - } - -} +package org.testng.internal; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.regex.Pattern; + +import org.testng.IClass; +import org.testng.IRetryAnalyzer; +import org.testng.ITestClass; +import org.testng.ITestNGMethod; +import org.testng.ITestObjectFactory; +import org.testng.ITestResult; +import org.testng.annotations.ITestOrConfiguration; +import org.testng.collections.Lists; +import org.testng.collections.Maps; +import org.testng.collections.Sets; +import org.testng.internal.annotations.DisabledRetryAnalyzer; +import org.testng.internal.annotations.IAnnotationFinder; +import org.testng.internal.invokers.IInvocationStatus; +import org.testng.xml.XmlTest; + +/** Superclass to represent both @Test and @Configuration methods. */ +public abstract class BaseTestMethod implements ITestNGMethod, IInvocationStatus { + + private static final Pattern SPACE_SEPARATOR_PATTERN = Pattern.compile(" +"); + + /** + * The test class on which the test method was found. Note that this is not necessarily the + * declaring class. + */ + protected ITestClass m_testClass; + + protected final Class m_methodClass; + protected final ConstructorOrMethod m_method; + private String m_signature; + protected String m_id = ""; + protected long m_date = -1; + protected final IAnnotationFinder m_annotationFinder; + protected String[] m_groups = {}; + protected String[] m_groupsDependedUpon = {}; + protected String[] m_methodsDependedUpon = {}; + protected String[] m_beforeGroups = {}; + protected String[] m_afterGroups = {}; + private boolean m_isAlwaysRun; + private boolean m_enabled; + + private final String m_methodName; + // If a depended group is not found + private String m_missingGroup; + private String m_description = null; + protected AtomicInteger m_currentInvocationCount = new AtomicInteger(0); + private int m_parameterInvocationCount = 1; + private Callable m_moreInvocationChecker; + private IRetryAnalyzer m_retryAnalyzer = null; + private Class m_retryAnalyzerClass = null; + private boolean m_skipFailedInvocations = true; + private long m_invocationTimeOut = 0L; + + private List m_invocationNumbers = Lists.newArrayList(); + private final Collection m_failedInvocationNumbers = new ConcurrentLinkedQueue<>(); + private long m_timeOut = 0; + + private boolean m_ignoreMissingDependencies; + private int m_priority; + private int m_interceptedPriority; + + private XmlTest m_xmlTest; + private final Object m_instance; + + private final Map m_testMethodToRetryAnalyzer = Maps.newConcurrentMap(); + protected final ITestObjectFactory m_objectFactory; + + public BaseTestMethod( + ITestObjectFactory objectFactory, + String methodName, + ConstructorOrMethod com, + IAnnotationFinder annotationFinder, + Object instance) { + m_objectFactory = objectFactory; + m_methodClass = com.getDeclaringClass(); + m_method = com; + m_methodName = methodName; + m_annotationFinder = annotationFinder; + m_instance = instance; + } + + /** {@inheritDoc} */ + @Override + public boolean isAlwaysRun() { + return m_isAlwaysRun; + } + + protected void setAlwaysRun(boolean alwaysRun) { + m_isAlwaysRun = alwaysRun; + } + + /** {@inheritDoc} */ + @Override + public Class getRealClass() { + return m_methodClass; + } + + /** {@inheritDoc} */ + @Override + public ITestClass getTestClass() { + return m_testClass; + } + + /** {@inheritDoc} */ + @Override + public void setTestClass(ITestClass tc) { + if (tc == null) { + throw new IllegalArgumentException("test class cannot be null"); + } + boolean assignable = m_method.getDeclaringClass().isAssignableFrom(tc.getRealClass()); + if (!assignable) { + throw new IllegalArgumentException( + "mismatch in classes between " + + tc.getName() + + " and " + + m_method.getDeclaringClass().getName()); + } + + m_testClass = tc; + } + + /** {@inheritDoc} */ + @Override + public String getMethodName() { + return m_methodName; + } + + @Override + public Object getInstance() { + return IParameterInfo.embeddedInstance(m_instance); + } + + /** {@inheritDoc} */ + @Override + public long[] getInstanceHashCodes() { + return m_testClass.getInstanceHashCodes(); + } + + /** + * {@inheritDoc} + * + * @return the addition of groups defined on the class and on this method. + */ + @Override + public String[] getGroups() { + return m_groups; + } + + /** {@inheritDoc} */ + @Override + public String[] getGroupsDependedUpon() { + return m_groupsDependedUpon; + } + + /** {@inheritDoc} */ + @Override + public String[] getMethodsDependedUpon() { + return m_methodsDependedUpon; + } + + /** {@inheritDoc} */ + @Override + public boolean isTest() { + return false; + } + + /** {@inheritDoc} */ + @Override + public boolean isBeforeSuiteConfiguration() { + return false; + } + + /** {@inheritDoc} */ + @Override + public boolean isAfterSuiteConfiguration() { + return false; + } + + /** {@inheritDoc} */ + @Override + public boolean isBeforeTestConfiguration() { + return false; + } + + /** {@inheritDoc} */ + @Override + public boolean isAfterTestConfiguration() { + return false; + } + + /** {@inheritDoc} */ + @Override + public boolean isBeforeGroupsConfiguration() { + return false; + } + + /** {@inheritDoc} */ + @Override + public boolean isAfterGroupsConfiguration() { + return false; + } + + /** {@inheritDoc} */ + @Override + public boolean isBeforeClassConfiguration() { + return false; + } + + /** {@inheritDoc} */ + @Override + public boolean isAfterClassConfiguration() { + return false; + } + + /** {@inheritDoc} */ + @Override + public boolean isBeforeMethodConfiguration() { + return false; + } + + /** {@inheritDoc} */ + @Override + public boolean isAfterMethodConfiguration() { + return false; + } + + /** {@inheritDoc} */ + @Override + public long getTimeOut() { + return m_timeOut != 0 ? m_timeOut : (m_xmlTest != null ? m_xmlTest.getTimeOut(0) : 0); + } + + @Override + public void setTimeOut(long timeOut) { + m_timeOut = timeOut; + } + + /** + * {@inheritDoc} + * + * @return the number of times this method needs to be invoked. + */ + @Override + public int getInvocationCount() { + return 1; + } + + /** No-op. */ + @Override + public void setInvocationCount(int counter) {} + + /** {@inheritDoc} Default value for successPercentage. */ + @Override + public int getSuccessPercentage() { + return 100; + } + + /** {@inheritDoc} */ + @Override + public String getId() { + return m_id; + } + + /** {@inheritDoc} */ + @Override + public void setId(String id) { + m_id = id; + } + + /** + * {@inheritDoc} + * + * @return Returns the date. + */ + @Override + public long getDate() { + return m_date; + } + + /** + * {@inheritDoc} + * + * @param date The date to set. + */ + @Override + public void setDate(long date) { + m_date = date; + } + + /** {@inheritDoc} */ + @Override + public boolean canRunFromClass(IClass testClass) { + return m_methodClass.isAssignableFrom(testClass.getRealClass()); + } + + /** + * {@inheritDoc} Compares two BaseTestMethod using the test class then the associated Java Method. + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + + BaseTestMethod other = (BaseTestMethod) obj; + + boolean isEqual = + m_testClass == null + ? other.m_testClass == null + : other.m_testClass != null + && m_testClass.getRealClass().equals(other.m_testClass.getRealClass()) + && getInstance() == other.getInstance(); + + return isEqual && getConstructorOrMethod().equals(other.getConstructorOrMethod()); + } + + /** + * {@inheritDoc} This implementation returns the associated Java Method's hash code. + * + * @return the associated Java Method's hash code. + */ + @Override + public int hashCode() { + return m_method.hashCode(); + } + + protected void initGroups(Class annotationClass) { + ITestOrConfiguration annotation = + getAnnotationFinder().findAnnotation(getConstructorOrMethod(), annotationClass); + Object object = getInstance(); + Class clazz = getConstructorOrMethod().getDeclaringClass(); + if (object != null) { + clazz = object.getClass(); + } + ITestOrConfiguration classAnnotation = getAnnotationFinder().findAnnotation(clazz, annotationClass); + + setGroups( + getStringArray( + null != annotation ? annotation.getGroups() : null, + null != classAnnotation ? classAnnotation.getGroups() : null)); + + initRestOfGroupDependencies(annotationClass); + } + + protected void initBeforeAfterGroups( + Class annotationClass, String[] groups) { + String[] groupsAtMethodLevel = + calculateGroupsTouseConsideringValuesAndGroupValues(annotationClass, groups); + // @BeforeGroups and @AfterGroups annotation cannot be used at Class level. So its always null + setGroups(getStringArray(groupsAtMethodLevel, null)); + initRestOfGroupDependencies(annotationClass); + } + + private String[] calculateGroupsTouseConsideringValuesAndGroupValues( + Class annotationClass, String[] groups) { + if (groups == null || groups.length == 0) { + ITestOrConfiguration annotation = + getAnnotationFinder().findAnnotation(getConstructorOrMethod(), annotationClass); + groups = null != annotation ? annotation.getGroups() : null; + } + return groups; + } + + private void initRestOfGroupDependencies(Class annotationClass) { + // + // Init groups depended upon + // + ITestOrConfiguration annotation = + getAnnotationFinder().findAnnotation(getConstructorOrMethod(), annotationClass); + ITestOrConfiguration classAnnotation = + getAnnotationFinder() + .findAnnotation(getConstructorOrMethod().getDeclaringClass(), annotationClass); + + Map> xgd = calculateXmlGroupDependencies(m_xmlTest); + List xmlGroupDependencies = Lists.newArrayList(); + for (String g : getGroups()) { + Set gdu = xgd.get(g); + if (gdu != null) { + xmlGroupDependencies.addAll(gdu); + } + } + setGroupsDependedUpon( + getStringArray( + null != annotation ? annotation.getDependsOnGroups() : null, + null != classAnnotation ? classAnnotation.getDependsOnGroups() : null), + xmlGroupDependencies); + + String[] methodsDependedUpon = + getStringArray( + null != annotation ? annotation.getDependsOnMethods() : null, + null != classAnnotation ? classAnnotation.getDependsOnMethods() : null); + // Qualify these methods if they don't have a package + for (int i = 0; i < methodsDependedUpon.length; i++) { + String m = methodsDependedUpon[i]; + if (!m.contains(".")) { + m = MethodHelper.calculateMethodCanonicalName(m_methodClass, methodsDependedUpon[i]); + methodsDependedUpon[i] = m != null ? m : methodsDependedUpon[i]; + } + } + setMethodsDependedUpon(methodsDependedUpon); + } + + private static Map> calculateXmlGroupDependencies(XmlTest xmlTest) { + Map> result = Maps.newHashMap(); + if (xmlTest == null) { + return result; + } + + for (Map.Entry e : xmlTest.getXmlDependencyGroups().entrySet()) { + String name = e.getKey(); + String dependsOn = e.getValue(); + Set set = result.computeIfAbsent(name, s -> Sets.newHashSet()); + set.addAll(Arrays.asList(SPACE_SEPARATOR_PATTERN.split(dependsOn))); + } + + return result; + } + + protected IAnnotationFinder getAnnotationFinder() { + return m_annotationFinder; + } + + protected IClass getIClass() { + return m_testClass; + } + + static StringBuilder stringify(String cls, ConstructorOrMethod method) { + StringBuilder result = + new StringBuilder(cls).append(".").append(method.getName()).append("("); + return result.append(method.stringifyParameterTypes()).append(")"); + } + + private String computeSignature() { + String classLong = m_method.getDeclaringClass().getName(); + String cls = classLong.substring(classLong.lastIndexOf(".") + 1); + StringBuilder result = stringify(cls, m_method); + result + .append("[pri:") + .append(getPriority()) + .append(", instance:") + .append(getInstance()) + .append(instanceParameters()) + .append("]"); + + return result.toString(); + } + + public String getSimpleName() { + return m_method.getDeclaringClass().getSimpleName() + "." + m_method.getName(); + } + + private String instanceParameters() { + IParameterInfo instance = getFactoryMethodParamsInfo(); + if (instance != null ) { + return ", instance params:" + Arrays.toString(instance.getParameters()); + } + return ""; + } + + protected String getSignature() { + if (m_signature == null) { + m_signature = computeSignature(); + } + return m_signature; + } + + /** {@inheritDoc} */ + @Override + public String toString() { + return getSignature(); + } + + protected String[] getStringArray(String[] methodArray, String[] classArray) { + final Set vResult = Sets.newHashSet(); + if (null != methodArray) { + Collections.addAll(vResult, methodArray); + } + if (null != classArray) { + Collections.addAll(vResult, classArray); + } + return vResult.toArray(new String[0]); + } + + protected void setGroups(String[] groups) { + m_groups = groups; + } + + protected void setGroupsDependedUpon(String[] groups, Collection xmlGroupDependencies) { + List l = Lists.newArrayList(); + l.addAll(Arrays.asList(groups)); + l.addAll(xmlGroupDependencies); + m_groupsDependedUpon = l.toArray(new String[0]); + } + + protected void setMethodsDependedUpon(String[] methods) { + m_methodsDependedUpon = methods; + } + + /** {@inheritDoc} */ + @Override + public void addMethodDependedUpon(String method) { + String[] newMethods = new String[m_methodsDependedUpon.length + 1]; + newMethods[0] = method; + System.arraycopy(m_methodsDependedUpon, 0, newMethods, 1, m_methodsDependedUpon.length); + m_methodsDependedUpon = newMethods; + } + + /** {@inheritDoc} */ + @Override + public String getMissingGroup() { + return m_missingGroup; + } + + /** {@inheritDoc} */ + @Override + public void setMissingGroup(String group) { + m_missingGroup = group; + } + + /** {@inheritDoc} */ + @Override + public int getThreadPoolSize() { + return 0; + } + + /** No-op. */ + @Override + public void setThreadPoolSize(int threadPoolSize) {} + + @Override + public void setDescription(String description) { + m_description = description; + } + + /** {@inheritDoc} */ + @Override + public String getDescription() { + return m_description; + } + + public void setEnabled(boolean enabled) { + m_enabled = enabled; + } + + @Override + public boolean getEnabled() { + return m_enabled; + } + + /** {@inheritDoc} */ + @Override + public String[] getBeforeGroups() { + return m_beforeGroups; + } + + /** {@inheritDoc} */ + @Override + public String[] getAfterGroups() { + return m_afterGroups; + } + + @Override + public void incrementCurrentInvocationCount() { + m_currentInvocationCount.incrementAndGet(); + } + + @Override + public int getCurrentInvocationCount() { + return m_currentInvocationCount.get(); + } + + @Override + public void setParameterInvocationCount(int n) { + m_parameterInvocationCount = n; + } + + @Override + public int getParameterInvocationCount() { + return m_parameterInvocationCount; + } + + @Override + public void setMoreInvocationChecker(Callable moreInvocationChecker) { + m_moreInvocationChecker = moreInvocationChecker; + } + + @Override + public boolean hasMoreInvocation() { + if (m_moreInvocationChecker != null) { + try { + return m_moreInvocationChecker.call(); + } catch (Exception e) { + // Should never append + throw new RuntimeException(e); + } + } + return getCurrentInvocationCount() < getInvocationCount() * getParameterInvocationCount(); + } + + @Override + public abstract ITestNGMethod clone(); + + @Override + public IRetryAnalyzer getRetryAnalyzer(ITestResult result) { + return getRetryAnalyzerConsideringMethodParameters(result); + } + + @Override + public void setRetryAnalyzerClass(Class clazz) { + m_retryAnalyzerClass = clazz == null ? DisabledRetryAnalyzer.class : clazz; + m_retryAnalyzer = m_objectFactory.newInstance(m_retryAnalyzerClass); + } + + @Override + public Class getRetryAnalyzerClass() { + return m_retryAnalyzerClass; + } + + @Override + public boolean skipFailedInvocations() { + return m_skipFailedInvocations; + } + + @Override + public void setSkipFailedInvocations(boolean s) { + m_skipFailedInvocations = s; + } + + public void setInvocationTimeOut(long timeOut) { + m_invocationTimeOut = timeOut; + } + + @Override + public long getInvocationTimeOut() { + return m_invocationTimeOut; + } + + @Override + public boolean ignoreMissingDependencies() { + return m_ignoreMissingDependencies; + } + + @Override + public void setIgnoreMissingDependencies(boolean i) { + m_ignoreMissingDependencies = i; + } + + @Override + public List getInvocationNumbers() { + return m_invocationNumbers; + } + + @Override + public void setInvocationNumbers(List numbers) { + m_invocationNumbers = numbers; + } + + @Override + public List getFailedInvocationNumbers() { + return new ArrayList<>(m_failedInvocationNumbers); + } + + @Override + public void addFailedInvocationNumber(int number) { + m_failedInvocationNumbers.add(number); + } + + @Override + public int getPriority() { + return m_priority; + } + + @Override + public void setPriority(int priority) { + m_priority = priority; + } + + @Override + public int getInterceptedPriority() { + return m_interceptedPriority; + } + + @Override + public void setInterceptedPriority(int priority) { + m_interceptedPriority = priority; + } + + @Override + public XmlTest getXmlTest() { + return m_xmlTest; + } + + public void setXmlTest(XmlTest xmlTest) { + m_xmlTest = xmlTest; + } + + @Override + public ConstructorOrMethod getConstructorOrMethod() { + return m_method; + } + + @Override + public Class[] getParameterTypes() { + return m_method.getParameterTypes(); + } + + @Override + public Map findMethodParameters(XmlTest test) { + return XmlTestUtils.findMethodParameters(test, getTestClass().getName(), getMethodName()); + } + + @Override + public String getQualifiedName() { + return getRealClass().getName() + "." + getMethodName(); + } + + @Override + public IParameterInfo getFactoryMethodParamsInfo() { + if (m_instance instanceof IParameterInfo) { + return (IParameterInfo) m_instance; + } + return null; + } + + private long invocationTime; + + @Override + public void setInvokedAt(long date) { + this.invocationTime = date; + } + + @Override + public long getInvocationTime() { + return invocationTime; + } + + private IRetryAnalyzer getRetryAnalyzerConsideringMethodParameters(ITestResult tr) { + Object[] key = tr.getParameters(); + IRetryAnalyzer retryAnalyzer = this.m_retryAnalyzer; + if (key != null && key.length != 0 && retryAnalyzer != null) { + final String keyAsString = getSimpleName() + "#" + getParameterInvocationCount(); + final IRetryAnalyzer currentRetryAnalyzerInMap = m_testMethodToRetryAnalyzer.get(keyAsString); + if (currentRetryAnalyzerInMap == null || currentRetryAnalyzerInMap.getClass() != retryAnalyzer.getClass()) { + retryAnalyzer = m_testMethodToRetryAnalyzer.compute( + keyAsString, + (s, ra) -> m_objectFactory.newInstance(this.m_retryAnalyzer.getClass())); + } else { + retryAnalyzer = currentRetryAnalyzerInMap; + } + } + return retryAnalyzer; + } + +} diff --git a/testng-core/src/main/java/org/testng/internal/ConfigurationGroupMethods.java b/testng-core/src/main/java/org/testng/internal/ConfigurationGroupMethods.java index 711b47f9d9..8ca05848bc 100644 --- a/testng-core/src/main/java/org/testng/internal/ConfigurationGroupMethods.java +++ b/testng-core/src/main/java/org/testng/internal/ConfigurationGroupMethods.java @@ -1,138 +1,138 @@ -package org.testng.internal; - -import org.testng.ITestNGMethod; -import org.testng.collections.Lists; -import org.testng.collections.Maps; - -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -/** - * This class wraps access to beforeGroups and afterGroups methods, since they are passed around the - * various invokers and potentially modified in different threads. - * - * @since 5.3 (Mar 2, 2006) - */ -public class ConfigurationGroupMethods { - - /** The list of beforeGroups methods keyed by the name of the group */ - private final Map> m_beforeGroupsMethods; - - private final Set beforeGroupsThatHaveAlreadyRun = - Collections.newSetFromMap(new ConcurrentHashMap<>()); - private final Set afterGroupsThatHaveAlreadyRun = - Collections.newSetFromMap(new ConcurrentHashMap<>()); - - /** The list of afterGroups methods keyed by the name of the group */ - private final Map> m_afterGroupsMethods; - - /** The list of all test methods */ - private final ITestNGMethod[] m_allMethods; - - /** A map that returns the last method belonging to the given group */ - private volatile Map> m_afterGroupsMap = null; - - public ConfigurationGroupMethods( - IContainer container, - Map> beforeGroupsMethods, - Map> afterGroupsMethods) { - m_allMethods = container.getItems(); - m_beforeGroupsMethods = new ConcurrentHashMap<>(beforeGroupsMethods); - m_afterGroupsMethods = new ConcurrentHashMap<>(afterGroupsMethods); - } - - public Map> getBeforeGroupsMethods() { - return m_beforeGroupsMethods; - } - - public Map> getAfterGroupsMethods() { - return m_afterGroupsMethods; - } - - /** - * @param group The group name - * @param method The test method - * @return true if the passed method is the last to run for the group. This method is used to - * figure out when is the right time to invoke afterGroups methods. - */ - public boolean isLastMethodForGroup(String group, ITestNGMethod method) { - - // If we have more invocation to do, this is not the last one yet - if (method.hasMoreInvocation()) { - return false; - } - - // This Mutex ensures that this edit check runs sequentially for one ITestNGMethod - // method at a time because this object is being shared between all the ITestNGMethod objects. - synchronized (this) { - if (m_afterGroupsMap == null) { - m_afterGroupsMap = initializeAfterGroupsMap(); - } - - List methodsInGroup = m_afterGroupsMap.get(group); - - if (null == methodsInGroup || methodsInGroup.isEmpty()) { - return false; - } - - methodsInGroup.remove(method); - - // Note: == is not good enough here as we may work with ITestNGMethod clones - return methodsInGroup.isEmpty(); - } - } - - private Map> initializeAfterGroupsMap() { - Map> result = Maps.newConcurrentMap(); - for (ITestNGMethod m : m_allMethods) { - String[] groups = m.getGroups(); - for (String g : groups) { - List methodsInGroup = result.computeIfAbsent(g, key -> Lists.newArrayList()); - methodsInGroup.add(m); - } - } - - synchronized (afterGroupsThatHaveAlreadyRun) { - afterGroupsThatHaveAlreadyRun.clear(); - } - - return result; - } - - public List getBeforeGroupMethodsForGroup(String group) { - synchronized (beforeGroupsThatHaveAlreadyRun) { - return retrieve(beforeGroupsThatHaveAlreadyRun, m_beforeGroupsMethods, group); - } - } - - public List getAfterGroupMethodsForGroup(String group) { - synchronized (afterGroupsThatHaveAlreadyRun) { - return retrieve(afterGroupsThatHaveAlreadyRun, m_afterGroupsMethods, group); - } - } - - public void removeBeforeGroups(String[] groups) { - for (String group : groups) { - m_beforeGroupsMethods.remove(group); - } - } - - public void removeAfterGroups(Collection groups) { - for (String group : groups) { - m_afterGroupsMethods.remove(group); - } - } - - private static List retrieve( - Set tracker, Map> map, String group) { - if (tracker.contains(group)) { - return Collections.emptyList(); - } - tracker.add(group); - return map.get(group); - } -} +package org.testng.internal; + +import org.testng.ITestNGMethod; +import org.testng.collections.Lists; +import org.testng.collections.Maps; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +/** + * This class wraps access to beforeGroups and afterGroups methods, since they are passed around the + * various invokers and potentially modified in different threads. + * + * @since 5.3 (Mar 2, 2006) + */ +public class ConfigurationGroupMethods { + + /** The list of beforeGroups methods keyed by the name of the group */ + private final Map> m_beforeGroupsMethods; + + private final Set beforeGroupsThatHaveAlreadyRun = + Collections.newSetFromMap(new ConcurrentHashMap<>()); + private final Set afterGroupsThatHaveAlreadyRun = + Collections.newSetFromMap(new ConcurrentHashMap<>()); + + /** The list of afterGroups methods keyed by the name of the group */ + private final Map> m_afterGroupsMethods; + + /** The list of all test methods */ + private final ITestNGMethod[] m_allMethods; + + /** A map that returns the last method belonging to the given group */ + private volatile Map> m_afterGroupsMap = null; + + public ConfigurationGroupMethods( + IContainer container, + Map> beforeGroupsMethods, + Map> afterGroupsMethods) { + m_allMethods = container.getItems(); + m_beforeGroupsMethods = new ConcurrentHashMap<>(beforeGroupsMethods); + m_afterGroupsMethods = new ConcurrentHashMap<>(afterGroupsMethods); + } + + public Map> getBeforeGroupsMethods() { + return m_beforeGroupsMethods; + } + + public Map> getAfterGroupsMethods() { + return m_afterGroupsMethods; + } + + /** + * @param group The group name + * @param method The test method + * @return true if the passed method is the last to run for the group. This method is used to + * figure out when is the right time to invoke afterGroups methods. + */ + public boolean isLastMethodForGroup(String group, ITestNGMethod method) { + + // If we have more invocation to do, this is not the last one yet + if (method.hasMoreInvocation()) { + return false; + } + + // This Mutex ensures that this edit check runs sequentially for one ITestNGMethod + // method at a time because this object is being shared between all the ITestNGMethod objects. + synchronized (this) { + if (m_afterGroupsMap == null) { + m_afterGroupsMap = initializeAfterGroupsMap(); + } + + List methodsInGroup = m_afterGroupsMap.get(group); + + if (null == methodsInGroup || methodsInGroup.isEmpty()) { + return false; + } + + methodsInGroup.remove(method); + + // Note: == is not good enough here as we may work with ITestNGMethod clones + return methodsInGroup.isEmpty(); + } + } + + private Map> initializeAfterGroupsMap() { + Map> result = Maps.newConcurrentMap(); + for (ITestNGMethod m : m_allMethods) { + String[] groups = m.getGroups(); + for (String g : groups) { + List methodsInGroup = result.computeIfAbsent(g, key -> Lists.newArrayList()); + methodsInGroup.add(m); + } + } + + synchronized (afterGroupsThatHaveAlreadyRun) { + afterGroupsThatHaveAlreadyRun.clear(); + } + + return result; + } + + public List getBeforeGroupMethodsForGroup(String group) { + synchronized (beforeGroupsThatHaveAlreadyRun) { + return retrieve(beforeGroupsThatHaveAlreadyRun, m_beforeGroupsMethods, group); + } + } + + public List getAfterGroupMethodsForGroup(String group) { + synchronized (afterGroupsThatHaveAlreadyRun) { + return retrieve(afterGroupsThatHaveAlreadyRun, m_afterGroupsMethods, group); + } + } + + public void removeBeforeGroups(String[] groups) { + for (String group : groups) { + m_beforeGroupsMethods.remove(group); + } + } + + public void removeAfterGroups(Collection groups) { + for (String group : groups) { + m_afterGroupsMethods.remove(group); + } + } + + private static List retrieve( + Set tracker, Map> map, String group) { + if (tracker.contains(group)) { + return Collections.emptyList(); + } + tracker.add(group); + return map.get(group); + } +} diff --git a/testng-core/src/main/java/org/testng/internal/DefaultMethodSelectorContext.java b/testng-core/src/main/java/org/testng/internal/DefaultMethodSelectorContext.java index e4cf2b1e4c..18a6e2ecb9 100644 --- a/testng-core/src/main/java/org/testng/internal/DefaultMethodSelectorContext.java +++ b/testng-core/src/main/java/org/testng/internal/DefaultMethodSelectorContext.java @@ -1,33 +1,33 @@ -package org.testng.internal; - -import org.testng.IMethodSelectorContext; -import org.testng.collections.Maps; - -import java.util.Map; - -/** - * Simple implementation of IMethodSelectorContext - * - *

Created on Jan 3, 2007 - * - * @author Cedric Beust - */ -public class DefaultMethodSelectorContext implements IMethodSelectorContext { - private final Map m_userData = Maps.newHashMap(); - private boolean m_isStopped = false; - - @Override - public Map getUserData() { - return m_userData; - } - - @Override - public boolean isStopped() { - return m_isStopped; - } - - @Override - public void setStopped(boolean stopped) { - m_isStopped = stopped; - } -} +package org.testng.internal; + +import org.testng.IMethodSelectorContext; +import org.testng.collections.Maps; + +import java.util.Map; + +/** + * Simple implementation of IMethodSelectorContext + * + *

Created on Jan 3, 2007 + * + * @author Cedric Beust + */ +public class DefaultMethodSelectorContext implements IMethodSelectorContext { + private final Map m_userData = Maps.newHashMap(); + private boolean m_isStopped = false; + + @Override + public Map getUserData() { + return m_userData; + } + + @Override + public boolean isStopped() { + return m_isStopped; + } + + @Override + public void setStopped(boolean stopped) { + m_isStopped = stopped; + } +} diff --git a/testng-core/src/main/java/org/testng/internal/IResultListener.java b/testng-core/src/main/java/org/testng/internal/IResultListener.java index d4995aad94..2506b3233c 100644 --- a/testng-core/src/main/java/org/testng/internal/IResultListener.java +++ b/testng-core/src/main/java/org/testng/internal/IResultListener.java @@ -1,11 +1,11 @@ -package org.testng.internal; - -import org.testng.IConfigurationListener; -import org.testng.ITestListener; - -/** - * A convenient interface to use when implementing listeners. - * - * @author Alexandru Popescu - */ -public interface IResultListener extends ITestListener, IConfigurationListener {} +package org.testng.internal; + +import org.testng.IConfigurationListener; +import org.testng.ITestListener; + +/** + * A convenient interface to use when implementing listeners. + * + * @author Alexandru Popescu + */ +public interface IResultListener extends ITestListener, IConfigurationListener {} diff --git a/testng-core/src/main/java/org/testng/internal/MethodGroupsHelper.java b/testng-core/src/main/java/org/testng/internal/MethodGroupsHelper.java index 65fae06724..7b09d5186e 100644 --- a/testng-core/src/main/java/org/testng/internal/MethodGroupsHelper.java +++ b/testng-core/src/main/java/org/testng/internal/MethodGroupsHelper.java @@ -1,279 +1,279 @@ -package org.testng.internal; - -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Predicate; -import java.util.regex.Pattern; - -import java.util.stream.Collectors; -import java.util.stream.Stream; -import org.testng.ITestClass; -import org.testng.ITestNGMethod; -import org.testng.annotations.IConfigurationAnnotation; -import org.testng.annotations.ITestOrConfiguration; -import org.testng.collections.Lists; -import org.testng.collections.Maps; -import org.testng.internal.annotations.AnnotationHelper; -import org.testng.internal.annotations.IAnnotationFinder; -import org.testng.internal.collections.Pair; - -/** - * Collections of helper methods to help deal with test methods - */ -public class MethodGroupsHelper { - - private static final Map PATTERN_CACHE = new ConcurrentHashMap<>(); - private static final Map, Boolean> MATCH_CACHE = new ConcurrentHashMap<>(); - - /** - * Collect all the methods that belong to the included groups and exclude all the methods that - * belong to an excluded group. - */ - static void collectMethodsByGroup( - ITestNGMethod[] methods, - boolean forTests, - List outIncludedMethods, - List outExcludedMethods, - RunInfo runInfo, - IAnnotationFinder finder, - boolean unique) { - for (ITestNGMethod tm : methods) { - boolean in = false; - Method m = tm.getConstructorOrMethod().getMethod(); - // - // @Test method - // - if (forTests) { - in = - MethodGroupsHelper.includeMethod( - AnnotationHelper.findTest(finder, m), - runInfo, - tm, - forTests, - unique, - outIncludedMethods); - } - - // - // @Configuration method - // - else { - IConfigurationAnnotation annotation = AnnotationHelper.findConfiguration(finder, m); - if (annotation.getAlwaysRun()) { - if (!unique || MethodGroupsHelper.isMethodAlreadyNotPresent(outIncludedMethods, tm)) { - in = true; - } - } else { - in = - MethodGroupsHelper.includeMethod( - AnnotationHelper.findTest(finder, tm), - runInfo, - tm, - forTests, - unique, - outIncludedMethods); - } - } - if (in) { - outIncludedMethods.add(tm); - } else { - outExcludedMethods.add(tm); - } - } - } - - private static boolean includeMethod( - ITestOrConfiguration annotation, - RunInfo runInfo, - ITestNGMethod tm, - boolean forTests, - boolean unique, - List outIncludedMethods) { - boolean result = false; - - if (MethodHelper.isEnabled(annotation)) { - if (runInfo.includeMethod(tm, forTests)) { - if (unique) { - if (MethodGroupsHelper.isMethodAlreadyNotPresent(outIncludedMethods, tm)) { - result = true; - } - } else { - result = true; - } - } - } - - return result; - } - - private static boolean isMethodAlreadyNotPresent(List result, ITestNGMethod tm) { - Class cls = tm.getConstructorOrMethod().getDeclaringClass(); - return result.parallelStream() - .map(ITestNGMethod::getConstructorOrMethod) - .filter(m -> m.getName().equals(tm.getConstructorOrMethod().getName())) - .map(ConstructorOrMethod::getDeclaringClass) - .noneMatch(eachCls -> eachCls.isAssignableFrom(cls) || cls.isAssignableFrom(eachCls)); - } - - /** @return the map of groups and their corresponding methods from the extraction of classes. */ - public static Map> findGroupsMethods( - Collection classes, boolean before) { - Map> result = Maps.newHashMap(); - for (ITestClass cls : classes) { - ITestNGMethod[] methods = before ? cls.getBeforeGroupsMethods() : cls.getAfterGroupsMethods(); - for (ITestNGMethod method : methods) { - String[] grp = before ? method.getBeforeGroups() : method.getAfterGroups(); - List groups = Stream.concat(Arrays.stream(grp), Arrays.stream(method.getGroups())) - .collect(Collectors.toList()); - for (String group : groups) { - List methodList = result.computeIfAbsent(group, k -> Lists.newArrayList()); - // NOTE(cbeust, 2007/01/23) - // BeforeGroups/AfterGroups methods should only be invoked once. - // I should probably use a map instead of a list for a contains(), but - // this list should usually be fairly short - if (!methodList.contains(method)) { - methodList.add(method); - } - } - } - } - - return result; - } - - protected static void findGroupTransitiveClosure( - List includedMethods, - List allMethods, - String[] includedGroups, - Set outGroups, - Set outMethods) { - Map runningMethods = includedMethods.stream().collect(Collectors.toMap(m -> m, m -> m)); - - Map runningGroups = Arrays.stream(includedGroups) - .collect(Collectors.toMap(g -> g, g -> g)); - - boolean keepGoing = true; - - Map newMethods = Maps.newHashMap(); - while (keepGoing) { - for (ITestNGMethod m : includedMethods) { - - // - // Depends on groups? - // Adds all included methods to runningMethods - // - String[] ig = m.getGroupsDependedUpon(); - for (String g : ig) { - if (!runningGroups.containsKey(g)) { - // Found a new included group, add all the methods it contains to - // our outMethod closure - runningGroups.put(g, g); - ITestNGMethod[] im = - MethodGroupsHelper.findMethodsThatBelongToGroup( - m, allMethods.toArray(new ITestNGMethod[0]), g); - for (ITestNGMethod thisMethod : im) { - if (!runningMethods.containsKey(thisMethod)) { - runningMethods.put(thisMethod, thisMethod); - newMethods.put(thisMethod, thisMethod); - } - } - } - } // groups - - // - // Depends on methods? - // Adds all depended methods to runningMethods - // - String[] mdu = m.getMethodsDependedUpon(); - for (String tm : mdu) { - ITestNGMethod thisMethod = MethodGroupsHelper.findMethodNamed(tm, allMethods); - if (thisMethod != null && !runningMethods.containsKey(thisMethod)) { - runningMethods.put(thisMethod, thisMethod); - newMethods.put(thisMethod, thisMethod); - } - } - } // methods - - // - // Only keep going if new methods have been added - // - keepGoing = newMethods.size() > 0; - includedMethods = Lists.newArrayList(); - includedMethods.addAll(newMethods.keySet()); - newMethods = Maps.newHashMap(); - } // while keepGoing - - outMethods.addAll(runningMethods.keySet()); - outGroups.addAll(runningGroups.keySet()); - } - - private static ITestNGMethod findMethodNamed(String tm, List allMethods) { - return allMethods.stream() - .filter(m -> m.getQualifiedName().equals(tm)) - .findFirst() - .orElse(null); - } - - /** - * Only used if a group is missing to flag an error on that method - * - * @param method if no group is found, group regex is set as this method's missing group - * @param methods list of methods to search - * @param groupRegexp regex representing the group - * @return all the methods that belong to the group specified by the regular expression - * groupRegExp. methods[] is the list of all the methods we are choosing from and method is - * the method that owns the dependsOnGroups statement (only used if a group is missing to flag - * an error on that method). - */ - public static ITestNGMethod[] findMethodsThatBelongToGroup( - ITestNGMethod method, ITestNGMethod[] methods, String groupRegexp) { - ITestNGMethod[] found = findMethodsThatBelongToGroup(methods, groupRegexp); - - if (found.length == 0) { - method.setMissingGroup(groupRegexp); - } - - return found; - } - - /** - * @param methods list of methods to search - * @param groupRegexp regex representing the group - * @return all the methods that belong to the group specified by the regular expression - * groupRegExp. methods[] is the list of all the methods we are choosing from. - */ - protected static ITestNGMethod[] findMethodsThatBelongToGroup( - ITestNGMethod[] methods, String groupRegexp) { - final Pattern pattern = getPattern(groupRegexp); - Predicate matchingGroups = tm -> Arrays.stream(tm.getGroups()) - .anyMatch(group -> isMatch(pattern, group)); - return Arrays.stream(methods) - .filter(matchingGroups) - .toArray(ITestNGMethod[]::new); - } - - private static Boolean isMatch(Pattern pattern, String group) { - Pair cacheKey = Pair.create(pattern.pattern(), group); - Boolean match = MATCH_CACHE.get(cacheKey); - if (match == null) { - match = pattern.matcher(group).matches(); - MATCH_CACHE.put(cacheKey, match); - } - return match; - } - - private static Pattern getPattern(String groupRegexp) { - Pattern groupPattern = PATTERN_CACHE.get(groupRegexp); - if (groupPattern == null) { - groupPattern = Pattern.compile(groupRegexp); - PATTERN_CACHE.put(groupRegexp, groupPattern); - } - return groupPattern; - } - -} +package org.testng.internal; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Predicate; +import java.util.regex.Pattern; + +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.testng.ITestClass; +import org.testng.ITestNGMethod; +import org.testng.annotations.IConfigurationAnnotation; +import org.testng.annotations.ITestOrConfiguration; +import org.testng.collections.Lists; +import org.testng.collections.Maps; +import org.testng.internal.annotations.AnnotationHelper; +import org.testng.internal.annotations.IAnnotationFinder; +import org.testng.internal.collections.Pair; + +/** + * Collections of helper methods to help deal with test methods + */ +public class MethodGroupsHelper { + + private static final Map PATTERN_CACHE = new ConcurrentHashMap<>(); + private static final Map, Boolean> MATCH_CACHE = new ConcurrentHashMap<>(); + + /** + * Collect all the methods that belong to the included groups and exclude all the methods that + * belong to an excluded group. + */ + static void collectMethodsByGroup( + ITestNGMethod[] methods, + boolean forTests, + List outIncludedMethods, + List outExcludedMethods, + RunInfo runInfo, + IAnnotationFinder finder, + boolean unique) { + for (ITestNGMethod tm : methods) { + boolean in = false; + Method m = tm.getConstructorOrMethod().getMethod(); + // + // @Test method + // + if (forTests) { + in = + MethodGroupsHelper.includeMethod( + AnnotationHelper.findTest(finder, m), + runInfo, + tm, + forTests, + unique, + outIncludedMethods); + } + + // + // @Configuration method + // + else { + IConfigurationAnnotation annotation = AnnotationHelper.findConfiguration(finder, m); + if (annotation.getAlwaysRun()) { + if (!unique || MethodGroupsHelper.isMethodAlreadyNotPresent(outIncludedMethods, tm)) { + in = true; + } + } else { + in = + MethodGroupsHelper.includeMethod( + AnnotationHelper.findTest(finder, tm), + runInfo, + tm, + forTests, + unique, + outIncludedMethods); + } + } + if (in) { + outIncludedMethods.add(tm); + } else { + outExcludedMethods.add(tm); + } + } + } + + private static boolean includeMethod( + ITestOrConfiguration annotation, + RunInfo runInfo, + ITestNGMethod tm, + boolean forTests, + boolean unique, + List outIncludedMethods) { + boolean result = false; + + if (MethodHelper.isEnabled(annotation)) { + if (runInfo.includeMethod(tm, forTests)) { + if (unique) { + if (MethodGroupsHelper.isMethodAlreadyNotPresent(outIncludedMethods, tm)) { + result = true; + } + } else { + result = true; + } + } + } + + return result; + } + + private static boolean isMethodAlreadyNotPresent(List result, ITestNGMethod tm) { + Class cls = tm.getConstructorOrMethod().getDeclaringClass(); + return result.parallelStream() + .map(ITestNGMethod::getConstructorOrMethod) + .filter(m -> m.getName().equals(tm.getConstructorOrMethod().getName())) + .map(ConstructorOrMethod::getDeclaringClass) + .noneMatch(eachCls -> eachCls.isAssignableFrom(cls) || cls.isAssignableFrom(eachCls)); + } + + /** @return the map of groups and their corresponding methods from the extraction of classes. */ + public static Map> findGroupsMethods( + Collection classes, boolean before) { + Map> result = Maps.newHashMap(); + for (ITestClass cls : classes) { + ITestNGMethod[] methods = before ? cls.getBeforeGroupsMethods() : cls.getAfterGroupsMethods(); + for (ITestNGMethod method : methods) { + String[] grp = before ? method.getBeforeGroups() : method.getAfterGroups(); + List groups = Stream.concat(Arrays.stream(grp), Arrays.stream(method.getGroups())) + .collect(Collectors.toList()); + for (String group : groups) { + List methodList = result.computeIfAbsent(group, k -> Lists.newArrayList()); + // NOTE(cbeust, 2007/01/23) + // BeforeGroups/AfterGroups methods should only be invoked once. + // I should probably use a map instead of a list for a contains(), but + // this list should usually be fairly short + if (!methodList.contains(method)) { + methodList.add(method); + } + } + } + } + + return result; + } + + protected static void findGroupTransitiveClosure( + List includedMethods, + List allMethods, + String[] includedGroups, + Set outGroups, + Set outMethods) { + Map runningMethods = includedMethods.stream().collect(Collectors.toMap(m -> m, m -> m)); + + Map runningGroups = Arrays.stream(includedGroups) + .collect(Collectors.toMap(g -> g, g -> g)); + + boolean keepGoing = true; + + Map newMethods = Maps.newHashMap(); + while (keepGoing) { + for (ITestNGMethod m : includedMethods) { + + // + // Depends on groups? + // Adds all included methods to runningMethods + // + String[] ig = m.getGroupsDependedUpon(); + for (String g : ig) { + if (!runningGroups.containsKey(g)) { + // Found a new included group, add all the methods it contains to + // our outMethod closure + runningGroups.put(g, g); + ITestNGMethod[] im = + MethodGroupsHelper.findMethodsThatBelongToGroup( + m, allMethods.toArray(new ITestNGMethod[0]), g); + for (ITestNGMethod thisMethod : im) { + if (!runningMethods.containsKey(thisMethod)) { + runningMethods.put(thisMethod, thisMethod); + newMethods.put(thisMethod, thisMethod); + } + } + } + } // groups + + // + // Depends on methods? + // Adds all depended methods to runningMethods + // + String[] mdu = m.getMethodsDependedUpon(); + for (String tm : mdu) { + ITestNGMethod thisMethod = MethodGroupsHelper.findMethodNamed(tm, allMethods); + if (thisMethod != null && !runningMethods.containsKey(thisMethod)) { + runningMethods.put(thisMethod, thisMethod); + newMethods.put(thisMethod, thisMethod); + } + } + } // methods + + // + // Only keep going if new methods have been added + // + keepGoing = newMethods.size() > 0; + includedMethods = Lists.newArrayList(); + includedMethods.addAll(newMethods.keySet()); + newMethods = Maps.newHashMap(); + } // while keepGoing + + outMethods.addAll(runningMethods.keySet()); + outGroups.addAll(runningGroups.keySet()); + } + + private static ITestNGMethod findMethodNamed(String tm, List allMethods) { + return allMethods.stream() + .filter(m -> m.getQualifiedName().equals(tm)) + .findFirst() + .orElse(null); + } + + /** + * Only used if a group is missing to flag an error on that method + * + * @param method if no group is found, group regex is set as this method's missing group + * @param methods list of methods to search + * @param groupRegexp regex representing the group + * @return all the methods that belong to the group specified by the regular expression + * groupRegExp. methods[] is the list of all the methods we are choosing from and method is + * the method that owns the dependsOnGroups statement (only used if a group is missing to flag + * an error on that method). + */ + public static ITestNGMethod[] findMethodsThatBelongToGroup( + ITestNGMethod method, ITestNGMethod[] methods, String groupRegexp) { + ITestNGMethod[] found = findMethodsThatBelongToGroup(methods, groupRegexp); + + if (found.length == 0) { + method.setMissingGroup(groupRegexp); + } + + return found; + } + + /** + * @param methods list of methods to search + * @param groupRegexp regex representing the group + * @return all the methods that belong to the group specified by the regular expression + * groupRegExp. methods[] is the list of all the methods we are choosing from. + */ + protected static ITestNGMethod[] findMethodsThatBelongToGroup( + ITestNGMethod[] methods, String groupRegexp) { + final Pattern pattern = getPattern(groupRegexp); + Predicate matchingGroups = tm -> Arrays.stream(tm.getGroups()) + .anyMatch(group -> isMatch(pattern, group)); + return Arrays.stream(methods) + .filter(matchingGroups) + .toArray(ITestNGMethod[]::new); + } + + private static Boolean isMatch(Pattern pattern, String group) { + Pair cacheKey = Pair.create(pattern.pattern(), group); + Boolean match = MATCH_CACHE.get(cacheKey); + if (match == null) { + match = pattern.matcher(group).matches(); + MATCH_CACHE.put(cacheKey, match); + } + return match; + } + + private static Pattern getPattern(String groupRegexp) { + Pattern groupPattern = PATTERN_CACHE.get(groupRegexp); + if (groupPattern == null) { + groupPattern = Pattern.compile(groupRegexp); + PATTERN_CACHE.put(groupRegexp, groupPattern); + } + return groupPattern; + } + +} diff --git a/testng-core/src/main/java/org/testng/internal/MethodHelper.java b/testng-core/src/main/java/org/testng/internal/MethodHelper.java index d088af2aa2..8b0e6c0d61 100644 --- a/testng-core/src/main/java/org/testng/internal/MethodHelper.java +++ b/testng-core/src/main/java/org/testng/internal/MethodHelper.java @@ -1,495 +1,495 @@ -package org.testng.internal; - -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.Collection; -import java.util.Comparator; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicReference; -import java.util.regex.Pattern; - -import java.util.stream.Collectors; -import java.util.stream.Stream; -import org.testng.IMethodInstance; -import org.testng.ITestClass; -import org.testng.ITestNGMethod; -import org.testng.TestNGException; -import org.testng.annotations.IConfigurationAnnotation; -import org.testng.annotations.ITestAnnotation; -import org.testng.annotations.ITestOrConfiguration; -import org.testng.collections.Lists; -import org.testng.collections.Sets; -import org.testng.internal.annotations.AnnotationHelper; -import org.testng.internal.annotations.IAnnotationFinder; -import org.testng.internal.collections.Pair; -import org.testng.internal.invokers.IInvocationStatus; -import org.testng.util.TimeUtils; -import org.testng.xml.XmlTest; - -/** - * Collection of helper methods to help sort and arrange methods. - */ -public class MethodHelper { - private static final Map> GRAPH_CACHE = - new ConcurrentHashMap<>(); - private static final Map CANONICAL_NAME_CACHE = new ConcurrentHashMap<>(); - private static final Map, Boolean> MATCH_CACHE = new ConcurrentHashMap<>(); - - /** - * Collects and orders test or configuration methods - * - * @param methods methods to be worked on - * @param forTests true for test methods, false for configuration methods - * @param runInfo - {@link RunInfo} object. - * @param finder annotation finder - * @param unique true for unique methods, false otherwise - * @param outExcludedMethods - A List of excluded {@link ITestNGMethod} methods. - * @return list of ordered methods - */ - public static ITestNGMethod[] collectAndOrderMethods( - List methods, - boolean forTests, - RunInfo runInfo, - IAnnotationFinder finder, - boolean unique, - List outExcludedMethods, - Comparator comparator) { - AtomicReference results = new AtomicReference<>(); - List includedMethods = Lists.newArrayList(); - TimeUtils.computeAndShowTime("MethodGroupsHelper.collectMethodsByGroup()", - () -> MethodGroupsHelper.collectMethodsByGroup( - methods.toArray(new ITestNGMethod[0]), - forTests, - includedMethods, - outExcludedMethods, - runInfo, - finder, - unique) - ); - TimeUtils.computeAndShowTime("MethodGroupsHelper.sortMethods()", - () -> results.set(sortMethods(forTests, includedMethods, comparator) - .toArray(new ITestNGMethod[]{})) - ); - return results.get(); - } - - /** - * Finds TestNG methods that the specified TestNG method depends upon - * - * @param m TestNG method - * @param methods list of methods to search for depended upon methods - * @return list of methods that match the criteria - */ - protected static ITestNGMethod[] findDependedUponMethods( - ITestNGMethod m, List methods) { - ITestNGMethod[] methodsArray = methods.toArray(new ITestNGMethod[0]); - return findDependedUponMethods(m, methodsArray); - } - - /** - * Finds TestNG methods that the specified TestNG method depends upon - * - * @param m TestNG method - * @param methods list of methods to search for depended upon methods - * @return list of methods that match the criteria - */ - public static ITestNGMethod[] findDependedUponMethods( - ITestNGMethod m, ITestNGMethod[] methods) { - - String canonicalMethodName = calculateMethodCanonicalName(m); - - List vResult = Lists.newArrayList(); - String regexp = null; - for (String fullyQualifiedRegexp : m.getMethodsDependedUpon()) { - boolean foundAtLeastAMethod = false; - - if (null != fullyQualifiedRegexp) { - // Escapes $ in regexps as it is not meant for end - line matching, but inner class matches. - regexp = fullyQualifiedRegexp.replace("$", "\\$"); - MatchResults results = matchMethod(methods, regexp); - foundAtLeastAMethod = results.foundAtLeastAMethod; - vResult.addAll(results.matchedMethods); - if (!foundAtLeastAMethod) { - // Replace the declaring class name in the dependsOnMethods value with - // the fully qualified test class name and retry the method matching. - int lastIndex = regexp.lastIndexOf('.'); - String newMethodName; - if (lastIndex != -1) { - String clazzName = (m.getTestClass() != null ? m.getTestClass().getRealClass() : m.getRealClass()).getName(); - newMethodName = clazzName + regexp.substring(lastIndex); - results = matchMethod(methods, newMethodName); - foundAtLeastAMethod = results.foundAtLeastAMethod; - vResult.addAll(results.matchedMethods); - } - } - } - - if (!foundAtLeastAMethod) { - if (m.ignoreMissingDependencies()) { - continue; - } - if (m.isAlwaysRun()) { - continue; - } - Method maybeReferringTo = findMethodByName(m, regexp); - if (maybeReferringTo != null) { - throw new TestNGException( - canonicalMethodName - + "() is depending on method " - + maybeReferringTo - + ", which is not annotated with @Test or not included."); - } - throw new TestNGException( - canonicalMethodName + "() depends on nonexistent method " + regexp); - } - } // end for - - return vResult.toArray(new ITestNGMethod[0]); - } - - /** - * Finds method based on regex and TestNGMethod. If regex doesn't represent the class name, uses - * the TestNG method's class name. - * - * @param testngMethod TestNG method - * @param regExp regex representing a method and/or related class name - */ - private static Method findMethodByName(ITestNGMethod testngMethod, String regExp) { - if (regExp == null) { - return null; - } - int lastDot = regExp.lastIndexOf('.'); - String className, methodName; - if (lastDot == -1) { - className = testngMethod.getConstructorOrMethod().getDeclaringClass().getCanonicalName(); - methodName = regExp; - } else { - methodName = regExp.substring(lastDot + 1); - className = regExp.substring(0, lastDot); - } - - try { - Class c = Class.forName(className); - for (Method m : c.getDeclaredMethods()) { - if (methodName.equals(m.getName())) { - return m; - } - } - } catch (Exception e) { - // only logging - Utils.log("MethodHelper", 3, "Caught exception while searching for methods using regex"); - } - return null; - } - - public static boolean isEnabled(Class objectClass, IAnnotationFinder finder) { - ITestAnnotation testClassAnnotation = AnnotationHelper.findTest(finder, objectClass); - return isEnabled(testClassAnnotation); - } - - public static boolean isEnabled(Method m, IAnnotationFinder finder) { - ITestAnnotation annotation = AnnotationHelper.findTest(finder, m); - - // If no method annotation, look for one on the class - if (null == annotation) { - annotation = AnnotationHelper.findTest(finder, m.getDeclaringClass()); - } - - return isEnabled(annotation); - } - - public static boolean isEnabled(ITestOrConfiguration test) { - return null == test || test.getEnabled(); - } - - public static boolean isAlwaysRun(IConfigurationAnnotation configurationAnnotation) { - if (null == configurationAnnotation) { - return false; - } - - boolean alwaysRun = false; - if ((configurationAnnotation.getAfterSuite() - || configurationAnnotation.getAfterTest() - || configurationAnnotation.getAfterTestClass() - || configurationAnnotation.getAfterTestMethod() - || configurationAnnotation.getBeforeTestMethod() - || configurationAnnotation.getBeforeTestClass() - || configurationAnnotation.getBeforeTest() - || configurationAnnotation.getBeforeSuite() - || configurationAnnotation.getBeforeGroups().length != 0 - || configurationAnnotation.getAfterGroups().length != 0) - && configurationAnnotation.getAlwaysRun()) { - alwaysRun = true; - } - - return alwaysRun; - } - - /** Extracts the unique list of ITestNGMethods. */ - public static List uniqueMethodList(Collection> methods) { - Set resultSet = Sets.newHashSet(); - - for (List l : methods) { - resultSet.addAll(l); - } - - return Lists.newArrayList(resultSet); - } - - private static Graph topologicalSort( - ITestNGMethod[] methods, - List sequentialList, - List parallelList, - final Comparator comparator) { - Graph result = - new Graph<>((o1, o2) -> comparator.compare(o1.getObject(), o2.getObject())); - - if (methods.length == 0) { - return result; - } - - // - // Create the graph - // - - Map> testInstances = sortMethodsByInstance(methods); - - XmlTest xmlTest = null; - for (ITestNGMethod m : methods) { - if (xmlTest == null) { - xmlTest = m.getXmlTest(); - } - result.addNode(m); - - List predecessors = Lists.newArrayList(); - - String[] methodsDependedUpon = m.getMethodsDependedUpon(); - if (methodsDependedUpon.length > 0) { - ITestNGMethod[] methodsNamed; - // Method has instance - if (m.getInstance() != null) { - // Get other methods with the same instance - List instanceMethods = testInstances.get(m.getInstance()); - try { - // Search for other methods that depends upon with the same instance - methodsNamed = MethodHelper.findDependedUponMethods(m, instanceMethods); - } catch (TestNGException e) { - // Maybe this method has a dependency on a method that resides in a different instance. - // Lets try searching for all methods now - methodsNamed = MethodHelper.findDependedUponMethods(m, methods); - } - } else { - // Search all methods - methodsNamed = MethodHelper.findDependedUponMethods(m, methods); - } - predecessors.addAll(Arrays.asList(methodsNamed)); - } - if (XmlTest.isGroupBasedExecution(xmlTest)) { - String[] groupsDependedUpon = m.getGroupsDependedUpon(); - if (groupsDependedUpon.length > 0) { - for (String group : groupsDependedUpon) { - ITestNGMethod[] methodsThatBelongToGroup = - MethodGroupsHelper.findMethodsThatBelongToGroup(m, methods, group); - predecessors.addAll(Arrays.asList(methodsThatBelongToGroup)); - } - } - } - - for (ITestNGMethod predecessor : predecessors) { - result.addPredecessor(m, predecessor); - } - } - - result.topologicalSort(); - sequentialList.addAll(result.getStrictlySortedNodes()); - parallelList.addAll(result.getIndependentNodes()); - - return result; - } - - /** - * This method is used to create a map of test instances and their associated method(s) . Used to - * decrease the scope to only a methods instance when trying to find method dependencies. - * - * @param methods Methods to be sorted - * @return Map of Instances as the keys and the methods associated with the instance as the values - */ - private static Map> sortMethodsByInstance(ITestNGMethod[] methods) { - return Arrays.stream(methods) - .parallel() - .filter(m -> Objects.nonNull(m.getInstance())) - .collect(Collectors.groupingBy(ITestNGMethod::getInstance, Collectors.toList())); - } - - protected static String calculateMethodCanonicalName(ITestNGMethod m) { - return calculateMethodCanonicalName(m.getConstructorOrMethod().getMethod()); - } - - private static String calculateMethodCanonicalName(Method m) { - String result = CANONICAL_NAME_CACHE.get(m); - if (result != null) { - return result; - } - - String packageName = m.getDeclaringClass().getName() + "." + m.getName(); - - // Try to find the method on this class or parents - Class cls = m.getDeclaringClass(); - while (cls != Object.class) { - try { - if (cls.getDeclaredMethod(m.getName(), m.getParameterTypes()) != null) { - packageName = cls.getName(); - break; - } - } catch (Exception e) { - // ignore - } - cls = cls.getSuperclass(); - } - - result = packageName + "." + m.getName(); - CANONICAL_NAME_CACHE.put(m, result); - return result; - } - - private static List sortMethods( - boolean forTests, List allMethods, Comparator comparator) { - List sl = Lists.newArrayList(); - List pl = Lists.newArrayList(); - ITestNGMethod[] allMethodsArray = allMethods.toArray(new ITestNGMethod[0]); - - // Fix the method inheritance if these are @Configuration methods to make - // sure base classes are invoked before child classes if 'before' and the - // other way around if they are 'after' - if (!forTests && allMethodsArray.length > 0) { - ITestNGMethod m = allMethodsArray[0]; - boolean before = - m.isBeforeClassConfiguration() - || m.isBeforeMethodConfiguration() - || m.isBeforeSuiteConfiguration() - || m.isBeforeTestConfiguration(); - MethodInheritance.fixMethodInheritance(allMethodsArray, before); - } - - topologicalSort(allMethodsArray, sl, pl, comparator); - - List result = Lists.newArrayList(); - result.addAll(sl); - result.addAll(pl); - return result; - } - - /** @return A sorted array containing all the methods 'method' depends on */ - public static List getMethodsDependedUpon( - ITestNGMethod method, ITestNGMethod[] methods, Comparator comparator) { - Graph g = GRAPH_CACHE.get(methods); - if (g == null) { - List parallelList = Lists.newArrayList(); - List sequentialList = Lists.newArrayList(); - g = topologicalSort(methods, sequentialList, parallelList, comparator); - GRAPH_CACHE.put(methods, g); - } - - return g.findPredecessors(method); - } - - // TODO: This needs to be revisited so that, we dont update the parameter list "methodList" - // but we are returning the values. - public static void fixMethodsWithClass( - ITestNGMethod[] methods, ITestClass testCls, List methodList) { - for (ITestNGMethod itm : methods) { - itm.setTestClass(testCls); - - if (methodList != null) { - methodList.add(itm); - } - } - } - - public static List methodsToMethodInstances(List sl) { - return sl.stream().map(MethodInstance::new).collect(Collectors.toList()); - } - - public static List methodInstancesToMethods( - List methodInstances) { - return methodInstances.stream().map(IMethodInstance::getMethod).collect(Collectors.toList()); - } - - public static void dumpInvokedMethodInfoToConsole( - ITestNGMethod[] methods, int currentVerbosity) { - if (currentVerbosity < 3) { - return; - } - System.out.println("===== Invoked methods"); - Arrays.stream(methods).filter(m -> m instanceof IInvocationStatus) - .filter(m -> ((IInvocationStatus) m).getInvocationTime() > 0) - .forEach(im -> { - if (im.isTest()) { - System.out.print(" "); - } else if (isConfigurationMethod(im)) { - System.out.print(" "); - } else { - return; - } - System.out.println("" + im); - }); - System.out.println("====="); - } - - private static boolean isConfigurationMethod(ITestNGMethod tm) { - return tm.isBeforeSuiteConfiguration() || tm.isAfterSuiteConfiguration() || - tm.isBeforeTestConfiguration() || tm.isAfterTestConfiguration() || - tm.isBeforeClassConfiguration() || tm.isAfterClassConfiguration() || - tm.isBeforeGroupsConfiguration() || tm.isAfterGroupsConfiguration() || - tm.isBeforeMethodConfiguration() || tm.isAfterMethodConfiguration(); - } - - protected static String calculateMethodCanonicalName(Class methodClass, String methodName) { - Set methods = ClassHelper.getAvailableMethods(methodClass); // TESTNG-139 - return methods.stream() - .filter(method -> methodName.equals(method.getName())) - .findFirst() - .map(MethodHelper::calculateMethodCanonicalName) - .orElse(null); - } - - public static void clear(Stream methods) { - methods.filter(Objects::nonNull) - .forEach(CANONICAL_NAME_CACHE::remove); - } - - public static long calculateTimeOut(ITestNGMethod tm) { - return tm.getTimeOut() > 0 ? tm.getTimeOut() : tm.getInvocationTimeOut(); - } - - private static MatchResults matchMethod(ITestNGMethod[] methods, String regexp) { - MatchResults results = new MatchResults(); - boolean usePackage = regexp.indexOf('.') != -1; - Pattern pattern = Pattern.compile(regexp); - for (ITestNGMethod method : methods) { - ConstructorOrMethod thisMethod = method.getConstructorOrMethod(); - String thisMethodName = thisMethod.getName(); - String methodName = usePackage ? calculateMethodCanonicalName(method) : thisMethodName; - Pair cacheKey = Pair.create(regexp, methodName); - Boolean match = MATCH_CACHE.get(cacheKey); - if (match == null) { - match = pattern.matcher(methodName).matches(); - MATCH_CACHE.put(cacheKey, match); - } - if (match) { - results.matchedMethods.add(method); - results.foundAtLeastAMethod = true; - } - } - return results; - } - - private static class MatchResults { - private final List matchedMethods = Lists.newArrayList(); - private boolean foundAtLeastAMethod = false; - } -} +package org.testng.internal; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicReference; +import java.util.regex.Pattern; + +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.testng.IMethodInstance; +import org.testng.ITestClass; +import org.testng.ITestNGMethod; +import org.testng.TestNGException; +import org.testng.annotations.IConfigurationAnnotation; +import org.testng.annotations.ITestAnnotation; +import org.testng.annotations.ITestOrConfiguration; +import org.testng.collections.Lists; +import org.testng.collections.Sets; +import org.testng.internal.annotations.AnnotationHelper; +import org.testng.internal.annotations.IAnnotationFinder; +import org.testng.internal.collections.Pair; +import org.testng.internal.invokers.IInvocationStatus; +import org.testng.util.TimeUtils; +import org.testng.xml.XmlTest; + +/** + * Collection of helper methods to help sort and arrange methods. + */ +public class MethodHelper { + private static final Map> GRAPH_CACHE = + new ConcurrentHashMap<>(); + private static final Map CANONICAL_NAME_CACHE = new ConcurrentHashMap<>(); + private static final Map, Boolean> MATCH_CACHE = new ConcurrentHashMap<>(); + + /** + * Collects and orders test or configuration methods + * + * @param methods methods to be worked on + * @param forTests true for test methods, false for configuration methods + * @param runInfo - {@link RunInfo} object. + * @param finder annotation finder + * @param unique true for unique methods, false otherwise + * @param outExcludedMethods - A List of excluded {@link ITestNGMethod} methods. + * @return list of ordered methods + */ + public static ITestNGMethod[] collectAndOrderMethods( + List methods, + boolean forTests, + RunInfo runInfo, + IAnnotationFinder finder, + boolean unique, + List outExcludedMethods, + Comparator comparator) { + AtomicReference results = new AtomicReference<>(); + List includedMethods = Lists.newArrayList(); + TimeUtils.computeAndShowTime("MethodGroupsHelper.collectMethodsByGroup()", + () -> MethodGroupsHelper.collectMethodsByGroup( + methods.toArray(new ITestNGMethod[0]), + forTests, + includedMethods, + outExcludedMethods, + runInfo, + finder, + unique) + ); + TimeUtils.computeAndShowTime("MethodGroupsHelper.sortMethods()", + () -> results.set(sortMethods(forTests, includedMethods, comparator) + .toArray(new ITestNGMethod[]{})) + ); + return results.get(); + } + + /** + * Finds TestNG methods that the specified TestNG method depends upon + * + * @param m TestNG method + * @param methods list of methods to search for depended upon methods + * @return list of methods that match the criteria + */ + protected static ITestNGMethod[] findDependedUponMethods( + ITestNGMethod m, List methods) { + ITestNGMethod[] methodsArray = methods.toArray(new ITestNGMethod[0]); + return findDependedUponMethods(m, methodsArray); + } + + /** + * Finds TestNG methods that the specified TestNG method depends upon + * + * @param m TestNG method + * @param methods list of methods to search for depended upon methods + * @return list of methods that match the criteria + */ + public static ITestNGMethod[] findDependedUponMethods( + ITestNGMethod m, ITestNGMethod[] methods) { + + String canonicalMethodName = calculateMethodCanonicalName(m); + + List vResult = Lists.newArrayList(); + String regexp = null; + for (String fullyQualifiedRegexp : m.getMethodsDependedUpon()) { + boolean foundAtLeastAMethod = false; + + if (null != fullyQualifiedRegexp) { + // Escapes $ in regexps as it is not meant for end - line matching, but inner class matches. + regexp = fullyQualifiedRegexp.replace("$", "\\$"); + MatchResults results = matchMethod(methods, regexp); + foundAtLeastAMethod = results.foundAtLeastAMethod; + vResult.addAll(results.matchedMethods); + if (!foundAtLeastAMethod) { + // Replace the declaring class name in the dependsOnMethods value with + // the fully qualified test class name and retry the method matching. + int lastIndex = regexp.lastIndexOf('.'); + String newMethodName; + if (lastIndex != -1) { + String clazzName = (m.getTestClass() != null ? m.getTestClass().getRealClass() : m.getRealClass()).getName(); + newMethodName = clazzName + regexp.substring(lastIndex); + results = matchMethod(methods, newMethodName); + foundAtLeastAMethod = results.foundAtLeastAMethod; + vResult.addAll(results.matchedMethods); + } + } + } + + if (!foundAtLeastAMethod) { + if (m.ignoreMissingDependencies()) { + continue; + } + if (m.isAlwaysRun()) { + continue; + } + Method maybeReferringTo = findMethodByName(m, regexp); + if (maybeReferringTo != null) { + throw new TestNGException( + canonicalMethodName + + "() is depending on method " + + maybeReferringTo + + ", which is not annotated with @Test or not included."); + } + throw new TestNGException( + canonicalMethodName + "() depends on nonexistent method " + regexp); + } + } // end for + + return vResult.toArray(new ITestNGMethod[0]); + } + + /** + * Finds method based on regex and TestNGMethod. If regex doesn't represent the class name, uses + * the TestNG method's class name. + * + * @param testngMethod TestNG method + * @param regExp regex representing a method and/or related class name + */ + private static Method findMethodByName(ITestNGMethod testngMethod, String regExp) { + if (regExp == null) { + return null; + } + int lastDot = regExp.lastIndexOf('.'); + String className, methodName; + if (lastDot == -1) { + className = testngMethod.getConstructorOrMethod().getDeclaringClass().getCanonicalName(); + methodName = regExp; + } else { + methodName = regExp.substring(lastDot + 1); + className = regExp.substring(0, lastDot); + } + + try { + Class c = Class.forName(className); + for (Method m : c.getDeclaredMethods()) { + if (methodName.equals(m.getName())) { + return m; + } + } + } catch (Exception e) { + // only logging + Utils.log("MethodHelper", 3, "Caught exception while searching for methods using regex"); + } + return null; + } + + public static boolean isEnabled(Class objectClass, IAnnotationFinder finder) { + ITestAnnotation testClassAnnotation = AnnotationHelper.findTest(finder, objectClass); + return isEnabled(testClassAnnotation); + } + + public static boolean isEnabled(Method m, IAnnotationFinder finder) { + ITestAnnotation annotation = AnnotationHelper.findTest(finder, m); + + // If no method annotation, look for one on the class + if (null == annotation) { + annotation = AnnotationHelper.findTest(finder, m.getDeclaringClass()); + } + + return isEnabled(annotation); + } + + public static boolean isEnabled(ITestOrConfiguration test) { + return null == test || test.getEnabled(); + } + + public static boolean isAlwaysRun(IConfigurationAnnotation configurationAnnotation) { + if (null == configurationAnnotation) { + return false; + } + + boolean alwaysRun = false; + if ((configurationAnnotation.getAfterSuite() + || configurationAnnotation.getAfterTest() + || configurationAnnotation.getAfterTestClass() + || configurationAnnotation.getAfterTestMethod() + || configurationAnnotation.getBeforeTestMethod() + || configurationAnnotation.getBeforeTestClass() + || configurationAnnotation.getBeforeTest() + || configurationAnnotation.getBeforeSuite() + || configurationAnnotation.getBeforeGroups().length != 0 + || configurationAnnotation.getAfterGroups().length != 0) + && configurationAnnotation.getAlwaysRun()) { + alwaysRun = true; + } + + return alwaysRun; + } + + /** Extracts the unique list of ITestNGMethods. */ + public static List uniqueMethodList(Collection> methods) { + Set resultSet = Sets.newHashSet(); + + for (List l : methods) { + resultSet.addAll(l); + } + + return Lists.newArrayList(resultSet); + } + + private static Graph topologicalSort( + ITestNGMethod[] methods, + List sequentialList, + List parallelList, + final Comparator comparator) { + Graph result = + new Graph<>((o1, o2) -> comparator.compare(o1.getObject(), o2.getObject())); + + if (methods.length == 0) { + return result; + } + + // + // Create the graph + // + + Map> testInstances = sortMethodsByInstance(methods); + + XmlTest xmlTest = null; + for (ITestNGMethod m : methods) { + if (xmlTest == null) { + xmlTest = m.getXmlTest(); + } + result.addNode(m); + + List predecessors = Lists.newArrayList(); + + String[] methodsDependedUpon = m.getMethodsDependedUpon(); + if (methodsDependedUpon.length > 0) { + ITestNGMethod[] methodsNamed; + // Method has instance + if (m.getInstance() != null) { + // Get other methods with the same instance + List instanceMethods = testInstances.get(m.getInstance()); + try { + // Search for other methods that depends upon with the same instance + methodsNamed = MethodHelper.findDependedUponMethods(m, instanceMethods); + } catch (TestNGException e) { + // Maybe this method has a dependency on a method that resides in a different instance. + // Lets try searching for all methods now + methodsNamed = MethodHelper.findDependedUponMethods(m, methods); + } + } else { + // Search all methods + methodsNamed = MethodHelper.findDependedUponMethods(m, methods); + } + predecessors.addAll(Arrays.asList(methodsNamed)); + } + if (XmlTest.isGroupBasedExecution(xmlTest)) { + String[] groupsDependedUpon = m.getGroupsDependedUpon(); + if (groupsDependedUpon.length > 0) { + for (String group : groupsDependedUpon) { + ITestNGMethod[] methodsThatBelongToGroup = + MethodGroupsHelper.findMethodsThatBelongToGroup(m, methods, group); + predecessors.addAll(Arrays.asList(methodsThatBelongToGroup)); + } + } + } + + for (ITestNGMethod predecessor : predecessors) { + result.addPredecessor(m, predecessor); + } + } + + result.topologicalSort(); + sequentialList.addAll(result.getStrictlySortedNodes()); + parallelList.addAll(result.getIndependentNodes()); + + return result; + } + + /** + * This method is used to create a map of test instances and their associated method(s) . Used to + * decrease the scope to only a methods instance when trying to find method dependencies. + * + * @param methods Methods to be sorted + * @return Map of Instances as the keys and the methods associated with the instance as the values + */ + private static Map> sortMethodsByInstance(ITestNGMethod[] methods) { + return Arrays.stream(methods) + .parallel() + .filter(m -> Objects.nonNull(m.getInstance())) + .collect(Collectors.groupingBy(ITestNGMethod::getInstance, Collectors.toList())); + } + + protected static String calculateMethodCanonicalName(ITestNGMethod m) { + return calculateMethodCanonicalName(m.getConstructorOrMethod().getMethod()); + } + + private static String calculateMethodCanonicalName(Method m) { + String result = CANONICAL_NAME_CACHE.get(m); + if (result != null) { + return result; + } + + String packageName = m.getDeclaringClass().getName() + "." + m.getName(); + + // Try to find the method on this class or parents + Class cls = m.getDeclaringClass(); + while (cls != Object.class) { + try { + if (cls.getDeclaredMethod(m.getName(), m.getParameterTypes()) != null) { + packageName = cls.getName(); + break; + } + } catch (Exception e) { + // ignore + } + cls = cls.getSuperclass(); + } + + result = packageName + "." + m.getName(); + CANONICAL_NAME_CACHE.put(m, result); + return result; + } + + private static List sortMethods( + boolean forTests, List allMethods, Comparator comparator) { + List sl = Lists.newArrayList(); + List pl = Lists.newArrayList(); + ITestNGMethod[] allMethodsArray = allMethods.toArray(new ITestNGMethod[0]); + + // Fix the method inheritance if these are @Configuration methods to make + // sure base classes are invoked before child classes if 'before' and the + // other way around if they are 'after' + if (!forTests && allMethodsArray.length > 0) { + ITestNGMethod m = allMethodsArray[0]; + boolean before = + m.isBeforeClassConfiguration() + || m.isBeforeMethodConfiguration() + || m.isBeforeSuiteConfiguration() + || m.isBeforeTestConfiguration(); + MethodInheritance.fixMethodInheritance(allMethodsArray, before); + } + + topologicalSort(allMethodsArray, sl, pl, comparator); + + List result = Lists.newArrayList(); + result.addAll(sl); + result.addAll(pl); + return result; + } + + /** @return A sorted array containing all the methods 'method' depends on */ + public static List getMethodsDependedUpon( + ITestNGMethod method, ITestNGMethod[] methods, Comparator comparator) { + Graph g = GRAPH_CACHE.get(methods); + if (g == null) { + List parallelList = Lists.newArrayList(); + List sequentialList = Lists.newArrayList(); + g = topologicalSort(methods, sequentialList, parallelList, comparator); + GRAPH_CACHE.put(methods, g); + } + + return g.findPredecessors(method); + } + + // TODO: This needs to be revisited so that, we dont update the parameter list "methodList" + // but we are returning the values. + public static void fixMethodsWithClass( + ITestNGMethod[] methods, ITestClass testCls, List methodList) { + for (ITestNGMethod itm : methods) { + itm.setTestClass(testCls); + + if (methodList != null) { + methodList.add(itm); + } + } + } + + public static List methodsToMethodInstances(List sl) { + return sl.stream().map(MethodInstance::new).collect(Collectors.toList()); + } + + public static List methodInstancesToMethods( + List methodInstances) { + return methodInstances.stream().map(IMethodInstance::getMethod).collect(Collectors.toList()); + } + + public static void dumpInvokedMethodInfoToConsole( + ITestNGMethod[] methods, int currentVerbosity) { + if (currentVerbosity < 3) { + return; + } + System.out.println("===== Invoked methods"); + Arrays.stream(methods).filter(m -> m instanceof IInvocationStatus) + .filter(m -> ((IInvocationStatus) m).getInvocationTime() > 0) + .forEach(im -> { + if (im.isTest()) { + System.out.print(" "); + } else if (isConfigurationMethod(im)) { + System.out.print(" "); + } else { + return; + } + System.out.println("" + im); + }); + System.out.println("====="); + } + + private static boolean isConfigurationMethod(ITestNGMethod tm) { + return tm.isBeforeSuiteConfiguration() || tm.isAfterSuiteConfiguration() || + tm.isBeforeTestConfiguration() || tm.isAfterTestConfiguration() || + tm.isBeforeClassConfiguration() || tm.isAfterClassConfiguration() || + tm.isBeforeGroupsConfiguration() || tm.isAfterGroupsConfiguration() || + tm.isBeforeMethodConfiguration() || tm.isAfterMethodConfiguration(); + } + + protected static String calculateMethodCanonicalName(Class methodClass, String methodName) { + Set methods = ClassHelper.getAvailableMethods(methodClass); // TESTNG-139 + return methods.stream() + .filter(method -> methodName.equals(method.getName())) + .findFirst() + .map(MethodHelper::calculateMethodCanonicalName) + .orElse(null); + } + + public static void clear(Stream methods) { + methods.filter(Objects::nonNull) + .forEach(CANONICAL_NAME_CACHE::remove); + } + + public static long calculateTimeOut(ITestNGMethod tm) { + return tm.getTimeOut() > 0 ? tm.getTimeOut() : tm.getInvocationTimeOut(); + } + + private static MatchResults matchMethod(ITestNGMethod[] methods, String regexp) { + MatchResults results = new MatchResults(); + boolean usePackage = regexp.indexOf('.') != -1; + Pattern pattern = Pattern.compile(regexp); + for (ITestNGMethod method : methods) { + ConstructorOrMethod thisMethod = method.getConstructorOrMethod(); + String thisMethodName = thisMethod.getName(); + String methodName = usePackage ? calculateMethodCanonicalName(method) : thisMethodName; + Pair cacheKey = Pair.create(regexp, methodName); + Boolean match = MATCH_CACHE.get(cacheKey); + if (match == null) { + match = pattern.matcher(methodName).matches(); + MATCH_CACHE.put(cacheKey, match); + } + if (match) { + results.matchedMethods.add(method); + results.foundAtLeastAMethod = true; + } + } + return results; + } + + private static class MatchResults { + private final List matchedMethods = Lists.newArrayList(); + private boolean foundAtLeastAMethod = false; + } +} diff --git a/testng-core/src/main/java/org/testng/internal/MethodInstance.java b/testng-core/src/main/java/org/testng/internal/MethodInstance.java index 0643f14e02..75d1f519d4 100644 --- a/testng-core/src/main/java/org/testng/internal/MethodInstance.java +++ b/testng-core/src/main/java/org/testng/internal/MethodInstance.java @@ -1,92 +1,92 @@ -package org.testng.internal; - -import org.testng.IMethodInstance; -import org.testng.ITestNGMethod; -import org.testng.collections.Objects; -import org.testng.xml.XmlClass; -import org.testng.xml.XmlInclude; -import org.testng.xml.XmlTest; - -import java.util.Comparator; -import java.util.List; - -public class MethodInstance implements IMethodInstance { - private final ITestNGMethod m_method; - - public MethodInstance(ITestNGMethod method) { - m_method = method; - } - - @Override - public ITestNGMethod getMethod() { - return m_method; - } - - @Override - public Object getInstance() { - return m_method.getInstance(); - } - - @Override - public String toString() { - return Objects.toStringHelper(getClass()) - .add("method", m_method) - .add("instance", getInstance()) - .toString(); - } - - public static final Comparator SORT_BY_INDEX = - new Comparator() { - @Override - public int compare(IMethodInstance o1, IMethodInstance o2) { - // If the two methods are in different - XmlTest test1 = o1.getMethod().getTestClass().getXmlTest(); - XmlTest test2 = o2.getMethod().getTestClass().getXmlTest(); - - // If the two methods are not in the same , we can't compare them - if (!test1.getName().equals(test2.getName())) { - return 0; - } - - int result = 0; - - // If the two methods are in the same , compare them by their method - // index, otherwise compare them with their class index. - XmlClass class1 = o1.getMethod().getTestClass().getXmlClass(); - XmlClass class2 = o2.getMethod().getTestClass().getXmlClass(); - - // This can happen if these classes came from a @Factory, in which case, they - // don't have an associated XmlClass - if (class1 == null || class2 == null) { - if (class1 != null) return -1; - if (class2 != null) return 1; - return 0; - } - - if (!class1.getName().equals(class2.getName())) { - int index1 = class1.getIndex(); - int index2 = class2.getIndex(); - result = index1 - index2; - } else { - XmlInclude include1 = - findXmlInclude(class1.getIncludedMethods(), o1.getMethod().getMethodName()); - XmlInclude include2 = - findXmlInclude(class2.getIncludedMethods(), o2.getMethod().getMethodName()); - if (include1 != null && include2 != null) { - result = include1.getIndex() - include2.getIndex(); - } - } - - return result; - } - - private XmlInclude findXmlInclude(List includedMethods, String methodName) { - for (XmlInclude xi : includedMethods) { - if (xi.getName().equals(methodName)) { - return xi; - } - } - return null; - } - }; -} +package org.testng.internal; + +import org.testng.IMethodInstance; +import org.testng.ITestNGMethod; +import org.testng.collections.Objects; +import org.testng.xml.XmlClass; +import org.testng.xml.XmlInclude; +import org.testng.xml.XmlTest; + +import java.util.Comparator; +import java.util.List; + +public class MethodInstance implements IMethodInstance { + private final ITestNGMethod m_method; + + public MethodInstance(ITestNGMethod method) { + m_method = method; + } + + @Override + public ITestNGMethod getMethod() { + return m_method; + } + + @Override + public Object getInstance() { + return m_method.getInstance(); + } + + @Override + public String toString() { + return Objects.toStringHelper(getClass()) + .add("method", m_method) + .add("instance", getInstance()) + .toString(); + } + + public static final Comparator SORT_BY_INDEX = + new Comparator() { + @Override + public int compare(IMethodInstance o1, IMethodInstance o2) { + // If the two methods are in different + XmlTest test1 = o1.getMethod().getTestClass().getXmlTest(); + XmlTest test2 = o2.getMethod().getTestClass().getXmlTest(); + + // If the two methods are not in the same , we can't compare them + if (!test1.getName().equals(test2.getName())) { + return 0; + } + + int result = 0; + + // If the two methods are in the same , compare them by their method + // index, otherwise compare them with their class index. + XmlClass class1 = o1.getMethod().getTestClass().getXmlClass(); + XmlClass class2 = o2.getMethod().getTestClass().getXmlClass(); + + // This can happen if these classes came from a @Factory, in which case, they + // don't have an associated XmlClass + if (class1 == null || class2 == null) { + if (class1 != null) return -1; + if (class2 != null) return 1; + return 0; + } + + if (!class1.getName().equals(class2.getName())) { + int index1 = class1.getIndex(); + int index2 = class2.getIndex(); + result = index1 - index2; + } else { + XmlInclude include1 = + findXmlInclude(class1.getIncludedMethods(), o1.getMethod().getMethodName()); + XmlInclude include2 = + findXmlInclude(class2.getIncludedMethods(), o2.getMethod().getMethodName()); + if (include1 != null && include2 != null) { + result = include1.getIndex() - include2.getIndex(); + } + } + + return result; + } + + private XmlInclude findXmlInclude(List includedMethods, String methodName) { + for (XmlInclude xi : includedMethods) { + if (xi.getName().equals(methodName)) { + return xi; + } + } + return null; + } + }; +} diff --git a/testng-core/src/main/java/org/testng/internal/NoOpTestClass.java b/testng-core/src/main/java/org/testng/internal/NoOpTestClass.java index 31e9caf384..43dae05b62 100644 --- a/testng-core/src/main/java/org/testng/internal/NoOpTestClass.java +++ b/testng-core/src/main/java/org/testng/internal/NoOpTestClass.java @@ -1,171 +1,171 @@ -package org.testng.internal; - -import org.testng.ITestClass; -import org.testng.ITestNGMethod; -import org.testng.xml.XmlClass; -import org.testng.xml.XmlTest; - -public class NoOpTestClass implements ITestClass { - - protected Class m_testClass = null; - - // Test methods - protected ITestNGMethod[] m_beforeClassMethods = new ITestNGMethod[0]; - protected ITestNGMethod[] m_beforeTestMethods = new ITestNGMethod[0]; - protected ITestNGMethod[] m_testMethods = new ITestNGMethod[0]; - protected ITestNGMethod[] m_afterClassMethods = new ITestNGMethod[0]; - protected ITestNGMethod[] m_afterTestMethods = new ITestNGMethod[0]; - protected ITestNGMethod[] m_beforeSuiteMethods = new ITestNGMethod[0]; - protected ITestNGMethod[] m_afterSuiteMethods = new ITestNGMethod[0]; - protected ITestNGMethod[] m_beforeTestConfMethods = new ITestNGMethod[0]; - protected ITestNGMethod[] m_afterTestConfMethods = new ITestNGMethod[0]; - protected ITestNGMethod[] m_beforeGroupsMethods = new ITestNGMethod[0]; - protected ITestNGMethod[] m_afterGroupsMethods = new ITestNGMethod[0]; - - private final Object[] m_instances; - private final long[] m_instanceHashes; - - private final XmlTest m_xmlTest; - private final XmlClass m_xmlClass; - - protected NoOpTestClass() { - m_instances = null; - m_instanceHashes = null; - m_xmlTest = null; - m_xmlClass = null; - } - - public NoOpTestClass(ITestClass testClass) { - m_testClass = testClass.getRealClass(); - m_beforeSuiteMethods = testClass.getBeforeSuiteMethods(); - m_beforeTestConfMethods = testClass.getBeforeTestConfigurationMethods(); - m_beforeGroupsMethods = testClass.getBeforeGroupsMethods(); - m_beforeClassMethods = testClass.getBeforeClassMethods(); - m_beforeTestMethods = testClass.getBeforeTestMethods(); - m_afterSuiteMethods = testClass.getAfterSuiteMethods(); - m_afterTestConfMethods = testClass.getAfterTestConfigurationMethods(); - m_afterGroupsMethods = testClass.getAfterGroupsMethods(); - m_afterClassMethods = testClass.getAfterClassMethods(); - m_afterTestMethods = testClass.getAfterTestMethods(); - m_instances = testClass.getInstances(true); - m_instanceHashes = testClass.getInstanceHashCodes(); - m_xmlTest = testClass.getXmlTest(); - m_xmlClass = testClass.getXmlClass(); - } - - public void setBeforeTestMethods(ITestNGMethod[] beforeTestMethods) { - m_beforeTestMethods = beforeTestMethods; - } - - public void setAfterTestMethod(ITestNGMethod[] afterTestMethods) { - m_afterTestMethods = afterTestMethods; - } - - /** @return Returns the afterClassMethods. */ - @Override - public ITestNGMethod[] getAfterClassMethods() { - return m_afterClassMethods; - } - - /** @return Returns the afterTestMethods. */ - @Override - public ITestNGMethod[] getAfterTestMethods() { - return m_afterTestMethods; - } - - /** @return Returns the beforeClassMethods. */ - @Override - public ITestNGMethod[] getBeforeClassMethods() { - return m_beforeClassMethods; - } - - /** @return Returns the beforeTestMethods. */ - @Override - public ITestNGMethod[] getBeforeTestMethods() { - return m_beforeTestMethods; - } - - /** @return Returns the testMethods. */ - @Override - public ITestNGMethod[] getTestMethods() { - return m_testMethods; - } - - @Override - public ITestNGMethod[] getBeforeSuiteMethods() { - return m_beforeSuiteMethods; - } - - @Override - public ITestNGMethod[] getAfterSuiteMethods() { - return m_afterSuiteMethods; - } - - @Override - public ITestNGMethod[] getBeforeTestConfigurationMethods() { - return m_beforeTestConfMethods; - } - - @Override - public ITestNGMethod[] getAfterTestConfigurationMethods() { - return m_afterTestConfMethods; - } - - /** @return all @Configuration methods that should be invoked before certain groups */ - @Override - public ITestNGMethod[] getBeforeGroupsMethods() { - return m_beforeGroupsMethods; - } - - /** @return all @Configuration methods that should be invoked after certain groups */ - @Override - public ITestNGMethod[] getAfterGroupsMethods() { - return m_afterGroupsMethods; - } - - /** @see org.testng.ITestClass#getInstanceHashCodes() */ - @Override - public long[] getInstanceHashCodes() { - return m_instanceHashes; - } - - /** @see org.testng.ITestClass#getInstances(boolean) */ - @Override - public Object[] getInstances(boolean reuse) { - return m_instances; - } - - @Override - public String getName() { - return m_testClass.getName(); - } - - @Override - public Class getRealClass() { - return m_testClass; - } - - /** @see org.testng.IClass#addInstance(java.lang.Object) */ - @Override - public void addInstance(Object instance) {} - - public void setTestClass(Class declaringClass) { - m_testClass = declaringClass; - } - - @Override - public String getTestName() { - // TODO Auto-generated method stub - return null; - } - - @Override - public XmlTest getXmlTest() { - return m_xmlTest; - } - - @Override - public XmlClass getXmlClass() { - return m_xmlClass; - } -} +package org.testng.internal; + +import org.testng.ITestClass; +import org.testng.ITestNGMethod; +import org.testng.xml.XmlClass; +import org.testng.xml.XmlTest; + +public class NoOpTestClass implements ITestClass { + + protected Class m_testClass = null; + + // Test methods + protected ITestNGMethod[] m_beforeClassMethods = new ITestNGMethod[0]; + protected ITestNGMethod[] m_beforeTestMethods = new ITestNGMethod[0]; + protected ITestNGMethod[] m_testMethods = new ITestNGMethod[0]; + protected ITestNGMethod[] m_afterClassMethods = new ITestNGMethod[0]; + protected ITestNGMethod[] m_afterTestMethods = new ITestNGMethod[0]; + protected ITestNGMethod[] m_beforeSuiteMethods = new ITestNGMethod[0]; + protected ITestNGMethod[] m_afterSuiteMethods = new ITestNGMethod[0]; + protected ITestNGMethod[] m_beforeTestConfMethods = new ITestNGMethod[0]; + protected ITestNGMethod[] m_afterTestConfMethods = new ITestNGMethod[0]; + protected ITestNGMethod[] m_beforeGroupsMethods = new ITestNGMethod[0]; + protected ITestNGMethod[] m_afterGroupsMethods = new ITestNGMethod[0]; + + private final Object[] m_instances; + private final long[] m_instanceHashes; + + private final XmlTest m_xmlTest; + private final XmlClass m_xmlClass; + + protected NoOpTestClass() { + m_instances = null; + m_instanceHashes = null; + m_xmlTest = null; + m_xmlClass = null; + } + + public NoOpTestClass(ITestClass testClass) { + m_testClass = testClass.getRealClass(); + m_beforeSuiteMethods = testClass.getBeforeSuiteMethods(); + m_beforeTestConfMethods = testClass.getBeforeTestConfigurationMethods(); + m_beforeGroupsMethods = testClass.getBeforeGroupsMethods(); + m_beforeClassMethods = testClass.getBeforeClassMethods(); + m_beforeTestMethods = testClass.getBeforeTestMethods(); + m_afterSuiteMethods = testClass.getAfterSuiteMethods(); + m_afterTestConfMethods = testClass.getAfterTestConfigurationMethods(); + m_afterGroupsMethods = testClass.getAfterGroupsMethods(); + m_afterClassMethods = testClass.getAfterClassMethods(); + m_afterTestMethods = testClass.getAfterTestMethods(); + m_instances = testClass.getInstances(true); + m_instanceHashes = testClass.getInstanceHashCodes(); + m_xmlTest = testClass.getXmlTest(); + m_xmlClass = testClass.getXmlClass(); + } + + public void setBeforeTestMethods(ITestNGMethod[] beforeTestMethods) { + m_beforeTestMethods = beforeTestMethods; + } + + public void setAfterTestMethod(ITestNGMethod[] afterTestMethods) { + m_afterTestMethods = afterTestMethods; + } + + /** @return Returns the afterClassMethods. */ + @Override + public ITestNGMethod[] getAfterClassMethods() { + return m_afterClassMethods; + } + + /** @return Returns the afterTestMethods. */ + @Override + public ITestNGMethod[] getAfterTestMethods() { + return m_afterTestMethods; + } + + /** @return Returns the beforeClassMethods. */ + @Override + public ITestNGMethod[] getBeforeClassMethods() { + return m_beforeClassMethods; + } + + /** @return Returns the beforeTestMethods. */ + @Override + public ITestNGMethod[] getBeforeTestMethods() { + return m_beforeTestMethods; + } + + /** @return Returns the testMethods. */ + @Override + public ITestNGMethod[] getTestMethods() { + return m_testMethods; + } + + @Override + public ITestNGMethod[] getBeforeSuiteMethods() { + return m_beforeSuiteMethods; + } + + @Override + public ITestNGMethod[] getAfterSuiteMethods() { + return m_afterSuiteMethods; + } + + @Override + public ITestNGMethod[] getBeforeTestConfigurationMethods() { + return m_beforeTestConfMethods; + } + + @Override + public ITestNGMethod[] getAfterTestConfigurationMethods() { + return m_afterTestConfMethods; + } + + /** @return all @Configuration methods that should be invoked before certain groups */ + @Override + public ITestNGMethod[] getBeforeGroupsMethods() { + return m_beforeGroupsMethods; + } + + /** @return all @Configuration methods that should be invoked after certain groups */ + @Override + public ITestNGMethod[] getAfterGroupsMethods() { + return m_afterGroupsMethods; + } + + /** @see org.testng.ITestClass#getInstanceHashCodes() */ + @Override + public long[] getInstanceHashCodes() { + return m_instanceHashes; + } + + /** @see org.testng.ITestClass#getInstances(boolean) */ + @Override + public Object[] getInstances(boolean reuse) { + return m_instances; + } + + @Override + public String getName() { + return m_testClass.getName(); + } + + @Override + public Class getRealClass() { + return m_testClass; + } + + /** @see org.testng.IClass#addInstance(java.lang.Object) */ + @Override + public void addInstance(Object instance) {} + + public void setTestClass(Class declaringClass) { + m_testClass = declaringClass; + } + + @Override + public String getTestName() { + // TODO Auto-generated method stub + return null; + } + + @Override + public XmlTest getXmlTest() { + return m_xmlTest; + } + + @Override + public XmlClass getXmlClass() { + return m_xmlClass; + } +} diff --git a/testng-core/src/main/java/org/testng/internal/TestNGMethod.java b/testng-core/src/main/java/org/testng/internal/TestNGMethod.java index c1d6a7a209..6d45261eee 100644 --- a/testng-core/src/main/java/org/testng/internal/TestNGMethod.java +++ b/testng-core/src/main/java/org/testng/internal/TestNGMethod.java @@ -1,230 +1,230 @@ -package org.testng.internal; - -import java.util.Arrays; -import java.util.Objects; - -import org.testng.IDataProviderMethod; -import org.testng.ITestClass; -import org.testng.ITestNGMethod; -import org.testng.ITestObjectFactory; -import org.testng.annotations.CustomAttribute; -import org.testng.annotations.ITestAnnotation; -import org.testng.internal.annotations.AnnotationHelper; -import org.testng.internal.annotations.IAnnotationFinder; -import org.testng.xml.XmlClass; -import org.testng.xml.XmlInclude; -import org.testng.xml.XmlTest; - -import java.lang.reflect.Method; -import java.util.Collections; -import java.util.List; - -/** This class represents a test method. */ -public class TestNGMethod extends BaseTestMethod { - - private int m_threadPoolSize = 0; - private int m_invocationCount = 1; - private int m_successPercentage = 100; - private boolean isDataDriven = false; - private CustomAttribute[] m_attributes = {}; - private IDataProviderMethod dataProviderMethod = null; - - /** Constructs a TestNGMethod */ - public TestNGMethod( - ITestObjectFactory objectFactory, - Method method, - IAnnotationFinder finder, - XmlTest xmlTest, - Object instance - ) { - this(objectFactory, method, finder, true, xmlTest, instance); - } - - private TestNGMethod( - ITestObjectFactory objectFactory, - Method method, - IAnnotationFinder finder, - boolean initialize, - XmlTest xmlTest, - Object instance) { - super(objectFactory, method.getName(), new ConstructorOrMethod(method), finder, instance); - setXmlTest(xmlTest); - - if (initialize) { - init(xmlTest); - } - } - - /** {@inheritDoc} */ - @Override - public int getInvocationCount() { - return m_invocationCount; - } - - /** {@inheritDoc} */ - @Override - public int getSuccessPercentage() { - return m_successPercentage; - } - - /** {@inheritDoc} */ - @Override - public boolean isTest() { - return true; - } - - private void init(XmlTest xmlTest) { - setXmlTest(xmlTest); - String className = m_method.getDeclaringClass().getName(); - Object obj = getInstance(); - if (obj != null) { - className = obj.getClass().getName(); - } - setInvocationNumbers(xmlTest.getInvocationNumbers(className + "." + m_method.getName())); - - ITestAnnotation testAnnotation = - AnnotationHelper.findTest(getAnnotationFinder(), m_method.getMethod()); - - if (testAnnotation == null) { - // Try on the class - testAnnotation = - AnnotationHelper.findTest(getAnnotationFinder(), m_method.getDeclaringClass()); - } - - if (null != testAnnotation) { - setTimeOut(testAnnotation.getTimeOut()); - m_successPercentage = testAnnotation.getSuccessPercentage(); - isDataDriven = doesTestAnnotationHaveADataProvider(testAnnotation); - - setInvocationCount(testAnnotation.getInvocationCount()); - setThreadPoolSize(testAnnotation.getThreadPoolSize()); - setAlwaysRun(testAnnotation.getAlwaysRun()); - setDescription(findDescription(testAnnotation, xmlTest)); - setEnabled(testAnnotation.getEnabled()); - setRetryAnalyzerClass(testAnnotation.getRetryAnalyzerClass()); - setSkipFailedInvocations(testAnnotation.skipFailedInvocations()); - setInvocationTimeOut(testAnnotation.invocationTimeOut()); - setIgnoreMissingDependencies(testAnnotation.ignoreMissingDependencies()); - setPriority(testAnnotation.getPriority()); - m_attributes = testAnnotation.getAttributes(); - } - - // Groups - initGroups(ITestAnnotation.class); - } - - private static boolean doesTestAnnotationHaveADataProvider(ITestAnnotation testAnnotation) { - return !testAnnotation.getDataProvider().trim().isEmpty() - || testAnnotation.getDataProviderClass() != null; - } - - private String findDescription(ITestAnnotation testAnnotation, XmlTest xmlTest) { - String result = testAnnotation.getDescription(); - if (result != null) { - return result; - } - List classes = xmlTest.getXmlClasses(); - return classes.stream() - .filter(this::classNameMatcher) - .flatMap(xmlClass -> xmlClass.getIncludedMethods().stream()) - .filter(this::methodNameMatcher) - .map(XmlInclude::getDescription) - .filter(Objects::nonNull) - .findFirst().orElse(""); - } - - private boolean classNameMatcher(XmlClass xmlClass) { - return xmlClass.getName().equals(m_method.getMethod().getDeclaringClass().getName()); - } - - private boolean methodNameMatcher(XmlInclude xmlInclude) { - return xmlInclude.getName().equals(m_method.getName()); - } - - /** {@inheritDoc} */ - @Override - public int getThreadPoolSize() { - return m_threadPoolSize; - } - - /** Sets the number of threads on which this method should be invoked. */ - @Override - public void setThreadPoolSize(int threadPoolSize) { - m_threadPoolSize = threadPoolSize; - } - - /** Sets the number of invocations for this method. */ - @Override - public void setInvocationCount(int counter) { - m_invocationCount = counter; - } - - /** - * Clones the current TestNGMethod and its @BeforeMethod and @AfterMethod methods. - * - * @see org.testng.internal.BaseTestMethod#clone() - */ - @Override - public BaseTestMethod clone() { - TestNGMethod clone = - new TestNGMethod( - m_objectFactory, - getConstructorOrMethod().getMethod(), - getAnnotationFinder(), - false, - getXmlTest(), - getInstance()); - ITestClass tc = getTestClass(); - NoOpTestClass testClass = new NoOpTestClass(tc); - testClass.setBeforeTestMethods(clone(tc.getBeforeTestMethods())); - testClass.setAfterTestMethod(clone(tc.getAfterTestMethods())); - clone.m_testClass = testClass; - clone.setDate(getDate()); - clone.setGroups(getGroups()); - clone.setGroupsDependedUpon(getGroupsDependedUpon(), Collections.emptyList()); - clone.setMethodsDependedUpon(getMethodsDependedUpon()); - clone.setAlwaysRun(isAlwaysRun()); - clone.m_beforeGroups = getBeforeGroups(); - clone.m_afterGroups = getAfterGroups(); - clone.m_currentInvocationCount = m_currentInvocationCount; - clone.setMissingGroup(getMissingGroup()); - clone.setThreadPoolSize(getThreadPoolSize()); - clone.setDescription(getDescription()); - clone.setEnabled(getEnabled()); - clone.setParameterInvocationCount(getParameterInvocationCount()); - clone.setInvocationCount(getInvocationCount()); - clone.m_successPercentage = getSuccessPercentage(); - clone.setTimeOut(getTimeOut()); - clone.setRetryAnalyzerClass(getRetryAnalyzerClass()); - clone.setSkipFailedInvocations(skipFailedInvocations()); - clone.setInvocationNumbers(getInvocationNumbers()); - clone.setPriority(getPriority()); - - return clone; - } - - private static ITestNGMethod[] clone(ITestNGMethod[] sources) { - return Arrays.stream(sources) - .map(ITestNGMethod::clone) - .toArray(ITestNGMethod[]::new); - } - - @Override - public boolean isDataDriven() { - return isDataDriven; - } - - @Override - public CustomAttribute[] getAttributes() { - return m_attributes; - } - - @Override - public IDataProviderMethod getDataProviderMethod() { - return dataProviderMethod; - } - - public void setDataProviderMethod(IDataProviderMethod dataProviderMethod) { - this.dataProviderMethod = dataProviderMethod; - } -} +package org.testng.internal; + +import java.util.Arrays; +import java.util.Objects; + +import org.testng.IDataProviderMethod; +import org.testng.ITestClass; +import org.testng.ITestNGMethod; +import org.testng.ITestObjectFactory; +import org.testng.annotations.CustomAttribute; +import org.testng.annotations.ITestAnnotation; +import org.testng.internal.annotations.AnnotationHelper; +import org.testng.internal.annotations.IAnnotationFinder; +import org.testng.xml.XmlClass; +import org.testng.xml.XmlInclude; +import org.testng.xml.XmlTest; + +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.List; + +/** This class represents a test method. */ +public class TestNGMethod extends BaseTestMethod { + + private int m_threadPoolSize = 0; + private int m_invocationCount = 1; + private int m_successPercentage = 100; + private boolean isDataDriven = false; + private CustomAttribute[] m_attributes = {}; + private IDataProviderMethod dataProviderMethod = null; + + /** Constructs a TestNGMethod */ + public TestNGMethod( + ITestObjectFactory objectFactory, + Method method, + IAnnotationFinder finder, + XmlTest xmlTest, + Object instance + ) { + this(objectFactory, method, finder, true, xmlTest, instance); + } + + private TestNGMethod( + ITestObjectFactory objectFactory, + Method method, + IAnnotationFinder finder, + boolean initialize, + XmlTest xmlTest, + Object instance) { + super(objectFactory, method.getName(), new ConstructorOrMethod(method), finder, instance); + setXmlTest(xmlTest); + + if (initialize) { + init(xmlTest); + } + } + + /** {@inheritDoc} */ + @Override + public int getInvocationCount() { + return m_invocationCount; + } + + /** {@inheritDoc} */ + @Override + public int getSuccessPercentage() { + return m_successPercentage; + } + + /** {@inheritDoc} */ + @Override + public boolean isTest() { + return true; + } + + private void init(XmlTest xmlTest) { + setXmlTest(xmlTest); + String className = m_method.getDeclaringClass().getName(); + Object obj = getInstance(); + if (obj != null) { + className = obj.getClass().getName(); + } + setInvocationNumbers(xmlTest.getInvocationNumbers(className + "." + m_method.getName())); + + ITestAnnotation testAnnotation = + AnnotationHelper.findTest(getAnnotationFinder(), m_method.getMethod()); + + if (testAnnotation == null) { + // Try on the class + testAnnotation = + AnnotationHelper.findTest(getAnnotationFinder(), m_method.getDeclaringClass()); + } + + if (null != testAnnotation) { + setTimeOut(testAnnotation.getTimeOut()); + m_successPercentage = testAnnotation.getSuccessPercentage(); + isDataDriven = doesTestAnnotationHaveADataProvider(testAnnotation); + + setInvocationCount(testAnnotation.getInvocationCount()); + setThreadPoolSize(testAnnotation.getThreadPoolSize()); + setAlwaysRun(testAnnotation.getAlwaysRun()); + setDescription(findDescription(testAnnotation, xmlTest)); + setEnabled(testAnnotation.getEnabled()); + setRetryAnalyzerClass(testAnnotation.getRetryAnalyzerClass()); + setSkipFailedInvocations(testAnnotation.skipFailedInvocations()); + setInvocationTimeOut(testAnnotation.invocationTimeOut()); + setIgnoreMissingDependencies(testAnnotation.ignoreMissingDependencies()); + setPriority(testAnnotation.getPriority()); + m_attributes = testAnnotation.getAttributes(); + } + + // Groups + initGroups(ITestAnnotation.class); + } + + private static boolean doesTestAnnotationHaveADataProvider(ITestAnnotation testAnnotation) { + return !testAnnotation.getDataProvider().trim().isEmpty() + || testAnnotation.getDataProviderClass() != null; + } + + private String findDescription(ITestAnnotation testAnnotation, XmlTest xmlTest) { + String result = testAnnotation.getDescription(); + if (result != null) { + return result; + } + List classes = xmlTest.getXmlClasses(); + return classes.stream() + .filter(this::classNameMatcher) + .flatMap(xmlClass -> xmlClass.getIncludedMethods().stream()) + .filter(this::methodNameMatcher) + .map(XmlInclude::getDescription) + .filter(Objects::nonNull) + .findFirst().orElse(""); + } + + private boolean classNameMatcher(XmlClass xmlClass) { + return xmlClass.getName().equals(m_method.getMethod().getDeclaringClass().getName()); + } + + private boolean methodNameMatcher(XmlInclude xmlInclude) { + return xmlInclude.getName().equals(m_method.getName()); + } + + /** {@inheritDoc} */ + @Override + public int getThreadPoolSize() { + return m_threadPoolSize; + } + + /** Sets the number of threads on which this method should be invoked. */ + @Override + public void setThreadPoolSize(int threadPoolSize) { + m_threadPoolSize = threadPoolSize; + } + + /** Sets the number of invocations for this method. */ + @Override + public void setInvocationCount(int counter) { + m_invocationCount = counter; + } + + /** + * Clones the current TestNGMethod and its @BeforeMethod and @AfterMethod methods. + * + * @see org.testng.internal.BaseTestMethod#clone() + */ + @Override + public BaseTestMethod clone() { + TestNGMethod clone = + new TestNGMethod( + m_objectFactory, + getConstructorOrMethod().getMethod(), + getAnnotationFinder(), + false, + getXmlTest(), + getInstance()); + ITestClass tc = getTestClass(); + NoOpTestClass testClass = new NoOpTestClass(tc); + testClass.setBeforeTestMethods(clone(tc.getBeforeTestMethods())); + testClass.setAfterTestMethod(clone(tc.getAfterTestMethods())); + clone.m_testClass = testClass; + clone.setDate(getDate()); + clone.setGroups(getGroups()); + clone.setGroupsDependedUpon(getGroupsDependedUpon(), Collections.emptyList()); + clone.setMethodsDependedUpon(getMethodsDependedUpon()); + clone.setAlwaysRun(isAlwaysRun()); + clone.m_beforeGroups = getBeforeGroups(); + clone.m_afterGroups = getAfterGroups(); + clone.m_currentInvocationCount = m_currentInvocationCount; + clone.setMissingGroup(getMissingGroup()); + clone.setThreadPoolSize(getThreadPoolSize()); + clone.setDescription(getDescription()); + clone.setEnabled(getEnabled()); + clone.setParameterInvocationCount(getParameterInvocationCount()); + clone.setInvocationCount(getInvocationCount()); + clone.m_successPercentage = getSuccessPercentage(); + clone.setTimeOut(getTimeOut()); + clone.setRetryAnalyzerClass(getRetryAnalyzerClass()); + clone.setSkipFailedInvocations(skipFailedInvocations()); + clone.setInvocationNumbers(getInvocationNumbers()); + clone.setPriority(getPriority()); + + return clone; + } + + private static ITestNGMethod[] clone(ITestNGMethod[] sources) { + return Arrays.stream(sources) + .map(ITestNGMethod::clone) + .toArray(ITestNGMethod[]::new); + } + + @Override + public boolean isDataDriven() { + return isDataDriven; + } + + @Override + public CustomAttribute[] getAttributes() { + return m_attributes; + } + + @Override + public IDataProviderMethod getDataProviderMethod() { + return dataProviderMethod; + } + + public void setDataProviderMethod(IDataProviderMethod dataProviderMethod) { + this.dataProviderMethod = dataProviderMethod; + } +} diff --git a/testng-core/src/main/java/org/testng/internal/annotations/TestAnnotation.java b/testng-core/src/main/java/org/testng/internal/annotations/TestAnnotation.java index 07df49b922..9ce02643ca 100644 --- a/testng-core/src/main/java/org/testng/internal/annotations/TestAnnotation.java +++ b/testng-core/src/main/java/org/testng/internal/annotations/TestAnnotation.java @@ -1,194 +1,194 @@ -package org.testng.internal.annotations; - -import org.testng.IRetryAnalyzer; -import org.testng.annotations.CustomAttribute; -import org.testng.annotations.ITestAnnotation; - -/** - * An implementation of ITest - */ -public class TestAnnotation extends TestOrConfiguration implements ITestAnnotation { - - private long m_invocationTimeOut = 0; - private int m_invocationCount = 1; - private int m_threadPoolSize = 0; - private int m_successPercentage = 100; - private String m_dataProvider = ""; - private boolean m_alwaysRun = false; - private Class[] m_expectedExceptions = {}; - private String m_expectedExceptionsMessageRegExp = ".*"; - private String m_suiteName = ""; - private String m_testName = ""; - private boolean m_singleThreaded = false; - private Class m_dataProviderClass = null; - private Class m_retryAnalyzerClass = null; - private boolean m_skipFailedInvocations = false; - private boolean m_ignoreMissingDependencies = false; - private CustomAttribute[] m_attributes = {}; - - /** @return the expectedExceptions */ - @Override - public Class[] getExpectedExceptions() { - return m_expectedExceptions; - } - - /** @param expectedExceptions the expectedExceptions to set */ - @Override - public void setExpectedExceptions(Class[] expectedExceptions) { - m_expectedExceptions = expectedExceptions; - } - - @Override - public String getExpectedExceptionsMessageRegExp() { - return m_expectedExceptionsMessageRegExp; - } - - @Override - public void setExpectedExceptionsMessageRegExp(String expectedExceptionsMessageRegExp) { - m_expectedExceptionsMessageRegExp = expectedExceptionsMessageRegExp; - } - - @Override - public void setAlwaysRun(boolean alwaysRun) { - m_alwaysRun = alwaysRun; - } - - @Override - public void setDataProvider(String dataProvider) { - m_dataProvider = dataProvider; - } - - @Override - public Class getDataProviderClass() { - return m_dataProviderClass; - } - - @Override - public void setDataProviderClass(Class dataProviderClass) { - m_dataProviderClass = dataProviderClass; - } - - @Override - public void setInvocationCount(int invocationCount) { - m_invocationCount = invocationCount; - } - - @Override - public void setSuccessPercentage(int successPercentage) { - m_successPercentage = successPercentage; - } - - @Override - public int getInvocationCount() { - return m_invocationCount; - } - - @Override - public long invocationTimeOut() { - return m_invocationTimeOut; - } - - @Override - public void setInvocationTimeOut(long timeOut) { - m_invocationTimeOut = timeOut; - } - - @Override - public int getSuccessPercentage() { - return m_successPercentage; - } - - @Override - public String getDataProvider() { - return m_dataProvider; - } - - @Override - public boolean getAlwaysRun() { - return m_alwaysRun; - } - - @Override - public int getThreadPoolSize() { - return m_threadPoolSize; - } - - @Override - public void setThreadPoolSize(int threadPoolSize) { - m_threadPoolSize = threadPoolSize; - } - - @Override - public String getSuiteName() { - return m_suiteName; - } - - @Override - public void setSuiteName(String xmlSuite) { - m_suiteName = xmlSuite; - } - - @Override - public String getTestName() { - return m_testName; - } - - @Override - public void setTestName(String xmlTest) { - m_testName = xmlTest; - } - - @Override - public boolean getSingleThreaded() { - return m_singleThreaded; - } - - @Override - public void setSingleThreaded(boolean singleThreaded) { - m_singleThreaded = singleThreaded; - } - - @Override - public void setRetryAnalyzer(Class c) { - m_retryAnalyzerClass = c; - } - - @Override - public Class getRetryAnalyzerClass() { - return m_retryAnalyzerClass; - } - - @Override - public void setSkipFailedInvocations(boolean skip) { - m_skipFailedInvocations = skip; - } - - @Override - public boolean skipFailedInvocations() { - return m_skipFailedInvocations; - } - - @Override - public void setIgnoreMissingDependencies(boolean ignore) { - m_ignoreMissingDependencies = ignore; - } - - @Override - public boolean ignoreMissingDependencies() { - return m_ignoreMissingDependencies; - } - - @Override - public CustomAttribute[] getAttributes() { - return m_attributes; - } - - @Override - public void setAttributes(CustomAttribute[] attributes) { - m_attributes = attributes; - } - - private static boolean isRetryAnalyzerNotTestNGInjected(Class c) { - return !DisabledRetryAnalyzer.class.equals(c); - } -} +package org.testng.internal.annotations; + +import org.testng.IRetryAnalyzer; +import org.testng.annotations.CustomAttribute; +import org.testng.annotations.ITestAnnotation; + +/** + * An implementation of ITest + */ +public class TestAnnotation extends TestOrConfiguration implements ITestAnnotation { + + private long m_invocationTimeOut = 0; + private int m_invocationCount = 1; + private int m_threadPoolSize = 0; + private int m_successPercentage = 100; + private String m_dataProvider = ""; + private boolean m_alwaysRun = false; + private Class[] m_expectedExceptions = {}; + private String m_expectedExceptionsMessageRegExp = ".*"; + private String m_suiteName = ""; + private String m_testName = ""; + private boolean m_singleThreaded = false; + private Class m_dataProviderClass = null; + private Class m_retryAnalyzerClass = null; + private boolean m_skipFailedInvocations = false; + private boolean m_ignoreMissingDependencies = false; + private CustomAttribute[] m_attributes = {}; + + /** @return the expectedExceptions */ + @Override + public Class[] getExpectedExceptions() { + return m_expectedExceptions; + } + + /** @param expectedExceptions the expectedExceptions to set */ + @Override + public void setExpectedExceptions(Class[] expectedExceptions) { + m_expectedExceptions = expectedExceptions; + } + + @Override + public String getExpectedExceptionsMessageRegExp() { + return m_expectedExceptionsMessageRegExp; + } + + @Override + public void setExpectedExceptionsMessageRegExp(String expectedExceptionsMessageRegExp) { + m_expectedExceptionsMessageRegExp = expectedExceptionsMessageRegExp; + } + + @Override + public void setAlwaysRun(boolean alwaysRun) { + m_alwaysRun = alwaysRun; + } + + @Override + public void setDataProvider(String dataProvider) { + m_dataProvider = dataProvider; + } + + @Override + public Class getDataProviderClass() { + return m_dataProviderClass; + } + + @Override + public void setDataProviderClass(Class dataProviderClass) { + m_dataProviderClass = dataProviderClass; + } + + @Override + public void setInvocationCount(int invocationCount) { + m_invocationCount = invocationCount; + } + + @Override + public void setSuccessPercentage(int successPercentage) { + m_successPercentage = successPercentage; + } + + @Override + public int getInvocationCount() { + return m_invocationCount; + } + + @Override + public long invocationTimeOut() { + return m_invocationTimeOut; + } + + @Override + public void setInvocationTimeOut(long timeOut) { + m_invocationTimeOut = timeOut; + } + + @Override + public int getSuccessPercentage() { + return m_successPercentage; + } + + @Override + public String getDataProvider() { + return m_dataProvider; + } + + @Override + public boolean getAlwaysRun() { + return m_alwaysRun; + } + + @Override + public int getThreadPoolSize() { + return m_threadPoolSize; + } + + @Override + public void setThreadPoolSize(int threadPoolSize) { + m_threadPoolSize = threadPoolSize; + } + + @Override + public String getSuiteName() { + return m_suiteName; + } + + @Override + public void setSuiteName(String xmlSuite) { + m_suiteName = xmlSuite; + } + + @Override + public String getTestName() { + return m_testName; + } + + @Override + public void setTestName(String xmlTest) { + m_testName = xmlTest; + } + + @Override + public boolean getSingleThreaded() { + return m_singleThreaded; + } + + @Override + public void setSingleThreaded(boolean singleThreaded) { + m_singleThreaded = singleThreaded; + } + + @Override + public void setRetryAnalyzer(Class c) { + m_retryAnalyzerClass = c; + } + + @Override + public Class getRetryAnalyzerClass() { + return m_retryAnalyzerClass; + } + + @Override + public void setSkipFailedInvocations(boolean skip) { + m_skipFailedInvocations = skip; + } + + @Override + public boolean skipFailedInvocations() { + return m_skipFailedInvocations; + } + + @Override + public void setIgnoreMissingDependencies(boolean ignore) { + m_ignoreMissingDependencies = ignore; + } + + @Override + public boolean ignoreMissingDependencies() { + return m_ignoreMissingDependencies; + } + + @Override + public CustomAttribute[] getAttributes() { + return m_attributes; + } + + @Override + public void setAttributes(CustomAttribute[] attributes) { + m_attributes = attributes; + } + + private static boolean isRetryAnalyzerNotTestNGInjected(Class c) { + return !DisabledRetryAnalyzer.class.equals(c); + } +} diff --git a/testng-core/src/main/java/org/testng/internal/thread/ThreadUtil.java b/testng-core/src/main/java/org/testng/internal/thread/ThreadUtil.java index 8fbcc5f648..3d62e775f6 100644 --- a/testng-core/src/main/java/org/testng/internal/thread/ThreadUtil.java +++ b/testng-core/src/main/java/org/testng/internal/thread/ThreadUtil.java @@ -1,99 +1,99 @@ -package org.testng.internal.thread; - -import java.util.concurrent.ThreadFactory; -import org.testng.collections.Lists; -import org.testng.internal.Utils; -import org.testng.log4testng.Logger; - -import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -/** - * A helper class to interface TestNG concurrency usage. - */ -public class ThreadUtil { - - public static final String THREAD_NAME = "TestNG"; - - /** @return true if the current thread was created by TestNG. */ - public static boolean isTestNGThread() { - return Thread.currentThread().getName().contains(THREAD_NAME); - } - - /** - * Parallel execution of the tasks. The startup is synchronized so this method - * emulates a load test. - * - * @param tasks the list of tasks to be run - * @param threadPoolSize the size of the parallel threads to be used to execute the tasks - * @param timeout a maximum timeout to wait for tasks finalization - */ - public static void execute( - String name, - List tasks, - int threadPoolSize, - long timeout) { - - Utils.log( - "ThreadUtil", - 2, - "Starting executor timeOut:" - + timeout - + "ms" - + " workers:" - + tasks.size() - + " threadPoolSize:" - + threadPoolSize); - ExecutorService pooledExecutor = - new ThreadPoolExecutor( - threadPoolSize, - threadPoolSize, - timeout, - TimeUnit.MILLISECONDS, - new LinkedBlockingQueue<>(), - new TestNGThreadFactory(name)); - - List> callables = Lists.newArrayList(); - for (final Runnable task : tasks) { - callables.add( - () -> { - task.run(); - return null; - }); - } - try { - if (timeout != 0) { - pooledExecutor.invokeAll(callables, timeout, TimeUnit.MILLISECONDS); - } else { - pooledExecutor.invokeAll(callables); - } - } catch (InterruptedException handled) { - Logger.getLogger(ThreadUtil.class).error(handled.getMessage(), handled); - Thread.currentThread().interrupt(); - } finally { - pooledExecutor.shutdown(); - } - } - - /** Returns a readable name of the current executing thread. */ - public static String currentThreadInfo() { - Thread thread = Thread.currentThread(); - return thread.getName() + "@" + thread.hashCode(); - } - - public static ExecutorService createExecutor(int threadCount, String threadFactoryName) { - ThreadFactory tf = new TestNGThreadFactory("method=" + threadFactoryName); - return new ThreadPoolExecutor( - threadCount, - threadCount, - 0L, - TimeUnit.MILLISECONDS, - new LinkedBlockingQueue<>(), - tf); - } - -} +package org.testng.internal.thread; + +import java.util.concurrent.ThreadFactory; +import org.testng.collections.Lists; +import org.testng.internal.Utils; +import org.testng.log4testng.Logger; + +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * A helper class to interface TestNG concurrency usage. + */ +public class ThreadUtil { + + public static final String THREAD_NAME = "TestNG"; + + /** @return true if the current thread was created by TestNG. */ + public static boolean isTestNGThread() { + return Thread.currentThread().getName().contains(THREAD_NAME); + } + + /** + * Parallel execution of the tasks. The startup is synchronized so this method + * emulates a load test. + * + * @param tasks the list of tasks to be run + * @param threadPoolSize the size of the parallel threads to be used to execute the tasks + * @param timeout a maximum timeout to wait for tasks finalization + */ + public static void execute( + String name, + List tasks, + int threadPoolSize, + long timeout) { + + Utils.log( + "ThreadUtil", + 2, + "Starting executor timeOut:" + + timeout + + "ms" + + " workers:" + + tasks.size() + + " threadPoolSize:" + + threadPoolSize); + ExecutorService pooledExecutor = + new ThreadPoolExecutor( + threadPoolSize, + threadPoolSize, + timeout, + TimeUnit.MILLISECONDS, + new LinkedBlockingQueue<>(), + new TestNGThreadFactory(name)); + + List> callables = Lists.newArrayList(); + for (final Runnable task : tasks) { + callables.add( + () -> { + task.run(); + return null; + }); + } + try { + if (timeout != 0) { + pooledExecutor.invokeAll(callables, timeout, TimeUnit.MILLISECONDS); + } else { + pooledExecutor.invokeAll(callables); + } + } catch (InterruptedException handled) { + Logger.getLogger(ThreadUtil.class).error(handled.getMessage(), handled); + Thread.currentThread().interrupt(); + } finally { + pooledExecutor.shutdown(); + } + } + + /** Returns a readable name of the current executing thread. */ + public static String currentThreadInfo() { + Thread thread = Thread.currentThread(); + return thread.getName() + "@" + thread.hashCode(); + } + + public static ExecutorService createExecutor(int threadCount, String threadFactoryName) { + ThreadFactory tf = new TestNGThreadFactory("method=" + threadFactoryName); + return new ThreadPoolExecutor( + threadCount, + threadCount, + 0L, + TimeUnit.MILLISECONDS, + new LinkedBlockingQueue<>(), + tf); + } + +} diff --git a/testng-core/src/main/java/org/testng/junit/IJUnitTestRunner.java b/testng-core/src/main/java/org/testng/junit/IJUnitTestRunner.java index 283d9fe7d5..0f8d50d57c 100644 --- a/testng-core/src/main/java/org/testng/junit/IJUnitTestRunner.java +++ b/testng-core/src/main/java/org/testng/junit/IJUnitTestRunner.java @@ -1,48 +1,48 @@ -package org.testng.junit; - -import java.util.Collection; -import java.util.List; - -import org.testng.IInvokedMethodListener; -import org.testng.ITestNGMethod; -import org.testng.ITestObjectFactory; -import org.testng.TestNGException; -import org.testng.internal.ITestResultNotifier; -import org.testng.internal.Utils; - -/** - * An abstraction interface over JUnit test runners. - * - */ -public interface IJUnitTestRunner { - - void setInvokedMethodListeners(Collection listener); - - @Deprecated - default void setTestResultNotifier(ITestResultNotifier notifier) {} - - void run(Class junitTestClass, String... methods); - - List getTestMethods(); - - static IJUnitTestRunner createTestRunner(ITestObjectFactory objectFactory, ITestResultNotifier runner) { - IJUnitTestRunner tr; - try { - // try to get runner for JUnit 4 first - Class.forName("org.junit.Test"); - tr = new JUnit4TestRunner(objectFactory, runner); - } catch (Throwable t) { - Utils.log(IJUnitTestRunner.class.getSimpleName(), 2, "JUnit 4 was not found on the classpath"); - try { - // fallback to JUnit 3 - Class.forName("junit.framework.Test"); - tr = new JUnitTestRunner(objectFactory, runner); - } catch (Exception ex) { - Utils.log(IJUnitTestRunner.class.getSimpleName(), 2, "JUnit 3 was not found on the classpath"); - // there's no JUnit on the classpath - throw new TestNGException("Cannot create JUnit runner", ex); - } - } - return tr; - } -} +package org.testng.junit; + +import java.util.Collection; +import java.util.List; + +import org.testng.IInvokedMethodListener; +import org.testng.ITestNGMethod; +import org.testng.ITestObjectFactory; +import org.testng.TestNGException; +import org.testng.internal.ITestResultNotifier; +import org.testng.internal.Utils; + +/** + * An abstraction interface over JUnit test runners. + * + */ +public interface IJUnitTestRunner { + + void setInvokedMethodListeners(Collection listener); + + @Deprecated + default void setTestResultNotifier(ITestResultNotifier notifier) {} + + void run(Class junitTestClass, String... methods); + + List getTestMethods(); + + static IJUnitTestRunner createTestRunner(ITestObjectFactory objectFactory, ITestResultNotifier runner) { + IJUnitTestRunner tr; + try { + // try to get runner for JUnit 4 first + Class.forName("org.junit.Test"); + tr = new JUnit4TestRunner(objectFactory, runner); + } catch (Throwable t) { + Utils.log(IJUnitTestRunner.class.getSimpleName(), 2, "JUnit 4 was not found on the classpath"); + try { + // fallback to JUnit 3 + Class.forName("junit.framework.Test"); + tr = new JUnitTestRunner(objectFactory, runner); + } catch (Exception ex) { + Utils.log(IJUnitTestRunner.class.getSimpleName(), 2, "JUnit 3 was not found on the classpath"); + // there's no JUnit on the classpath + throw new TestNGException("Cannot create JUnit runner", ex); + } + } + return tr; + } +} diff --git a/testng-core/src/main/java/org/testng/junit/JUnitTestRunner.java b/testng-core/src/main/java/org/testng/junit/JUnitTestRunner.java index 5fd6526438..8d20b62080 100644 --- a/testng-core/src/main/java/org/testng/junit/JUnitTestRunner.java +++ b/testng-core/src/main/java/org/testng/junit/JUnitTestRunner.java @@ -1,259 +1,259 @@ -package org.testng.junit; - -import java.lang.reflect.Constructor; - -import org.testng.ITestNGMethod; -import org.testng.ITestResult; -import org.testng.TestNGException; -import org.testng.collections.Lists; -import org.testng.internal.ITestResultNotifier; -import org.testng.internal.invokers.InvokedMethod; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.Calendar; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.WeakHashMap; - -import junit.framework.AssertionFailedError; -import junit.framework.Test; -import junit.framework.TestListener; -import junit.framework.TestResult; -import junit.framework.TestSuite; -import org.testng.*; -import org.testng.internal.TestListenerHelper; - -/** - * A JUnit TestRunner that records/triggers all information/events necessary to TestNG. - */ -public class JUnitTestRunner implements TestListener, IJUnitTestRunner { - public static final String SUITE_METHODNAME = "suite"; - - private final ITestObjectFactory m_objectFactory; - private final ITestResultNotifier m_parentRunner; - - private Map m_tests = new WeakHashMap<>(); - private List m_methods = Lists.newArrayList(); - private Collection m_invokedMethodListeners = Lists.newArrayList(); - - public JUnitTestRunner(ITestObjectFactory objectFactory, ITestResultNotifier tr) { - m_objectFactory = objectFactory; - m_parentRunner = tr; - } - - /** - * Needed from TestRunner in order to figure out what JUnit test methods were run. - * - * @return the list of all JUnit test methods run - */ - @Override - public List getTestMethods() { - return m_methods; - } - - /** @see junit.framework.TestListener#startTest(junit.framework.Test) */ - @Override - public void startTest(Test test) { - m_tests.put(test, new TestRunInfo(Calendar.getInstance().getTimeInMillis())); - } - - /** @see junit.framework.TestListener#addError(junit.framework.Test, java.lang.Throwable) */ - @Override - public void addError(Test test, Throwable t) { - recordFailure(test, t); - } - - /** - * @see junit.framework.TestListener#addFailure(junit.framework.Test, - * junit.framework.AssertionFailedError) - */ - @Override - public void addFailure(Test test, AssertionFailedError t) { - recordFailure(test, t); - } - - private void recordFailure(Test test, Throwable t) { - TestRunInfo tri = m_tests.get(test); - if (null != tri) { - tri.setThrowable(t); - } - } - - /** @see junit.framework.TestListener#endTest(junit.framework.Test) */ - @Override - public void endTest(Test test) { - TestRunInfo tri = m_tests.get(test); - if (null == tri) { - return; // HINT: this should never happen. How do I protect myself? - } - - org.testng.internal.TestResult tr = recordResults(test, tri); - - TestListenerHelper.runTestListeners(tr, m_parentRunner.getTestListeners()); - } - - public void setInvokedMethodListeners(Collection listeners) { - m_invokedMethodListeners = listeners; - } - - private org.testng.internal.TestResult recordResults(Test test, TestRunInfo tri) { - JUnitTestClass tc = new JUnit3TestClass(test); - JUnitTestMethod tm = new JUnit3TestMethod(m_objectFactory, tc, test); - - org.testng.internal.TestResult tr = - org.testng.internal.TestResult.newEndTimeAwareTestResult(tm, null, - tri.m_failure, tri.m_start); - - if (tri.isFailure()) { - tr.setStatus(ITestResult.FAILURE); - m_parentRunner.addFailedTest(tm, tr); - } else { - m_parentRunner.addPassedTest(tm, tr); - } - - InvokedMethod im = new InvokedMethod(tri.m_start, tr); - tm.setInvokedAt(im.getDate()); - m_methods.add(tm); - for (IInvokedMethodListener l : m_invokedMethodListeners) { - l.beforeInvocation(im, tr); - } - - return tr; - } - - /** - * Returns the Test corresponding to the given suite. This is a template method, subclasses - * override runFailed(), clearStatus(). - * - * @param testClass The test class - * @param methods The test methods - * @return The corresponding Test - */ - protected Test getTest(Class testClass, String... methods) { - if (methods.length > 0) { - TestSuite ts = new TestSuite(); - try { - Constructor c = testClass.getConstructor(String.class); - for (String m : methods) { - try { - ts.addTest(m_objectFactory.newInstance(c, m)); - } catch (TestNGException ex) { - runFailed(testClass, ex.getMessage()); - } catch (IllegalArgumentException ex) { - runFailed(testClass, "actual and formal parameters differ " + ex); - } - } - } catch (NoSuchMethodException ex) { - runFailed(testClass, "no constructor accepting String argument found " + ex); - } catch (SecurityException ex) { - runFailed(testClass, "security exception " + ex); - } - return ts; - } - Method suiteMethod = null; - try { - suiteMethod = testClass.getMethod(SUITE_METHODNAME, new Class[0]); - } catch (Exception e) { - - // try to extract a test suite automatically - return new TestSuite(testClass); - } - if (!Modifier.isStatic(suiteMethod.getModifiers())) { - runFailed(testClass, "suite() method must be static"); - - return null; - } - Test test = null; - try { - test = (Test) suiteMethod.invoke(null, (Object[]) new Class[0]); // static method - if (test == null) { - return test; - } - } catch (InvocationTargetException e) { - runFailed(testClass, "failed to invoke method suite():" + e.getTargetException().toString()); - - return null; - } catch (IllegalAccessException e) { - runFailed(testClass, "failed to invoke method suite():" + e.toString()); - - return null; - } - - return test; - } - - /** - * A start implementation that ignores the TestResult - * - * @param testClass the JUnit test class - */ - @Override - public void run(Class testClass, String... methods) { - start(testClass, methods); - } - - /** - * Starts a test run. Analyzes the command line arguments and runs the given test suite. - * - * @param testCase The test class to run - * @param methods The test methods to run - * @return The test result - */ - public TestResult start(Class testCase, String... methods) { - try { - Test suite = getTest(testCase, methods); - - if (null != suite) { - return doRun(suite); - } else { - runFailed(testCase, "could not create/run JUnit test suite"); - } - } catch (Exception e) { - runFailed(testCase, "could not create/run JUnit test suite: " + e.getMessage()); - } - - return null; - } - - protected void runFailed(Class clazz, String message) { - throw new TestNGException( - "Failure in JUnit mode for class " + clazz.getName() + ": " + message); - } - - /** - * Creates the TestResult to be used for the test run. - * - * @return The created test result - */ - protected TestResult createTestResult() { - return new TestResult(); - } - - protected TestResult doRun(Test suite) { - TestResult result = createTestResult(); - result.addListener(this); - suite.run(result); - - return result; - } - - private static class TestRunInfo { - private final long m_start; - private Throwable m_failure; - - public TestRunInfo(long start) { - m_start = start; - } - - public boolean isFailure() { - return null != m_failure; - } - - public void setThrowable(Throwable t) { - m_failure = t; - } - } -} +package org.testng.junit; + +import java.lang.reflect.Constructor; + +import org.testng.ITestNGMethod; +import org.testng.ITestResult; +import org.testng.TestNGException; +import org.testng.collections.Lists; +import org.testng.internal.ITestResultNotifier; +import org.testng.internal.invokers.InvokedMethod; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.Calendar; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; + +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestListener; +import junit.framework.TestResult; +import junit.framework.TestSuite; +import org.testng.*; +import org.testng.internal.TestListenerHelper; + +/** + * A JUnit TestRunner that records/triggers all information/events necessary to TestNG. + */ +public class JUnitTestRunner implements TestListener, IJUnitTestRunner { + public static final String SUITE_METHODNAME = "suite"; + + private final ITestObjectFactory m_objectFactory; + private final ITestResultNotifier m_parentRunner; + + private Map m_tests = new WeakHashMap<>(); + private List m_methods = Lists.newArrayList(); + private Collection m_invokedMethodListeners = Lists.newArrayList(); + + public JUnitTestRunner(ITestObjectFactory objectFactory, ITestResultNotifier tr) { + m_objectFactory = objectFactory; + m_parentRunner = tr; + } + + /** + * Needed from TestRunner in order to figure out what JUnit test methods were run. + * + * @return the list of all JUnit test methods run + */ + @Override + public List getTestMethods() { + return m_methods; + } + + /** @see junit.framework.TestListener#startTest(junit.framework.Test) */ + @Override + public void startTest(Test test) { + m_tests.put(test, new TestRunInfo(Calendar.getInstance().getTimeInMillis())); + } + + /** @see junit.framework.TestListener#addError(junit.framework.Test, java.lang.Throwable) */ + @Override + public void addError(Test test, Throwable t) { + recordFailure(test, t); + } + + /** + * @see junit.framework.TestListener#addFailure(junit.framework.Test, + * junit.framework.AssertionFailedError) + */ + @Override + public void addFailure(Test test, AssertionFailedError t) { + recordFailure(test, t); + } + + private void recordFailure(Test test, Throwable t) { + TestRunInfo tri = m_tests.get(test); + if (null != tri) { + tri.setThrowable(t); + } + } + + /** @see junit.framework.TestListener#endTest(junit.framework.Test) */ + @Override + public void endTest(Test test) { + TestRunInfo tri = m_tests.get(test); + if (null == tri) { + return; // HINT: this should never happen. How do I protect myself? + } + + org.testng.internal.TestResult tr = recordResults(test, tri); + + TestListenerHelper.runTestListeners(tr, m_parentRunner.getTestListeners()); + } + + public void setInvokedMethodListeners(Collection listeners) { + m_invokedMethodListeners = listeners; + } + + private org.testng.internal.TestResult recordResults(Test test, TestRunInfo tri) { + JUnitTestClass tc = new JUnit3TestClass(test); + JUnitTestMethod tm = new JUnit3TestMethod(m_objectFactory, tc, test); + + org.testng.internal.TestResult tr = + org.testng.internal.TestResult.newEndTimeAwareTestResult(tm, null, + tri.m_failure, tri.m_start); + + if (tri.isFailure()) { + tr.setStatus(ITestResult.FAILURE); + m_parentRunner.addFailedTest(tm, tr); + } else { + m_parentRunner.addPassedTest(tm, tr); + } + + InvokedMethod im = new InvokedMethod(tri.m_start, tr); + tm.setInvokedAt(im.getDate()); + m_methods.add(tm); + for (IInvokedMethodListener l : m_invokedMethodListeners) { + l.beforeInvocation(im, tr); + } + + return tr; + } + + /** + * Returns the Test corresponding to the given suite. This is a template method, subclasses + * override runFailed(), clearStatus(). + * + * @param testClass The test class + * @param methods The test methods + * @return The corresponding Test + */ + protected Test getTest(Class testClass, String... methods) { + if (methods.length > 0) { + TestSuite ts = new TestSuite(); + try { + Constructor c = testClass.getConstructor(String.class); + for (String m : methods) { + try { + ts.addTest(m_objectFactory.newInstance(c, m)); + } catch (TestNGException ex) { + runFailed(testClass, ex.getMessage()); + } catch (IllegalArgumentException ex) { + runFailed(testClass, "actual and formal parameters differ " + ex); + } + } + } catch (NoSuchMethodException ex) { + runFailed(testClass, "no constructor accepting String argument found " + ex); + } catch (SecurityException ex) { + runFailed(testClass, "security exception " + ex); + } + return ts; + } + Method suiteMethod = null; + try { + suiteMethod = testClass.getMethod(SUITE_METHODNAME, new Class[0]); + } catch (Exception e) { + + // try to extract a test suite automatically + return new TestSuite(testClass); + } + if (!Modifier.isStatic(suiteMethod.getModifiers())) { + runFailed(testClass, "suite() method must be static"); + + return null; + } + Test test = null; + try { + test = (Test) suiteMethod.invoke(null, (Object[]) new Class[0]); // static method + if (test == null) { + return test; + } + } catch (InvocationTargetException e) { + runFailed(testClass, "failed to invoke method suite():" + e.getTargetException().toString()); + + return null; + } catch (IllegalAccessException e) { + runFailed(testClass, "failed to invoke method suite():" + e.toString()); + + return null; + } + + return test; + } + + /** + * A start implementation that ignores the TestResult + * + * @param testClass the JUnit test class + */ + @Override + public void run(Class testClass, String... methods) { + start(testClass, methods); + } + + /** + * Starts a test run. Analyzes the command line arguments and runs the given test suite. + * + * @param testCase The test class to run + * @param methods The test methods to run + * @return The test result + */ + public TestResult start(Class testCase, String... methods) { + try { + Test suite = getTest(testCase, methods); + + if (null != suite) { + return doRun(suite); + } else { + runFailed(testCase, "could not create/run JUnit test suite"); + } + } catch (Exception e) { + runFailed(testCase, "could not create/run JUnit test suite: " + e.getMessage()); + } + + return null; + } + + protected void runFailed(Class clazz, String message) { + throw new TestNGException( + "Failure in JUnit mode for class " + clazz.getName() + ": " + message); + } + + /** + * Creates the TestResult to be used for the test run. + * + * @return The created test result + */ + protected TestResult createTestResult() { + return new TestResult(); + } + + protected TestResult doRun(Test suite) { + TestResult result = createTestResult(); + result.addListener(this); + suite.run(result); + + return result; + } + + private static class TestRunInfo { + private final long m_start; + private Throwable m_failure; + + public TestRunInfo(long start) { + m_start = start; + } + + public boolean isFailure() { + return null != m_failure; + } + + public void setThrowable(Throwable t) { + m_failure = t; + } + } +} diff --git a/testng-core/src/main/java/org/testng/thread/IWorker.java b/testng-core/src/main/java/org/testng/thread/IWorker.java index 5ab0e6fffd..55709b87f3 100644 --- a/testng-core/src/main/java/org/testng/thread/IWorker.java +++ b/testng-core/src/main/java/org/testng/thread/IWorker.java @@ -1,30 +1,30 @@ -package org.testng.thread; - -import java.util.List; - -/** A runnable object that is used by {@code GraphThreadPoolExecutor} to execute tasks */ -public interface IWorker extends Runnable, Comparable> { - - /** @return list of tasks this worker is working on. */ - List getTasks(); - - /** @return the maximum time allowed for the worker to complete the task. */ - long getTimeOut(); - - /** @return the priority of this task. */ - int getPriority(); - - default long getCurrentThreadId() { - return -1; - } - - default void setThreadIdToRunOn(long threadIdToRunOn) {} - - default long getThreadIdToRunOn() { - return -1; - } - - default boolean completed() { - return true; - } -} +package org.testng.thread; + +import java.util.List; + +/** A runnable object that is used by {@code GraphThreadPoolExecutor} to execute tasks */ +public interface IWorker extends Runnable, Comparable> { + + /** @return list of tasks this worker is working on. */ + List getTasks(); + + /** @return the maximum time allowed for the worker to complete the task. */ + long getTimeOut(); + + /** @return the priority of this task. */ + int getPriority(); + + default long getCurrentThreadId() { + return -1; + } + + default void setThreadIdToRunOn(long threadIdToRunOn) {} + + default long getThreadIdToRunOn() { + return -1; + } + + default boolean completed() { + return true; + } +} diff --git a/testng-core/src/main/java/org/testng/util/RetryAnalyzerCount.java b/testng-core/src/main/java/org/testng/util/RetryAnalyzerCount.java index 0ab8fbcb2d..6e10f25e8c 100644 --- a/testng-core/src/main/java/org/testng/util/RetryAnalyzerCount.java +++ b/testng-core/src/main/java/org/testng/util/RetryAnalyzerCount.java @@ -1,49 +1,49 @@ -package org.testng.util; - -import java.util.concurrent.atomic.AtomicInteger; - -import org.testng.IRetryAnalyzer; -import org.testng.ITestResult; - -/** - * An implementation of IRetryAnalyzer that allows you to specify the maximum number of times you - * want your test to be retried. - * - * @author tocman@gmail.com (Jeremie Lenfant-Engelmann) - */ -public abstract class RetryAnalyzerCount implements IRetryAnalyzer { - - // Default retry once. - AtomicInteger count = new AtomicInteger(1); - - /** @param count the max number of time the method needs to be retried. */ - protected void setCount(int count) { - this.count.set(count); - } - - /** @return the current counter value */ - protected int getCount() { - return this.count.get(); - } - - /** - * Retries the test if count is not 0. - * - * @param result The result of the test. - */ - @Override - public boolean retry(ITestResult result) { - if (count.getAndDecrement() > 0) { - return retryMethod(result); - } - return false; - } - - /** - * The method implemented by the class that test if the test must be retried or not. - * - * @param result The result of the test. - * @return true if the test must be retried, false otherwise. - */ - public abstract boolean retryMethod(ITestResult result); -} +package org.testng.util; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.testng.IRetryAnalyzer; +import org.testng.ITestResult; + +/** + * An implementation of IRetryAnalyzer that allows you to specify the maximum number of times you + * want your test to be retried. + * + * @author tocman@gmail.com (Jeremie Lenfant-Engelmann) + */ +public abstract class RetryAnalyzerCount implements IRetryAnalyzer { + + // Default retry once. + AtomicInteger count = new AtomicInteger(1); + + /** @param count the max number of time the method needs to be retried. */ + protected void setCount(int count) { + this.count.set(count); + } + + /** @return the current counter value */ + protected int getCount() { + return this.count.get(); + } + + /** + * Retries the test if count is not 0. + * + * @param result The result of the test. + */ + @Override + public boolean retry(ITestResult result) { + if (count.getAndDecrement() > 0) { + return retryMethod(result); + } + return false; + } + + /** + * The method implemented by the class that test if the test must be retried or not. + * + * @param result The result of the test. + * @return true if the test must be retried, false otherwise. + */ + public abstract boolean retryMethod(ITestResult result); +} diff --git a/testng-core/src/main/resources/testng.css b/testng-core/src/main/resources/testng.css index 3904800f0c..5124ba863b 100644 --- a/testng-core/src/main/resources/testng.css +++ b/testng-core/src/main/resources/testng.css @@ -1,9 +1,9 @@ -.invocation-failed, .test-failed { background-color: #DD0000; } -.invocation-percent, .test-percent { background-color: #006600; } -.invocation-passed, .test-passed { background-color: #00AA00; } -.invocation-skipped, .test-skipped { background-color: #CCCC00; } - -.main-page { - font-size: x-large; -} - +.invocation-failed, .test-failed { background-color: #DD0000; } +.invocation-percent, .test-percent { background-color: #006600; } +.invocation-passed, .test-passed { background-color: #00AA00; } +.invocation-skipped, .test-skipped { background-color: #CCCC00; } + +.main-page { + font-size: x-large; +} + diff --git a/testng-core/src/test/java/ConverterSample4.java b/testng-core/src/test/java/ConverterSample4.java index c33c2a5d8c..815db4b54b 100644 --- a/testng-core/src/test/java/ConverterSample4.java +++ b/testng-core/src/test/java/ConverterSample4.java @@ -1,82 +1,82 @@ - -/* - * Created on 12-Sep-2006 by micheb10 - * it is at the wrong location, but it's easier to leave it here. - * Also, do not change the line numbers since the test will make sure - * that the tags are generated in hardcoded line numbers - */ - -/** - * Sample file for the Javadocv annotations to Java 5 annotations converter for the default package - * @author micheb10 12-Sep-2006 - * - */ -public class ConverterSample4 { - /** - * This comment line should be preserved - * @testng.before-suite alwaysRun = "true" - */ - public void beforeSuiteAlwaysRun() { - // We are just checking appropriate annotations are added so we don't care about body - } - - /** - * @testng.test - */ - public void plainTest() { - // Empty body - } - - /** - * @testng.test - * @testng.expected-exceptions - * value = "java.lang.NullPointerException java.lang.NumberFormatException" - */ - public void expectedExceptions() { - // Empty body - } - - /** - * @testng.test groups = "groupA groupB" - */ - public void testGroups() { - // Empty body - } - - /** - * @testng.after-method - */ - public void afterMethod() { - // Empty body - } - - /** - * This key should be preserved - * @author The author is a standard tag and should not be touched - * @testng.test groups = "groupA" - * dependsOnMethods = "expectedExceptions" timeOut="3000" unrecognised="futureProof" - * @version another standard tag should not be changed - * @testng.expected-exceptions - * value = "java.lang.NullPointerException java.lang.NumberFormatException" - * - */ - public void testEverything() { - - } - - /** - * @testng.data-provider name="test1" - */ - public Object[][] dataProvider() { - return null; - } - - /** - * @testng.factory - */ - @SuppressWarnings({"unchecked", "deprecation"}) - public Object[] factory() { - return null; - } - -} + +/* + * Created on 12-Sep-2006 by micheb10 + * it is at the wrong location, but it's easier to leave it here. + * Also, do not change the line numbers since the test will make sure + * that the tags are generated in hardcoded line numbers + */ + +/** + * Sample file for the Javadocv annotations to Java 5 annotations converter for the default package + * @author micheb10 12-Sep-2006 + * + */ +public class ConverterSample4 { + /** + * This comment line should be preserved + * @testng.before-suite alwaysRun = "true" + */ + public void beforeSuiteAlwaysRun() { + // We are just checking appropriate annotations are added so we don't care about body + } + + /** + * @testng.test + */ + public void plainTest() { + // Empty body + } + + /** + * @testng.test + * @testng.expected-exceptions + * value = "java.lang.NullPointerException java.lang.NumberFormatException" + */ + public void expectedExceptions() { + // Empty body + } + + /** + * @testng.test groups = "groupA groupB" + */ + public void testGroups() { + // Empty body + } + + /** + * @testng.after-method + */ + public void afterMethod() { + // Empty body + } + + /** + * This key should be preserved + * @author The author is a standard tag and should not be touched + * @testng.test groups = "groupA" + * dependsOnMethods = "expectedExceptions" timeOut="3000" unrecognised="futureProof" + * @version another standard tag should not be changed + * @testng.expected-exceptions + * value = "java.lang.NullPointerException java.lang.NumberFormatException" + * + */ + public void testEverything() { + + } + + /** + * @testng.data-provider name="test1" + */ + public Object[][] dataProvider() { + return null; + } + + /** + * @testng.factory + */ + @SuppressWarnings({"unchecked", "deprecation"}) + public Object[] factory() { + return null; + } + +} diff --git a/testng-core/src/test/java/org/testng/internal/MethodHelperTest.java b/testng-core/src/test/java/org/testng/internal/MethodHelperTest.java index a756cb4436..0d2ec8ac71 100644 --- a/testng-core/src/test/java/org/testng/internal/MethodHelperTest.java +++ b/testng-core/src/test/java/org/testng/internal/MethodHelperTest.java @@ -1,42 +1,42 @@ -package org.testng.internal; - -import org.testng.ITestNGMethod; -import org.testng.Reporter; -import org.testng.ITestObjectFactory; -import org.testng.TestNGException; -import org.testng.annotations.Test; -import org.testng.internal.annotations.IAnnotationFinder; -import org.testng.internal.annotations.JDK15AnnotationFinder; -import org.testng.internal.issue2195.TestClass; -import test.tmp.AnnotationTransformer; - -public class MethodHelperTest { - - @Test(expectedExceptions = {TestNGException.class}) - public void findDependedUponMethods() throws NoSuchMethodException { - - TestClass testClass = new TestClass(); - ConstructorOrMethod constructorOrMethod = - new ConstructorOrMethod(testClass.getClass().getMethod("dummyMethod")); - IAnnotationFinder annotationFinder = new JDK15AnnotationFinder(new AnnotationTransformer()); - ITestNGMethod method = new ConfigurationMethod( - new ITestObjectFactory() {}, - constructorOrMethod, - annotationFinder, - false, - false, - false, - false, - false, - false, - true, - false, - new String[0], - new String[0], - Reporter.getCurrentTestResult().getTestContext().getCurrentXmlTest(), testClass); - method.addMethodDependedUpon("dummyDependsOnMethod"); - ITestNGMethod[] methods = new ITestNGMethod[0]; - - MethodHelper.findDependedUponMethods(method, methods); - } -} +package org.testng.internal; + +import org.testng.ITestNGMethod; +import org.testng.Reporter; +import org.testng.ITestObjectFactory; +import org.testng.TestNGException; +import org.testng.annotations.Test; +import org.testng.internal.annotations.IAnnotationFinder; +import org.testng.internal.annotations.JDK15AnnotationFinder; +import org.testng.internal.issue2195.TestClass; +import test.tmp.AnnotationTransformer; + +public class MethodHelperTest { + + @Test(expectedExceptions = {TestNGException.class}) + public void findDependedUponMethods() throws NoSuchMethodException { + + TestClass testClass = new TestClass(); + ConstructorOrMethod constructorOrMethod = + new ConstructorOrMethod(testClass.getClass().getMethod("dummyMethod")); + IAnnotationFinder annotationFinder = new JDK15AnnotationFinder(new AnnotationTransformer()); + ITestNGMethod method = new ConfigurationMethod( + new ITestObjectFactory() {}, + constructorOrMethod, + annotationFinder, + false, + false, + false, + false, + false, + false, + true, + false, + new String[0], + new String[0], + Reporter.getCurrentTestResult().getTestContext().getCurrentXmlTest(), testClass); + method.addMethodDependedUpon("dummyDependsOnMethod"); + ITestNGMethod[] methods = new ITestNGMethod[0]; + + MethodHelper.findDependedUponMethods(method, methods); + } +} diff --git a/testng-core/src/test/java/org/testng/internal/MethodInstanceTest.java b/testng-core/src/test/java/org/testng/internal/MethodInstanceTest.java index 05b9bd08ff..f298e9d930 100644 --- a/testng-core/src/test/java/org/testng/internal/MethodInstanceTest.java +++ b/testng-core/src/test/java/org/testng/internal/MethodInstanceTest.java @@ -1,546 +1,546 @@ -package org.testng.internal; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.concurrent.Callable; - -import org.testng.Assert; -import org.testng.IClass; -import org.testng.IRetryAnalyzer; -import org.testng.ITestClass; -import org.testng.ITestNGMethod; -import org.testng.ITestResult; -import org.testng.annotations.Test; -import org.testng.xml.XmlClass; -import org.testng.xml.XmlInclude; -import org.testng.xml.XmlTest; - -/** - * Unit tests for {@link MethodInstance}. - * - * @author Andreas Kluth - */ -public class MethodInstanceTest { - - public static void main(String[] args) { - new MethodInstanceTest().sortByIndexSatisfiesContract(); - } - - @Test - public void sortByIndexSatisfiesContract() { - - // Build a list of entries imposing the same behavior as the live suite, reduced to the - // minimum to create the same condition. - List methods = new ArrayList<>(); - methods.add(buildMethodInstance("unittests", "StateTest", 1, "aCategorization")); - methods.add(buildMethodInstance("unittests", "StateTest", 1, "bCategorization")); - methods.add(buildMethodInstance("unittests", "StateTest", 1, "cCategorization")); - methods.add(buildMethodInstance("unittests", "StateTest", 1, "dCategorization")); - methods.add(buildMethodInstance("unittests", "StateTest", 1, "eCategorization")); - methods.add(buildMethodInstance("unittests", "StateTest", 1, "fCategorization")); - methods.add(buildMethodInstance("unittests", "StatusTest", 2, "aStatus")); - methods.add(buildTestNgFactoryMethodInstance("unittests")); - methods.add(buildTestNgFactoryMethodInstance("unittests")); - methods.add(buildTestNgFactoryMethodInstance("unittests")); - methods.add(buildTestNgFactoryMethodInstance("unittests")); - methods.add(buildTestNgFactoryMethodInstance("unittests")); - methods.add(buildTestNgFactoryMethodInstance("unittests")); - methods.add(buildTestNgFactoryMethodInstance("unittests")); - methods.add(buildTestNgFactoryMethodInstance("unittests")); - methods.add(buildTestNgFactoryMethodInstance("unittests")); - methods.add(buildTestNgFactoryMethodInstance("unittests")); - methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "aChangeTest")); - methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "bChangeTest")); - methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "cChangeTest")); - methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "dChangeTest")); - methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "eChangeTest")); - methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "fChangeTest")); - methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "gChangeTest")); - methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "eChangeTest")); - methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "hChangeTest")); - methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "iChangeTest")); - methods.add(buildMethodInstance("unittests", "IdentifierClassTest", 4, "aIdentifier")); - methods.add(buildMethodInstance("unittests", "IdentifierClassTest", 4, "bIdentifier")); - methods.add(buildMethodInstance("unittests", "StatisticsTest", 0, "aStatistics")); - methods.add(buildMethodInstance("unittests", "StatisticsTest", 0, "bStatistics")); - methods.add(buildMethodInstance("unittests", "StatisticsTest", 0, "cStatistics")); - - try { - methods.sort(MethodInstance.SORT_BY_INDEX); - } catch (IllegalArgumentException ex) { - Assert.fail("Comparison method violates its general contract!"); - } - } - - private MethodInstance buildTestNgFactoryMethodInstance(String xmlTestName) { - TestClassStub testClass = new TestClassStub(new XmlTestStub(xmlTestName), null); - return new MethodInstance(new TestNGMethodStub(null, testClass)); - } - - private MethodInstance buildMethodInstance( - String xmlTestName, String xmlClassName, int xmlClassIndex, String methodName) { - TestClassStub testClass = - new TestClassStub( - new XmlTestStub(xmlTestName), new XmlClassStub(xmlClassName, xmlClassIndex)); - return new MethodInstance(new TestNGMethodStub(methodName, testClass)); - } - - public static class XmlClassStub extends XmlClass { - - private int index; - private String name; - - public XmlClassStub(String name, int index) { - this.name = name; - this.index = index; - } - - @Override - public String getName() { - return name; - } - - @Override - public int getIndex() { - return index; - } - - @Override - public List getIncludedMethods() { - return Collections.emptyList(); - } - } - - public static class XmlTestStub extends XmlTest { - - private String name; - - public XmlTestStub(String name) { - this.name = name; - } - - @Override - public String getName() { - return name; - } - } - - public static class TestClassStub implements ITestClass { - - private XmlTest xmlTest; - private XmlClass xmlClass; - - public TestClassStub(XmlTest xmlTest, XmlClass xmlClass) { - this.xmlTest = xmlTest; - this.xmlClass = xmlClass; - } - - @Override - public String getName() { - return null; - } - - @Override - public XmlTest getXmlTest() { - return xmlTest; - } - - @Override - public XmlClass getXmlClass() { - return xmlClass; - } - - @Override - public String getTestName() { - return null; - } - - @Override - public Class getRealClass() { - return xmlClass.getSupportClass(); - } - - @Override - public void addInstance(Object instance) {} - - @Override - public Object[] getInstances(boolean reuse) { - return null; - } - - @Override - public long[] getInstanceHashCodes() { - return null; - } - - @Override - public ITestNGMethod[] getTestMethods() { - return new ITestNGMethod[0]; - } - - @Override - public ITestNGMethod[] getBeforeTestMethods() { - return new ITestNGMethod[0]; - } - - @Override - public ITestNGMethod[] getAfterTestMethods() { - return new ITestNGMethod[0]; - } - - @Override - public ITestNGMethod[] getBeforeClassMethods() { - return new ITestNGMethod[0]; - } - - @Override - public ITestNGMethod[] getAfterClassMethods() { - return new ITestNGMethod[0]; - } - - @Override - public ITestNGMethod[] getBeforeSuiteMethods() { - return new ITestNGMethod[0]; - } - - @Override - public ITestNGMethod[] getAfterSuiteMethods() { - return new ITestNGMethod[0]; - } - - @Override - public ITestNGMethod[] getBeforeTestConfigurationMethods() { - return new ITestNGMethod[0]; - } - - @Override - public ITestNGMethod[] getAfterTestConfigurationMethods() { - return new ITestNGMethod[0]; - } - - @Override - public ITestNGMethod[] getBeforeGroupsMethods() { - return new ITestNGMethod[0]; - } - - @Override - public ITestNGMethod[] getAfterGroupsMethods() { - return new ITestNGMethod[0]; - } - } - - public static class TestNGMethodStub implements ITestNGMethod { - - private TestClassStub testClassStub; - private String methodName; - - public TestNGMethodStub(String methodName, TestClassStub testClassStub) { - this.methodName = methodName; - this.testClassStub = testClassStub; - } - - @Override - public ITestNGMethod clone() { - return (TestNGMethodStub) this; - } - - @Override - public Class getRealClass() { - return null; - } - - @Override - public ITestClass getTestClass() { - return testClassStub; - } - - @Override - public void setTestClass(ITestClass cls) {} - - @Override - public String getMethodName() { - return methodName; - } - - @Override - public Object getInstance() { - return null; - } - - @Override - public long[] getInstanceHashCodes() { - return null; - } - - @Override - public String[] getGroups() { - return null; - } - - @Override - public String[] getGroupsDependedUpon() { - return null; - } - - @Override - public String getMissingGroup() { - return null; - } - - @Override - public void setMissingGroup(String group) {} - - @Override - public String[] getBeforeGroups() { - return null; - } - - @Override - public String[] getAfterGroups() { - return null; - } - - @Override - public String[] getMethodsDependedUpon() { - return null; - } - - @Override - public void addMethodDependedUpon(String methodName) {} - - @Override - public boolean isTest() { - return false; - } - - @Override - public boolean isBeforeMethodConfiguration() { - return false; - } - - @Override - public boolean isAfterMethodConfiguration() { - return false; - } - - @Override - public boolean isBeforeClassConfiguration() { - return false; - } - - @Override - public boolean isAfterClassConfiguration() { - return false; - } - - @Override - public boolean isBeforeSuiteConfiguration() { - return false; - } - - @Override - public boolean isAfterSuiteConfiguration() { - return false; - } - - @Override - public boolean isBeforeTestConfiguration() { - return false; - } - - @Override - public boolean isAfterTestConfiguration() { - return false; - } - - @Override - public boolean isBeforeGroupsConfiguration() { - return false; - } - - @Override - public boolean isAfterGroupsConfiguration() { - return false; - } - - @Override - public long getTimeOut() { - return 0; - } - - @Override - public void setTimeOut(long timeOut) {} - - @Override - public int getInvocationCount() { - return 0; - } - - @Override - public void setInvocationCount(int count) {} - - @Override - public void setMoreInvocationChecker(Callable moreInvocationChecker) {} - - @Override - public boolean hasMoreInvocation() { - return false; - } - - @Override - public int getSuccessPercentage() { - return 0; - } - - @Override - public String getId() { - return null; - } - - @Override - public void setId(String id) {} - - @Override - public long getDate() { - return 0; - } - - @Override - public void setDate(long date) {} - - @Override - public boolean canRunFromClass(IClass testClass) { - return false; - } - - @Override - public boolean isAlwaysRun() { - return false; - } - - @Override - public int getThreadPoolSize() { - return 0; - } - - @Override - public void setThreadPoolSize(int threadPoolSize) {} - - @Override - public boolean getEnabled() { - return false; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public void setDescription(String description) {} - - @Override - public void incrementCurrentInvocationCount() {} - - @Override - public int getCurrentInvocationCount() { - return 0; - } - - @Override - public void setParameterInvocationCount(int n) {} - - @Override - public int getParameterInvocationCount() { - return 0; - } - - @Override - public IRetryAnalyzer getRetryAnalyzer(ITestResult result) { - return null; - } - - @Override - public Class getRetryAnalyzerClass() { - return null; - } - - @Override - public void setRetryAnalyzerClass(Class clazz) { } - - @Override - public boolean skipFailedInvocations() { - return false; - } - - @Override - public void setSkipFailedInvocations(boolean skip) {} - - @Override - public long getInvocationTimeOut() { - return 0; - } - - @Override - public boolean ignoreMissingDependencies() { - return false; - } - - @Override - public void setIgnoreMissingDependencies(boolean ignore) {} - - @Override - public List getInvocationNumbers() { - return null; - } - - @Override - public void setInvocationNumbers(List numbers) {} - - @Override - public void addFailedInvocationNumber(int number) {} - - @Override - public List getFailedInvocationNumbers() { - return null; - } - - @Override - public int getPriority() { - return 0; - } - - @Override - public void setPriority(int priority) {} - - @Override - public int getInterceptedPriority() { - return 0; - } - - @Override - public void setInterceptedPriority(int priority) {} - - @Override - public XmlTest getXmlTest() { - return null; - } - - @Override - public ConstructorOrMethod getConstructorOrMethod() { - return null; - } - - @Override - public Map findMethodParameters(XmlTest test) { - return null; - } - - @Override - public String getQualifiedName() { - return getRealClass().getName() + "." + getMethodName(); - } - } -} +package org.testng.internal; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.Callable; + +import org.testng.Assert; +import org.testng.IClass; +import org.testng.IRetryAnalyzer; +import org.testng.ITestClass; +import org.testng.ITestNGMethod; +import org.testng.ITestResult; +import org.testng.annotations.Test; +import org.testng.xml.XmlClass; +import org.testng.xml.XmlInclude; +import org.testng.xml.XmlTest; + +/** + * Unit tests for {@link MethodInstance}. + * + * @author Andreas Kluth + */ +public class MethodInstanceTest { + + public static void main(String[] args) { + new MethodInstanceTest().sortByIndexSatisfiesContract(); + } + + @Test + public void sortByIndexSatisfiesContract() { + + // Build a list of entries imposing the same behavior as the live suite, reduced to the + // minimum to create the same condition. + List methods = new ArrayList<>(); + methods.add(buildMethodInstance("unittests", "StateTest", 1, "aCategorization")); + methods.add(buildMethodInstance("unittests", "StateTest", 1, "bCategorization")); + methods.add(buildMethodInstance("unittests", "StateTest", 1, "cCategorization")); + methods.add(buildMethodInstance("unittests", "StateTest", 1, "dCategorization")); + methods.add(buildMethodInstance("unittests", "StateTest", 1, "eCategorization")); + methods.add(buildMethodInstance("unittests", "StateTest", 1, "fCategorization")); + methods.add(buildMethodInstance("unittests", "StatusTest", 2, "aStatus")); + methods.add(buildTestNgFactoryMethodInstance("unittests")); + methods.add(buildTestNgFactoryMethodInstance("unittests")); + methods.add(buildTestNgFactoryMethodInstance("unittests")); + methods.add(buildTestNgFactoryMethodInstance("unittests")); + methods.add(buildTestNgFactoryMethodInstance("unittests")); + methods.add(buildTestNgFactoryMethodInstance("unittests")); + methods.add(buildTestNgFactoryMethodInstance("unittests")); + methods.add(buildTestNgFactoryMethodInstance("unittests")); + methods.add(buildTestNgFactoryMethodInstance("unittests")); + methods.add(buildTestNgFactoryMethodInstance("unittests")); + methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "aChangeTest")); + methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "bChangeTest")); + methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "cChangeTest")); + methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "dChangeTest")); + methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "eChangeTest")); + methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "fChangeTest")); + methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "gChangeTest")); + methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "eChangeTest")); + methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "hChangeTest")); + methods.add(buildMethodInstance("unittests", "ChangeTest", 3, "iChangeTest")); + methods.add(buildMethodInstance("unittests", "IdentifierClassTest", 4, "aIdentifier")); + methods.add(buildMethodInstance("unittests", "IdentifierClassTest", 4, "bIdentifier")); + methods.add(buildMethodInstance("unittests", "StatisticsTest", 0, "aStatistics")); + methods.add(buildMethodInstance("unittests", "StatisticsTest", 0, "bStatistics")); + methods.add(buildMethodInstance("unittests", "StatisticsTest", 0, "cStatistics")); + + try { + methods.sort(MethodInstance.SORT_BY_INDEX); + } catch (IllegalArgumentException ex) { + Assert.fail("Comparison method violates its general contract!"); + } + } + + private MethodInstance buildTestNgFactoryMethodInstance(String xmlTestName) { + TestClassStub testClass = new TestClassStub(new XmlTestStub(xmlTestName), null); + return new MethodInstance(new TestNGMethodStub(null, testClass)); + } + + private MethodInstance buildMethodInstance( + String xmlTestName, String xmlClassName, int xmlClassIndex, String methodName) { + TestClassStub testClass = + new TestClassStub( + new XmlTestStub(xmlTestName), new XmlClassStub(xmlClassName, xmlClassIndex)); + return new MethodInstance(new TestNGMethodStub(methodName, testClass)); + } + + public static class XmlClassStub extends XmlClass { + + private int index; + private String name; + + public XmlClassStub(String name, int index) { + this.name = name; + this.index = index; + } + + @Override + public String getName() { + return name; + } + + @Override + public int getIndex() { + return index; + } + + @Override + public List getIncludedMethods() { + return Collections.emptyList(); + } + } + + public static class XmlTestStub extends XmlTest { + + private String name; + + public XmlTestStub(String name) { + this.name = name; + } + + @Override + public String getName() { + return name; + } + } + + public static class TestClassStub implements ITestClass { + + private XmlTest xmlTest; + private XmlClass xmlClass; + + public TestClassStub(XmlTest xmlTest, XmlClass xmlClass) { + this.xmlTest = xmlTest; + this.xmlClass = xmlClass; + } + + @Override + public String getName() { + return null; + } + + @Override + public XmlTest getXmlTest() { + return xmlTest; + } + + @Override + public XmlClass getXmlClass() { + return xmlClass; + } + + @Override + public String getTestName() { + return null; + } + + @Override + public Class getRealClass() { + return xmlClass.getSupportClass(); + } + + @Override + public void addInstance(Object instance) {} + + @Override + public Object[] getInstances(boolean reuse) { + return null; + } + + @Override + public long[] getInstanceHashCodes() { + return null; + } + + @Override + public ITestNGMethod[] getTestMethods() { + return new ITestNGMethod[0]; + } + + @Override + public ITestNGMethod[] getBeforeTestMethods() { + return new ITestNGMethod[0]; + } + + @Override + public ITestNGMethod[] getAfterTestMethods() { + return new ITestNGMethod[0]; + } + + @Override + public ITestNGMethod[] getBeforeClassMethods() { + return new ITestNGMethod[0]; + } + + @Override + public ITestNGMethod[] getAfterClassMethods() { + return new ITestNGMethod[0]; + } + + @Override + public ITestNGMethod[] getBeforeSuiteMethods() { + return new ITestNGMethod[0]; + } + + @Override + public ITestNGMethod[] getAfterSuiteMethods() { + return new ITestNGMethod[0]; + } + + @Override + public ITestNGMethod[] getBeforeTestConfigurationMethods() { + return new ITestNGMethod[0]; + } + + @Override + public ITestNGMethod[] getAfterTestConfigurationMethods() { + return new ITestNGMethod[0]; + } + + @Override + public ITestNGMethod[] getBeforeGroupsMethods() { + return new ITestNGMethod[0]; + } + + @Override + public ITestNGMethod[] getAfterGroupsMethods() { + return new ITestNGMethod[0]; + } + } + + public static class TestNGMethodStub implements ITestNGMethod { + + private TestClassStub testClassStub; + private String methodName; + + public TestNGMethodStub(String methodName, TestClassStub testClassStub) { + this.methodName = methodName; + this.testClassStub = testClassStub; + } + + @Override + public ITestNGMethod clone() { + return (TestNGMethodStub) this; + } + + @Override + public Class getRealClass() { + return null; + } + + @Override + public ITestClass getTestClass() { + return testClassStub; + } + + @Override + public void setTestClass(ITestClass cls) {} + + @Override + public String getMethodName() { + return methodName; + } + + @Override + public Object getInstance() { + return null; + } + + @Override + public long[] getInstanceHashCodes() { + return null; + } + + @Override + public String[] getGroups() { + return null; + } + + @Override + public String[] getGroupsDependedUpon() { + return null; + } + + @Override + public String getMissingGroup() { + return null; + } + + @Override + public void setMissingGroup(String group) {} + + @Override + public String[] getBeforeGroups() { + return null; + } + + @Override + public String[] getAfterGroups() { + return null; + } + + @Override + public String[] getMethodsDependedUpon() { + return null; + } + + @Override + public void addMethodDependedUpon(String methodName) {} + + @Override + public boolean isTest() { + return false; + } + + @Override + public boolean isBeforeMethodConfiguration() { + return false; + } + + @Override + public boolean isAfterMethodConfiguration() { + return false; + } + + @Override + public boolean isBeforeClassConfiguration() { + return false; + } + + @Override + public boolean isAfterClassConfiguration() { + return false; + } + + @Override + public boolean isBeforeSuiteConfiguration() { + return false; + } + + @Override + public boolean isAfterSuiteConfiguration() { + return false; + } + + @Override + public boolean isBeforeTestConfiguration() { + return false; + } + + @Override + public boolean isAfterTestConfiguration() { + return false; + } + + @Override + public boolean isBeforeGroupsConfiguration() { + return false; + } + + @Override + public boolean isAfterGroupsConfiguration() { + return false; + } + + @Override + public long getTimeOut() { + return 0; + } + + @Override + public void setTimeOut(long timeOut) {} + + @Override + public int getInvocationCount() { + return 0; + } + + @Override + public void setInvocationCount(int count) {} + + @Override + public void setMoreInvocationChecker(Callable moreInvocationChecker) {} + + @Override + public boolean hasMoreInvocation() { + return false; + } + + @Override + public int getSuccessPercentage() { + return 0; + } + + @Override + public String getId() { + return null; + } + + @Override + public void setId(String id) {} + + @Override + public long getDate() { + return 0; + } + + @Override + public void setDate(long date) {} + + @Override + public boolean canRunFromClass(IClass testClass) { + return false; + } + + @Override + public boolean isAlwaysRun() { + return false; + } + + @Override + public int getThreadPoolSize() { + return 0; + } + + @Override + public void setThreadPoolSize(int threadPoolSize) {} + + @Override + public boolean getEnabled() { + return false; + } + + @Override + public String getDescription() { + return null; + } + + @Override + public void setDescription(String description) {} + + @Override + public void incrementCurrentInvocationCount() {} + + @Override + public int getCurrentInvocationCount() { + return 0; + } + + @Override + public void setParameterInvocationCount(int n) {} + + @Override + public int getParameterInvocationCount() { + return 0; + } + + @Override + public IRetryAnalyzer getRetryAnalyzer(ITestResult result) { + return null; + } + + @Override + public Class getRetryAnalyzerClass() { + return null; + } + + @Override + public void setRetryAnalyzerClass(Class clazz) { } + + @Override + public boolean skipFailedInvocations() { + return false; + } + + @Override + public void setSkipFailedInvocations(boolean skip) {} + + @Override + public long getInvocationTimeOut() { + return 0; + } + + @Override + public boolean ignoreMissingDependencies() { + return false; + } + + @Override + public void setIgnoreMissingDependencies(boolean ignore) {} + + @Override + public List getInvocationNumbers() { + return null; + } + + @Override + public void setInvocationNumbers(List numbers) {} + + @Override + public void addFailedInvocationNumber(int number) {} + + @Override + public List getFailedInvocationNumbers() { + return null; + } + + @Override + public int getPriority() { + return 0; + } + + @Override + public void setPriority(int priority) {} + + @Override + public int getInterceptedPriority() { + return 0; + } + + @Override + public void setInterceptedPriority(int priority) {} + + @Override + public XmlTest getXmlTest() { + return null; + } + + @Override + public ConstructorOrMethod getConstructorOrMethod() { + return null; + } + + @Override + public Map findMethodParameters(XmlTest test) { + return null; + } + + @Override + public String getQualifiedName() { + return getRealClass().getName() + "." + getMethodName(); + } + } +} diff --git a/testng-core/src/test/java/org/testng/internal/conflistener/FailingAfterClass.java b/testng-core/src/test/java/org/testng/internal/conflistener/FailingAfterClass.java index 00a633ac2d..362a205ec6 100644 --- a/testng-core/src/test/java/org/testng/internal/conflistener/FailingAfterClass.java +++ b/testng-core/src/test/java/org/testng/internal/conflistener/FailingAfterClass.java @@ -1,14 +1,14 @@ -package org.testng.internal.conflistener; - -import org.testng.annotations.AfterClass; -import org.testng.annotations.Test; - -public class FailingAfterClass { - @AfterClass - public void failingAfterClass() { - throw new RuntimeException("expected @AfterClass failure"); - } - - @Test - public void testBeforeFailingAfterClass() {} -} +package org.testng.internal.conflistener; + +import org.testng.annotations.AfterClass; +import org.testng.annotations.Test; + +public class FailingAfterClass { + @AfterClass + public void failingAfterClass() { + throw new RuntimeException("expected @AfterClass failure"); + } + + @Test + public void testBeforeFailingAfterClass() {} +} diff --git a/testng-core/src/test/java/org/testng/internal/conflistener/FailingAfterSuite.java b/testng-core/src/test/java/org/testng/internal/conflistener/FailingAfterSuite.java index bbc180ac29..2569bbe338 100644 --- a/testng-core/src/test/java/org/testng/internal/conflistener/FailingAfterSuite.java +++ b/testng-core/src/test/java/org/testng/internal/conflistener/FailingAfterSuite.java @@ -1,14 +1,14 @@ -package org.testng.internal.conflistener; - -import org.testng.annotations.AfterSuite; -import org.testng.annotations.Test; - -public class FailingAfterSuite { - @AfterSuite(alwaysRun = true) - public void afterSuite() { - throw new RuntimeException("Test exception"); - } - - @Test - public void dummytest() {} -} +package org.testng.internal.conflistener; + +import org.testng.annotations.AfterSuite; +import org.testng.annotations.Test; + +public class FailingAfterSuite { + @AfterSuite(alwaysRun = true) + public void afterSuite() { + throw new RuntimeException("Test exception"); + } + + @Test + public void dummytest() {} +} diff --git a/testng-core/src/test/java/org/testng/internal/conflistener/FailingAfterTest.java b/testng-core/src/test/java/org/testng/internal/conflistener/FailingAfterTest.java index bd59812eb6..8f3f510654 100644 --- a/testng-core/src/test/java/org/testng/internal/conflistener/FailingAfterTest.java +++ b/testng-core/src/test/java/org/testng/internal/conflistener/FailingAfterTest.java @@ -1,17 +1,17 @@ -package org.testng.internal.conflistener; - -import org.testng.annotations.AfterTest; -import org.testng.annotations.Test; - -public class FailingAfterTest { - @AfterTest(alwaysRun = true) - public void afterTest() { - throw new RuntimeException("Test exception"); - } - - @AfterTest - public void skippedAfterTest() {} - - @Test - public void dummytest() {} -} +package org.testng.internal.conflistener; + +import org.testng.annotations.AfterTest; +import org.testng.annotations.Test; + +public class FailingAfterTest { + @AfterTest(alwaysRun = true) + public void afterTest() { + throw new RuntimeException("Test exception"); + } + + @AfterTest + public void skippedAfterTest() {} + + @Test + public void dummytest() {} +} diff --git a/testng-core/src/test/java/org/testng/internal/conflistener/FailingBeforeClass.java b/testng-core/src/test/java/org/testng/internal/conflistener/FailingBeforeClass.java index e3476e8dd6..0a8f88cfb3 100644 --- a/testng-core/src/test/java/org/testng/internal/conflistener/FailingBeforeClass.java +++ b/testng-core/src/test/java/org/testng/internal/conflistener/FailingBeforeClass.java @@ -1,14 +1,14 @@ -package org.testng.internal.conflistener; - -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -public class FailingBeforeClass { - @BeforeClass - public void failingBeforeClass() { - throw new RuntimeException("expected @BeforeClass failure"); - } - - @Test - public void testAfterFailingBeforeClass() {} -} +package org.testng.internal.conflistener; + +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class FailingBeforeClass { + @BeforeClass + public void failingBeforeClass() { + throw new RuntimeException("expected @BeforeClass failure"); + } + + @Test + public void testAfterFailingBeforeClass() {} +} diff --git a/testng-core/src/test/java/org/testng/internal/conflistener/FailingBeforeSuite.java b/testng-core/src/test/java/org/testng/internal/conflistener/FailingBeforeSuite.java index f479a9301c..0c6dce6fcb 100644 --- a/testng-core/src/test/java/org/testng/internal/conflistener/FailingBeforeSuite.java +++ b/testng-core/src/test/java/org/testng/internal/conflistener/FailingBeforeSuite.java @@ -1,14 +1,14 @@ -package org.testng.internal.conflistener; - -import org.testng.annotations.BeforeSuite; -import org.testng.annotations.Test; - -public class FailingBeforeSuite { - @BeforeSuite - public void beforeSuite() { - throw new RuntimeException("Test exception"); - } - - @Test - public void dummytest() {} -} +package org.testng.internal.conflistener; + +import org.testng.annotations.BeforeSuite; +import org.testng.annotations.Test; + +public class FailingBeforeSuite { + @BeforeSuite + public void beforeSuite() { + throw new RuntimeException("Test exception"); + } + + @Test + public void dummytest() {} +} diff --git a/testng-core/src/test/java/org/testng/internal/conflistener/FailingBeforeTest.java b/testng-core/src/test/java/org/testng/internal/conflistener/FailingBeforeTest.java index 57d3e0875e..3989f2f587 100644 --- a/testng-core/src/test/java/org/testng/internal/conflistener/FailingBeforeTest.java +++ b/testng-core/src/test/java/org/testng/internal/conflistener/FailingBeforeTest.java @@ -1,18 +1,18 @@ -package org.testng.internal.conflistener; - -import org.testng.annotations.BeforeSuite; -import org.testng.annotations.BeforeTest; -import org.testng.annotations.Test; - -public class FailingBeforeTest { - @BeforeSuite - public void passingBeforeSuite() {} - - @BeforeTest - public void beforeTest() { - throw new RuntimeException("Test exception"); - } - - @Test - public void dummytest() {} -} +package org.testng.internal.conflistener; + +import org.testng.annotations.BeforeSuite; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +public class FailingBeforeTest { + @BeforeSuite + public void passingBeforeSuite() {} + + @BeforeTest + public void beforeTest() { + throw new RuntimeException("Test exception"); + } + + @Test + public void dummytest() {} +} diff --git a/testng-core/src/test/java/org/testng/internal/conflistener/testng.xml b/testng-core/src/test/java/org/testng/internal/conflistener/testng.xml index 1dbe3da0ca..4ac53e9d97 100644 --- a/testng-core/src/test/java/org/testng/internal/conflistener/testng.xml +++ b/testng-core/src/test/java/org/testng/internal/conflistener/testng.xml @@ -1,31 +1,31 @@ - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testng-core/src/test/java/test/BaseTest.java b/testng-core/src/test/java/test/BaseTest.java index 691f2e4755..ac78c61f27 100644 --- a/testng-core/src/test/java/test/BaseTest.java +++ b/testng-core/src/test/java/test/BaseTest.java @@ -1,424 +1,424 @@ -package test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.testng.Assert; -import org.testng.IClassListener; -import org.testng.IInvokedMethodListener; -import org.testng.ISuite; -import org.testng.ITestObjectFactory; -import org.testng.ITestResult; -import org.testng.ITestRunnerFactory; -import org.testng.SuiteRunner; -import org.testng.TestListenerAdapter; -import org.testng.TestRunner; -import org.testng.annotations.BeforeMethod; -import org.testng.collections.Lists; -import org.testng.collections.Maps; -import org.testng.internal.Configuration; -import org.testng.internal.IConfiguration; -import org.testng.internal.Systematiser; -import org.testng.reporters.JUnitXMLReporter; -import org.testng.reporters.TestHTMLReporter; -import org.testng.xml.XmlClass; -import org.testng.xml.XmlInclude; -import org.testng.xml.XmlMethodSelector; -import org.testng.xml.XmlPackage; -import org.testng.xml.XmlScript; -import org.testng.xml.XmlSuite; -import org.testng.xml.XmlTest; - -/** - * Base class for tests - * @since May 5, 2004 - */ -public class BaseTest extends BaseDistributedTest { - private static final String m_outputDirectory= "test-output-tests"; - - private XmlSuite m_suite= null; - private final ITestRunnerFactory m_testRunnerFactory; - private final IConfiguration m_configuration; - - private Integer m_verbose = null; - - public BaseTest() { - m_configuration = new Configuration(); - m_configuration.setObjectFactory(new ITestObjectFactory() {}); - m_testRunnerFactory = new InternalTestRunnerFactory(this); - } - - protected void setParallel(XmlSuite.ParallelMode parallel) { - getTest().setParallel(parallel); - } - - protected void setVerbose(int n) { - m_verbose = n; - } - - protected void setTestTimeOut(long n) { - getTest().setTimeOut(n); - } - - protected void setSuiteTimeOut(long n) { - m_suite.setTimeOut(Long.toString(n)); - } - - protected void setJUnit(boolean f) { - getTest().setJUnit(f); - } - - protected void setThreadCount(int count) { - getTest().getSuite().setThreadCount(count); - } - - private Map m_tests= new HashMap<>(); - private Map>> m_passedTests= new HashMap<>(); - private Map>> m_failedTests= new HashMap<>(); - private Map>> m_skippedTests= new HashMap<>(); - private Map>> m_passedConfigs= new HashMap<>(); - private Map>> m_failedConfigs= new HashMap<>(); - private Map>> m_skippedConfigs= new HashMap<>(); - private Map>> m_failedButWithinSuccessPercentageTests= new HashMap<>(); - - protected Map> getTests(Map>> map) { - return map.computeIfAbsent(getId(), k -> new HashMap<>()); - } - - protected XmlTest getTest() { - return m_tests.get(getId()); - } - - protected void setTests(Map>> map, Map> m) { - map.put(getId(), m); - } - - public Map> getFailedTests() { - return getTests(m_failedTests); - } - - public Map> getFailedButWithinSuccessPercentageTests() { - return getTests(m_failedButWithinSuccessPercentageTests); - } - - public Map> getPassedTests() { - return getTests(m_passedTests); - } - - public Map> getSkippedTests() { - return getTests(m_skippedTests); - } - - public Map> getFailedConfigs() { - return getTests(m_failedConfigs); - } - - public Map> getPassedConfigs() { - return getTests(m_passedConfigs); - } - - public Map> getSkippedConfigs() { - return getTests(m_skippedConfigs); - } - - public void setSkippedTests(Map> m) { - setTests(m_skippedTests, m); - } - - public void setPassedTests(Map> m) { - setTests(m_passedTests, m); - } - - public void setFailedTests(Map> m) { - setTests(m_failedTests, m); - } - - public void setFailedButWithinSuccessPercentageTests(Map> m) { - setTests(m_failedButWithinSuccessPercentageTests, m); - } - - public void setSkippedConfigs(Map> m) { - setTests(m_skippedConfigs, m); - } - - public void setPassedConfigs(Map> m) { - setTests(m_passedConfigs, m); - } - - public void setFailedConfigs(Map> m) { - setTests(m_failedConfigs, m); - } - - - protected void run() { - assert null != getTest() : "Test wasn't set, maybe @Configuration methodSetUp() was never called"; - setPassedTests(Maps.newHashMap()); - setFailedTests(Maps.newHashMap()); - setSkippedTests(Maps.newHashMap()); - setPassedConfigs(Maps.newHashMap()); - setFailedConfigs(Maps.newHashMap()); - setSkippedConfigs(Maps.newHashMap()); - setFailedButWithinSuccessPercentageTests(Maps.newHashMap()); - - m_suite.setVerbose(m_verbose != null ? m_verbose : 0); - SuiteRunner suite = new SuiteRunner(m_configuration, - m_suite, m_outputDirectory, m_testRunnerFactory, Systematiser.getComparator()); - - suite.run(); - } - - protected void addMethodSelector(String className, int priority) { - XmlMethodSelector methodSelector= new XmlMethodSelector(); - methodSelector.setName(className); - methodSelector.setPriority(priority); - getTest().getMethodSelectors().add(methodSelector); - } - - protected void addClasses(Class... classes) { - for (Class clazz : classes) { - addClass(clazz); - } - } - - protected XmlClass addClass(Class cls) { - return addClass(cls.getName()); - } - - protected XmlClass addClass(String className) { - XmlClass result= new XmlClass(className); - getTest().getXmlClasses().add(result); - - return result; - } - - protected void setScript(String language, String expression) { - XmlScript script = new XmlScript(); - script.setExpression(expression); - script.setLanguage(language); - getTest().setScript(script); - } - - protected void addPackage(String pkgName, String[] included, String[] excluded) { - XmlPackage pkg= new XmlPackage(); - pkg.setName(pkgName); - pkg.getInclude().addAll(Arrays.asList(included)); - pkg.getExclude().addAll(Arrays.asList(excluded)); - getTest().getSuite().getXmlPackages().add(pkg); - } - - private XmlClass findClass(String className) { - for(XmlClass cl : getTest().getXmlClasses()) { - if(cl.getName().equals(className)) { - return cl; - } - } - - return addClass(className); - } - - public void addIncludedMethod(String className, String m) { - XmlClass xmlClass= findClass(className); - xmlClass.getIncludedMethods().add(new XmlInclude(m)); - getTest().getXmlClasses().add(xmlClass); - } - - public void addExcludedMethod(String className, String m) { - XmlClass xmlClass= findClass(className); - xmlClass.getExcludedMethods().add(m); - getTest().getXmlClasses().add(xmlClass); - } - - public void addIncludedGroup(String g) { - getTest().addIncludedGroup(g); - } - - public void addExcludedGroup(String g) { - getTest().addExcludedGroup(g); - } - - @BeforeMethod(groups= { "init", "initTest" }) - public void methodSetUp() { - m_suite= new XmlSuite(); - m_suite.setName("Internal_suite"); - XmlTest xmlTest= new XmlTest(m_suite); - xmlTest.setName("Internal_test_failures_are_expected"); - m_tests.put(getId(), xmlTest); - } - - private void addTest(Map> tests, ITestResult t) { - List l = tests - .computeIfAbsent(t.getMethod().getMethodName(), k -> new ArrayList<>()); - l.add(t); - } - - public void addPassedTest(ITestResult t) { - addTest(getPassedTests(), t); - } - - public void addFailedTest(ITestResult t) { - addTest(getFailedTests(), t); - } - - public void addFailedButWithinSuccessPercentageTest(ITestResult t) { - addTest(getFailedButWithinSuccessPercentageTests(), t); - } - - public void addSkippedTest(ITestResult t) { - addTest(getSkippedTests(), t); - } - - public void addPassedConfig(ITestResult t) { - addTest(getPassedConfigs(), t); - } - - public void addFailedConfig(ITestResult t) { - addTest(getFailedConfigs(), t); - } - - public void addSkippedConfig(ITestResult t) { - addTest(getSkippedConfigs(), t); - } - - protected Long getId() { - return 42L; - } - - public XmlSuite getSuite() { - return m_suite; - } - - public void setSuite(XmlSuite suite) { - m_suite = suite; - } - - /** - * Used for instanceCount testing, when we need to look inside the - * TestResult to count the various SUCCESS/FAIL/FAIL_BUT_OK - */ - protected void verifyResults(Map> tests, - int expected, - String message) { - if(tests.size() > 0) { - Set keys = tests.keySet(); - Object firstKey= keys.iterator().next(); - List passedResult= tests.get(firstKey); - int n= passedResult.size(); - assert n == expected : "Expected " + expected + " " + message + " but found " + n; - } - else { - assert expected == 0 : "Expected " + expected + " " + message + " but found " - + tests.size(); - } - } - - protected static void verifyInstanceNames(Map> actual, - String[] expected) - { - List actualNames = Lists.newArrayList(); - for (Map.Entry> es : actual.entrySet()) { - for (ITestResult tr : es.getValue()) { - Object instance = tr.getInstance(); - actualNames.add(es.getKey() + "#" + (instance != null ? instance.toString() : "")); - } - } - Assert.assertEqualsNoOrder(actualNames.toArray(), expected); - } - - protected void verifyPassedTests(String... expectedPassed) { - verifyTests("Passed", expectedPassed, getPassedTests()); - } - - protected void verifyFailedTests(String... expectedFailed) { - verifyTests("Failed", expectedFailed, getFailedTests()); - } - - protected void verifySkippedTests(String... expectedSkipped) { - verifyTests("Skipped", expectedSkipped, getSkippedTests()); - } - - private static class InternalTestRunnerFactory implements ITestRunnerFactory { - private final BaseTest m_baseTest; - - public InternalTestRunnerFactory(final BaseTest baseTest) { - m_baseTest = baseTest; - } - - @Override - public TestRunner newTestRunner(ISuite suite, XmlTest test, - Collection listeners, List classListeners) { - TestRunner testRunner= new TestRunner(m_baseTest.m_configuration, suite, test, false, - listeners, classListeners, Systematiser.getComparator()); - - testRunner.addListener(new TestHTMLReporter()); - testRunner.addListener(new JUnitXMLReporter()); - testRunner.addListener(new TestListener(m_baseTest)); - if (listeners != null) { - for (IInvokedMethodListener l : listeners) { - testRunner.addListener(l); - } - } - - return testRunner; - } - } - - protected void runTest(String cls, String[] passed, String[] failed, String[] skipped) { - addClass(cls); - run(); - verifyTests("Passed", passed, getPassedTests()); - verifyTests("Failed", failed, getFailedTests()); - verifyTests("Skipped", skipped, getSkippedTests()); - } - -} // BaseTest - -//////////////////////////// - -class TestListener extends TestListenerAdapter { - private static BaseTest m_test= null; - - public TestListener(BaseTest t1) { - m_test= t1; - } - - @Override - public void onTestSuccess(ITestResult tr) { - m_test.addPassedTest(tr); - } - - @Override - public void onTestFailure(ITestResult tr) { - m_test.addFailedTest(tr); - } - - @Override - public void onTestFailedButWithinSuccessPercentage(ITestResult result) { - m_test.addFailedButWithinSuccessPercentageTest(result); - } - - @Override - public void onTestSkipped(ITestResult tr) { - m_test.addSkippedTest(tr); - } - - @Override - public void onConfigurationSuccess(ITestResult tr) { - m_test.addPassedConfig(tr); - } - - @Override - public void onConfigurationFailure(ITestResult tr) { - m_test.addFailedConfig(tr); - } - - @Override - public void onConfigurationSkip(ITestResult tr) { - m_test.addSkippedConfig(tr); - } - -} // TestListener +package test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.testng.Assert; +import org.testng.IClassListener; +import org.testng.IInvokedMethodListener; +import org.testng.ISuite; +import org.testng.ITestObjectFactory; +import org.testng.ITestResult; +import org.testng.ITestRunnerFactory; +import org.testng.SuiteRunner; +import org.testng.TestListenerAdapter; +import org.testng.TestRunner; +import org.testng.annotations.BeforeMethod; +import org.testng.collections.Lists; +import org.testng.collections.Maps; +import org.testng.internal.Configuration; +import org.testng.internal.IConfiguration; +import org.testng.internal.Systematiser; +import org.testng.reporters.JUnitXMLReporter; +import org.testng.reporters.TestHTMLReporter; +import org.testng.xml.XmlClass; +import org.testng.xml.XmlInclude; +import org.testng.xml.XmlMethodSelector; +import org.testng.xml.XmlPackage; +import org.testng.xml.XmlScript; +import org.testng.xml.XmlSuite; +import org.testng.xml.XmlTest; + +/** + * Base class for tests + * @since May 5, 2004 + */ +public class BaseTest extends BaseDistributedTest { + private static final String m_outputDirectory= "test-output-tests"; + + private XmlSuite m_suite= null; + private final ITestRunnerFactory m_testRunnerFactory; + private final IConfiguration m_configuration; + + private Integer m_verbose = null; + + public BaseTest() { + m_configuration = new Configuration(); + m_configuration.setObjectFactory(new ITestObjectFactory() {}); + m_testRunnerFactory = new InternalTestRunnerFactory(this); + } + + protected void setParallel(XmlSuite.ParallelMode parallel) { + getTest().setParallel(parallel); + } + + protected void setVerbose(int n) { + m_verbose = n; + } + + protected void setTestTimeOut(long n) { + getTest().setTimeOut(n); + } + + protected void setSuiteTimeOut(long n) { + m_suite.setTimeOut(Long.toString(n)); + } + + protected void setJUnit(boolean f) { + getTest().setJUnit(f); + } + + protected void setThreadCount(int count) { + getTest().getSuite().setThreadCount(count); + } + + private Map m_tests= new HashMap<>(); + private Map>> m_passedTests= new HashMap<>(); + private Map>> m_failedTests= new HashMap<>(); + private Map>> m_skippedTests= new HashMap<>(); + private Map>> m_passedConfigs= new HashMap<>(); + private Map>> m_failedConfigs= new HashMap<>(); + private Map>> m_skippedConfigs= new HashMap<>(); + private Map>> m_failedButWithinSuccessPercentageTests= new HashMap<>(); + + protected Map> getTests(Map>> map) { + return map.computeIfAbsent(getId(), k -> new HashMap<>()); + } + + protected XmlTest getTest() { + return m_tests.get(getId()); + } + + protected void setTests(Map>> map, Map> m) { + map.put(getId(), m); + } + + public Map> getFailedTests() { + return getTests(m_failedTests); + } + + public Map> getFailedButWithinSuccessPercentageTests() { + return getTests(m_failedButWithinSuccessPercentageTests); + } + + public Map> getPassedTests() { + return getTests(m_passedTests); + } + + public Map> getSkippedTests() { + return getTests(m_skippedTests); + } + + public Map> getFailedConfigs() { + return getTests(m_failedConfigs); + } + + public Map> getPassedConfigs() { + return getTests(m_passedConfigs); + } + + public Map> getSkippedConfigs() { + return getTests(m_skippedConfigs); + } + + public void setSkippedTests(Map> m) { + setTests(m_skippedTests, m); + } + + public void setPassedTests(Map> m) { + setTests(m_passedTests, m); + } + + public void setFailedTests(Map> m) { + setTests(m_failedTests, m); + } + + public void setFailedButWithinSuccessPercentageTests(Map> m) { + setTests(m_failedButWithinSuccessPercentageTests, m); + } + + public void setSkippedConfigs(Map> m) { + setTests(m_skippedConfigs, m); + } + + public void setPassedConfigs(Map> m) { + setTests(m_passedConfigs, m); + } + + public void setFailedConfigs(Map> m) { + setTests(m_failedConfigs, m); + } + + + protected void run() { + assert null != getTest() : "Test wasn't set, maybe @Configuration methodSetUp() was never called"; + setPassedTests(Maps.newHashMap()); + setFailedTests(Maps.newHashMap()); + setSkippedTests(Maps.newHashMap()); + setPassedConfigs(Maps.newHashMap()); + setFailedConfigs(Maps.newHashMap()); + setSkippedConfigs(Maps.newHashMap()); + setFailedButWithinSuccessPercentageTests(Maps.newHashMap()); + + m_suite.setVerbose(m_verbose != null ? m_verbose : 0); + SuiteRunner suite = new SuiteRunner(m_configuration, + m_suite, m_outputDirectory, m_testRunnerFactory, Systematiser.getComparator()); + + suite.run(); + } + + protected void addMethodSelector(String className, int priority) { + XmlMethodSelector methodSelector= new XmlMethodSelector(); + methodSelector.setName(className); + methodSelector.setPriority(priority); + getTest().getMethodSelectors().add(methodSelector); + } + + protected void addClasses(Class... classes) { + for (Class clazz : classes) { + addClass(clazz); + } + } + + protected XmlClass addClass(Class cls) { + return addClass(cls.getName()); + } + + protected XmlClass addClass(String className) { + XmlClass result= new XmlClass(className); + getTest().getXmlClasses().add(result); + + return result; + } + + protected void setScript(String language, String expression) { + XmlScript script = new XmlScript(); + script.setExpression(expression); + script.setLanguage(language); + getTest().setScript(script); + } + + protected void addPackage(String pkgName, String[] included, String[] excluded) { + XmlPackage pkg= new XmlPackage(); + pkg.setName(pkgName); + pkg.getInclude().addAll(Arrays.asList(included)); + pkg.getExclude().addAll(Arrays.asList(excluded)); + getTest().getSuite().getXmlPackages().add(pkg); + } + + private XmlClass findClass(String className) { + for(XmlClass cl : getTest().getXmlClasses()) { + if(cl.getName().equals(className)) { + return cl; + } + } + + return addClass(className); + } + + public void addIncludedMethod(String className, String m) { + XmlClass xmlClass= findClass(className); + xmlClass.getIncludedMethods().add(new XmlInclude(m)); + getTest().getXmlClasses().add(xmlClass); + } + + public void addExcludedMethod(String className, String m) { + XmlClass xmlClass= findClass(className); + xmlClass.getExcludedMethods().add(m); + getTest().getXmlClasses().add(xmlClass); + } + + public void addIncludedGroup(String g) { + getTest().addIncludedGroup(g); + } + + public void addExcludedGroup(String g) { + getTest().addExcludedGroup(g); + } + + @BeforeMethod(groups= { "init", "initTest" }) + public void methodSetUp() { + m_suite= new XmlSuite(); + m_suite.setName("Internal_suite"); + XmlTest xmlTest= new XmlTest(m_suite); + xmlTest.setName("Internal_test_failures_are_expected"); + m_tests.put(getId(), xmlTest); + } + + private void addTest(Map> tests, ITestResult t) { + List l = tests + .computeIfAbsent(t.getMethod().getMethodName(), k -> new ArrayList<>()); + l.add(t); + } + + public void addPassedTest(ITestResult t) { + addTest(getPassedTests(), t); + } + + public void addFailedTest(ITestResult t) { + addTest(getFailedTests(), t); + } + + public void addFailedButWithinSuccessPercentageTest(ITestResult t) { + addTest(getFailedButWithinSuccessPercentageTests(), t); + } + + public void addSkippedTest(ITestResult t) { + addTest(getSkippedTests(), t); + } + + public void addPassedConfig(ITestResult t) { + addTest(getPassedConfigs(), t); + } + + public void addFailedConfig(ITestResult t) { + addTest(getFailedConfigs(), t); + } + + public void addSkippedConfig(ITestResult t) { + addTest(getSkippedConfigs(), t); + } + + protected Long getId() { + return 42L; + } + + public XmlSuite getSuite() { + return m_suite; + } + + public void setSuite(XmlSuite suite) { + m_suite = suite; + } + + /** + * Used for instanceCount testing, when we need to look inside the + * TestResult to count the various SUCCESS/FAIL/FAIL_BUT_OK + */ + protected void verifyResults(Map> tests, + int expected, + String message) { + if(tests.size() > 0) { + Set keys = tests.keySet(); + Object firstKey= keys.iterator().next(); + List passedResult= tests.get(firstKey); + int n= passedResult.size(); + assert n == expected : "Expected " + expected + " " + message + " but found " + n; + } + else { + assert expected == 0 : "Expected " + expected + " " + message + " but found " + + tests.size(); + } + } + + protected static void verifyInstanceNames(Map> actual, + String[] expected) + { + List actualNames = Lists.newArrayList(); + for (Map.Entry> es : actual.entrySet()) { + for (ITestResult tr : es.getValue()) { + Object instance = tr.getInstance(); + actualNames.add(es.getKey() + "#" + (instance != null ? instance.toString() : "")); + } + } + Assert.assertEqualsNoOrder(actualNames.toArray(), expected); + } + + protected void verifyPassedTests(String... expectedPassed) { + verifyTests("Passed", expectedPassed, getPassedTests()); + } + + protected void verifyFailedTests(String... expectedFailed) { + verifyTests("Failed", expectedFailed, getFailedTests()); + } + + protected void verifySkippedTests(String... expectedSkipped) { + verifyTests("Skipped", expectedSkipped, getSkippedTests()); + } + + private static class InternalTestRunnerFactory implements ITestRunnerFactory { + private final BaseTest m_baseTest; + + public InternalTestRunnerFactory(final BaseTest baseTest) { + m_baseTest = baseTest; + } + + @Override + public TestRunner newTestRunner(ISuite suite, XmlTest test, + Collection listeners, List classListeners) { + TestRunner testRunner= new TestRunner(m_baseTest.m_configuration, suite, test, false, + listeners, classListeners, Systematiser.getComparator()); + + testRunner.addListener(new TestHTMLReporter()); + testRunner.addListener(new JUnitXMLReporter()); + testRunner.addListener(new TestListener(m_baseTest)); + if (listeners != null) { + for (IInvokedMethodListener l : listeners) { + testRunner.addListener(l); + } + } + + return testRunner; + } + } + + protected void runTest(String cls, String[] passed, String[] failed, String[] skipped) { + addClass(cls); + run(); + verifyTests("Passed", passed, getPassedTests()); + verifyTests("Failed", failed, getFailedTests()); + verifyTests("Skipped", skipped, getSkippedTests()); + } + +} // BaseTest + +//////////////////////////// + +class TestListener extends TestListenerAdapter { + private static BaseTest m_test= null; + + public TestListener(BaseTest t1) { + m_test= t1; + } + + @Override + public void onTestSuccess(ITestResult tr) { + m_test.addPassedTest(tr); + } + + @Override + public void onTestFailure(ITestResult tr) { + m_test.addFailedTest(tr); + } + + @Override + public void onTestFailedButWithinSuccessPercentage(ITestResult result) { + m_test.addFailedButWithinSuccessPercentageTest(result); + } + + @Override + public void onTestSkipped(ITestResult tr) { + m_test.addSkippedTest(tr); + } + + @Override + public void onConfigurationSuccess(ITestResult tr) { + m_test.addPassedConfig(tr); + } + + @Override + public void onConfigurationFailure(ITestResult tr) { + m_test.addFailedConfig(tr); + } + + @Override + public void onConfigurationSkip(ITestResult tr) { + m_test.addSkippedConfig(tr); + } + +} // TestListener diff --git a/testng-core/src/test/java/test/ant/MultipleThreadTest.java b/testng-core/src/test/java/test/ant/MultipleThreadTest.java index d34a9f3e70..480a31a9da 100644 --- a/testng-core/src/test/java/test/ant/MultipleThreadTest.java +++ b/testng-core/src/test/java/test/ant/MultipleThreadTest.java @@ -1,132 +1,132 @@ -package test.ant; - -import org.testng.Assert; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; -/** - * Tests that more than one thread is used for running tests - * - * @author micheb10 2 Oct 2006 - */ -public class MultipleThreadTest { - public static Set _threads; - - @BeforeClass - public void prepareHashSet() { - _threads = Collections.synchronizedSet(new HashSet<>()); - } - - @Test - public void recordThread00() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread01() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread02() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread03() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread04() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread05() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread06() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread07() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread08() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread09() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread10() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread11() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread12() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread13() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread14() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread15() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread16() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread17() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread18() { - _threads.add(Thread.currentThread()); - } - - @Test - public void recordThread19() { - _threads.add(Thread.currentThread()); - } - - @AfterClass - public void confirmMultipleThreads() { - Assert.assertTrue( - _threads.size() > 1, - "More than one thread should have been used for running the tests - " - + _threads.size() - + " was used"); - } -} +package test.ant; + +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +/** + * Tests that more than one thread is used for running tests + * + * @author micheb10 2 Oct 2006 + */ +public class MultipleThreadTest { + public static Set _threads; + + @BeforeClass + public void prepareHashSet() { + _threads = Collections.synchronizedSet(new HashSet<>()); + } + + @Test + public void recordThread00() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread01() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread02() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread03() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread04() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread05() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread06() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread07() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread08() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread09() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread10() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread11() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread12() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread13() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread14() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread15() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread16() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread17() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread18() { + _threads.add(Thread.currentThread()); + } + + @Test + public void recordThread19() { + _threads.add(Thread.currentThread()); + } + + @AfterClass + public void confirmMultipleThreads() { + Assert.assertTrue( + _threads.size() > 1, + "More than one thread should have been used for running the tests - " + + _threads.size() + + " was used"); + } +} diff --git a/testng-core/src/test/java/test/configuration/Base.java b/testng-core/src/test/java/test/configuration/Base.java index 2247eaa3fe..4aa7096b76 100644 --- a/testng-core/src/test/java/test/configuration/Base.java +++ b/testng-core/src/test/java/test/configuration/Base.java @@ -1,21 +1,21 @@ -package test.configuration; - -import org.testng.annotations.BeforeGroups; -import org.testng.annotations.BeforeTest; -import org.testng.annotations.Test; - -@Test(groups = {"base"}) -public class Base { - static int m_count; - - @BeforeTest - public void init() { - m_count = 0; - } - - @BeforeGroups(groups = "foo") - public void beforeGroups() { - m_count++; - } - -} +package test.configuration; + +import org.testng.annotations.BeforeGroups; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +@Test(groups = {"base"}) +public class Base { + static int m_count; + + @BeforeTest + public void init() { + m_count = 0; + } + + @BeforeGroups(groups = "foo") + public void beforeGroups() { + m_count++; + } + +} diff --git a/testng-core/src/test/java/test/configuration/BaseGroupsASampleTest.java b/testng-core/src/test/java/test/configuration/BaseGroupsASampleTest.java index 89ed4d8cbb..cb1769a9c6 100644 --- a/testng-core/src/test/java/test/configuration/BaseGroupsASampleTest.java +++ b/testng-core/src/test/java/test/configuration/BaseGroupsASampleTest.java @@ -1,10 +1,10 @@ -package test.configuration; - -import org.testng.annotations.Test; - -public class BaseGroupsASampleTest extends Base { - @Test(groups = "foo") - public void a() { - // System.out.println( "a" ); - } -} +package test.configuration; + +import org.testng.annotations.Test; + +public class BaseGroupsASampleTest extends Base { + @Test(groups = "foo") + public void a() { + // System.out.println( "a" ); + } +} diff --git a/testng-core/src/test/java/test/configuration/BaseGroupsBSampleTest.java b/testng-core/src/test/java/test/configuration/BaseGroupsBSampleTest.java index 45f0c628dd..1bef66a0b2 100644 --- a/testng-core/src/test/java/test/configuration/BaseGroupsBSampleTest.java +++ b/testng-core/src/test/java/test/configuration/BaseGroupsBSampleTest.java @@ -1,8 +1,8 @@ -package test.configuration; - -import org.testng.annotations.Test; - -public class BaseGroupsBSampleTest extends Base { - @Test(groups = "foo") - public void b() {} -} +package test.configuration; + +import org.testng.annotations.Test; + +public class BaseGroupsBSampleTest extends Base { + @Test(groups = "foo") + public void b() {} +} diff --git a/testng-core/src/test/java/test/configuration/BaseGroupsTest.java b/testng-core/src/test/java/test/configuration/BaseGroupsTest.java index 27ab75763b..fc189ddb7b 100644 --- a/testng-core/src/test/java/test/configuration/BaseGroupsTest.java +++ b/testng-core/src/test/java/test/configuration/BaseGroupsTest.java @@ -1,56 +1,56 @@ -package test.configuration; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.testng.TestNG; -import org.testng.annotations.Test; -import org.testng.xml.XmlSuite; -import org.testng.xml.XmlTest; -import test.InvokedMethodNameListener; -import test.SimpleBaseTest; -import test.configuration.github1338.FirstGitHub1338Sample; -import test.configuration.github1338.SecondGitHub1338Sample; -import test.configuration.github1338.ThirdGitHub1338Sample; - -public class BaseGroupsTest extends SimpleBaseTest { - - @Test( - description = - "Verify that a base class with a BeforeGroups method only gets invoked once, " - + "no matter how many subclasses it has") - public void verifySingleInvocation() { - TestNG tng = create(BaseGroupsASampleTest.class, BaseGroupsBSampleTest.class); - tng.setGroups("foo"); - - InvokedMethodNameListener listener = new InvokedMethodNameListener(); - tng.addListener(listener); - - tng.run(); - - assertThat(listener.getSkippedMethodNames()).isEmpty(); - assertThat(listener.getFailedMethodNames()).isEmpty(); - assertThat(listener.getSucceedMethodNames()).containsOnlyOnce("beforeGroups"); - } - - @Test(description = "https://github.com/cbeust/testng/issues/1338") - public void verifyBeforeGroupUseAppropriateInstance() { - XmlSuite suite = createXmlSuite("Suite"); - XmlTest test = - createXmlTest( - suite, - "Test", - SecondGitHub1338Sample.class, - FirstGitHub1338Sample.class, - ThirdGitHub1338Sample.class); - createXmlGroups(test, "group1"); - TestNG tng = create(suite); - - InvokedMethodNameListener listener = new InvokedMethodNameListener(); - tng.addListener(listener); - - tng.run(); - - assertThat(listener.getSkippedMethodNames()).isEmpty(); - assertThat(listener.getFailedMethodNames()).isEmpty(); - } -} +package test.configuration; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.testng.TestNG; +import org.testng.annotations.Test; +import org.testng.xml.XmlSuite; +import org.testng.xml.XmlTest; +import test.InvokedMethodNameListener; +import test.SimpleBaseTest; +import test.configuration.github1338.FirstGitHub1338Sample; +import test.configuration.github1338.SecondGitHub1338Sample; +import test.configuration.github1338.ThirdGitHub1338Sample; + +public class BaseGroupsTest extends SimpleBaseTest { + + @Test( + description = + "Verify that a base class with a BeforeGroups method only gets invoked once, " + + "no matter how many subclasses it has") + public void verifySingleInvocation() { + TestNG tng = create(BaseGroupsASampleTest.class, BaseGroupsBSampleTest.class); + tng.setGroups("foo"); + + InvokedMethodNameListener listener = new InvokedMethodNameListener(); + tng.addListener(listener); + + tng.run(); + + assertThat(listener.getSkippedMethodNames()).isEmpty(); + assertThat(listener.getFailedMethodNames()).isEmpty(); + assertThat(listener.getSucceedMethodNames()).containsOnlyOnce("beforeGroups"); + } + + @Test(description = "https://github.com/cbeust/testng/issues/1338") + public void verifyBeforeGroupUseAppropriateInstance() { + XmlSuite suite = createXmlSuite("Suite"); + XmlTest test = + createXmlTest( + suite, + "Test", + SecondGitHub1338Sample.class, + FirstGitHub1338Sample.class, + ThirdGitHub1338Sample.class); + createXmlGroups(test, "group1"); + TestNG tng = create(suite); + + InvokedMethodNameListener listener = new InvokedMethodNameListener(); + tng.addListener(listener); + + tng.run(); + + assertThat(listener.getSkippedMethodNames()).isEmpty(); + assertThat(listener.getFailedMethodNames()).isEmpty(); + } +} diff --git a/testng-core/src/test/java/test/configuration/BaseSuiteTest.java b/testng-core/src/test/java/test/configuration/BaseSuiteTest.java index dc758574cc..1b872a191e 100644 --- a/testng-core/src/test/java/test/configuration/BaseSuiteTest.java +++ b/testng-core/src/test/java/test/configuration/BaseSuiteTest.java @@ -1,16 +1,16 @@ -package test.configuration; - -import org.testng.annotations.BeforeSuite; - -import java.util.ArrayList; -import java.util.List; - -public class BaseSuiteTest { - public static List m_order; - - @BeforeSuite - public void beforeSuiteParent() { - m_order = new ArrayList<>(); - m_order.add(1); - } -} +package test.configuration; + +import org.testng.annotations.BeforeSuite; + +import java.util.ArrayList; +import java.util.List; + +public class BaseSuiteTest { + public static List m_order; + + @BeforeSuite + public void beforeSuiteParent() { + m_order = new ArrayList<>(); + m_order.add(1); + } +} diff --git a/testng-core/src/test/java/test/configuration/ConfigurationGroupBothSampleTest.java b/testng-core/src/test/java/test/configuration/ConfigurationGroupBothSampleTest.java index e356b22c75..c9c597f12f 100644 --- a/testng-core/src/test/java/test/configuration/ConfigurationGroupBothSampleTest.java +++ b/testng-core/src/test/java/test/configuration/ConfigurationGroupBothSampleTest.java @@ -1,55 +1,55 @@ -package test.configuration; - -import org.testng.annotations.AfterGroups; -import org.testng.annotations.BeforeGroups; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class ConfigurationGroupBothSampleTest { - static List m_list = Collections.synchronizedList(new ArrayList<>()); - - private static synchronized void addToList(Integer n) { - m_list.add(n); - } - - @BeforeGroups( - groups = {"twice"}, - value = {"twice"}) - public void a() { - ppp("BEFORE()"); - addToList(1); - } - - @Test( - groups = {"twice"}, - dataProvider = "MyData", - invocationCount = 2, - threadPoolSize = 2) - public void b(int a, int b) { - addToList(2); - ppp("B()" + a + "," + b); - } - - @AfterGroups( - groups = {"twice"}, - value = {"twice"}) - public void c() { - addToList(3); - ppp("AFTER()"); - } - - @DataProvider(name = "MyData") - public Object[][] input() { - return new Object[][] {{1, 1}, {2, 2}, {3, 3}}; - } - - private void ppp(String string) { - if (false) { - System.out.println("[A] " + string + " on Thread:" + Thread.currentThread()); - } - } -} +package test.configuration; + +import org.testng.annotations.AfterGroups; +import org.testng.annotations.BeforeGroups; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class ConfigurationGroupBothSampleTest { + static List m_list = Collections.synchronizedList(new ArrayList<>()); + + private static synchronized void addToList(Integer n) { + m_list.add(n); + } + + @BeforeGroups( + groups = {"twice"}, + value = {"twice"}) + public void a() { + ppp("BEFORE()"); + addToList(1); + } + + @Test( + groups = {"twice"}, + dataProvider = "MyData", + invocationCount = 2, + threadPoolSize = 2) + public void b(int a, int b) { + addToList(2); + ppp("B()" + a + "," + b); + } + + @AfterGroups( + groups = {"twice"}, + value = {"twice"}) + public void c() { + addToList(3); + ppp("AFTER()"); + } + + @DataProvider(name = "MyData") + public Object[][] input() { + return new Object[][] {{1, 1}, {2, 2}, {3, 3}}; + } + + private void ppp(String string) { + if (false) { + System.out.println("[A] " + string + " on Thread:" + Thread.currentThread()); + } + } +} diff --git a/testng-core/src/test/java/test/configuration/MultipleBeforeGroupTest.java b/testng-core/src/test/java/test/configuration/MultipleBeforeGroupTest.java index 4b966d627e..21e273a2f1 100644 --- a/testng-core/src/test/java/test/configuration/MultipleBeforeGroupTest.java +++ b/testng-core/src/test/java/test/configuration/MultipleBeforeGroupTest.java @@ -1,23 +1,23 @@ -package test.configuration; - -import org.testng.Assert; -import org.testng.annotations.BeforeGroups; -import org.testng.annotations.Test; - -@Test(groups = "foo") -public class MultipleBeforeGroupTest { - private int m_count = 0; - - @BeforeGroups("foo") - public void beforeGroups() { - m_count++; - } - - @Test() - public void test() {} - - @Test(dependsOnMethods = "test") - public void verify() { - Assert.assertEquals(1, m_count); - } -} +package test.configuration; + +import org.testng.Assert; +import org.testng.annotations.BeforeGroups; +import org.testng.annotations.Test; + +@Test(groups = "foo") +public class MultipleBeforeGroupTest { + private int m_count = 0; + + @BeforeGroups("foo") + public void beforeGroups() { + m_count++; + } + + @Test() + public void test() {} + + @Test(dependsOnMethods = "test") + public void verify() { + Assert.assertEquals(1, m_count); + } +} diff --git a/testng-core/src/test/java/test/configuration/ReflectMethodParametrizedConfigurationMethodTest.java b/testng-core/src/test/java/test/configuration/ReflectMethodParametrizedConfigurationMethodTest.java index beba9365b8..a5a67931b1 100644 --- a/testng-core/src/test/java/test/configuration/ReflectMethodParametrizedConfigurationMethodTest.java +++ b/testng-core/src/test/java/test/configuration/ReflectMethodParametrizedConfigurationMethodTest.java @@ -1,44 +1,44 @@ -package test.configuration; - -import org.testng.Assert; -import org.testng.annotations.AfterClass; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Map; - - -/** - * This class/interface - */ -public class ReflectMethodParametrizedConfigurationMethodTest { - private Map m_before= new HashMap<>(); - private Map m_after= new HashMap<>(); - - @BeforeMethod - public void beforeMethod(Method tobeInvokedTestMethod) { - m_before.put(tobeInvokedTestMethod.getName(), tobeInvokedTestMethod.getName()); - } - - @Test - public void test1() {} - - @Test - public void test2() {} - - @AfterMethod - public void afterMethod(Method invokedTestMethod) { - m_after.put(invokedTestMethod.getName(), invokedTestMethod.getName()); - } - - @AfterClass - public void assertBeforeAfterMethodsInvocations() { - Assert.assertTrue(m_before.containsKey("test1"), "@Test method should have been passed to @BeforeMethod"); - Assert.assertTrue(m_before.containsKey("test2"), "@Test method should have been passed to @BeforeMethod"); - Assert.assertTrue(m_after.containsKey("test1"), "@Test method should have been passed to @AfterMethod"); - Assert.assertTrue(m_before.containsKey("test2"), "@Test method should have been passed to @AfterMethod"); - } -} +package test.configuration; + +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + +/** + * This class/interface + */ +public class ReflectMethodParametrizedConfigurationMethodTest { + private Map m_before= new HashMap<>(); + private Map m_after= new HashMap<>(); + + @BeforeMethod + public void beforeMethod(Method tobeInvokedTestMethod) { + m_before.put(tobeInvokedTestMethod.getName(), tobeInvokedTestMethod.getName()); + } + + @Test + public void test1() {} + + @Test + public void test2() {} + + @AfterMethod + public void afterMethod(Method invokedTestMethod) { + m_after.put(invokedTestMethod.getName(), invokedTestMethod.getName()); + } + + @AfterClass + public void assertBeforeAfterMethodsInvocations() { + Assert.assertTrue(m_before.containsKey("test1"), "@Test method should have been passed to @BeforeMethod"); + Assert.assertTrue(m_before.containsKey("test2"), "@Test method should have been passed to @BeforeMethod"); + Assert.assertTrue(m_after.containsKey("test1"), "@Test method should have been passed to @AfterMethod"); + Assert.assertTrue(m_before.containsKey("test2"), "@Test method should have been passed to @AfterMethod"); + } +} diff --git a/testng-core/src/test/java/test/configuration/SuiteTest.java b/testng-core/src/test/java/test/configuration/SuiteTest.java index 8781ade72b..4b4f85f07b 100644 --- a/testng-core/src/test/java/test/configuration/SuiteTest.java +++ b/testng-core/src/test/java/test/configuration/SuiteTest.java @@ -1,22 +1,22 @@ -package test.configuration; - -import org.testng.annotations.BeforeSuite; -import org.testng.annotations.Test; - -public class SuiteTest extends BaseSuiteTest { - @BeforeSuite(dependsOnMethods = {"beforeSuiteParent"}) - public void beforeSuiteChild() { - m_order.add(2); - } - - // @AfterSuite(dependsOnMethods={"afterSuiteParent"}) - // public void afterSuiteChild(){ - // m_order.add(4); - // System.out.println("AFTER SUITE CHILD"); - // } - - @Test - public void test1() { - m_order.add(3); - } -} +package test.configuration; + +import org.testng.annotations.BeforeSuite; +import org.testng.annotations.Test; + +public class SuiteTest extends BaseSuiteTest { + @BeforeSuite(dependsOnMethods = {"beforeSuiteParent"}) + public void beforeSuiteChild() { + m_order.add(2); + } + + // @AfterSuite(dependsOnMethods={"afterSuiteParent"}) + // public void afterSuiteChild(){ + // m_order.add(4); + // System.out.println("AFTER SUITE CHILD"); + // } + + @Test + public void test1() { + m_order.add(3); + } +} diff --git a/testng-core/src/test/java/test/configuration/VerifySuiteTest.java b/testng-core/src/test/java/test/configuration/VerifySuiteTest.java index fb873cc8eb..dea35187d2 100644 --- a/testng-core/src/test/java/test/configuration/VerifySuiteTest.java +++ b/testng-core/src/test/java/test/configuration/VerifySuiteTest.java @@ -1,14 +1,14 @@ -package test.configuration; - -import org.testng.Assert; -import org.testng.annotations.AfterSuite; - -import java.util.Arrays; - -public class VerifySuiteTest { - - @AfterSuite - public void verify() { - Assert.assertEquals(Arrays.asList(1, 2, 3), SuiteTest.m_order); - } -} +package test.configuration; + +import org.testng.Assert; +import org.testng.annotations.AfterSuite; + +import java.util.Arrays; + +public class VerifySuiteTest { + + @AfterSuite + public void verify() { + Assert.assertEquals(Arrays.asList(1, 2, 3), SuiteTest.m_order); + } +} diff --git a/testng-core/src/test/java/test/dataprovider/FailingDataProviderTest.java b/testng-core/src/test/java/test/dataprovider/FailingDataProviderTest.java index 76339f54cd..8b6c693204 100644 --- a/testng-core/src/test/java/test/dataprovider/FailingDataProviderTest.java +++ b/testng-core/src/test/java/test/dataprovider/FailingDataProviderTest.java @@ -1,48 +1,48 @@ -package test.dataprovider; - -import org.testng.DataProviderInvocationException; -import org.testng.ITestResult; -import org.testng.annotations.Test; -import test.InvokedMethodNameListener; -import test.SimpleBaseTest; -import test.dataprovider.issue2157.TestClassWithDataProviderThatThrowsExceptions; - -import static org.assertj.core.api.Assertions.assertThat; - -public class FailingDataProviderTest extends SimpleBaseTest { - - @Test(description = "TESTNG-142: Exceptions in DataProvider are not reported as failed test") - public void failingDataProvider() { - InvokedMethodNameListener listener = run(FailingDataProviderSample.class); - - assertThat(listener.getSkippedMethodNames()).containsExactly("dpThrowingException"); - } - - @Test(description = "TESTNG-447: Abort when two data providers have the same name") - public void duplicateDataProviders() { - InvokedMethodNameListener listener = run(DuplicateDataProviderSample.class); - - assertThat(listener.getFailedBeforeInvocationMethodNames()).containsExactly("f"); - } - - @Test - public void failingDataProviderAndInvocationCount() { - InvokedMethodNameListener listener = run(DataProviderWithErrorSample.class); - - assertThat(listener.getSkippedMethodNames()) - .containsExactly( - "testShouldSkip", - "testShouldSkip", - "testShouldSkipEvenIfSuccessPercentage", - "testShouldSkipEvenIfSuccessPercentage"); - } - - @Test(description = "GITHUB-2157") - public void abortWhenDataProviderThrowsException() { - InvokedMethodNameListener listener = run(TestClassWithDataProviderThatThrowsExceptions.class); - ITestResult result = listener.getResult("testMethod"); - Throwable cause = result.getThrowable(); - assertThat(cause).isInstanceOf(DataProviderInvocationException.class); - assertThat(result.getStatus()).isEqualTo(ITestResult.FAILURE); - } -} +package test.dataprovider; + +import org.testng.DataProviderInvocationException; +import org.testng.ITestResult; +import org.testng.annotations.Test; +import test.InvokedMethodNameListener; +import test.SimpleBaseTest; +import test.dataprovider.issue2157.TestClassWithDataProviderThatThrowsExceptions; + +import static org.assertj.core.api.Assertions.assertThat; + +public class FailingDataProviderTest extends SimpleBaseTest { + + @Test(description = "TESTNG-142: Exceptions in DataProvider are not reported as failed test") + public void failingDataProvider() { + InvokedMethodNameListener listener = run(FailingDataProviderSample.class); + + assertThat(listener.getSkippedMethodNames()).containsExactly("dpThrowingException"); + } + + @Test(description = "TESTNG-447: Abort when two data providers have the same name") + public void duplicateDataProviders() { + InvokedMethodNameListener listener = run(DuplicateDataProviderSample.class); + + assertThat(listener.getFailedBeforeInvocationMethodNames()).containsExactly("f"); + } + + @Test + public void failingDataProviderAndInvocationCount() { + InvokedMethodNameListener listener = run(DataProviderWithErrorSample.class); + + assertThat(listener.getSkippedMethodNames()) + .containsExactly( + "testShouldSkip", + "testShouldSkip", + "testShouldSkipEvenIfSuccessPercentage", + "testShouldSkipEvenIfSuccessPercentage"); + } + + @Test(description = "GITHUB-2157") + public void abortWhenDataProviderThrowsException() { + InvokedMethodNameListener listener = run(TestClassWithDataProviderThatThrowsExceptions.class); + ITestResult result = listener.getResult("testMethod"); + Throwable cause = result.getThrowable(); + assertThat(cause).isInstanceOf(DataProviderInvocationException.class); + assertThat(result.getStatus()).isEqualTo(ITestResult.FAILURE); + } +} diff --git a/testng-core/src/test/java/test/dataprovider/TestContextTest.java b/testng-core/src/test/java/test/dataprovider/TestContextTest.java index 44b7c20c60..0ded891186 100644 --- a/testng-core/src/test/java/test/dataprovider/TestContextTest.java +++ b/testng-core/src/test/java/test/dataprovider/TestContextTest.java @@ -1,46 +1,46 @@ -package test.dataprovider; - -import org.testng.TestNG; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; -import test.InvokedMethodNameListener; -import test.SimpleBaseTest; - -import static org.assertj.core.api.Assertions.assertThat; - -public class TestContextTest extends SimpleBaseTest { - - @Test - public void verifySix() { - // Not including any group, so the two test methods should fail - TestNG tng = create(TestContextSample.class); - - InvokedMethodNameListener listener = new InvokedMethodNameListener(); - tng.addListener(listener); - - tng.run(); - - assertThat(listener.getFailedMethodNames()).hasSize(2); - } - - @DataProvider - public static Object[][] dp() { - return new Object[][] {{10, "verifyTen"}, {5, "verifyFive"}}; - } - - @Test(dataProvider = "dp") - public void verify(int number, String passed) { - TestNG tng = create(TestContextSample.class); - tng.setGroups(String.valueOf(number)); - - InvokedMethodNameListener listener = new InvokedMethodNameListener(); - tng.addListener(listener); - - tng.run(); - - assertThat(listener.getSucceedMethodNames()).hasSize(1); - assertThat(listener.getSucceedMethodNames().get(0)) - .matches(passed + "\\(\\[foo(,foo){" + (number - 1) + "}?\\]\\)"); - assertThat(listener.getFailedMethodNames()).isEmpty(); - } -} +package test.dataprovider; + +import org.testng.TestNG; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; +import test.InvokedMethodNameListener; +import test.SimpleBaseTest; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TestContextTest extends SimpleBaseTest { + + @Test + public void verifySix() { + // Not including any group, so the two test methods should fail + TestNG tng = create(TestContextSample.class); + + InvokedMethodNameListener listener = new InvokedMethodNameListener(); + tng.addListener(listener); + + tng.run(); + + assertThat(listener.getFailedMethodNames()).hasSize(2); + } + + @DataProvider + public static Object[][] dp() { + return new Object[][] {{10, "verifyTen"}, {5, "verifyFive"}}; + } + + @Test(dataProvider = "dp") + public void verify(int number, String passed) { + TestNG tng = create(TestContextSample.class); + tng.setGroups(String.valueOf(number)); + + InvokedMethodNameListener listener = new InvokedMethodNameListener(); + tng.addListener(listener); + + tng.run(); + + assertThat(listener.getSucceedMethodNames()).hasSize(1); + assertThat(listener.getSucceedMethodNames().get(0)) + .matches(passed + "\\(\\[foo(,foo){" + (number - 1) + "}?\\]\\)"); + assertThat(listener.getFailedMethodNames()).isEmpty(); + } +} diff --git a/testng-core/src/test/java/test/dependent/DependsOnProtectedMethodTest.java b/testng-core/src/test/java/test/dependent/DependsOnProtectedMethodTest.java index 0f89f2db34..0c4b875a3c 100644 --- a/testng-core/src/test/java/test/dependent/DependsOnProtectedMethodTest.java +++ b/testng-core/src/test/java/test/dependent/DependsOnProtectedMethodTest.java @@ -1,27 +1,27 @@ -package test.dependent; - -import org.testng.Assert; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -/** This class/interface */ -public class DependsOnProtectedMethodTest { - private boolean m_before1 = false; - private boolean m_before2 = false; - - @BeforeMethod(dependsOnMethods = {"before2"}) - protected void before() { - m_before1 = true; - } - - @BeforeMethod - protected void before2() { - m_before2 = true; - } - - @Test - public void verifyBeforeInvocations() { - Assert.assertTrue( - m_before1 && m_before2, "Protected dependent @BeforeMethods should have been invoked"); - } -} +package test.dependent; + +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +/** This class/interface */ +public class DependsOnProtectedMethodTest { + private boolean m_before1 = false; + private boolean m_before2 = false; + + @BeforeMethod(dependsOnMethods = {"before2"}) + protected void before() { + m_before1 = true; + } + + @BeforeMethod + protected void before2() { + m_before2 = true; + } + + @Test + public void verifyBeforeInvocations() { + Assert.assertTrue( + m_before1 && m_before2, "Protected dependent @BeforeMethods should have been invoked"); + } +} diff --git a/testng-core/src/test/java/test/expectedexceptions/ParametersExceptionTest.java b/testng-core/src/test/java/test/expectedexceptions/ParametersExceptionTest.java index cb3b241a82..207c1a1926 100644 --- a/testng-core/src/test/java/test/expectedexceptions/ParametersExceptionTest.java +++ b/testng-core/src/test/java/test/expectedexceptions/ParametersExceptionTest.java @@ -1,28 +1,28 @@ -package test.expectedexceptions; - -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; -import org.xml.sax.SAXException; - -import java.io.IOException; -import java.lang.reflect.Method; - -/** This class/interface */ -public class ParametersExceptionTest { - @Test(dataProvider = "A") - public void testA(Exception err) { - System.out.println("testA"); - } - - @DataProvider(name = "A") - protected Object[][] dp() { - return new Object[][] {{new IOException(), new SAXException()}}; - } - - @AfterMethod - protected void verify(Method method) { - Assert.assertTrue(false, "forced failure"); - } -} +package test.expectedexceptions; + +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; +import org.xml.sax.SAXException; + +import java.io.IOException; +import java.lang.reflect.Method; + +/** This class/interface */ +public class ParametersExceptionTest { + @Test(dataProvider = "A") + public void testA(Exception err) { + System.out.println("testA"); + } + + @DataProvider(name = "A") + protected Object[][] dp() { + return new Object[][] {{new IOException(), new SAXException()}}; + } + + @AfterMethod + protected void verify(Method method) { + Assert.assertTrue(false, "forced failure"); + } +} diff --git a/testng-core/src/test/java/test/factory/classconf/XClassOrderWithFactory.java b/testng-core/src/test/java/test/factory/classconf/XClassOrderWithFactory.java index b52d7dfff8..598ac9fe80 100644 --- a/testng-core/src/test/java/test/factory/classconf/XClassOrderWithFactory.java +++ b/testng-core/src/test/java/test/factory/classconf/XClassOrderWithFactory.java @@ -1,31 +1,31 @@ -package test.factory.classconf; - -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Factory; -import org.testng.annotations.Test; - -public class XClassOrderWithFactory { - public static final String EXPECTED_LOG = "BTABTABTA"; - public static final StringBuffer LOG = new StringBuffer(); - - @Factory - public Object[] createInstances() { - return new Object[] {new XClassOrderTest(), new XClassOrderTest(), new XClassOrderTest()}; - } - - public static class XClassOrderTest { - @BeforeClass - public void beforeClass() { - LOG.append("B"); - } - - public @Test void test() { - LOG.append("T"); - } - - public @AfterClass void afterClass() { - LOG.append("A"); - } - } -} +package test.factory.classconf; + +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Factory; +import org.testng.annotations.Test; + +public class XClassOrderWithFactory { + public static final String EXPECTED_LOG = "BTABTABTA"; + public static final StringBuffer LOG = new StringBuffer(); + + @Factory + public Object[] createInstances() { + return new Object[] {new XClassOrderTest(), new XClassOrderTest(), new XClassOrderTest()}; + } + + public static class XClassOrderTest { + @BeforeClass + public void beforeClass() { + LOG.append("B"); + } + + public @Test void test() { + LOG.append("T"); + } + + public @AfterClass void afterClass() { + LOG.append("A"); + } + } +} diff --git a/testng-core/src/test/java/test/factory/classconf/XClassOrderWithFactoryTest.java b/testng-core/src/test/java/test/factory/classconf/XClassOrderWithFactoryTest.java index 23b4f45c4f..cbf519f5de 100644 --- a/testng-core/src/test/java/test/factory/classconf/XClassOrderWithFactoryTest.java +++ b/testng-core/src/test/java/test/factory/classconf/XClassOrderWithFactoryTest.java @@ -1,19 +1,19 @@ -package test.factory.classconf; - -import org.testng.Assert; -import org.testng.TestListenerAdapter; -import org.testng.TestNG; -import org.testng.annotations.Test; - - -public class XClassOrderWithFactoryTest { - @Test - public void testBeforeAfterClassInvocationsWithFactory() { - TestNG testng= new TestNG(); - testng.setTestClasses(new Class[] {XClassOrderWithFactory.class}); - TestListenerAdapter tla = new TestListenerAdapter(); - testng.addListener(tla); - testng.run(); - Assert.assertEquals(XClassOrderWithFactory.LOG.toString(), XClassOrderWithFactory.EXPECTED_LOG); - } -} +package test.factory.classconf; + +import org.testng.Assert; +import org.testng.TestListenerAdapter; +import org.testng.TestNG; +import org.testng.annotations.Test; + + +public class XClassOrderWithFactoryTest { + @Test + public void testBeforeAfterClassInvocationsWithFactory() { + TestNG testng= new TestNG(); + testng.setTestClasses(new Class[] {XClassOrderWithFactory.class}); + TestListenerAdapter tla = new TestListenerAdapter(); + testng.addListener(tla); + testng.run(); + Assert.assertEquals(XClassOrderWithFactory.LOG.toString(), XClassOrderWithFactory.EXPECTED_LOG); + } +} diff --git a/testng-core/src/test/java/test/groupinvocation/DummyTest.java b/testng-core/src/test/java/test/groupinvocation/DummyTest.java index 31bd3217e9..1ff72fd205 100644 --- a/testng-core/src/test/java/test/groupinvocation/DummyTest.java +++ b/testng-core/src/test/java/test/groupinvocation/DummyTest.java @@ -1,37 +1,37 @@ -package test.groupinvocation; - -import org.testng.Assert; -import org.testng.annotations.AfterClass; -import org.testng.annotations.Test; - -import java.util.HashMap; -import java.util.Map; - -public class DummyTest { - private static Map s_externalClassGroups = new HashMap<>(); - - @Test(groups = {"a"}) - public void testA() {} - - @Test(groups = {"b"}) - public void testB() {} - - @Test(groups = {"a", "b"}) - public void testAB() {} - - @AfterClass(alwaysRun = true) - public void checkInvocations() { - Integer hashCode1 = s_externalClassGroups.get("beforeGroups"); - Assert.assertNotNull(hashCode1, "External @BeforeGroups not invoked"); - Integer hashCode2 = s_externalClassGroups.get("afterGroups"); - Assert.assertNotNull(hashCode2, "External @AfterGroups not invoked"); - Assert.assertEquals( - hashCode1, - hashCode2, - "External @BeforeGroups and @AfterGroups were not invoked on the" + " same class instance"); - } - - public static void recordInvocation(String string, int i) { - s_externalClassGroups.put(string, i); - } -} +package test.groupinvocation; + +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.Test; + +import java.util.HashMap; +import java.util.Map; + +public class DummyTest { + private static Map s_externalClassGroups = new HashMap<>(); + + @Test(groups = {"a"}) + public void testA() {} + + @Test(groups = {"b"}) + public void testB() {} + + @Test(groups = {"a", "b"}) + public void testAB() {} + + @AfterClass(alwaysRun = true) + public void checkInvocations() { + Integer hashCode1 = s_externalClassGroups.get("beforeGroups"); + Assert.assertNotNull(hashCode1, "External @BeforeGroups not invoked"); + Integer hashCode2 = s_externalClassGroups.get("afterGroups"); + Assert.assertNotNull(hashCode2, "External @AfterGroups not invoked"); + Assert.assertEquals( + hashCode1, + hashCode2, + "External @BeforeGroups and @AfterGroups were not invoked on the" + " same class instance"); + } + + public static void recordInvocation(String string, int i) { + s_externalClassGroups.put(string, i); + } +} diff --git a/testng-core/src/test/java/test/groupinvocation/DummyTest2.java b/testng-core/src/test/java/test/groupinvocation/DummyTest2.java index c1fc5332b8..49506c851f 100644 --- a/testng-core/src/test/java/test/groupinvocation/DummyTest2.java +++ b/testng-core/src/test/java/test/groupinvocation/DummyTest2.java @@ -1,19 +1,19 @@ -package test.groupinvocation; - -import org.testng.Assert; -import org.testng.annotations.AfterClass; -import org.testng.annotations.Test; - -public class DummyTest2 { - private boolean m_invoked = false; - - @Test(groups = {"A"}) - public void dummyTest() { - m_invoked = true; - } - - @AfterClass(alwaysRun = true) - public void checkInvocations() { - Assert.assertFalse(m_invoked, "@Test method invoked even if @BeforeGroups failed"); - } -} +package test.groupinvocation; + +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.Test; + +public class DummyTest2 { + private boolean m_invoked = false; + + @Test(groups = {"A"}) + public void dummyTest() { + m_invoked = true; + } + + @AfterClass(alwaysRun = true) + public void checkInvocations() { + Assert.assertFalse(m_invoked, "@Test method invoked even if @BeforeGroups failed"); + } +} diff --git a/testng-core/src/test/java/test/groupinvocation/FailingBeforeGroupMethod.java b/testng-core/src/test/java/test/groupinvocation/FailingBeforeGroupMethod.java index 3fa56be1f9..f0a487531f 100644 --- a/testng-core/src/test/java/test/groupinvocation/FailingBeforeGroupMethod.java +++ b/testng-core/src/test/java/test/groupinvocation/FailingBeforeGroupMethod.java @@ -1,10 +1,10 @@ -package test.groupinvocation; - -import org.testng.annotations.BeforeGroups; - -public class FailingBeforeGroupMethod { - @BeforeGroups(groups = {"A"}) - public void beforeGroupA() { - throw new RuntimeException("Failing @BeforeGroups beforeGroupA method"); - } -} +package test.groupinvocation; + +import org.testng.annotations.BeforeGroups; + +public class FailingBeforeGroupMethod { + @BeforeGroups(groups = {"A"}) + public void beforeGroupA() { + throw new RuntimeException("Failing @BeforeGroups beforeGroupA method"); + } +} diff --git a/testng-core/src/test/java/test/groupinvocation/GroupConfiguration.java b/testng-core/src/test/java/test/groupinvocation/GroupConfiguration.java index b203752651..043de9d969 100644 --- a/testng-core/src/test/java/test/groupinvocation/GroupConfiguration.java +++ b/testng-core/src/test/java/test/groupinvocation/GroupConfiguration.java @@ -1,16 +1,16 @@ -package test.groupinvocation; - -import org.testng.annotations.AfterGroups; -import org.testng.annotations.BeforeGroups; - -public class GroupConfiguration { - @BeforeGroups(groups = {"a"}) - public void beforeGroups() { - DummyTest.recordInvocation("beforeGroups", hashCode()); - } - - @AfterGroups(groups = {"a"}) - public void afterGroups() { - DummyTest.recordInvocation("afterGroups", hashCode()); - } -} +package test.groupinvocation; + +import org.testng.annotations.AfterGroups; +import org.testng.annotations.BeforeGroups; + +public class GroupConfiguration { + @BeforeGroups(groups = {"a"}) + public void beforeGroups() { + DummyTest.recordInvocation("beforeGroups", hashCode()); + } + + @AfterGroups(groups = {"a"}) + public void afterGroups() { + DummyTest.recordInvocation("afterGroups", hashCode()); + } +} diff --git a/testng-core/src/test/java/test/groupinvocation/testng.xml b/testng-core/src/test/java/test/groupinvocation/testng.xml index c7812bcb36..83c90798cb 100644 --- a/testng-core/src/test/java/test/groupinvocation/testng.xml +++ b/testng-core/src/test/java/test/groupinvocation/testng.xml @@ -1,34 +1,34 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/testng-core/src/test/java/test/issue1430/TestFileToClass.java b/testng-core/src/test/java/test/issue1430/TestFileToClass.java index 7b779f0ba6..9e684e0982 100644 --- a/testng-core/src/test/java/test/issue1430/TestFileToClass.java +++ b/testng-core/src/test/java/test/issue1430/TestFileToClass.java @@ -1,18 +1,18 @@ -package test.issue1430; - -import org.testng.Assert; -import org.testng.annotations.Test; -import org.testng.internal.ClassHelper; - -public class TestFileToClass { - - @Test - public void testFileToClass() { - Class c1 = this.getClass(); - String p = c1.getResource("TestFileToClass.class").getPath(); - Class c2 = ClassHelper.fileToClass(p); - Assert.assertNotNull(c2); - Assert.assertEquals(c1.getName(), c2.getName()); - } - -} +package test.issue1430; + +import org.testng.Assert; +import org.testng.annotations.Test; +import org.testng.internal.ClassHelper; + +public class TestFileToClass { + + @Test + public void testFileToClass() { + Class c1 = this.getClass(); + String p = c1.getResource("TestFileToClass.class").getPath(); + Class c2 = ClassHelper.fileToClass(p); + Assert.assertNotNull(c2); + Assert.assertEquals(c1.getName(), c2.getName()); + } + +} diff --git a/testng-core/src/test/java/test/jason/Main.java b/testng-core/src/test/java/test/jason/Main.java index 7dd3850aa3..b3f91ababb 100644 --- a/testng-core/src/test/java/test/jason/Main.java +++ b/testng-core/src/test/java/test/jason/Main.java @@ -1,17 +1,17 @@ -package test.jason; - -import org.testng.annotations.AfterClass; -import org.testng.annotations.Test; - -public class Main extends MainBase { - public static boolean m_passed = false; - - @AfterClass - public void afterClass() { - m_passed = true; - } - - @Test(description = "This test is run") - public void test1() throws InterruptedException { - } -} +package test.jason; + +import org.testng.annotations.AfterClass; +import org.testng.annotations.Test; + +public class Main extends MainBase { + public static boolean m_passed = false; + + @AfterClass + public void afterClass() { + m_passed = true; + } + + @Test(description = "This test is run") + public void test1() throws InterruptedException { + } +} diff --git a/testng-core/src/test/java/test/jason/MainBase.java b/testng-core/src/test/java/test/jason/MainBase.java index 0710dcdf66..de3d1899b7 100644 --- a/testng-core/src/test/java/test/jason/MainBase.java +++ b/testng-core/src/test/java/test/jason/MainBase.java @@ -1,10 +1,10 @@ -package test.jason; - -import org.testng.annotations.Test; - -public class MainBase { - - @Test(description = "This test is never run but prevents AfterClass") - public void checkReportsExist() { - } -} +package test.jason; + +import org.testng.annotations.Test; + +public class MainBase { + + @Test(description = "This test is never run but prevents AfterClass") + public void checkReportsExist() { + } +} diff --git a/testng-core/src/test/java/test/methodselectors/NoTest.java b/testng-core/src/test/java/test/methodselectors/NoTest.java index 55e1a89908..0db8b11878 100644 --- a/testng-core/src/test/java/test/methodselectors/NoTest.java +++ b/testng-core/src/test/java/test/methodselectors/NoTest.java @@ -1,4 +1,4 @@ -package test.methodselectors; - -public @interface NoTest { -} +package test.methodselectors; + +public @interface NoTest { +} diff --git a/testng-core/src/test/java/test/methodselectors/NoTestSelector.java b/testng-core/src/test/java/test/methodselectors/NoTestSelector.java index eb4b3b7b79..850ea65843 100644 --- a/testng-core/src/test/java/test/methodselectors/NoTestSelector.java +++ b/testng-core/src/test/java/test/methodselectors/NoTestSelector.java @@ -1,26 +1,26 @@ -package test.methodselectors; - -import org.testng.IMethodSelector; -import org.testng.IMethodSelectorContext; -import org.testng.ITestNGMethod; - -import java.util.List; - -public class NoTestSelector implements IMethodSelector { - - @Override - public boolean includeMethod(IMethodSelectorContext context, - ITestNGMethod method, boolean isTestMethod) - { - ppp("NOTEST RETURNING FALSE FOR " + method); - return false; - } - - @Override - public void setTestMethods(List testMethods) { - } - - private static void ppp(String s) { -// System.out.println("[NoTestSelector] " + s); - } -} +package test.methodselectors; + +import org.testng.IMethodSelector; +import org.testng.IMethodSelectorContext; +import org.testng.ITestNGMethod; + +import java.util.List; + +public class NoTestSelector implements IMethodSelector { + + @Override + public boolean includeMethod(IMethodSelectorContext context, + ITestNGMethod method, boolean isTestMethod) + { + ppp("NOTEST RETURNING FALSE FOR " + method); + return false; + } + + @Override + public void setTestMethods(List testMethods) { + } + + private static void ppp(String s) { +// System.out.println("[NoTestSelector] " + s); + } +} diff --git a/testng-core/src/test/java/test/methodselectors/PrioritySampleTest.java b/testng-core/src/test/java/test/methodselectors/PrioritySampleTest.java index 6d66bd70b1..bfddb746f0 100644 --- a/testng-core/src/test/java/test/methodselectors/PrioritySampleTest.java +++ b/testng-core/src/test/java/test/methodselectors/PrioritySampleTest.java @@ -1,21 +1,21 @@ -package test.methodselectors; - -import org.testng.annotations.Test; - -public class PrioritySampleTest { - - @Test - public void alwaysRun() { - ppp("ALWAYS"); - } - - @Test - @NoTest - public void neverRun() { - ppp("NEVER"); - } - - private static void ppp(String s) { - System.out.println("[PrioritySampleTest] " + s); - } -} +package test.methodselectors; + +import org.testng.annotations.Test; + +public class PrioritySampleTest { + + @Test + public void alwaysRun() { + ppp("ALWAYS"); + } + + @Test + @NoTest + public void neverRun() { + ppp("NEVER"); + } + + private static void ppp(String s) { + System.out.println("[PrioritySampleTest] " + s); + } +} diff --git a/testng-core/src/test/java/test/methodselectors/PriorityTest.java b/testng-core/src/test/java/test/methodselectors/PriorityTest.java index 93166d9bf4..9b5de9b0a9 100644 --- a/testng-core/src/test/java/test/methodselectors/PriorityTest.java +++ b/testng-core/src/test/java/test/methodselectors/PriorityTest.java @@ -1,52 +1,52 @@ -package test.methodselectors; - -import org.testng.Assert; -import org.testng.ITestNGListener; -import org.testng.ITestResult; -import org.testng.TestListenerAdapter; -import org.testng.TestNG; -import org.testng.annotations.Test; - -import java.util.List; - -public class PriorityTest { - - private void runTest(int priority, String[] passedTests) { - TestNG tng = new TestNG(); - tng.setTestClasses(new Class[] { PrioritySampleTest.class }); - tng.addMethodSelector("test.methodselectors.NoTestSelector", priority); - TestListenerAdapter tla = new TestListenerAdapter(); - tng.addListener((ITestNGListener) tla); - tng.run(); - - List passed = tla.getPassedTests(); - Assert.assertEquals(passedTests.length, passed.size()); - if (passedTests.length == 1) { - String passed0 = passed.get(0).getName(); - Assert.assertEquals(passed0, passedTests[0]); - } - if (passedTests.length == 2) { - String passed0 = passed.get(0).getName(); - String passed1 = passed.get(1).getName(); - Assert.assertTrue(passed0.equals(passedTests[0]) || passed0.equals(passedTests[1])); - Assert.assertTrue(passed1.equals(passedTests[0]) || passed1.equals(passedTests[1])); - } - } - -// @Test - public void negativePriority() { - runTest(-5, new String[] {}); - } - - @Test - public void lessThanTenPriority() { - runTest(5, new String[] { "alwaysRun" }); - } - -// @Test - public void greaterThanTenPriority() { - runTest(15, new String[] { "alwaysRun", "neverRun" }); - } - - -} +package test.methodselectors; + +import org.testng.Assert; +import org.testng.ITestNGListener; +import org.testng.ITestResult; +import org.testng.TestListenerAdapter; +import org.testng.TestNG; +import org.testng.annotations.Test; + +import java.util.List; + +public class PriorityTest { + + private void runTest(int priority, String[] passedTests) { + TestNG tng = new TestNG(); + tng.setTestClasses(new Class[] { PrioritySampleTest.class }); + tng.addMethodSelector("test.methodselectors.NoTestSelector", priority); + TestListenerAdapter tla = new TestListenerAdapter(); + tng.addListener((ITestNGListener) tla); + tng.run(); + + List passed = tla.getPassedTests(); + Assert.assertEquals(passedTests.length, passed.size()); + if (passedTests.length == 1) { + String passed0 = passed.get(0).getName(); + Assert.assertEquals(passed0, passedTests[0]); + } + if (passedTests.length == 2) { + String passed0 = passed.get(0).getName(); + String passed1 = passed.get(1).getName(); + Assert.assertTrue(passed0.equals(passedTests[0]) || passed0.equals(passedTests[1])); + Assert.assertTrue(passed1.equals(passedTests[0]) || passed1.equals(passedTests[1])); + } + } + +// @Test + public void negativePriority() { + runTest(-5, new String[] {}); + } + + @Test + public void lessThanTenPriority() { + runTest(5, new String[] { "alwaysRun" }); + } + +// @Test + public void greaterThanTenPriority() { + runTest(15, new String[] { "alwaysRun", "neverRun" }); + } + + +} diff --git a/testng-core/src/test/java/test/multiplelisteners/SimpleReporter.java b/testng-core/src/test/java/test/multiplelisteners/SimpleReporter.java index 460ef19159..41717e404a 100644 --- a/testng-core/src/test/java/test/multiplelisteners/SimpleReporter.java +++ b/testng-core/src/test/java/test/multiplelisteners/SimpleReporter.java @@ -1,35 +1,35 @@ -package test.multiplelisteners; - -import org.testng.IReporter; -import org.testng.ISuite; -import org.testng.SuiteRunner; -import org.testng.TestListenerAdapter; -import org.testng.internal.IConfiguration; -import org.testng.xml.XmlSuite; -import test.listeners.ListenerAssert; - -import java.lang.reflect.Field; -import java.util.List; - -public class SimpleReporter implements IReporter -{ - @Override - public void generateReport(final List xmlSuites, final List suites, - final String outputDirectory) - { - for (final ISuite iSuite : suites) - { - try - { - final Field field = SuiteRunner.class.getDeclaredField("configuration"); - field.setAccessible(true); - final IConfiguration conf = (IConfiguration) field.get(iSuite); - ListenerAssert.assertListenerType(conf.getConfigurationListeners(), TestListenerAdapter.class); - } - catch (final Exception e) - { - throw new RuntimeException(e); - } - } - } -} +package test.multiplelisteners; + +import org.testng.IReporter; +import org.testng.ISuite; +import org.testng.SuiteRunner; +import org.testng.TestListenerAdapter; +import org.testng.internal.IConfiguration; +import org.testng.xml.XmlSuite; +import test.listeners.ListenerAssert; + +import java.lang.reflect.Field; +import java.util.List; + +public class SimpleReporter implements IReporter +{ + @Override + public void generateReport(final List xmlSuites, final List suites, + final String outputDirectory) + { + for (final ISuite iSuite : suites) + { + try + { + final Field field = SuiteRunner.class.getDeclaredField("configuration"); + field.setAccessible(true); + final IConfiguration conf = (IConfiguration) field.get(iSuite); + ListenerAssert.assertListenerType(conf.getConfigurationListeners(), TestListenerAdapter.class); + } + catch (final Exception e) + { + throw new RuntimeException(e); + } + } + } +} diff --git a/testng-core/src/test/java/test/multiplelisteners/Test1.java b/testng-core/src/test/java/test/multiplelisteners/Test1.java index e42d086f39..3937ba7443 100644 --- a/testng-core/src/test/java/test/multiplelisteners/Test1.java +++ b/testng-core/src/test/java/test/multiplelisteners/Test1.java @@ -1,12 +1,12 @@ -package test.multiplelisteners; - -import org.testng.annotations.Test; - -@Test -public class Test1 -{ - public void test() - { -// Reporter.log("test1", true); - } -} +package test.multiplelisteners; + +import org.testng.annotations.Test; + +@Test +public class Test1 +{ + public void test() + { +// Reporter.log("test1", true); + } +} diff --git a/testng-core/src/test/java/test/multiplelisteners/TestMaker.java b/testng-core/src/test/java/test/multiplelisteners/TestMaker.java index 967dcf3998..a32bf6dd5c 100644 --- a/testng-core/src/test/java/test/multiplelisteners/TestMaker.java +++ b/testng-core/src/test/java/test/multiplelisteners/TestMaker.java @@ -1,52 +1,52 @@ -package test.multiplelisteners; - -import org.testng.TestListenerAdapter; -import org.testng.TestNG; -import org.testng.annotations.Test; -import org.testng.collections.Lists; -import org.testng.xml.XmlClass; -import org.testng.xml.XmlSuite; -import org.testng.xml.XmlTest; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -public class TestMaker -{ - @Test(description = "Make sure only one listener is created and not 2^3") - public void run() - { - final TestNG tng = new TestNG(); - tng.setUseDefaultListeners(false); - tng.setListenerClasses(Arrays.asList(TestListenerAdapter.class, SimpleReporter.class)); - final List suites = createSuites(); - tng.setXmlSuites(suites); - tng.run(); - -// Reporter.log(tng.getSuiteListeners().size() + "", true); -// for (final XmlSuite xmlSuite : suites) -// { -// Reporter.log(xmlSuite.getName() + ": " + xmlSuite.getListeners().size(), true); -// } - } - - private List createSuites() - { - final List ret = Lists.newArrayList(); - for (int i = 0; i < 3; i++) - { - ret.add(createSuite(i)); - } - return ret; - } - - private XmlSuite createSuite(final int nr) - { - final XmlSuite suite = new XmlSuite(); - suite.setName("Suite_" + nr); - - new XmlTest(suite).setXmlClasses(Collections.singletonList(new XmlClass(Test1.class))); - return suite; - } -} +package test.multiplelisteners; + +import org.testng.TestListenerAdapter; +import org.testng.TestNG; +import org.testng.annotations.Test; +import org.testng.collections.Lists; +import org.testng.xml.XmlClass; +import org.testng.xml.XmlSuite; +import org.testng.xml.XmlTest; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class TestMaker +{ + @Test(description = "Make sure only one listener is created and not 2^3") + public void run() + { + final TestNG tng = new TestNG(); + tng.setUseDefaultListeners(false); + tng.setListenerClasses(Arrays.asList(TestListenerAdapter.class, SimpleReporter.class)); + final List suites = createSuites(); + tng.setXmlSuites(suites); + tng.run(); + +// Reporter.log(tng.getSuiteListeners().size() + "", true); +// for (final XmlSuite xmlSuite : suites) +// { +// Reporter.log(xmlSuite.getName() + ": " + xmlSuite.getListeners().size(), true); +// } + } + + private List createSuites() + { + final List ret = Lists.newArrayList(); + for (int i = 0; i < 3; i++) + { + ret.add(createSuite(i)); + } + return ret; + } + + private XmlSuite createSuite(final int nr) + { + final XmlSuite suite = new XmlSuite(); + suite.setName("Suite_" + nr); + + new XmlTest(suite).setXmlClasses(Collections.singletonList(new XmlClass(Test1.class))); + return suite; + } +} diff --git a/testng-core/src/test/java/test/objectfactory/CombinedTestAndObjectFactorySample.java b/testng-core/src/test/java/test/objectfactory/CombinedTestAndObjectFactorySample.java index 4249983d53..08853c2356 100644 --- a/testng-core/src/test/java/test/objectfactory/CombinedTestAndObjectFactorySample.java +++ b/testng-core/src/test/java/test/objectfactory/CombinedTestAndObjectFactorySample.java @@ -1,38 +1,38 @@ -package test.objectfactory; - -import org.testng.Assert; -import org.testng.ITestObjectFactory; -import org.testng.annotations.ObjectFactory; -import org.testng.annotations.Test; -import org.testng.internal.objects.InstanceCreator; - -import java.lang.reflect.Constructor; - -public class CombinedTestAndObjectFactorySample implements ITestObjectFactory { - - private boolean configured = false; - - @ObjectFactory - public ITestObjectFactory create() { - return new CombinedTestAndObjectFactorySample(); - } - - @Test - public void isConfigured() { - Assert.assertTrue(configured, "Should have been configured by object factory"); - } - - @Override - public T newInstance(Constructor constructor, Object... params) { - try { - T o = InstanceCreator.newInstance(constructor, params); - if (o instanceof CombinedTestAndObjectFactorySample) { - CombinedTestAndObjectFactorySample s = (CombinedTestAndObjectFactorySample) o; - s.configured = true; - } - return o; - } catch (Exception e) { - throw new RuntimeException(e); - } - } -} +package test.objectfactory; + +import org.testng.Assert; +import org.testng.ITestObjectFactory; +import org.testng.annotations.ObjectFactory; +import org.testng.annotations.Test; +import org.testng.internal.objects.InstanceCreator; + +import java.lang.reflect.Constructor; + +public class CombinedTestAndObjectFactorySample implements ITestObjectFactory { + + private boolean configured = false; + + @ObjectFactory + public ITestObjectFactory create() { + return new CombinedTestAndObjectFactorySample(); + } + + @Test + public void isConfigured() { + Assert.assertTrue(configured, "Should have been configured by object factory"); + } + + @Override + public T newInstance(Constructor constructor, Object... params) { + try { + T o = InstanceCreator.newInstance(constructor, params); + if (o instanceof CombinedTestAndObjectFactorySample) { + CombinedTestAndObjectFactorySample s = (CombinedTestAndObjectFactorySample) o; + s.configured = true; + } + return o; + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/testng-core/src/test/java/test/objectfactory/CombinedTestAndObjectFactoryTest.java b/testng-core/src/test/java/test/objectfactory/CombinedTestAndObjectFactoryTest.java index 76e0e9fcd7..aa0dfd9d7c 100644 --- a/testng-core/src/test/java/test/objectfactory/CombinedTestAndObjectFactoryTest.java +++ b/testng-core/src/test/java/test/objectfactory/CombinedTestAndObjectFactoryTest.java @@ -1,27 +1,27 @@ -package test.objectfactory; - -import org.testng.ITestNGListener; -import org.testng.TestNG; -import org.testng.annotations.Test; - -import test.InvokedMethodNameListener; -import test.SimpleBaseTest; - -import static org.assertj.core.api.Assertions.assertThat; - -public class CombinedTestAndObjectFactoryTest extends SimpleBaseTest { - - @Test - void combinedTestAndObjectFactory() { - TestNG tng = create(CombinedTestAndObjectFactorySample.class); - - InvokedMethodNameListener listener = new InvokedMethodNameListener(); - tng.addListener((ITestNGListener) listener); - - tng.run(); - - assertThat(listener.getFailedMethodNames()).isEmpty(); - assertThat(listener.getSkippedMethodNames()).isEmpty(); - assertThat(listener.getSucceedMethodNames()).containsExactly("isConfigured"); - } -} +package test.objectfactory; + +import org.testng.ITestNGListener; +import org.testng.TestNG; +import org.testng.annotations.Test; + +import test.InvokedMethodNameListener; +import test.SimpleBaseTest; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CombinedTestAndObjectFactoryTest extends SimpleBaseTest { + + @Test + void combinedTestAndObjectFactory() { + TestNG tng = create(CombinedTestAndObjectFactorySample.class); + + InvokedMethodNameListener listener = new InvokedMethodNameListener(); + tng.addListener((ITestNGListener) listener); + + tng.run(); + + assertThat(listener.getFailedMethodNames()).isEmpty(); + assertThat(listener.getSkippedMethodNames()).isEmpty(); + assertThat(listener.getSucceedMethodNames()).containsExactly("isConfigured"); + } +} diff --git a/testng-core/src/test/java/test/regression/groupsordering/A.java b/testng-core/src/test/java/test/regression/groupsordering/A.java index b6f7b62536..a6662d7e6e 100644 --- a/testng-core/src/test/java/test/regression/groupsordering/A.java +++ b/testng-core/src/test/java/test/regression/groupsordering/A.java @@ -1,12 +1,12 @@ -package test.regression.groupsordering; - - -import org.testng.annotations.Test; - -public class A extends Base { - - @Test(groups= "a") - public void testA() { - s_childAWasRun= true; - } -} +package test.regression.groupsordering; + + +import org.testng.annotations.Test; + +public class A extends Base { + + @Test(groups= "a") + public void testA() { + s_childAWasRun= true; + } +} diff --git a/testng-core/src/test/java/test/regression/groupsordering/B.java b/testng-core/src/test/java/test/regression/groupsordering/B.java index 9ecb928a6d..ef55d42274 100644 --- a/testng-core/src/test/java/test/regression/groupsordering/B.java +++ b/testng-core/src/test/java/test/regression/groupsordering/B.java @@ -1,12 +1,12 @@ -package test.regression.groupsordering; - - -import org.testng.annotations.Test; - -public class B extends Base { - - @Test(groups= "a") - public void testB() throws Exception { - Base.s_childBWasRun= true; - } -} +package test.regression.groupsordering; + + +import org.testng.annotations.Test; + +public class B extends Base { + + @Test(groups= "a") + public void testB() throws Exception { + Base.s_childBWasRun= true; + } +} diff --git a/testng-core/src/test/java/test/regression/groupsordering/Base.java b/testng-core/src/test/java/test/regression/groupsordering/Base.java index 2389ec197a..8eb3969ba6 100644 --- a/testng-core/src/test/java/test/regression/groupsordering/Base.java +++ b/testng-core/src/test/java/test/regression/groupsordering/Base.java @@ -1,24 +1,24 @@ -package test.regression.groupsordering; - -import org.testng.Assert; -import org.testng.annotations.AfterGroups; -import org.testng.annotations.BeforeGroups; - -public abstract class Base { - protected static boolean s_childAWasRun; - protected static boolean s_childBWasRun; - - @BeforeGroups(value= "a", groups= "a") - public void setUp() { - Assert.assertFalse(s_childAWasRun || s_childBWasRun, "Static field was not reset: @AfterGroup method not invoked"); - } - - @AfterGroups(value= "a", groups= "a") - public void tearDown() { - Assert.assertTrue(s_childAWasRun, "Child A was not run"); - Assert.assertTrue(s_childBWasRun, "Child B was not run"); - s_childAWasRun = false; - s_childBWasRun = false; - } - -} +package test.regression.groupsordering; + +import org.testng.Assert; +import org.testng.annotations.AfterGroups; +import org.testng.annotations.BeforeGroups; + +public abstract class Base { + protected static boolean s_childAWasRun; + protected static boolean s_childBWasRun; + + @BeforeGroups(value= "a", groups= "a") + public void setUp() { + Assert.assertFalse(s_childAWasRun || s_childBWasRun, "Static field was not reset: @AfterGroup method not invoked"); + } + + @AfterGroups(value= "a", groups= "a") + public void tearDown() { + Assert.assertTrue(s_childAWasRun, "Child A was not run"); + Assert.assertTrue(s_childBWasRun, "Child B was not run"); + s_childAWasRun = false; + s_childBWasRun = false; + } + +} diff --git a/testng-core/src/test/java/test/regression/groupsordering/testng.xml b/testng-core/src/test/java/test/regression/groupsordering/testng.xml index 54238105b9..8e8ab21fa4 100644 --- a/testng-core/src/test/java/test/regression/groupsordering/testng.xml +++ b/testng-core/src/test/java/test/regression/groupsordering/testng.xml @@ -1,22 +1,22 @@ - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testng-core/src/test/java/test/reports/issue2069/Dummy1.java b/testng-core/src/test/java/test/reports/issue2069/Dummy1.java index dd361d17f1..2c4132838b 100755 --- a/testng-core/src/test/java/test/reports/issue2069/Dummy1.java +++ b/testng-core/src/test/java/test/reports/issue2069/Dummy1.java @@ -1,15 +1,15 @@ -package test.reports.issue2069; - -import junit.framework.Test; -import junit.framework.TestSuite; - -public class Dummy1 extends TestSuite { - - public static Test suite() { - final TestSuite suite = new TestSuite("Failing report"); - suite.addTestSuite(Dummy3.class); - return suite; - } - -} - +package test.reports.issue2069; + +import junit.framework.Test; +import junit.framework.TestSuite; + +public class Dummy1 extends TestSuite { + + public static Test suite() { + final TestSuite suite = new TestSuite("Failing report"); + suite.addTestSuite(Dummy3.class); + return suite; + } + +} + diff --git a/testng-core/src/test/java/test/reports/issue2069/Dummy2.java b/testng-core/src/test/java/test/reports/issue2069/Dummy2.java index b6900d1dd7..4e5c0972f8 100755 --- a/testng-core/src/test/java/test/reports/issue2069/Dummy2.java +++ b/testng-core/src/test/java/test/reports/issue2069/Dummy2.java @@ -1,10 +1,10 @@ -package test.reports.issue2069; - -import junit.framework.TestCase; - -public class Dummy2 extends TestCase { - - public void testScheduleDelete() { - } -} - +package test.reports.issue2069; + +import junit.framework.TestCase; + +public class Dummy2 extends TestCase { + + public void testScheduleDelete() { + } +} + diff --git a/testng-core/src/test/java/test/reports/issue2069/Dummy3.java b/testng-core/src/test/java/test/reports/issue2069/Dummy3.java index b1f74b972a..7f7ccbad0d 100755 --- a/testng-core/src/test/java/test/reports/issue2069/Dummy3.java +++ b/testng-core/src/test/java/test/reports/issue2069/Dummy3.java @@ -1,19 +1,19 @@ -package test.reports.issue2069; - -import junit.framework.TestCase; -import org.junit.Assert; -import org.junit.Test; - -public class Dummy3 extends TestCase { - - @Test - public void testFails() { - Assert.assertEquals("test3 assertion that will fail", "1", "2"); - } - - @Test - public void testSucceeds() { - Assert.assertEquals("test3 assertion", "1", "1"); - } -} - +package test.reports.issue2069; + +import junit.framework.TestCase; +import org.junit.Assert; +import org.junit.Test; + +public class Dummy3 extends TestCase { + + @Test + public void testFails() { + Assert.assertEquals("test3 assertion that will fail", "1", "2"); + } + + @Test + public void testSucceeds() { + Assert.assertEquals("test3 assertion", "1", "1"); + } +} + diff --git a/testng-core/src/test/java/test/reports/issue2069/Dummy4.java b/testng-core/src/test/java/test/reports/issue2069/Dummy4.java index e7ecf9ec55..862076911e 100755 --- a/testng-core/src/test/java/test/reports/issue2069/Dummy4.java +++ b/testng-core/src/test/java/test/reports/issue2069/Dummy4.java @@ -1,10 +1,10 @@ -package test.reports.issue2069; - -import org.testng.annotations.Test; - -public class Dummy4 { - - @Test - public void testngTest() { - } -} +package test.reports.issue2069; + +import org.testng.annotations.Test; + +public class Dummy4 { + + @Test + public void testngTest() { + } +} diff --git a/testng-core/src/test/java/test/retryAnalyzer/InvocationCountTest.java b/testng-core/src/test/java/test/retryAnalyzer/InvocationCountTest.java index 4ad33412bc..ab38d41e24 100644 --- a/testng-core/src/test/java/test/retryAnalyzer/InvocationCountTest.java +++ b/testng-core/src/test/java/test/retryAnalyzer/InvocationCountTest.java @@ -1,185 +1,185 @@ -package test.retryAnalyzer; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.fail; - -import java.util.concurrent.atomic.AtomicInteger; - -import org.testng.IRetryAnalyzer; -import org.testng.ITestResult; -import org.testng.TestNG; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; - -import com.google.common.collect.ConcurrentHashMultiset; -import com.google.common.collect.Multiset; - -/** - * retryAnalyzer parameter unit tests. - * @author tocman@gmail.com (Jeremie Lenfant-Engelmann) - * - */ -public final class InvocationCountTest implements IRetryAnalyzer { - static final Multiset invocations = ConcurrentHashMultiset.create(); - private static final AtomicInteger retriesRemaining = new AtomicInteger(100); - private static final int MAX_RETRY = 2; - - static int tcid1 = 0; - static int tcid2 = 0; - static int tcid3 = 0; - - private int r1 = 0; - private int r2 = 1; - private int r3 = 0; - private int r7 = 0; - private static int value = 42; - private int executionNumber = 0; - - @Test(retryAnalyzer = InvocationCountTest.class) - public void testAnnotationWithNoRetries() { - } - - @Test(retryAnalyzer = InvocationCountTest.class) - public void testAnnotationWithOneRetry() { - if (r1++ < 1) { - fail(); - } - } - - @Test(retryAnalyzer = InvocationCountTest.class) - public void testAnnotationWithSevenRetries() { - if (r7++ < 7) { - fail(); - } - } - - @Test(retryAnalyzer = ThreeRetries.class, successPercentage = 0) - public void failAfterThreeRetries() { - fail(); - } - - @Test(dependsOnMethods = { - "testAnnotationWithNoRetries", - "testAnnotationWithOneRetry", - "testAnnotationWithSevenRetries", - "failAfterThreeRetries" - }, alwaysRun = true) - public void checkInvocationCounts() { - assertEquals(invocations.count("testAnnotationWithNoRetries"), 0); - assertEquals(invocations.count("testAnnotationWithOneRetry"), 1); - assertEquals(invocations.count("testAnnotationWithSevenRetries"), 7); - assertEquals(invocations.count("failAfterThreeRetries"), 4); - } - - @Test (retryAnalyzer = InvocationCountTest.class, - dataProvider = "dataProvider3") - public void retryWithDataProvider(String tc) { - if ("tc1".equals(tc)) { - - if (tcid1++ < MAX_RETRY) { - fail(); - } - } - if ("tc2".equals(tc)) { - if (tcid2++ < MAX_RETRY) { - fail(); - } - } - if ("tc3".equals(tc)) { - if (tcid3++ < MAX_RETRY) { - fail(); - } - } - } - - - @Test ( - dependsOnMethods = { "retryWithDataProvider" }, alwaysRun = true) - public void checkRetryCounts() { - assertEquals(tcid1, 3); - assertEquals(tcid2, 3); - assertEquals(tcid3, 3); - } - - - - @DataProvider(name="dataProvider") - private Object[][] dataProvider() { - return new Object[][] { { 1, true }, { 2, false }, { 3, true }, - { 4, false } }; - } - - @DataProvider(name="dataProvider2") - private Object[][] dataProvider2() { - value = 42; - - return new Object[][] { { true }, { true } }; - } - - @DataProvider ( - name = "dataProvider3") - private Object[][] dataProvider3() { - return new Object[][] { {"tc1"}, {"tc2"}, {"tc3"} }; - } - - @Test(retryAnalyzer = InvocationCountTest.class, dataProvider = "dataProvider") - public void testAnnotationWithDataProvider(int paf, boolean test) { - executionNumber++; - if (paf == 2 && test == false) { - if (r2 >= 1) { - r2--; - fail(); - } - } - if (paf == 4){ - assertEquals(executionNumber,5); - } - } - - @Test(retryAnalyzer = InvocationCountTest.class, dataProvider = "dataProvider2") - public void testAnnotationWithDataProviderAndRecreateParameters(boolean dummy) { - if (r3 == 1) { - this.value = 0; - r3--; - fail(); - } else if (r3 == 0) { - assertEquals(this.value, 42); - } - } - - @Test - public void withFactory() { - TestNG tng = new TestNG(); - tng.setTestClasses(new Class[] { MyFactory.class}); - FactoryTest.m_count = 0; - - tng.run(); - - assertEquals(FactoryTest.m_count, 4); - } - - @Override - public boolean retry(ITestResult result) { - invocations.add(result.getName()); - return retriesRemaining.getAndDecrement() >= 0; - } - - public static class ThreeRetries implements IRetryAnalyzer { - private final AtomicInteger remainingRetries = new AtomicInteger(3); - - @Override - public boolean retry(ITestResult result) { - invocations.add(result.getName()); - return remainingRetries.getAndDecrement() > 0; - } - } - - public static class RetryCountTest implements IRetryAnalyzer { - private final AtomicInteger remainingRetries = new AtomicInteger(12); - - @Override - public boolean retry(ITestResult result) { - return remainingRetries.getAndDecrement() > 0; - } - } -} +package test.retryAnalyzer; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.fail; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.testng.IRetryAnalyzer; +import org.testng.ITestResult; +import org.testng.TestNG; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import com.google.common.collect.ConcurrentHashMultiset; +import com.google.common.collect.Multiset; + +/** + * retryAnalyzer parameter unit tests. + * @author tocman@gmail.com (Jeremie Lenfant-Engelmann) + * + */ +public final class InvocationCountTest implements IRetryAnalyzer { + static final Multiset invocations = ConcurrentHashMultiset.create(); + private static final AtomicInteger retriesRemaining = new AtomicInteger(100); + private static final int MAX_RETRY = 2; + + static int tcid1 = 0; + static int tcid2 = 0; + static int tcid3 = 0; + + private int r1 = 0; + private int r2 = 1; + private int r3 = 0; + private int r7 = 0; + private static int value = 42; + private int executionNumber = 0; + + @Test(retryAnalyzer = InvocationCountTest.class) + public void testAnnotationWithNoRetries() { + } + + @Test(retryAnalyzer = InvocationCountTest.class) + public void testAnnotationWithOneRetry() { + if (r1++ < 1) { + fail(); + } + } + + @Test(retryAnalyzer = InvocationCountTest.class) + public void testAnnotationWithSevenRetries() { + if (r7++ < 7) { + fail(); + } + } + + @Test(retryAnalyzer = ThreeRetries.class, successPercentage = 0) + public void failAfterThreeRetries() { + fail(); + } + + @Test(dependsOnMethods = { + "testAnnotationWithNoRetries", + "testAnnotationWithOneRetry", + "testAnnotationWithSevenRetries", + "failAfterThreeRetries" + }, alwaysRun = true) + public void checkInvocationCounts() { + assertEquals(invocations.count("testAnnotationWithNoRetries"), 0); + assertEquals(invocations.count("testAnnotationWithOneRetry"), 1); + assertEquals(invocations.count("testAnnotationWithSevenRetries"), 7); + assertEquals(invocations.count("failAfterThreeRetries"), 4); + } + + @Test (retryAnalyzer = InvocationCountTest.class, + dataProvider = "dataProvider3") + public void retryWithDataProvider(String tc) { + if ("tc1".equals(tc)) { + + if (tcid1++ < MAX_RETRY) { + fail(); + } + } + if ("tc2".equals(tc)) { + if (tcid2++ < MAX_RETRY) { + fail(); + } + } + if ("tc3".equals(tc)) { + if (tcid3++ < MAX_RETRY) { + fail(); + } + } + } + + + @Test ( + dependsOnMethods = { "retryWithDataProvider" }, alwaysRun = true) + public void checkRetryCounts() { + assertEquals(tcid1, 3); + assertEquals(tcid2, 3); + assertEquals(tcid3, 3); + } + + + + @DataProvider(name="dataProvider") + private Object[][] dataProvider() { + return new Object[][] { { 1, true }, { 2, false }, { 3, true }, + { 4, false } }; + } + + @DataProvider(name="dataProvider2") + private Object[][] dataProvider2() { + value = 42; + + return new Object[][] { { true }, { true } }; + } + + @DataProvider ( + name = "dataProvider3") + private Object[][] dataProvider3() { + return new Object[][] { {"tc1"}, {"tc2"}, {"tc3"} }; + } + + @Test(retryAnalyzer = InvocationCountTest.class, dataProvider = "dataProvider") + public void testAnnotationWithDataProvider(int paf, boolean test) { + executionNumber++; + if (paf == 2 && test == false) { + if (r2 >= 1) { + r2--; + fail(); + } + } + if (paf == 4){ + assertEquals(executionNumber,5); + } + } + + @Test(retryAnalyzer = InvocationCountTest.class, dataProvider = "dataProvider2") + public void testAnnotationWithDataProviderAndRecreateParameters(boolean dummy) { + if (r3 == 1) { + this.value = 0; + r3--; + fail(); + } else if (r3 == 0) { + assertEquals(this.value, 42); + } + } + + @Test + public void withFactory() { + TestNG tng = new TestNG(); + tng.setTestClasses(new Class[] { MyFactory.class}); + FactoryTest.m_count = 0; + + tng.run(); + + assertEquals(FactoryTest.m_count, 4); + } + + @Override + public boolean retry(ITestResult result) { + invocations.add(result.getName()); + return retriesRemaining.getAndDecrement() >= 0; + } + + public static class ThreeRetries implements IRetryAnalyzer { + private final AtomicInteger remainingRetries = new AtomicInteger(3); + + @Override + public boolean retry(ITestResult result) { + invocations.add(result.getName()); + return remainingRetries.getAndDecrement() > 0; + } + } + + public static class RetryCountTest implements IRetryAnalyzer { + private final AtomicInteger remainingRetries = new AtomicInteger(12); + + @Override + public boolean retry(ITestResult result) { + return remainingRetries.getAndDecrement() > 0; + } + } +} diff --git a/testng-core/src/test/java/test/retryAnalyzer/github1519/MyAnalyzer.java b/testng-core/src/test/java/test/retryAnalyzer/github1519/MyAnalyzer.java index b9f3224b98..ae6a5f1ef3 100755 --- a/testng-core/src/test/java/test/retryAnalyzer/github1519/MyAnalyzer.java +++ b/testng-core/src/test/java/test/retryAnalyzer/github1519/MyAnalyzer.java @@ -1,14 +1,14 @@ -package test.retryAnalyzer.github1519; - -import org.testng.IRetryAnalyzer; -import org.testng.ITestResult; - -public class MyAnalyzer implements IRetryAnalyzer { - - @Override - public boolean retry(ITestResult iTestResult) { - TestClassSample.messages.add("retry"); - TestClassSample.retry = true; - return true; - } -} +package test.retryAnalyzer.github1519; + +import org.testng.IRetryAnalyzer; +import org.testng.ITestResult; + +public class MyAnalyzer implements IRetryAnalyzer { + + @Override + public boolean retry(ITestResult iTestResult) { + TestClassSample.messages.add("retry"); + TestClassSample.retry = true; + return true; + } +} diff --git a/testng-core/src/test/java/test/retryAnalyzer/github1519/MyListener.java b/testng-core/src/test/java/test/retryAnalyzer/github1519/MyListener.java index 706cc5e2f9..51093efbbe 100755 --- a/testng-core/src/test/java/test/retryAnalyzer/github1519/MyListener.java +++ b/testng-core/src/test/java/test/retryAnalyzer/github1519/MyListener.java @@ -1,17 +1,17 @@ -package test.retryAnalyzer.github1519; - -import org.testng.IInvokedMethod; -import org.testng.IInvokedMethodListener; -import org.testng.ITestResult; - -public class MyListener implements IInvokedMethodListener { - @Override - public void beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) { - } - - @Override - public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) { - TestClassSample.messages.add("afterInvocation"); - } - -} +package test.retryAnalyzer.github1519; + +import org.testng.IInvokedMethod; +import org.testng.IInvokedMethodListener; +import org.testng.ITestResult; + +public class MyListener implements IInvokedMethodListener { + @Override + public void beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) { + } + + @Override + public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) { + TestClassSample.messages.add("afterInvocation"); + } + +} diff --git a/testng-core/src/test/java/test/retryAnalyzer/github1519/TestClassSample.java b/testng-core/src/test/java/test/retryAnalyzer/github1519/TestClassSample.java index a2fb68243b..ae363f4c92 100755 --- a/testng-core/src/test/java/test/retryAnalyzer/github1519/TestClassSample.java +++ b/testng-core/src/test/java/test/retryAnalyzer/github1519/TestClassSample.java @@ -1,17 +1,17 @@ -package test.retryAnalyzer.github1519; - -import org.testng.Assert; -import org.testng.annotations.Test; -import org.testng.collections.Lists; - -import java.util.List; - -public class TestClassSample { - static boolean retry = false; - public static List messages = Lists.newArrayList(); - - @Test(retryAnalyzer = MyAnalyzer.class) - public void testMethod() { - Assert.assertTrue(retry); - } +package test.retryAnalyzer.github1519; + +import org.testng.Assert; +import org.testng.annotations.Test; +import org.testng.collections.Lists; + +import java.util.List; + +public class TestClassSample { + static boolean retry = false; + public static List messages = Lists.newArrayList(); + + @Test(retryAnalyzer = MyAnalyzer.class) + public void testMethod() { + Assert.assertTrue(retry); + } } \ No newline at end of file diff --git a/testng-core/src/test/java/test/retryAnalyzer/github1600/Github1600Analyzer.java b/testng-core/src/test/java/test/retryAnalyzer/github1600/Github1600Analyzer.java index 0250ba4412..ad3940afff 100755 --- a/testng-core/src/test/java/test/retryAnalyzer/github1600/Github1600Analyzer.java +++ b/testng-core/src/test/java/test/retryAnalyzer/github1600/Github1600Analyzer.java @@ -1,25 +1,25 @@ -package test.retryAnalyzer.github1600; - -import org.testng.IRetryAnalyzer; -import org.testng.ITestResult; - -public class Github1600Analyzer implements IRetryAnalyzer { - - static final String RETRY = "RETRY"; - public static final String NO = "NO"; - static final String YES = "YES"; - private static int retryCount = 0; - private static final int MAX_RETRY_COUNT = 10; - - @Override - public boolean retry(ITestResult iTestResult) { - String attribute = (String) iTestResult.getAttribute(RETRY); - if (NO.equalsIgnoreCase(attribute)) { - return false; - } else if (YES.equalsIgnoreCase(attribute) || retryCount < MAX_RETRY_COUNT) { - retryCount++; - return true; - } - return false; - } -} +package test.retryAnalyzer.github1600; + +import org.testng.IRetryAnalyzer; +import org.testng.ITestResult; + +public class Github1600Analyzer implements IRetryAnalyzer { + + static final String RETRY = "RETRY"; + public static final String NO = "NO"; + static final String YES = "YES"; + private static int retryCount = 0; + private static final int MAX_RETRY_COUNT = 10; + + @Override + public boolean retry(ITestResult iTestResult) { + String attribute = (String) iTestResult.getAttribute(RETRY); + if (NO.equalsIgnoreCase(attribute)) { + return false; + } else if (YES.equalsIgnoreCase(attribute) || retryCount < MAX_RETRY_COUNT) { + retryCount++; + return true; + } + return false; + } +} diff --git a/testng-core/src/test/java/test/retryAnalyzer/github1600/Github1600Listener.java b/testng-core/src/test/java/test/retryAnalyzer/github1600/Github1600Listener.java index 5ff2862988..f724162bca 100755 --- a/testng-core/src/test/java/test/retryAnalyzer/github1600/Github1600Listener.java +++ b/testng-core/src/test/java/test/retryAnalyzer/github1600/Github1600Listener.java @@ -1,39 +1,39 @@ -package test.retryAnalyzer.github1600; - -import org.testng.IAnnotationTransformer; -import org.testng.IInvokedMethod; -import org.testng.IInvokedMethodListener; -import org.testng.IRetryAnalyzer; -import org.testng.ITestResult; -import org.testng.annotations.ITestAnnotation; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import org.testng.internal.annotations.DisabledRetryAnalyzer; - -public class Github1600Listener implements IInvokedMethodListener, IAnnotationTransformer { - @Override - public void beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) { - - } - - @Override - public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) { - if (iInvokedMethod.isTestMethod()) { - String attribute = Github1600Analyzer.NO; - if (iTestResult.getStatus() == ITestResult.SUCCESS) { - iTestResult.setStatus(ITestResult.FAILURE); - attribute = Github1600Analyzer.YES; - } - iTestResult.setAttribute(Github1600Analyzer.RETRY, attribute); - } - } - - @Override - public void transform(ITestAnnotation iTestAnnotation, Class aClass, Constructor constructor, Method method) { - Class retry = iTestAnnotation.getRetryAnalyzerClass(); - if (retry.equals(DisabledRetryAnalyzer.class)) { - iTestAnnotation.setRetryAnalyzer(Github1600Analyzer.class); - } - } -} +package test.retryAnalyzer.github1600; + +import org.testng.IAnnotationTransformer; +import org.testng.IInvokedMethod; +import org.testng.IInvokedMethodListener; +import org.testng.IRetryAnalyzer; +import org.testng.ITestResult; +import org.testng.annotations.ITestAnnotation; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import org.testng.internal.annotations.DisabledRetryAnalyzer; + +public class Github1600Listener implements IInvokedMethodListener, IAnnotationTransformer { + @Override + public void beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) { + + } + + @Override + public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) { + if (iInvokedMethod.isTestMethod()) { + String attribute = Github1600Analyzer.NO; + if (iTestResult.getStatus() == ITestResult.SUCCESS) { + iTestResult.setStatus(ITestResult.FAILURE); + attribute = Github1600Analyzer.YES; + } + iTestResult.setAttribute(Github1600Analyzer.RETRY, attribute); + } + } + + @Override + public void transform(ITestAnnotation iTestAnnotation, Class aClass, Constructor constructor, Method method) { + Class retry = iTestAnnotation.getRetryAnalyzerClass(); + if (retry.equals(DisabledRetryAnalyzer.class)) { + iTestAnnotation.setRetryAnalyzer(Github1600Analyzer.class); + } + } +} diff --git a/testng-core/src/test/java/test/retryAnalyzer/github1600/Github1600TestSample.java b/testng-core/src/test/java/test/retryAnalyzer/github1600/Github1600TestSample.java index f19aa802de..87a0917c91 100755 --- a/testng-core/src/test/java/test/retryAnalyzer/github1600/Github1600TestSample.java +++ b/testng-core/src/test/java/test/retryAnalyzer/github1600/Github1600TestSample.java @@ -1,15 +1,15 @@ -package test.retryAnalyzer.github1600; - -import org.testng.Assert; -import org.testng.annotations.Test; - -public class Github1600TestSample { - private static int a = 2; - - @Test - public void test1() { - Assert.assertEquals(a, 2); - a++; - } - +package test.retryAnalyzer.github1600; + +import org.testng.Assert; +import org.testng.annotations.Test; + +public class Github1600TestSample { + private static int a = 2; + + @Test + public void test1() { + Assert.assertEquals(a, 2); + a++; + } + } \ No newline at end of file diff --git a/testng-core/src/test/java/test/sample/ConverterSample3.java b/testng-core/src/test/java/test/sample/ConverterSample3.java index 29d629377b..770214ec07 100644 --- a/testng-core/src/test/java/test/sample/ConverterSample3.java +++ b/testng-core/src/test/java/test/sample/ConverterSample3.java @@ -1,94 +1,94 @@ -package test.sample; - -/* - * Created on 12-Sep-2006 by micheb10 - */ - -/** - * Sample file for the Javadoc annotations to Java 5 annotations converter for a non-default package - * @author micheb10 12-Sep-2006 - * @testng.test - */ -public class ConverterSample3 { - /** - * This comment line should be preserved - * @testng.before-suite alwaysRun = "true" - */ - public void beforeSuiteAlwaysRun() { - // We are just checking appropriate annotations are added so we don't care about body - } - - /** - * @testng.test - */ - public void plainTest() { - // Empty body - } - - /** - * @testng.test - * @testng.expected-exceptions - * value = "java.lang.NullPointerException java.lang.NumberFormatException" - */ - public void expectedExceptions() { - // Empty body - } - - /** - * @testng.test groups = "groupA groupB" - */ - public void testGroups() { - // Empty body - } - - /** - * @testng.after-method - */ - public void afterMethod() { - // Empty body - } - - /** - * This key should be preserved - * @author The author is a standard tag and should not be touched - * @testng.test groups = "groupA" alwaysRun=true - * dependsOnMethods = "expectedExceptions" timeOut="3000" - * @version another standard tag should not be changed - * @testng.expected-exceptions - * value = "java.lang.NullPointerException java.lang.NumberFormatException" - * @testng.parameters value="firstParameter secondParameter thirdParameter" - */ - public void testEverything() { - // Lots and lots of stuff - } - - /** - * @testng.data-provider name="test1" - */ - public Object[][] dataProvider() { - return null; - } - - /** - * @testng.factory - */ - public Object[] factory() { - return null; - } - - /** - * @testng.test - */ - public class TestInnerClass { - public void bareInnerMethod() { - // Empty body - } - - /** - * @testng.test - */ - public void testInnerMethod() { - // empty body - } - } -} +package test.sample; + +/* + * Created on 12-Sep-2006 by micheb10 + */ + +/** + * Sample file for the Javadoc annotations to Java 5 annotations converter for a non-default package + * @author micheb10 12-Sep-2006 + * @testng.test + */ +public class ConverterSample3 { + /** + * This comment line should be preserved + * @testng.before-suite alwaysRun = "true" + */ + public void beforeSuiteAlwaysRun() { + // We are just checking appropriate annotations are added so we don't care about body + } + + /** + * @testng.test + */ + public void plainTest() { + // Empty body + } + + /** + * @testng.test + * @testng.expected-exceptions + * value = "java.lang.NullPointerException java.lang.NumberFormatException" + */ + public void expectedExceptions() { + // Empty body + } + + /** + * @testng.test groups = "groupA groupB" + */ + public void testGroups() { + // Empty body + } + + /** + * @testng.after-method + */ + public void afterMethod() { + // Empty body + } + + /** + * This key should be preserved + * @author The author is a standard tag and should not be touched + * @testng.test groups = "groupA" alwaysRun=true + * dependsOnMethods = "expectedExceptions" timeOut="3000" + * @version another standard tag should not be changed + * @testng.expected-exceptions + * value = "java.lang.NullPointerException java.lang.NumberFormatException" + * @testng.parameters value="firstParameter secondParameter thirdParameter" + */ + public void testEverything() { + // Lots and lots of stuff + } + + /** + * @testng.data-provider name="test1" + */ + public Object[][] dataProvider() { + return null; + } + + /** + * @testng.factory + */ + public Object[] factory() { + return null; + } + + /** + * @testng.test + */ + public class TestInnerClass { + public void bareInnerMethod() { + // Empty body + } + + /** + * @testng.test + */ + public void testInnerMethod() { + // empty body + } + } +} diff --git a/testng-core/src/test/java/test/skipex/ConfigurationSkippedExceptionTest.java b/testng-core/src/test/java/test/skipex/ConfigurationSkippedExceptionTest.java index dd042088ae..aac6046b1f 100644 --- a/testng-core/src/test/java/test/skipex/ConfigurationSkippedExceptionTest.java +++ b/testng-core/src/test/java/test/skipex/ConfigurationSkippedExceptionTest.java @@ -1,20 +1,20 @@ -package test.skipex; - -import org.testng.SkipException; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - - -/** - * This class/interface - */ -public class ConfigurationSkippedExceptionTest { - @BeforeMethod - public void configurationLevelSkipException() { - throw new SkipException("some skip message"); - } - - @Test - public void dummyTest() { - } -} +package test.skipex; + +import org.testng.SkipException; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + + +/** + * This class/interface + */ +public class ConfigurationSkippedExceptionTest { + @BeforeMethod + public void configurationLevelSkipException() { + throw new SkipException("some skip message"); + } + + @Test + public void dummyTest() { + } +} diff --git a/testng-core/src/test/java/test/skipex/SkippedExceptionTest.java b/testng-core/src/test/java/test/skipex/SkippedExceptionTest.java index b0c9b9aae2..310eb994e7 100644 --- a/testng-core/src/test/java/test/skipex/SkippedExceptionTest.java +++ b/testng-core/src/test/java/test/skipex/SkippedExceptionTest.java @@ -1,51 +1,51 @@ -package test.skipex; - -import org.testng.Assert; -import org.testng.ITestNGListener; -import org.testng.ITestResult; -import org.testng.TestListenerAdapter; -import org.testng.TestNG; -import org.testng.annotations.Test; - -import java.util.List; - - -/** - * This class/interface - */ -public class SkippedExceptionTest { - @Test - public void skippedExceptionInConfigurationMethods() { - TestListenerAdapter listener= new TestListenerAdapter(); - TestNG test= new TestNG(false); - test.addListener((ITestNGListener)listener); - test.setTestClasses(new Class[] {ConfigurationSkippedExceptionTest.class}); - test.run(); - List confSkips= listener.getConfigurationSkips(); - List testSkips= listener.getSkippedTests(); - Assert.assertEquals(testSkips.size(), 1); - Assert.assertEquals(testSkips.get(0).getMethod().getMethodName(), "dummyTest"); - - Assert.assertEquals(confSkips.size(), 1); - Assert.assertEquals(confSkips.get(0).getMethod().getMethodName(), "configurationLevelSkipException"); - } - - - @Test - public void skippedExceptionInTestMethods() { - TestListenerAdapter listener= new TestListenerAdapter(); - TestNG test= new TestNG(false); - test.addListener(listener); - test.setTestClasses(new Class[] {TestSkippedExceptionTest.class}); - test.run(); - List skips= listener.getSkippedTests(); - List failures= listener.getFailedTests(); - List passed = listener.getPassedTests(); - Assert.assertEquals(skips.size(), 1); - Assert.assertEquals(failures.size(), 1); - Assert.assertEquals(passed.size(), 1); - Assert.assertEquals(skips.get(0).getMethod().getMethodName(), "genericSkipException"); - Assert.assertEquals(failures.get(0).getMethod().getMethodName(), "timedSkipException"); - Assert.assertEquals(passed.get(0).getMethod().getMethodName(), "genericExpectedSkipException"); - } -} +package test.skipex; + +import org.testng.Assert; +import org.testng.ITestNGListener; +import org.testng.ITestResult; +import org.testng.TestListenerAdapter; +import org.testng.TestNG; +import org.testng.annotations.Test; + +import java.util.List; + + +/** + * This class/interface + */ +public class SkippedExceptionTest { + @Test + public void skippedExceptionInConfigurationMethods() { + TestListenerAdapter listener= new TestListenerAdapter(); + TestNG test= new TestNG(false); + test.addListener((ITestNGListener)listener); + test.setTestClasses(new Class[] {ConfigurationSkippedExceptionTest.class}); + test.run(); + List confSkips= listener.getConfigurationSkips(); + List testSkips= listener.getSkippedTests(); + Assert.assertEquals(testSkips.size(), 1); + Assert.assertEquals(testSkips.get(0).getMethod().getMethodName(), "dummyTest"); + + Assert.assertEquals(confSkips.size(), 1); + Assert.assertEquals(confSkips.get(0).getMethod().getMethodName(), "configurationLevelSkipException"); + } + + + @Test + public void skippedExceptionInTestMethods() { + TestListenerAdapter listener= new TestListenerAdapter(); + TestNG test= new TestNG(false); + test.addListener(listener); + test.setTestClasses(new Class[] {TestSkippedExceptionTest.class}); + test.run(); + List skips= listener.getSkippedTests(); + List failures= listener.getFailedTests(); + List passed = listener.getPassedTests(); + Assert.assertEquals(skips.size(), 1); + Assert.assertEquals(failures.size(), 1); + Assert.assertEquals(passed.size(), 1); + Assert.assertEquals(skips.get(0).getMethod().getMethodName(), "genericSkipException"); + Assert.assertEquals(failures.get(0).getMethod().getMethodName(), "timedSkipException"); + Assert.assertEquals(passed.get(0).getMethod().getMethodName(), "genericExpectedSkipException"); + } +} diff --git a/testng-core/src/test/java/test/skipex/TestSkippedExceptionTest.java b/testng-core/src/test/java/test/skipex/TestSkippedExceptionTest.java index 13df2375d3..677347ae78 100644 --- a/testng-core/src/test/java/test/skipex/TestSkippedExceptionTest.java +++ b/testng-core/src/test/java/test/skipex/TestSkippedExceptionTest.java @@ -1,26 +1,26 @@ -package test.skipex; - -import org.testng.SkipException; -import org.testng.TimeBombSkipException; -import org.testng.annotations.Test; - - -/** - * This class/interface - */ -public class TestSkippedExceptionTest { - @Test - public void genericSkipException() { - throw new SkipException("genericSkipException is skipped for now"); - } - - @Test(expectedExceptions = SkipException.class) - public void genericExpectedSkipException() { - throw new SkipException("genericExpectedSkipException should not be skipped"); - } - - @Test - public void timedSkipException() { - throw new TimeBombSkipException("timedSkipException is time bombed", "2007/04/10"); - } -} +package test.skipex; + +import org.testng.SkipException; +import org.testng.TimeBombSkipException; +import org.testng.annotations.Test; + + +/** + * This class/interface + */ +public class TestSkippedExceptionTest { + @Test + public void genericSkipException() { + throw new SkipException("genericSkipException is skipped for now"); + } + + @Test(expectedExceptions = SkipException.class) + public void genericExpectedSkipException() { + throw new SkipException("genericExpectedSkipException should not be skipped"); + } + + @Test + public void timedSkipException() { + throw new TimeBombSkipException("timedSkipException is time bombed", "2007/04/10"); + } +} diff --git a/testng-core/src/test/java/test/testng106/FailingSuiteFixture.java b/testng-core/src/test/java/test/testng106/FailingSuiteFixture.java index 2144de0ad8..387a34abbb 100644 --- a/testng-core/src/test/java/test/testng106/FailingSuiteFixture.java +++ b/testng-core/src/test/java/test/testng106/FailingSuiteFixture.java @@ -1,16 +1,16 @@ -package test.testng106; - -import org.testng.annotations.BeforeSuite; - - -/** - * TESTNG-106: failing @BeforeSuite doesn't skip all tests - */ -public class FailingSuiteFixture { - static int s_invocations = 0; - - @BeforeSuite - public void failingBeforeSuite() { - throw new RuntimeException(); - } -} +package test.testng106; + +import org.testng.annotations.BeforeSuite; + + +/** + * TESTNG-106: failing @BeforeSuite doesn't skip all tests + */ +public class FailingSuiteFixture { + static int s_invocations = 0; + + @BeforeSuite + public void failingBeforeSuite() { + throw new RuntimeException(); + } +} diff --git a/testng-core/src/test/java/test/testng106/Test1.java b/testng-core/src/test/java/test/testng106/Test1.java index daa5cf499a..c053d7ee11 100644 --- a/testng-core/src/test/java/test/testng106/Test1.java +++ b/testng-core/src/test/java/test/testng106/Test1.java @@ -1,15 +1,15 @@ -package test.testng106; - -import org.testng.annotations.Test; - - -/** - * This class/interface - */ -public class Test1 { - @Test - public void method1() { - System.out.println("method1"); - FailingSuiteFixture.s_invocations++; - } -} +package test.testng106; + +import org.testng.annotations.Test; + + +/** + * This class/interface + */ +public class Test1 { + @Test + public void method1() { + System.out.println("method1"); + FailingSuiteFixture.s_invocations++; + } +} diff --git a/testng-core/src/test/java/test/testng106/Test2.java b/testng-core/src/test/java/test/testng106/Test2.java index 0d5ba0f199..ebd6eefebb 100644 --- a/testng-core/src/test/java/test/testng106/Test2.java +++ b/testng-core/src/test/java/test/testng106/Test2.java @@ -1,21 +1,21 @@ -package test.testng106; - -import org.testng.annotations.Test; - - -/** - * This class/interface - */ -public class Test2 { - @Test - public void method2() { - System.out.println("method2"); - FailingSuiteFixture.s_invocations++; - } - - @Test - public void method3() { - System.out.println("method3"); - FailingSuiteFixture.s_invocations++; - } -} +package test.testng106; + +import org.testng.annotations.Test; + + +/** + * This class/interface + */ +public class Test2 { + @Test + public void method2() { + System.out.println("method2"); + FailingSuiteFixture.s_invocations++; + } + + @Test + public void method3() { + System.out.println("method3"); + FailingSuiteFixture.s_invocations++; + } +} diff --git a/testng-core/src/test/java/test/testng109/SkippedTestWithExpectedExceptionTest.java b/testng-core/src/test/java/test/testng109/SkippedTestWithExpectedExceptionTest.java index d757a26045..ccee40d68b 100644 --- a/testng-core/src/test/java/test/testng109/SkippedTestWithExpectedExceptionTest.java +++ b/testng-core/src/test/java/test/testng109/SkippedTestWithExpectedExceptionTest.java @@ -1,27 +1,27 @@ -package test.testng109; - -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - - -/** - * This class/interface - */ -public class SkippedTestWithExpectedExceptionTest { - @BeforeClass - public void setup() { - throw new RuntimeException("test-exception"); - } - - @Test - public void test1() - { -// empty - } - - @Test(expectedExceptions={OutOfMemoryError.class}) - public void test2() - { -// empty - } -} +package test.testng109; + +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + + +/** + * This class/interface + */ +public class SkippedTestWithExpectedExceptionTest { + @BeforeClass + public void setup() { + throw new RuntimeException("test-exception"); + } + + @Test + public void test1() + { +// empty + } + + @Test(expectedExceptions={OutOfMemoryError.class}) + public void test2() + { +// empty + } +} diff --git a/testng-core/src/test/java/test/testng173/ClassA.java b/testng-core/src/test/java/test/testng173/ClassA.java index 1a7091a61d..b1009b0ad7 100644 --- a/testng-core/src/test/java/test/testng173/ClassA.java +++ b/testng-core/src/test/java/test/testng173/ClassA.java @@ -1,15 +1,15 @@ -package test.testng173; - -import org.testng.annotations.Test; - -public class ClassA { - - @Test - public void test1() { - } - - @Test(dependsOnMethods = "test1") - public void test2() { - } - -} +package test.testng173; + +import org.testng.annotations.Test; + +public class ClassA { + + @Test + public void test1() { + } + + @Test(dependsOnMethods = "test1") + public void test2() { + } + +} diff --git a/testng-core/src/test/java/test/testng173/ClassB.java b/testng-core/src/test/java/test/testng173/ClassB.java index 7adb5abdfb..63245eb47b 100644 --- a/testng-core/src/test/java/test/testng173/ClassB.java +++ b/testng-core/src/test/java/test/testng173/ClassB.java @@ -1,18 +1,18 @@ -package test.testng173; - -import org.testng.annotations.*; - -public class ClassB { - @Test - public void testX() { - } - - @Test(dependsOnMethods = "testX") - public void test2() { - } - - @Test(dependsOnMethods = "test2") - public void test1() { - } - +package test.testng173; + +import org.testng.annotations.*; + +public class ClassB { + @Test + public void testX() { + } + + @Test(dependsOnMethods = "testX") + public void test2() { + } + + @Test(dependsOnMethods = "test2") + public void test1() { + } + } \ No newline at end of file diff --git a/testng-core/src/test/java/test/testng173/anotherpackage/ClassC.java b/testng-core/src/test/java/test/testng173/anotherpackage/ClassC.java index ee51d1f755..efcba901ca 100644 --- a/testng-core/src/test/java/test/testng173/anotherpackage/ClassC.java +++ b/testng-core/src/test/java/test/testng173/anotherpackage/ClassC.java @@ -1,22 +1,22 @@ -package test.testng173.anotherpackage; - -import org.testng.annotations.Test; - -public class ClassC { - - @Test - public void testX() { - - } - - @Test(dependsOnMethods = "testX") - public void test2() { - - } - - @Test(dependsOnMethods = "test2") - public void test1() { - - } - +package test.testng173.anotherpackage; + +import org.testng.annotations.Test; + +public class ClassC { + + @Test + public void testX() { + + } + + @Test(dependsOnMethods = "testX") + public void test2() { + + } + + @Test(dependsOnMethods = "test2") + public void test1() { + + } + } \ No newline at end of file diff --git a/testng-core/src/test/java/test/testng37/NullParameterTest.java b/testng-core/src/test/java/test/testng37/NullParameterTest.java index 7900b871f6..67c772ad6c 100644 --- a/testng-core/src/test/java/test/testng37/NullParameterTest.java +++ b/testng-core/src/test/java/test/testng37/NullParameterTest.java @@ -1,18 +1,18 @@ -package test.testng37; - -import org.testng.Assert; -import org.testng.annotations.Parameters; -import org.testng.annotations.Test; - - -/** - * This class/interface - */ -public class NullParameterTest { - @Test - @Parameters({"notnull", "nullvalue"}) - public void nullParameter(String notNull, int mustBeNull) { - Assert.assertNotNull(notNull, "not null parameter expected"); - Assert.assertNull(mustBeNull, "null parameter expected"); - } -} +package test.testng37; + +import org.testng.Assert; +import org.testng.annotations.Parameters; +import org.testng.annotations.Test; + + +/** + * This class/interface + */ +public class NullParameterTest { + @Test + @Parameters({"notnull", "nullvalue"}) + public void nullParameter(String notNull, int mustBeNull) { + Assert.assertNotNull(notNull, "not null parameter expected"); + Assert.assertNull(mustBeNull, "null parameter expected"); + } +} diff --git a/testng-core/src/test/java/test/testng56/ParallelTest.java b/testng-core/src/test/java/test/testng56/ParallelTest.java index cba0947a19..a5cc4707e3 100644 --- a/testng-core/src/test/java/test/testng56/ParallelTest.java +++ b/testng-core/src/test/java/test/testng56/ParallelTest.java @@ -1,46 +1,46 @@ -package test.testng56; - -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - - -/** - * This class/interface - */ -public class ParallelTest { - @BeforeClass - public void setup() { - System.out.println(Thread.currentThread().getId() + ":setup"); - } - - @AfterClass - public void teardown() { - System.out.println(Thread.currentThread().getId() + ":teardown"); - } - - @Test - public void test1() { - System.out.println(Thread.currentThread().getId() + ":test1"); - } - - @Test(dependsOnMethods = {"test1"}) - public void test2() { - System.out.println(Thread.currentThread().getId() + ":test2"); - } - - @Test - public void test3() { - System.out.println(Thread.currentThread().getId() + ":test3"); - } - - @Test(dependsOnMethods = {"test3"}) - public void test4() { - System.out.println(Thread.currentThread().getId() + ":test4"); - } - - @Test - public void test5() { - System.out.println(Thread.currentThread().getId() + ":test5"); - } -} +package test.testng56; + +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + + +/** + * This class/interface + */ +public class ParallelTest { + @BeforeClass + public void setup() { + System.out.println(Thread.currentThread().getId() + ":setup"); + } + + @AfterClass + public void teardown() { + System.out.println(Thread.currentThread().getId() + ":teardown"); + } + + @Test + public void test1() { + System.out.println(Thread.currentThread().getId() + ":test1"); + } + + @Test(dependsOnMethods = {"test1"}) + public void test2() { + System.out.println(Thread.currentThread().getId() + ":test2"); + } + + @Test + public void test3() { + System.out.println(Thread.currentThread().getId() + ":test3"); + } + + @Test(dependsOnMethods = {"test3"}) + public void test4() { + System.out.println(Thread.currentThread().getId() + ":test4"); + } + + @Test + public void test5() { + System.out.println(Thread.currentThread().getId() + ":test5"); + } +} diff --git a/testng-core/src/test/java/test/testng59/Test1.java b/testng-core/src/test/java/test/testng59/Test1.java index 21ad93bba6..3fc7c13dfc 100644 --- a/testng-core/src/test/java/test/testng59/Test1.java +++ b/testng-core/src/test/java/test/testng59/Test1.java @@ -1,23 +1,23 @@ -package test.testng59; - -import org.testng.Assert; -import org.testng.annotations.AfterClass; -import org.testng.annotations.Test; - - -/** - * This class/interface - */ -public class Test1 { - private boolean m_run= false; - - @Test - public void test1() { - m_run= true; - } - - @AfterClass - public void checkWasRun() { - Assert.assertTrue(m_run, "test1() should have been run according to testng-59.xml"); - } -} +package test.testng59; + +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.Test; + + +/** + * This class/interface + */ +public class Test1 { + private boolean m_run= false; + + @Test + public void test1() { + m_run= true; + } + + @AfterClass + public void checkWasRun() { + Assert.assertTrue(m_run, "test1() should have been run according to testng-59.xml"); + } +} diff --git a/testng-core/src/test/java/test/testng59/Test2.java b/testng-core/src/test/java/test/testng59/Test2.java index f718951be7..4e3433c7ef 100644 --- a/testng-core/src/test/java/test/testng59/Test2.java +++ b/testng-core/src/test/java/test/testng59/Test2.java @@ -1,23 +1,23 @@ -package test.testng59; - -import org.testng.Assert; -import org.testng.annotations.AfterClass; -import org.testng.annotations.Test; - - -/** - * This class/interface - */ -public class Test2 { - private boolean m_run= false; - - @Test - public void test2() { - m_run= true; - } - - @AfterClass - public void checkWasRun() { - Assert.assertTrue(m_run, "test2() should have been run according to testng-59.xml"); - } -} +package test.testng59; + +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.Test; + + +/** + * This class/interface + */ +public class Test2 { + private boolean m_run= false; + + @Test + public void test2() { + m_run= true; + } + + @AfterClass + public void checkWasRun() { + Assert.assertTrue(m_run, "test2() should have been run according to testng-59.xml"); + } +} diff --git a/testng-core/src/test/java/test/testng93/SingleTestTest.java b/testng-core/src/test/java/test/testng93/SingleTestTest.java index 46d03d05dc..9cebcd5e09 100644 --- a/testng-core/src/test/java/test/testng93/SingleTestTest.java +++ b/testng-core/src/test/java/test/testng93/SingleTestTest.java @@ -1,25 +1,25 @@ -package test.testng93; - -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - - -/** - * This class/interface - */ -public class SingleTestTest { - @BeforeMethod(groups={"group1"}) - public void shouldRunBefore() { - System.out.println("Runs before"); - } - - @Test(groups={"group1"}) - public void theFirstActualTest() { - System.out.println("The first actual test"); - } - - @Test - public void theSecondActualTest() { - System.out.println("The second actual test"); - } -} +package test.testng93; + +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + + +/** + * This class/interface + */ +public class SingleTestTest { + @BeforeMethod(groups={"group1"}) + public void shouldRunBefore() { + System.out.println("Runs before"); + } + + @Test(groups={"group1"}) + public void theFirstActualTest() { + System.out.println("The first actual test"); + } + + @Test + public void theSecondActualTest() { + System.out.println("The second actual test"); + } +} diff --git a/testng-core/src/test/java/test/testng93/testng-93.xml b/testng-core/src/test/java/test/testng93/testng-93.xml index 1bcb8eddd9..5ab8a63fe2 100644 --- a/testng-core/src/test/java/test/testng93/testng-93.xml +++ b/testng-core/src/test/java/test/testng93/testng-93.xml @@ -1,18 +1,18 @@ - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testng-core/src/test/java/test/thread/B.java b/testng-core/src/test/java/test/thread/B.java index 489419f8d8..856a14978b 100644 --- a/testng-core/src/test/java/test/thread/B.java +++ b/testng-core/src/test/java/test/thread/B.java @@ -1,26 +1,26 @@ -package test.thread; - -import java.util.HashMap; -import java.util.Map; - -import org.testng.annotations.Test; -import org.testng.collections.Maps; - -public class B { - public static Map m_threadIds = Maps.newConcurrentMap(); - - public static void setUp() { - m_threadIds = Maps.newConcurrentMap(); - } - - @Test - public void f2() { - Long id = Thread.currentThread().getId(); - m_threadIds.put(id, id); - } - - private static void ppp(String s) { - System.out.println("[FactoryTest] " + s); - } - -} +package test.thread; + +import java.util.HashMap; +import java.util.Map; + +import org.testng.annotations.Test; +import org.testng.collections.Maps; + +public class B { + public static Map m_threadIds = Maps.newConcurrentMap(); + + public static void setUp() { + m_threadIds = Maps.newConcurrentMap(); + } + + @Test + public void f2() { + Long id = Thread.currentThread().getId(); + m_threadIds.put(id, id); + } + + private static void ppp(String s) { + System.out.println("[FactoryTest] " + s); + } + +} diff --git a/testng-core/src/test/java/test/thread/BaseSequentialSample.java b/testng-core/src/test/java/test/thread/BaseSequentialSample.java index 53bdb14d74..4a24b3e68e 100644 --- a/testng-core/src/test/java/test/thread/BaseSequentialSample.java +++ b/testng-core/src/test/java/test/thread/BaseSequentialSample.java @@ -1,26 +1,26 @@ -package test.thread; - -import java.util.Map; - -public class BaseSequentialSample { - - protected void addId(String method, long id) { - ppp(method + " ID:" + id); - getMap().put(id, id); - } - - Map getMap() { - Map result = Helper.getMap(getClass().getName()); - ppp("RETURNING MAP " + result + " THIS:" + this); - - return result; - } - - protected void ppp(String s) { - if (false) { - System.out.println("[" + getClass().getName() + " " - + Thread.currentThread().getId() + " " - + "] " + s); - } - } -} +package test.thread; + +import java.util.Map; + +public class BaseSequentialSample { + + protected void addId(String method, long id) { + ppp(method + " ID:" + id); + getMap().put(id, id); + } + + Map getMap() { + Map result = Helper.getMap(getClass().getName()); + ppp("RETURNING MAP " + result + " THIS:" + this); + + return result; + } + + protected void ppp(String s) { + if (false) { + System.out.println("[" + getClass().getName() + " " + + Thread.currentThread().getId() + " " + + "] " + s); + } + } +} diff --git a/testng-core/src/test/java/test/thread/Helper.java b/testng-core/src/test/java/test/thread/Helper.java index c5616c69ee..c4b20fb169 100644 --- a/testng-core/src/test/java/test/thread/Helper.java +++ b/testng-core/src/test/java/test/thread/Helper.java @@ -1,16 +1,16 @@ -package test.thread; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -public class Helper { - private static final Map> m_maps = new ConcurrentHashMap<>(); - - public static Map getMap(String className) { - return m_maps.computeIfAbsent(className, cn -> new ConcurrentHashMap<>()); - } - - public static void reset() { - m_maps.clear(); - } -} +package test.thread; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class Helper { + private static final Map> m_maps = new ConcurrentHashMap<>(); + + public static Map getMap(String className) { + return m_maps.computeIfAbsent(className, cn -> new ConcurrentHashMap<>()); + } + + public static void reset() { + m_maps.clear(); + } +} diff --git a/testng-core/src/test/java/test/thread/SequentialSample2Test.java b/testng-core/src/test/java/test/thread/SequentialSample2Test.java index 69c3cd6dfb..e86e020a7c 100644 --- a/testng-core/src/test/java/test/thread/SequentialSample2Test.java +++ b/testng-core/src/test/java/test/thread/SequentialSample2Test.java @@ -1,20 +1,20 @@ -package test.thread; - -import org.testng.annotations.Test; - -@Test(singleThreaded = true) -public class SequentialSample2Test extends BaseSequentialSample { - - public void f1() { - addId("SequentialSample2Test.f1()", Thread.currentThread().getId()); - } - - public void f2() { - addId("SequentialSample2Test.f2()", Thread.currentThread().getId()); - } - - public void f3() { - addId("SequentialSample2Test.f3()", Thread.currentThread().getId()); - } - -} +package test.thread; + +import org.testng.annotations.Test; + +@Test(singleThreaded = true) +public class SequentialSample2Test extends BaseSequentialSample { + + public void f1() { + addId("SequentialSample2Test.f1()", Thread.currentThread().getId()); + } + + public void f2() { + addId("SequentialSample2Test.f2()", Thread.currentThread().getId()); + } + + public void f3() { + addId("SequentialSample2Test.f3()", Thread.currentThread().getId()); + } + +} diff --git a/testng-core/src/test/java/test/thread/SequentialSample3Test.java b/testng-core/src/test/java/test/thread/SequentialSample3Test.java index 8fc676bbaa..ec4c149ead 100644 --- a/testng-core/src/test/java/test/thread/SequentialSample3Test.java +++ b/testng-core/src/test/java/test/thread/SequentialSample3Test.java @@ -1,20 +1,20 @@ -package test.thread; - -import org.testng.annotations.Test; - -@Test(singleThreaded = true) -public class SequentialSample3Test extends BaseSequentialSample { - - public void f1() { - addId("SequentialSample3Test.f1()", Thread.currentThread().getId()); - } - - public void f2() { - addId("SequentialSample3Test.f2()", Thread.currentThread().getId()); - } - - public void f3() { - addId("SequentialSample3Test.f3()", Thread.currentThread().getId()); - } - -} +package test.thread; + +import org.testng.annotations.Test; + +@Test(singleThreaded = true) +public class SequentialSample3Test extends BaseSequentialSample { + + public void f1() { + addId("SequentialSample3Test.f1()", Thread.currentThread().getId()); + } + + public void f2() { + addId("SequentialSample3Test.f2()", Thread.currentThread().getId()); + } + + public void f3() { + addId("SequentialSample3Test.f3()", Thread.currentThread().getId()); + } + +} diff --git a/testng-core/src/test/java/test/thread/SequentialSampleTest.java b/testng-core/src/test/java/test/thread/SequentialSampleTest.java index ef31e0f54b..8ae230166e 100644 --- a/testng-core/src/test/java/test/thread/SequentialSampleTest.java +++ b/testng-core/src/test/java/test/thread/SequentialSampleTest.java @@ -1,20 +1,20 @@ -package test.thread; - -import org.testng.annotations.Test; - -@Test(singleThreaded = true) -public class SequentialSampleTest extends BaseSequentialSample { - - public void f1() { - addId("SequentialSampleTest.f1()", Thread.currentThread().getId()); - } - - public void f2() { - addId("SequentialSampleTest.f2()", Thread.currentThread().getId()); - } - - public void f3() { - addId("SequentialSampleTest.f3()", Thread.currentThread().getId()); - } - -} +package test.thread; + +import org.testng.annotations.Test; + +@Test(singleThreaded = true) +public class SequentialSampleTest extends BaseSequentialSample { + + public void f1() { + addId("SequentialSampleTest.f1()", Thread.currentThread().getId()); + } + + public void f2() { + addId("SequentialSampleTest.f2()", Thread.currentThread().getId()); + } + + public void f3() { + addId("SequentialSampleTest.f3()", Thread.currentThread().getId()); + } + +} diff --git a/testng-core/src/test/java/test/thread/SequentialTest.java b/testng-core/src/test/java/test/thread/SequentialTest.java index 52e59a29b4..3bd1e2408b 100644 --- a/testng-core/src/test/java/test/thread/SequentialTest.java +++ b/testng-core/src/test/java/test/thread/SequentialTest.java @@ -1,101 +1,101 @@ -package test.thread; - -import org.testng.Assert; -import org.testng.annotations.Test; -import org.testng.xml.XmlSuite; - -import test.BaseTest; - -import java.util.HashMap; -import java.util.Map; - -public class SequentialTest extends BaseTest { - - @Test - public void verifySequential1() { - verifySequential(1); - } - - @Test - public void verifySequential2() { - verifySequential(2); - } - - @Test - public void verifySequential3() { - verifySequential(3); - } - - @Test - public void verifySingleThreaded1() { - verifySingleThreaded(1); - } - - @Test - public void verifySingleThreaded2() { - verifySingleThreaded(2); - } - - @Test - public void verifySingleThreaded3() { - verifySingleThreaded(3); - } - - public void verifySequential(int threadCount) { - runTest(threadCount, - SequentialSampleTest.class.getName(), - SequentialSample2Test.class.getName(), - SequentialSample3Test.class.getName()); - } - - public void verifySingleThreaded(int threadCount) { - runTest(threadCount, - SingleThreadedSampleTest.class.getName(), - SingleThreadedSample2Test.class.getName(), - SingleThreadedSample3Test.class.getName()); - } - - private void runTest(int threadCount, String... classes) { - Helper.reset(); - - for (String c : classes) { - addClass(c); - } - setParallel(XmlSuite.ParallelMode.METHODS); - setThreadCount(threadCount); - - run(); - - Map[] maps = new Map[] { - Helper.getMap(classes[0]), - Helper.getMap(classes[1]), - Helper.getMap(classes[2]), - }; - - for(Map m : maps) { - Assert.assertEquals(m.size(), 1); - } - - long[] ids = new long[] { - maps[0].keySet().iterator().next(), - maps[1].keySet().iterator().next(), - maps[2].keySet().iterator().next(), - }; - Map verifyMap = new HashMap<>(); - - for (long id : ids) { - verifyMap.put(id, id); - } - - Assert.assertEquals(verifyMap.size(), threadCount); - - ppp("COUNT:" + threadCount + " THREAD ID'S:" + ids[0] + " " + ids[1] + " " + ids[2]); - } - - private static void ppp(String s) { - if (false) { - System.out.println("[SequentialTest] " + s); - } - } - -} +package test.thread; + +import org.testng.Assert; +import org.testng.annotations.Test; +import org.testng.xml.XmlSuite; + +import test.BaseTest; + +import java.util.HashMap; +import java.util.Map; + +public class SequentialTest extends BaseTest { + + @Test + public void verifySequential1() { + verifySequential(1); + } + + @Test + public void verifySequential2() { + verifySequential(2); + } + + @Test + public void verifySequential3() { + verifySequential(3); + } + + @Test + public void verifySingleThreaded1() { + verifySingleThreaded(1); + } + + @Test + public void verifySingleThreaded2() { + verifySingleThreaded(2); + } + + @Test + public void verifySingleThreaded3() { + verifySingleThreaded(3); + } + + public void verifySequential(int threadCount) { + runTest(threadCount, + SequentialSampleTest.class.getName(), + SequentialSample2Test.class.getName(), + SequentialSample3Test.class.getName()); + } + + public void verifySingleThreaded(int threadCount) { + runTest(threadCount, + SingleThreadedSampleTest.class.getName(), + SingleThreadedSample2Test.class.getName(), + SingleThreadedSample3Test.class.getName()); + } + + private void runTest(int threadCount, String... classes) { + Helper.reset(); + + for (String c : classes) { + addClass(c); + } + setParallel(XmlSuite.ParallelMode.METHODS); + setThreadCount(threadCount); + + run(); + + Map[] maps = new Map[] { + Helper.getMap(classes[0]), + Helper.getMap(classes[1]), + Helper.getMap(classes[2]), + }; + + for(Map m : maps) { + Assert.assertEquals(m.size(), 1); + } + + long[] ids = new long[] { + maps[0].keySet().iterator().next(), + maps[1].keySet().iterator().next(), + maps[2].keySet().iterator().next(), + }; + Map verifyMap = new HashMap<>(); + + for (long id : ids) { + verifyMap.put(id, id); + } + + Assert.assertEquals(verifyMap.size(), threadCount); + + ppp("COUNT:" + threadCount + " THREAD ID'S:" + ids[0] + " " + ids[1] + " " + ids[2]); + } + + private static void ppp(String s) { + if (false) { + System.out.println("[SequentialTest] " + s); + } + } + +} diff --git a/testng-core/src/test/java/test/thread/SingleThreadedSample2Test.java b/testng-core/src/test/java/test/thread/SingleThreadedSample2Test.java index 37931b2fa0..3c82c7061b 100644 --- a/testng-core/src/test/java/test/thread/SingleThreadedSample2Test.java +++ b/testng-core/src/test/java/test/thread/SingleThreadedSample2Test.java @@ -1,23 +1,23 @@ -package test.thread; - -import org.testng.annotations.Test; - -@Test(singleThreaded = true) -public class SingleThreadedSample2Test extends BaseSequentialSample { - - @Test - public void f1() { - addId("SingleThreadedSample2Test.f1()", Thread.currentThread().getId()); - } - - @Test - public void f2() { - addId("SingleThreadedSample2Test.f2()", Thread.currentThread().getId()); - } - - @Test - public void f3() { - addId("SingleThreadedSample2Test.f3()", Thread.currentThread().getId()); - } - -} +package test.thread; + +import org.testng.annotations.Test; + +@Test(singleThreaded = true) +public class SingleThreadedSample2Test extends BaseSequentialSample { + + @Test + public void f1() { + addId("SingleThreadedSample2Test.f1()", Thread.currentThread().getId()); + } + + @Test + public void f2() { + addId("SingleThreadedSample2Test.f2()", Thread.currentThread().getId()); + } + + @Test + public void f3() { + addId("SingleThreadedSample2Test.f3()", Thread.currentThread().getId()); + } + +} diff --git a/testng-core/src/test/java/test/thread/SingleThreadedSample3Test.java b/testng-core/src/test/java/test/thread/SingleThreadedSample3Test.java index a2aa50f4ff..3f771c22e5 100644 --- a/testng-core/src/test/java/test/thread/SingleThreadedSample3Test.java +++ b/testng-core/src/test/java/test/thread/SingleThreadedSample3Test.java @@ -1,23 +1,23 @@ -package test.thread; - -import org.testng.annotations.Test; - -@Test(singleThreaded = true) -public class SingleThreadedSample3Test extends BaseSequentialSample { - - @Test - public void f1() { - addId("SingleThreadedSample3Test.f1()", Thread.currentThread().getId()); - } - - @Test // (dependsOnMethods = "f1") - public void f2() { - addId("SingleThreadedSample3Test.f2()", Thread.currentThread().getId()); - } - - @Test // (dependsOnMethods = "f2") - public void f3() { - addId("SingleThreadedSample3Test.f3()", Thread.currentThread().getId()); - } - -} +package test.thread; + +import org.testng.annotations.Test; + +@Test(singleThreaded = true) +public class SingleThreadedSample3Test extends BaseSequentialSample { + + @Test + public void f1() { + addId("SingleThreadedSample3Test.f1()", Thread.currentThread().getId()); + } + + @Test // (dependsOnMethods = "f1") + public void f2() { + addId("SingleThreadedSample3Test.f2()", Thread.currentThread().getId()); + } + + @Test // (dependsOnMethods = "f2") + public void f3() { + addId("SingleThreadedSample3Test.f3()", Thread.currentThread().getId()); + } + +} diff --git a/testng-core/src/test/java/test/thread/SingleThreadedSampleTest.java b/testng-core/src/test/java/test/thread/SingleThreadedSampleTest.java index 440da44c94..2c89b43f12 100644 --- a/testng-core/src/test/java/test/thread/SingleThreadedSampleTest.java +++ b/testng-core/src/test/java/test/thread/SingleThreadedSampleTest.java @@ -1,23 +1,23 @@ -package test.thread; - -import org.testng.annotations.Test; - -@Test(singleThreaded = true) -public class SingleThreadedSampleTest extends BaseSequentialSample { - - @Test - public void f1() { - addId("SingleThreadedSampleTest.f1()", Thread.currentThread().getId()); - } - - @Test - public void f2() { - addId("SingleThreadedSampleTest.f2()", Thread.currentThread().getId()); - } - - @Test - public void f3() { - addId("SingleThreadedSampleTest.f3()", Thread.currentThread().getId()); - } - -} +package test.thread; + +import org.testng.annotations.Test; + +@Test(singleThreaded = true) +public class SingleThreadedSampleTest extends BaseSequentialSample { + + @Test + public void f1() { + addId("SingleThreadedSampleTest.f1()", Thread.currentThread().getId()); + } + + @Test + public void f2() { + addId("SingleThreadedSampleTest.f2()", Thread.currentThread().getId()); + } + + @Test + public void f3() { + addId("SingleThreadedSampleTest.f3()", Thread.currentThread().getId()); + } + +} diff --git a/testng-core/src/test/java/test/thread/SuiteThreadCountTest.java b/testng-core/src/test/java/test/thread/SuiteThreadCountTest.java index 416bfc001e..e0ce13fe92 100644 --- a/testng-core/src/test/java/test/thread/SuiteThreadCountTest.java +++ b/testng-core/src/test/java/test/thread/SuiteThreadCountTest.java @@ -1,34 +1,34 @@ -package test.thread; - -import org.testng.Assert; -import org.testng.annotations.AfterClass; -import org.testng.annotations.Test; -import org.testng.internal.thread.ThreadUtil; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; - -/** - * Test for test level thread-count. - * - * @author Alex Popescu - */ -public class SuiteThreadCountTest { - private Set m_threads= Collections.synchronizedSet(new HashSet()); - - @Test - public void test1() { - m_threads.add(ThreadUtil.currentThreadInfo()); - } - - @Test - public void test2() { - m_threads.add(ThreadUtil.currentThreadInfo()); - } - - @AfterClass - public void checkThreading() { - Assert.assertEquals(m_threads.size(), 2, "Test should use 2 threads (suite level)"); - } -} +package test.thread; + +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.Test; +import org.testng.internal.thread.ThreadUtil; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +/** + * Test for test level thread-count. + * + * @author Alex Popescu + */ +public class SuiteThreadCountTest { + private Set m_threads= Collections.synchronizedSet(new HashSet()); + + @Test + public void test1() { + m_threads.add(ThreadUtil.currentThreadInfo()); + } + + @Test + public void test2() { + m_threads.add(ThreadUtil.currentThreadInfo()); + } + + @AfterClass + public void checkThreading() { + Assert.assertEquals(m_threads.size(), 2, "Test should use 2 threads (suite level)"); + } +} diff --git a/testng-core/src/test/java/test/thread/TestThreadCountTest.java b/testng-core/src/test/java/test/thread/TestThreadCountTest.java index 3f64c9977e..bdfac61af9 100644 --- a/testng-core/src/test/java/test/thread/TestThreadCountTest.java +++ b/testng-core/src/test/java/test/thread/TestThreadCountTest.java @@ -1,39 +1,39 @@ -package test.thread; - -import org.testng.Assert; -import org.testng.annotations.AfterClass; -import org.testng.annotations.Test; -import org.testng.internal.thread.ThreadUtil; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; - -/** - * Test for test level thread-count. - * - * @author Alex Popescu - */ -public class TestThreadCountTest { - private Set m_threads= Collections.synchronizedSet(new HashSet()); - - @Test - public void test1() { - m_threads.add(ThreadUtil.currentThreadInfo()); - } - - @Test - public void test2() { - m_threads.add(ThreadUtil.currentThreadInfo()); - } - - @Test - public void test3() { - m_threads.add(ThreadUtil.currentThreadInfo()); - } - - @AfterClass - public void checkThreading() { - Assert.assertEquals(m_threads.size(), 3, "Test should use 3 threads"); - } -} +package test.thread; + +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.Test; +import org.testng.internal.thread.ThreadUtil; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +/** + * Test for test level thread-count. + * + * @author Alex Popescu + */ +public class TestThreadCountTest { + private Set m_threads= Collections.synchronizedSet(new HashSet()); + + @Test + public void test1() { + m_threads.add(ThreadUtil.currentThreadInfo()); + } + + @Test + public void test2() { + m_threads.add(ThreadUtil.currentThreadInfo()); + } + + @Test + public void test3() { + m_threads.add(ThreadUtil.currentThreadInfo()); + } + + @AfterClass + public void checkThreading() { + Assert.assertEquals(m_threads.size(), 3, "Test should use 3 threads"); + } +} diff --git a/testng-core/src/test/java/test/thread/testng.xml b/testng-core/src/test/java/test/thread/testng.xml index b33b4ddc16..e9ef53ff6b 100644 --- a/testng-core/src/test/java/test/thread/testng.xml +++ b/testng-core/src/test/java/test/thread/testng.xml @@ -1,14 +1,14 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + \ No newline at end of file diff --git a/testng-core/src/test/resources/testng-ant.xml b/testng-core/src/test/resources/testng-ant.xml index 315c37e3a9..291dc4d668 100644 --- a/testng-core/src/test/resources/testng-ant.xml +++ b/testng-core/src/test/resources/testng-ant.xml @@ -1,10 +1,10 @@ - - - - - - - - - - + + + + + + + + + + diff --git a/testng-runner-api/src/main/java/org/testng/internal/TestResult.java b/testng-runner-api/src/main/java/org/testng/internal/TestResult.java index c70fa9f141..2902fb8859 100644 --- a/testng-runner-api/src/main/java/org/testng/internal/TestResult.java +++ b/testng-runner-api/src/main/java/org/testng/internal/TestResult.java @@ -1,468 +1,468 @@ -package org.testng.internal; - -import java.util.Arrays; -import java.util.Collections; -import java.util.UUID; -import java.util.stream.Collectors; -import org.testng.IAttributes; -import org.testng.IClass; -import org.testng.ITest; -import org.testng.ITestContext; -import org.testng.ITestNGMethod; -import org.testng.ITestResult; -import org.testng.Reporter; -import org.testng.TestNGException; -import org.testng.collections.Lists; -import org.testng.collections.Objects; - -import javax.annotation.Nonnull; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.List; -import java.util.Set; - -/** This class represents the result of a test. */ -public class TestResult implements ITestResult { - - private ITestNGMethod m_method = null; - private List skippedDueTo = Lists.newArrayList(); - private boolean skipAnalysed = false; - private int m_status = CREATED; - private Throwable m_throwable = null; - private long m_startMillis = 0; - private long m_endMillis = 0; - private String m_name = null; - private String m_host; - private Object[] m_parameters = {}; - private String m_instanceName; - private ITestContext m_context; - private int parameterIndex; - private boolean m_wasRetried; - private final IAttributes m_attributes = new Attributes(); - private final String id = UUID.randomUUID().toString(); - - private TestResult() { - //defeat instantiation. We have factory methods. - } - - public static TestResult newEmptyTestResult() { - return new TestResult(); - } - - public static TestResult newTestResultFor(ITestNGMethod method) { - return newContextAwareTestResult(method, null); - } - - public static TestResult newContextAwareTestResult(ITestNGMethod method, ITestContext ctx) { - TestResult result = newEmptyTestResult(); - long time = System.currentTimeMillis(); - result.init(method, ctx, null, time, 0L); - return result; - } - - public static TestResult newTestResultWithCauseAs(ITestNGMethod method, ITestContext ctx, - Throwable t) { - TestResult result = newEmptyTestResult(); - long time = System.currentTimeMillis(); - result.init(method, ctx, t, time, time); - return result; - } - - public static TestResult newEndTimeAwareTestResult(ITestNGMethod method, ITestContext ctx, - Throwable t, long start) { - TestResult result = newEmptyTestResult(); - long time = System.currentTimeMillis(); - result.init(method, ctx, t, start, time); - return result; - } - - public static TestResult newTestResultFrom(TestResult result, ITestNGMethod method, - ITestContext ctx, long start) { - TestResult testResult = newEmptyTestResult(); - testResult.setHost(result.getHost()); - testResult.setParameters(result.getParameters()); - testResult.setParameterIndex(result.getParameterIndex()); - testResult.init(method, ctx, null, start, 0L); - TestResult.copyAttributes(result, testResult); - return testResult; - } - - private void init(ITestNGMethod method, ITestContext ctx, Throwable t, long start, long end) { - m_throwable = t; - m_instanceName = method.getTestClass().getName(); - if (null == m_throwable) { - m_status = ITestResult.SUCCESS; - } - m_startMillis = start; - m_endMillis = end; - if (RuntimeBehavior.isMemoryFriendlyMode()) { - m_method = new LiteWeightTestNGMethod(method); - } else { - m_method = method; - } - m_context = ctx; - - Object instance = method.getInstance(); - - // Calculate the name: either the method name, ITest#getTestName or - // toString() if it's been overridden. - if (instance == null) { - m_name = m_method.getMethodName(); - return; - } - if (instance instanceof ITest) { - m_name = ((ITest) instance).getTestName(); - if (m_name != null) { - return; - } - m_name = m_method.getMethodName(); - if (Utils.getVerbose() > 1) { - String msg = String.format( - "Warning: [%s] implementation on class [%s] returned null. Defaulting to method name", - ITest.class.getName(), instance.getClass().getName()); - System.err.println(msg); - } - return; - } - if (method.getTestClass().getTestName() != null) { - m_name = method.getTestClass().getTestName(); - return; - } - String string = instance.toString(); - // Only display toString() if it's been overridden by the user - m_name = getMethod().getMethodName(); - try { - if (!Object.class - .getMethod("toString") - .equals(instance.getClass().getMethod("toString"))) { - m_instanceName = - string.startsWith("class ") ? string.substring("class ".length()) : string; - m_name = m_name + " on " + m_instanceName; - } - } catch (NoSuchMethodException ignore) { - // ignore - } - } - - @Override - public void setEndMillis(long millis) { - m_endMillis = millis; - } - - /** - * If this result's related instance implements ITest or use @Test(testName=...), returns its test - * name, otherwise returns null. - */ - @Override - public String getTestName() { - if (this.m_method == null) { - return null; - } - Object instance = this.m_method.getInstance(); - if (instance instanceof ITest) { - return ((ITest) instance).getTestName(); - } - if (m_method.getTestClass().getTestName() != null) { - return m_method.getTestClass().getTestName(); - } - return null; - } - - @Override - public String getName() { - return m_name; - } - - /** @return Returns the method. */ - @Override - public ITestNGMethod getMethod() { - return m_method; - } - - /** @param method The method to set. */ - public void setMethod(ITestNGMethod method) { - m_method = method; - } - - /** @return Returns the status. */ - @Override - public int getStatus() { - return m_status; - } - - /** @param status The status to set. */ - @Override - public void setStatus(int status) { - m_status = status; - } - - @Override - public boolean isSuccess() { - return ITestResult.SUCCESS == m_status; - } - - /** @return Returns the testClass. */ - @Override - public IClass getTestClass() { - return m_method.getTestClass(); - } - - /** @return Returns the throwable. */ - @Override - public Throwable getThrowable() { - return m_throwable; - } - - /** @param throwable The throwable to set. */ - @Override - public void setThrowable(Throwable throwable) { - m_throwable = throwable; - } - - /** @return Returns the endMillis. */ - @Override - public long getEndMillis() { - return m_endMillis; - } - - /** @return Returns the startMillis. */ - @Override - public long getStartMillis() { - return m_startMillis; - } - - @Override - public String toString() { - List output = Reporter.getOutput(this); - return Objects.toStringHelper(getClass()) - .omitNulls() - .omitEmptyStrings() - .add("name", getName()) - .add("status", toString(m_status)) - .add("method", m_method) - .add("output", !output.isEmpty() ? output.get(0) : null) - .toString(); - } - - private static String toString(int status) { - switch (status) { - case SUCCESS: - return "SUCCESS"; - case FAILURE: - return "FAILURE"; - case SKIP: - return "SKIP"; - case SUCCESS_PERCENTAGE_FAILURE: - return "SUCCESS WITHIN PERCENTAGE"; - case STARTED: - return "STARTED"; - case CREATED: - return "CREATED"; - default: - throw new TestNGException("Encountered an un-defined test status of [" + status + "]."); - } - } - - @Override - public String getHost() { - return m_host; - } - - public void setHost(String host) { - m_host = host; - } - - @Override - public Object[] getParameters() { - return m_parameters; - } - - @Override - public void setParameters(Object[] parameters) { - m_parameters = new Object[parameters.length]; - for (int i = 0; i < parameters.length; i++) { - // Copy parameter if possible because user may change it later - if (parameters[i] instanceof Cloneable) { - try { - Method clone = parameters[i].getClass().getDeclaredMethod("clone"); - m_parameters[i] = clone.invoke(parameters[i]); - } catch (NoSuchMethodException - | InvocationTargetException - | IllegalAccessException - | SecurityException e) { - m_parameters[i] = parameters[i]; - } - } else { - m_parameters[i] = parameters[i]; - } - } - } - - @Override - public Object getInstance() { - return IParameterInfo.embeddedInstance(this.m_method.getInstance()); - } - - @Override - public Object[] getFactoryParameters() { - IParameterInfo instance = this.m_method.getFactoryMethodParamsInfo(); - if (instance != null) { - return instance.getParameters(); - } - return new Object[0]; - } - - @Override - public Object getAttribute(String name) { - return m_attributes.getAttribute(name); - } - - @Override - public void setAttribute(String name, Object value) { - m_attributes.setAttribute(name, value); - } - - @Override - public Set getAttributeNames() { - return m_attributes.getAttributeNames(); - } - - @Override - public Object removeAttribute(String name) { - return m_attributes.removeAttribute(name); - } - - @Override - public ITestContext getTestContext() { - return m_context; - } - - public void setContext(ITestContext context) { - m_context = context; - } - - @Override - public int compareTo(@Nonnull ITestResult comparison) { - return Long.compare(getStartMillis(), comparison.getStartMillis()); - } - - @Override - public String getInstanceName() { - return m_instanceName; - } - - @Override - public void setTestName(String name) { - m_name = name; - } - - public void setParameterIndex(int parameterIndex) { - this.parameterIndex = parameterIndex; - } - - public int getParameterIndex() { - return parameterIndex; - } - - public boolean wasRetried() { - return m_wasRetried; - } - - public void setWasRetried(boolean wasRetried) { - this.m_wasRetried = wasRetried; - } - - public List getSkipCausedBy() { - if (this.m_status != SKIP || skipAnalysed) { - return Collections.unmodifiableList(skippedDueTo); - } - skipAnalysed = true; - //check if there were any config failures - Set skippedConfigs = m_context.getFailedConfigurations().getAllResults(); - for (ITestResult skippedConfig : skippedConfigs) { - if (isGlobalFailure(skippedConfig) || isRelated(skippedConfig)) { - //If there's a failure in @BeforeTest/@BeforeSuite/@BeforeClass - //then the reason is most often just one method. - skippedDueTo.add(skippedConfig.getMethod()); - } - if (belongToSameGroup(skippedConfig)) { - //If its @BeforeGroups then there's a chance that there could be more than one - //method. So lets add everything. - skippedDueTo.add(skippedConfig.getMethod()); - } - } - if (!skippedDueTo.isEmpty()) { - //If we found atleast one skipped due to reason, then its time to return back. - return Collections.unmodifiableList(skippedDueTo); - } - //Looks like we didnt have any configuration failures. So some upstream method perhaps failed. - if (m_method.getMethodsDependedUpon().length==0) { - //Maybe group dependencies exist ? - if (m_method.getGroupsDependedUpon().length==0) { - return Collections.emptyList(); - } - List upstreamGroups = Arrays.asList(m_method.getGroupsDependedUpon()); - List allFailures = Lists.merge(m_context.getFailedTests().getAllResults(), - m_context.getFailedButWithinSuccessPercentageTests().getAllResults()); - skippedDueTo = allFailures.stream() - .map(ITestResult::getMethod) - .filter(method -> { - List currentMethodGroups = Arrays.asList(method.getGroups()); - List interection = Lists.intersection(upstreamGroups, currentMethodGroups); - return !interection.isEmpty(); - }).collect(Collectors.toList()); - - return Collections.unmodifiableList(skippedDueTo); - } - List upstreamMethods = Arrays.asList(m_method.getMethodsDependedUpon()); - - //So we have dependsOnMethod failures - List allfailures = Lists.merge(m_context.getFailedTests().getAllResults(), - m_context.getFailedButWithinSuccessPercentageTests().getAllResults()); - skippedDueTo = allfailures.stream() - .map(ITestResult::getMethod) - .filter(method -> upstreamMethods.contains(method.getQualifiedName())) - .collect(Collectors.toList()); - return Collections.unmodifiableList(skippedDueTo); - } - - public String id() { - return id; - } - - private static boolean isGlobalFailure(ITestResult result) { - ITestNGMethod m = result.getMethod(); - return m.isBeforeTestConfiguration() || m.isBeforeSuiteConfiguration(); - } - - private boolean isRelated(ITestResult result) { - ITestNGMethod m = result.getMethod(); - if (!m.isBeforeClassConfiguration() && !m.isBeforeMethodConfiguration()) { - return false; - } - Object current = this.getInstance(); - Object thatObject = result.getInstance(); - return current.getClass().isAssignableFrom(thatObject.getClass()) || - thatObject.getClass().isAssignableFrom(current.getClass()); - } - - private boolean belongToSameGroup(ITestResult result) { - ITestNGMethod m = result.getMethod(); - if (!m.isBeforeGroupsConfiguration()) { - return false; - } - String[] mygroups = this.m_method.getGroups(); - if (mygroups.length == 0 || m.getGroups().length ==0) { - return false; - } - - List cfgMethodGroups = Arrays.asList(m.getGroups()); - return Arrays.stream(mygroups).anyMatch(cfgMethodGroups::contains); - } - - public static void copyAttributes(ITestResult source, ITestResult target) { - source - .getAttributeNames() - .forEach(name -> target.setAttribute(name, source.getAttribute(name))); - } -} +package org.testng.internal; + +import java.util.Arrays; +import java.util.Collections; +import java.util.UUID; +import java.util.stream.Collectors; +import org.testng.IAttributes; +import org.testng.IClass; +import org.testng.ITest; +import org.testng.ITestContext; +import org.testng.ITestNGMethod; +import org.testng.ITestResult; +import org.testng.Reporter; +import org.testng.TestNGException; +import org.testng.collections.Lists; +import org.testng.collections.Objects; + +import javax.annotation.Nonnull; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.List; +import java.util.Set; + +/** This class represents the result of a test. */ +public class TestResult implements ITestResult { + + private ITestNGMethod m_method = null; + private List skippedDueTo = Lists.newArrayList(); + private boolean skipAnalysed = false; + private int m_status = CREATED; + private Throwable m_throwable = null; + private long m_startMillis = 0; + private long m_endMillis = 0; + private String m_name = null; + private String m_host; + private Object[] m_parameters = {}; + private String m_instanceName; + private ITestContext m_context; + private int parameterIndex; + private boolean m_wasRetried; + private final IAttributes m_attributes = new Attributes(); + private final String id = UUID.randomUUID().toString(); + + private TestResult() { + //defeat instantiation. We have factory methods. + } + + public static TestResult newEmptyTestResult() { + return new TestResult(); + } + + public static TestResult newTestResultFor(ITestNGMethod method) { + return newContextAwareTestResult(method, null); + } + + public static TestResult newContextAwareTestResult(ITestNGMethod method, ITestContext ctx) { + TestResult result = newEmptyTestResult(); + long time = System.currentTimeMillis(); + result.init(method, ctx, null, time, 0L); + return result; + } + + public static TestResult newTestResultWithCauseAs(ITestNGMethod method, ITestContext ctx, + Throwable t) { + TestResult result = newEmptyTestResult(); + long time = System.currentTimeMillis(); + result.init(method, ctx, t, time, time); + return result; + } + + public static TestResult newEndTimeAwareTestResult(ITestNGMethod method, ITestContext ctx, + Throwable t, long start) { + TestResult result = newEmptyTestResult(); + long time = System.currentTimeMillis(); + result.init(method, ctx, t, start, time); + return result; + } + + public static TestResult newTestResultFrom(TestResult result, ITestNGMethod method, + ITestContext ctx, long start) { + TestResult testResult = newEmptyTestResult(); + testResult.setHost(result.getHost()); + testResult.setParameters(result.getParameters()); + testResult.setParameterIndex(result.getParameterIndex()); + testResult.init(method, ctx, null, start, 0L); + TestResult.copyAttributes(result, testResult); + return testResult; + } + + private void init(ITestNGMethod method, ITestContext ctx, Throwable t, long start, long end) { + m_throwable = t; + m_instanceName = method.getTestClass().getName(); + if (null == m_throwable) { + m_status = ITestResult.SUCCESS; + } + m_startMillis = start; + m_endMillis = end; + if (RuntimeBehavior.isMemoryFriendlyMode()) { + m_method = new LiteWeightTestNGMethod(method); + } else { + m_method = method; + } + m_context = ctx; + + Object instance = method.getInstance(); + + // Calculate the name: either the method name, ITest#getTestName or + // toString() if it's been overridden. + if (instance == null) { + m_name = m_method.getMethodName(); + return; + } + if (instance instanceof ITest) { + m_name = ((ITest) instance).getTestName(); + if (m_name != null) { + return; + } + m_name = m_method.getMethodName(); + if (Utils.getVerbose() > 1) { + String msg = String.format( + "Warning: [%s] implementation on class [%s] returned null. Defaulting to method name", + ITest.class.getName(), instance.getClass().getName()); + System.err.println(msg); + } + return; + } + if (method.getTestClass().getTestName() != null) { + m_name = method.getTestClass().getTestName(); + return; + } + String string = instance.toString(); + // Only display toString() if it's been overridden by the user + m_name = getMethod().getMethodName(); + try { + if (!Object.class + .getMethod("toString") + .equals(instance.getClass().getMethod("toString"))) { + m_instanceName = + string.startsWith("class ") ? string.substring("class ".length()) : string; + m_name = m_name + " on " + m_instanceName; + } + } catch (NoSuchMethodException ignore) { + // ignore + } + } + + @Override + public void setEndMillis(long millis) { + m_endMillis = millis; + } + + /** + * If this result's related instance implements ITest or use @Test(testName=...), returns its test + * name, otherwise returns null. + */ + @Override + public String getTestName() { + if (this.m_method == null) { + return null; + } + Object instance = this.m_method.getInstance(); + if (instance instanceof ITest) { + return ((ITest) instance).getTestName(); + } + if (m_method.getTestClass().getTestName() != null) { + return m_method.getTestClass().getTestName(); + } + return null; + } + + @Override + public String getName() { + return m_name; + } + + /** @return Returns the method. */ + @Override + public ITestNGMethod getMethod() { + return m_method; + } + + /** @param method The method to set. */ + public void setMethod(ITestNGMethod method) { + m_method = method; + } + + /** @return Returns the status. */ + @Override + public int getStatus() { + return m_status; + } + + /** @param status The status to set. */ + @Override + public void setStatus(int status) { + m_status = status; + } + + @Override + public boolean isSuccess() { + return ITestResult.SUCCESS == m_status; + } + + /** @return Returns the testClass. */ + @Override + public IClass getTestClass() { + return m_method.getTestClass(); + } + + /** @return Returns the throwable. */ + @Override + public Throwable getThrowable() { + return m_throwable; + } + + /** @param throwable The throwable to set. */ + @Override + public void setThrowable(Throwable throwable) { + m_throwable = throwable; + } + + /** @return Returns the endMillis. */ + @Override + public long getEndMillis() { + return m_endMillis; + } + + /** @return Returns the startMillis. */ + @Override + public long getStartMillis() { + return m_startMillis; + } + + @Override + public String toString() { + List output = Reporter.getOutput(this); + return Objects.toStringHelper(getClass()) + .omitNulls() + .omitEmptyStrings() + .add("name", getName()) + .add("status", toString(m_status)) + .add("method", m_method) + .add("output", !output.isEmpty() ? output.get(0) : null) + .toString(); + } + + private static String toString(int status) { + switch (status) { + case SUCCESS: + return "SUCCESS"; + case FAILURE: + return "FAILURE"; + case SKIP: + return "SKIP"; + case SUCCESS_PERCENTAGE_FAILURE: + return "SUCCESS WITHIN PERCENTAGE"; + case STARTED: + return "STARTED"; + case CREATED: + return "CREATED"; + default: + throw new TestNGException("Encountered an un-defined test status of [" + status + "]."); + } + } + + @Override + public String getHost() { + return m_host; + } + + public void setHost(String host) { + m_host = host; + } + + @Override + public Object[] getParameters() { + return m_parameters; + } + + @Override + public void setParameters(Object[] parameters) { + m_parameters = new Object[parameters.length]; + for (int i = 0; i < parameters.length; i++) { + // Copy parameter if possible because user may change it later + if (parameters[i] instanceof Cloneable) { + try { + Method clone = parameters[i].getClass().getDeclaredMethod("clone"); + m_parameters[i] = clone.invoke(parameters[i]); + } catch (NoSuchMethodException + | InvocationTargetException + | IllegalAccessException + | SecurityException e) { + m_parameters[i] = parameters[i]; + } + } else { + m_parameters[i] = parameters[i]; + } + } + } + + @Override + public Object getInstance() { + return IParameterInfo.embeddedInstance(this.m_method.getInstance()); + } + + @Override + public Object[] getFactoryParameters() { + IParameterInfo instance = this.m_method.getFactoryMethodParamsInfo(); + if (instance != null) { + return instance.getParameters(); + } + return new Object[0]; + } + + @Override + public Object getAttribute(String name) { + return m_attributes.getAttribute(name); + } + + @Override + public void setAttribute(String name, Object value) { + m_attributes.setAttribute(name, value); + } + + @Override + public Set getAttributeNames() { + return m_attributes.getAttributeNames(); + } + + @Override + public Object removeAttribute(String name) { + return m_attributes.removeAttribute(name); + } + + @Override + public ITestContext getTestContext() { + return m_context; + } + + public void setContext(ITestContext context) { + m_context = context; + } + + @Override + public int compareTo(@Nonnull ITestResult comparison) { + return Long.compare(getStartMillis(), comparison.getStartMillis()); + } + + @Override + public String getInstanceName() { + return m_instanceName; + } + + @Override + public void setTestName(String name) { + m_name = name; + } + + public void setParameterIndex(int parameterIndex) { + this.parameterIndex = parameterIndex; + } + + public int getParameterIndex() { + return parameterIndex; + } + + public boolean wasRetried() { + return m_wasRetried; + } + + public void setWasRetried(boolean wasRetried) { + this.m_wasRetried = wasRetried; + } + + public List getSkipCausedBy() { + if (this.m_status != SKIP || skipAnalysed) { + return Collections.unmodifiableList(skippedDueTo); + } + skipAnalysed = true; + //check if there were any config failures + Set skippedConfigs = m_context.getFailedConfigurations().getAllResults(); + for (ITestResult skippedConfig : skippedConfigs) { + if (isGlobalFailure(skippedConfig) || isRelated(skippedConfig)) { + //If there's a failure in @BeforeTest/@BeforeSuite/@BeforeClass + //then the reason is most often just one method. + skippedDueTo.add(skippedConfig.getMethod()); + } + if (belongToSameGroup(skippedConfig)) { + //If its @BeforeGroups then there's a chance that there could be more than one + //method. So lets add everything. + skippedDueTo.add(skippedConfig.getMethod()); + } + } + if (!skippedDueTo.isEmpty()) { + //If we found atleast one skipped due to reason, then its time to return back. + return Collections.unmodifiableList(skippedDueTo); + } + //Looks like we didnt have any configuration failures. So some upstream method perhaps failed. + if (m_method.getMethodsDependedUpon().length==0) { + //Maybe group dependencies exist ? + if (m_method.getGroupsDependedUpon().length==0) { + return Collections.emptyList(); + } + List upstreamGroups = Arrays.asList(m_method.getGroupsDependedUpon()); + List allFailures = Lists.merge(m_context.getFailedTests().getAllResults(), + m_context.getFailedButWithinSuccessPercentageTests().getAllResults()); + skippedDueTo = allFailures.stream() + .map(ITestResult::getMethod) + .filter(method -> { + List currentMethodGroups = Arrays.asList(method.getGroups()); + List interection = Lists.intersection(upstreamGroups, currentMethodGroups); + return !interection.isEmpty(); + }).collect(Collectors.toList()); + + return Collections.unmodifiableList(skippedDueTo); + } + List upstreamMethods = Arrays.asList(m_method.getMethodsDependedUpon()); + + //So we have dependsOnMethod failures + List allfailures = Lists.merge(m_context.getFailedTests().getAllResults(), + m_context.getFailedButWithinSuccessPercentageTests().getAllResults()); + skippedDueTo = allfailures.stream() + .map(ITestResult::getMethod) + .filter(method -> upstreamMethods.contains(method.getQualifiedName())) + .collect(Collectors.toList()); + return Collections.unmodifiableList(skippedDueTo); + } + + public String id() { + return id; + } + + private static boolean isGlobalFailure(ITestResult result) { + ITestNGMethod m = result.getMethod(); + return m.isBeforeTestConfiguration() || m.isBeforeSuiteConfiguration(); + } + + private boolean isRelated(ITestResult result) { + ITestNGMethod m = result.getMethod(); + if (!m.isBeforeClassConfiguration() && !m.isBeforeMethodConfiguration()) { + return false; + } + Object current = this.getInstance(); + Object thatObject = result.getInstance(); + return current.getClass().isAssignableFrom(thatObject.getClass()) || + thatObject.getClass().isAssignableFrom(current.getClass()); + } + + private boolean belongToSameGroup(ITestResult result) { + ITestNGMethod m = result.getMethod(); + if (!m.isBeforeGroupsConfiguration()) { + return false; + } + String[] mygroups = this.m_method.getGroups(); + if (mygroups.length == 0 || m.getGroups().length ==0) { + return false; + } + + List cfgMethodGroups = Arrays.asList(m.getGroups()); + return Arrays.stream(mygroups).anyMatch(cfgMethodGroups::contains); + } + + public static void copyAttributes(ITestResult source, ITestResult target) { + source + .getAttributeNames() + .forEach(name -> target.setAttribute(name, source.getAttribute(name))); + } +}