brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rich...@apache.org
Subject [2/4] incubator-brooklyn git commit: Add TestResourceUnavailableException
Date Mon, 01 Dec 2014 17:11:42 GMT
Add TestResourceUnavailableException

Provides a way to skip TestNG tests if a test resource is not available (such as in the source
distribution, which does not carry binary test resources)


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

Branch: refs/heads/master
Commit: 824dc156cbf6bf95e1e1f0b3cca8e6c1725e95df
Parents: 4b11fea
Author: Richard Downer <richard@apache.org>
Authored: Wed Nov 26 13:05:44 2014 +0000
Committer: Richard Downer <richard@apache.org>
Committed: Mon Dec 1 17:04:35 2014 +0000

----------------------------------------------------------------------
 .../test/TestResourceUnavailableException.java  | 140 +++++++++++++++++++
 1 file changed, 140 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/824dc156/utils/test-support/src/main/java/brooklyn/test/TestResourceUnavailableException.java
----------------------------------------------------------------------
diff --git a/utils/test-support/src/main/java/brooklyn/test/TestResourceUnavailableException.java
b/utils/test-support/src/main/java/brooklyn/test/TestResourceUnavailableException.java
new file mode 100644
index 0000000..0738184
--- /dev/null
+++ b/utils/test-support/src/main/java/brooklyn/test/TestResourceUnavailableException.java
@@ -0,0 +1,140 @@
+/*
+ * 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 brooklyn.test;
+
+import com.google.common.base.Throwables;
+import org.testng.SkipException;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Indicates that a test cannot be run as a necessary resource is not available.
+ *
+ * <p>This exception should be used by tests that need a particular test resource (i.e.,
a file that is conventionally
+ * in the <code>src/test/resources</code> folder, and that available as a classpath
resource at runtime) is not
+ * available. It will cause TestNG to mark the test as "skipped" with a suitable message.</p>
+ *
+ * <p>Some tests require binary files (such as OSGi bundles) which under Apache conventions
are not able to be
+ * distributed in our source code release. This exception allows such tests to become "optional"
so that they do not
+ * cause test failures when running the tests on a source distribution.</p>
+ *
+ * <p>Note that the single-string constructors expect the string to be the simple name
of the classpath resource that
+ * is not available. The exception message is then derived from this. The two-string constructors
take both the
+ * resource name and an explicit exception message.</p>
+ */
+@SuppressWarnings("UnusedDeclaration")
+public class TestResourceUnavailableException extends SkipException {
+
+    private final String resourceName;
+
+    /**
+     * Asserts that a resource is available on the classpath; otherwise, throws {@link TestResourceUnavailableException}
+     *
+     * Note that this will use the same classloader that was used to load this class.
+     *
+     * @param resourceName the classpath resource name, e.g.
+     *                     <code>/brooklyn/osgi/brooklyn-test-osgi-entities.jar</code>
+     */
+    public static void throwIfResourceUnavailable(Class<?> relativeToClass, String
resourceName) {
+        checkNotNull(relativeToClass, relativeToClass);
+        checkNotNull(resourceName, "resourceName");
+        checkArgument(!resourceName.isEmpty(), "resourceName must not be empty");
+        InputStream resource = relativeToClass.getResourceAsStream(resourceName);
+        if (resource == null)
+            throw new TestResourceUnavailableException(resourceName);
+
+        // just make sure we clean up the resource
+        try {
+            resource.close();
+        } catch (IOException e) {
+            Throwables.propagate(e);
+        }
+    }
+
+    /**
+     * Instantiate an exception, giving the name of the unavailable resource.
+     *
+     * @param resourceName the name of the resource on the classpath, e.g.
+     *                     <code>/brooklyn/osgi/brooklyn-test-osgi-entities.jar</code>
+     */
+    public TestResourceUnavailableException(String resourceName) {
+        super(messageFromResourceName(resourceName));
+        this.resourceName = resourceName;
+    }
+
+    /**
+     * Instantiate an exception, giving the name of the unavailable resource.
+     *
+     * @param resourceName the name of the resource on the classpath, e.g.
+     *                     <code>/brooklyn/osgi/brooklyn-test-osgi-entities.jar</code>
+     * @param cause the underlying exception that caused this one
+     */
+    public TestResourceUnavailableException(String resourceName, Throwable cause) {
+        super(messageFromResourceName(resourceName), cause);
+        this.resourceName = resourceName;
+    }
+
+    /**
+     * Instantiate an exception, giving the name of the unavailable resource.
+     *
+     * @param resourceName the name of the resource on the classpath, e.g.
+     *                     <code>/brooklyn/osgi/brooklyn-test-osgi-entities.jar</code>
+     * @param skipMessage the message associated with the exception
+     */
+    public TestResourceUnavailableException(String resourceName, String skipMessage) {
+        super(skipMessage);
+        this.resourceName = resourceName;
+    }
+
+    /**
+     * Instantiate an exception, giving the name of the unavailable resource.
+     *
+     * @param resourceName the name of the resource on the classpath, e.g.
+     *                     <code>/brooklyn/osgi/brooklyn-test-osgi-entities.jar</code>
+     * @param skipMessage the message associated with the exception
+     * @param cause the underlying exception that caused this one
+     */
+    public TestResourceUnavailableException(String resourceName, String skipMessage, Throwable
cause) {
+        super(skipMessage, cause);
+        this.resourceName = resourceName;
+    }
+
+    private static String messageFromResourceName(String resourceName) {
+        return String.format("Test resource '%s' not found; test skipped.", resourceName);
+    }
+
+    /**
+     * Get the name of the classpath resource that could not be loaded.
+     *
+     * @return the name of the classpath resource whose absence caused this exception.
+     */
+    public String getResourceName() {
+        return resourceName;
+    }
+
+    @Override
+    public boolean isSkip() {
+        return true;
+    }
+
+}


Mime
View raw message