Skip to content

Commit

Permalink
[test-support] Add @DisableOnWindows annotation for TestNG tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kemitix committed Nov 6, 2018
1 parent 18f65e1 commit c5a851d
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
1 change: 1 addition & 0 deletions parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
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();
}
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()));
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.apache.brooklyn.test.DisableOnWindowsListener
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");
}
}
}

0 comments on commit c5a851d

Please sign in to comment.