forked from apache/brooklyn-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test-support] Add @DisableOnWindows annotation for TestNG tests
- Loading branch information
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
test-support/src/main/java/org/apache/brooklyn/test/DisableOnWindows.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
57 changes: 57 additions & 0 deletions
57
test-support/src/main/java/org/apache/brooklyn/test/DisableOnWindowsListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())); | ||
} | ||
} | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
test-support/src/main/resources/META-INF/services/org.testng.ITestNGListener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.apache.brooklyn.test.DisableOnWindowsListener |
41 changes: 41 additions & 0 deletions
41
test-support/src/test/java/org/apache/brooklyn/test/DisableOnWindowsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} | ||
} |