[test-support] Add @DisableOnWindows annotation for TestNG tests

Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/c5a851d6
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/c5a851d6
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/c5a851d6

Branch: refs/heads/master
Commit: c5a851d626cf5069fe06c9dceac174f238892dbb
Parents: 18f65e1
Author: Paul Campbell <[email protected]>
Authored: Tue Nov 6 10:15:26 2018 +0000
Committer: Paul Campbell <[email protected]>
Committed: Tue Nov 6 15:15:58 2018 +0000

----------------------------------------------------------------------
 parent/pom.xml                                  |  1 +
 .../apache/brooklyn/test/DisableOnWindows.java  | 40 ++++++++++++++
 .../brooklyn/test/DisableOnWindowsListener.java | 57 ++++++++++++++++++++
 .../services/org.testng.ITestNGListener         |  1 +
 .../brooklyn/test/DisableOnWindowsTest.java     | 41 ++++++++++++++
 5 files changed, 140 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/c5a851d6/parent/pom.xml
----------------------------------------------------------------------
diff --git a/parent/pom.xml b/parent/pom.xml
index d6feccf..971c76e 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -1110,6 +1110,7 @@
                   <exclude>**/src/main/license/**</exclude>
                   <exclude>**/src/test/license/**</exclude>
                   <exclude>**/MANIFEST.MF</exclude>
+                  <exclude>**/META-INF/services/**</exclude>
                   <exclude>**/test-output/**</exclude>
                   <exclude>**/*.pem.pub</exclude>
                   <exclude>**/*.pem</exclude>

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/c5a851d6/test-support/src/main/java/org/apache/brooklyn/test/DisableOnWindows.java
----------------------------------------------------------------------
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 0000000..9fa2998
--- /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.
+ *
+ * <p>You must add the following to the class where this annotation is being 
used:
+ * {@code @Listeners(DisableOnWindows)}</p>
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+@Documented
+public @interface DisableOnWindows {
+
+    /**3
+     * Explain the reason for the test being disabled.
+     *
+     * <p>e.g. "Needs an ssh server listening on port 22 on localhost."</p>
+     */
+    String reason();
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/c5a851d6/test-support/src/main/java/org/apache/brooklyn/test/DisableOnWindowsListener.java
----------------------------------------------------------------------
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 0000000..743edde
--- /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()));
+            }
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/c5a851d6/test-support/src/main/resources/META-INF/services/org.testng.ITestNGListener
----------------------------------------------------------------------
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 0000000..584861a
--- /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

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/c5a851d6/test-support/src/test/java/org/apache/brooklyn/test/DisableOnWindowsTest.java
----------------------------------------------------------------------
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 0000000..9929f66
--- /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");
+        }
+    }
+}

Reply via email to