From c5a851d626cf5069fe06c9dceac174f238892dbb Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 6 Nov 2018 10:15:26 +0000 Subject: [PATCH] [test-support] Add @DisableOnWindows annotation for TestNG tests --- parent/pom.xml | 1 + .../brooklyn/test/DisableOnWindows.java | 40 +++++++++++++ .../test/DisableOnWindowsListener.java | 57 +++++++++++++++++++ .../services/org.testng.ITestNGListener | 1 + .../brooklyn/test/DisableOnWindowsTest.java | 41 +++++++++++++ 5 files changed, 140 insertions(+) create mode 100644 test-support/src/main/java/org/apache/brooklyn/test/DisableOnWindows.java create mode 100644 test-support/src/main/java/org/apache/brooklyn/test/DisableOnWindowsListener.java create mode 100644 test-support/src/main/resources/META-INF/services/org.testng.ITestNGListener create mode 100644 test-support/src/test/java/org/apache/brooklyn/test/DisableOnWindowsTest.java diff --git a/parent/pom.xml b/parent/pom.xml index d6feccfc23..971c76ebaa 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -1110,6 +1110,7 @@ **/src/main/license/** **/src/test/license/** **/MANIFEST.MF + **/META-INF/services/** **/test-output/** **/*.pem.pub **/*.pem diff --git a/test-support/src/main/java/org/apache/brooklyn/test/DisableOnWindows.java b/test-support/src/main/java/org/apache/brooklyn/test/DisableOnWindows.java new file mode 100644 index 0000000000..9fa2998cdd --- /dev/null +++ b/test-support/src/main/java/org/apache/brooklyn/test/DisableOnWindows.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.test; + +import java.lang.annotation.*; + +/** + * Used to indicate that a test should ne be executed on Windows. + * + *

You must add the following to the class where this annotation is being used: + * {@code @Listeners(DisableOnWindows)}

+ */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +@Documented +public @interface DisableOnWindows { + + /**3 + * Explain the reason for the test being disabled. + * + *

e.g. "Needs an ssh server listening on port 22 on localhost."

+ */ + String reason(); +} diff --git a/test-support/src/main/java/org/apache/brooklyn/test/DisableOnWindowsListener.java b/test-support/src/main/java/org/apache/brooklyn/test/DisableOnWindowsListener.java new file mode 100644 index 0000000000..743edde9bd --- /dev/null +++ b/test-support/src/main/java/org/apache/brooklyn/test/DisableOnWindowsListener.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.test; + +import org.apache.brooklyn.util.os.Os; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.IAnnotationTransformer; +import org.testng.annotations.ITestAnnotation; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +/** + * Scans all tests annotated with {@link DisableOnWindows} and, on Windows, sets {@code enabled=false} if the current + * environment is Windows.. + */ +public class DisableOnWindowsListener implements IAnnotationTransformer { + + private static final Logger LOG = LoggerFactory.getLogger(DisableOnWindowsListener.class); + + @Override + public void transform( + final ITestAnnotation annotation, + final Class testClass, + final Constructor testConstructor, + final Method testMethod + ) { + if (testMethod != null ) { + final DisableOnWindows disableOnWindows = testMethod.getAnnotation(DisableOnWindows.class); + if (disableOnWindows != null && Os.isMicrosoftWindows()) { + annotation.setEnabled(false); + LOG.info(String.format("Disabled: %s.%s - %s", + testMethod.getDeclaringClass().getName(), + testMethod.getName(), + disableOnWindows.reason())); + } + } + } + +} diff --git a/test-support/src/main/resources/META-INF/services/org.testng.ITestNGListener b/test-support/src/main/resources/META-INF/services/org.testng.ITestNGListener new file mode 100644 index 0000000000..584861a390 --- /dev/null +++ b/test-support/src/main/resources/META-INF/services/org.testng.ITestNGListener @@ -0,0 +1 @@ +org.apache.brooklyn.test.DisableOnWindowsListener \ No newline at end of file diff --git a/test-support/src/test/java/org/apache/brooklyn/test/DisableOnWindowsTest.java b/test-support/src/test/java/org/apache/brooklyn/test/DisableOnWindowsTest.java new file mode 100644 index 0000000000..9929f666ef --- /dev/null +++ b/test-support/src/test/java/org/apache/brooklyn/test/DisableOnWindowsTest.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.test; + +import org.apache.brooklyn.util.os.Os; +import org.testng.annotations.Test; + +import static org.apache.brooklyn.test.Asserts.assertTrue; +import static org.apache.brooklyn.test.Asserts.fail; + +public class DisableOnWindowsTest { + + @Test + public void alwaysRun() { + assertTrue(true); + } + + @Test + @DisableOnWindows(reason = "unit test") + public void isDisabledOnWindows() { + if (Os.isMicrosoftWindows()) { + fail("Test should have been disabled on windows"); + } + } +}