Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-25682] Adding test which failed in interfaceOfSuperclassAnno…
…tated.
  • Loading branch information
jglick committed Dec 16, 2014
1 parent f71aef0 commit 813ca86
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 38 deletions.
Expand Up @@ -26,9 +26,6 @@
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

/**
* Provides names for credentials.
Expand All @@ -55,30 +52,33 @@ public abstract class CredentialsNameProvider<C extends Credentials> {
*/
@NonNull
public static String name(@NonNull Credentials credentials) {
credentials.getClass(); // throw NPE if null
@CheckForNull
NameWith nameWith = credentials.getClass().getAnnotation(NameWith.class);
if (!isValidNameWithAnnotation(nameWith)) {
Queue<Class<?>> queue = new LinkedList<Class<?>>();
queue.addAll(Arrays.asList(credentials.getClass().getInterfaces()));
while (!queue.isEmpty()) {
Class<?> interfaze = queue.remove();
if (interfaze.getInterfaces().length > 0) {
queue.addAll(Arrays.asList(interfaze.getInterfaces()));
}
NameWith annotation = interfaze.getAnnotation(NameWith.class);
if (!isValidNameWithAnnotation(annotation)) {
continue;
}
if (nameWith == null || nameWith.priority() < annotation.priority()) {
nameWith = annotation;
}
}
Result result = name(credentials, credentials.getClass());
if (result != null) {
return result.name;
}
try {
return credentials.getDescriptor().getDisplayName();
} catch (AssertionError e) {
return credentials.getClass().getSimpleName();
}
if (isValidNameWithAnnotation(nameWith)) {
}

private static final class Result {
final String name;
final int priority;
Result(String name, int priority) {
this.name = name;
this.priority = priority;
}
}

@SuppressWarnings({"unchecked", "rawtypes"}) // missing type token in CredentialsNameProvider to get RTTI
private static @CheckForNull Result name(@NonNull Credentials credentials, @NonNull Class<?> clazz) {
NameWith nameWith = clazz.getAnnotation(NameWith.class);
if (nameWith != null) {
try {
CredentialsNameProvider nameProvider = nameWith.value().newInstance();
return nameProvider.getName(credentials);
return new Result(nameProvider.getName(credentials), nameWith.priority());
} catch (ClassCastException e) {
// ignore
} catch (InstantiationException e) {
Expand All @@ -87,21 +87,21 @@ public static String name(@NonNull Credentials credentials) {
// ignore
}
}
try {
return credentials.getDescriptor().getDisplayName();
} catch (AssertionError e) {
return credentials.getClass().getSimpleName();
Class<?> supe = clazz.getSuperclass();
Result result = null;
if (supe != null) {
result = name(credentials, supe);
if (result != null) {
return result;
}
}
for (Class<?> xface : clazz.getInterfaces()) {
Result _result = name(credentials, xface);
if (_result != null && (result == null || result.priority < _result.priority)) {
result = _result;
}
}
return result;
}

/**
* Check that the annotation is valid.
*
* @param nameWith the annotation.
* @return {@code true} only if the annotation is valid.
*/
@SuppressWarnings("ConstantConditions")
private static boolean isValidNameWithAnnotation(@CheckForNull NameWith nameWith) {
return nameWith != null && nameWith.value() != null;
}
}
@@ -0,0 +1,130 @@
/*
* The MIT License
*
* Copyright 2014 Jesse Glick.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.cloudbees.plugins.credentials;

import org.junit.Test;
import static org.junit.Assert.*;

public class CredentialsNameProviderTest {

@Test public void notAnnotated() {
assertEquals("C0", CredentialsNameProvider.name(new C0()));
}
private static class C0 extends TestCredentials {}

@Test public void directlyAnnotated() {
assertEquals("C1N", CredentialsNameProvider.name(new C1()));
}
@NameWith(C1N.class)
private static class C1 extends TestCredentials {}
public static class C1N extends TestCredentialsNameProvider {}

@Test public void superclassAnnotated() {
assertEquals("C2N", CredentialsNameProvider.name(new C3()));
}
@NameWith(C2N.class)
private static class C2 extends TestCredentials {}
public static class C2N extends TestCredentialsNameProvider {}
private static class C3 extends C2 {}

@Test public void interfaceAnnotated() {
assertEquals("I1N", CredentialsNameProvider.name(new C4()));
}
@NameWith(I1N.class)
private interface I1 extends Credentials {}
public static class I1N extends TestCredentialsNameProvider {}
private static class C4 extends TestCredentials implements I1 {}

@Test public void indirectInterfaceAnnotated() {
assertEquals("I2N", CredentialsNameProvider.name(new C5()));
}
@NameWith(I2N.class)
private interface I2 extends Credentials {}
public static class I2N extends TestCredentialsNameProvider {}
private interface I3 extends I2 {}
private static class C5 extends TestCredentials implements I3 {}

@Test public void interfaceOfSuperclassAnnotated() {
assertEquals("I4N", CredentialsNameProvider.name(new C7()));
}
@NameWith(I4N.class)
private interface I4 extends Credentials {}
public static class I4N extends TestCredentialsNameProvider {}
private static class C6 extends TestCredentials implements I4 {}
private static class C7 extends C6 {}

@Test public void overrideSuperclassAnnotation() {
assertEquals("C9N", CredentialsNameProvider.name(new C9()));
}
@NameWith(C8N.class)
private static class C8 extends TestCredentials {}
public static class C8N extends TestCredentialsNameProvider {}
@NameWith(C9N.class)
private static class C9 extends C8 {}
public static class C9N extends TestCredentialsNameProvider {}

@Test public void overrideInterfaceAnnotation() {
assertEquals("C10N", CredentialsNameProvider.name(new C10()));
}
@NameWith(I5N.class)
private interface I5 extends Credentials {}
public static class I5N extends TestCredentialsNameProvider {}
@NameWith(C10N.class)
private static class C10 extends TestCredentials implements I5 {}
public static class C10N extends TestCredentialsNameProvider {}

@Test public void interfacePriorities() {
assertEquals("I7N", CredentialsNameProvider.name(new C11()));
assertEquals("I7N", CredentialsNameProvider.name(new C12()));
}
@NameWith(value=I6N.class, priority=1)
private interface I6 extends Credentials {}
public static class I6N extends TestCredentialsNameProvider {}
@NameWith(value=I7N.class, priority=2)
private interface I7 extends Credentials {}
public static class I7N extends TestCredentialsNameProvider {}
private static class C11 extends TestCredentials implements I6, I7 {}
private static class C12 extends TestCredentials implements I7, I6 {}

private static abstract class TestCredentials implements Credentials {
@Override public CredentialsScope getScope() {
return CredentialsScope.GLOBAL;
}
@Override public CredentialsDescriptor getDescriptor() {
return new CredentialsDescriptor() {
@Override public String getDisplayName() {
return TestCredentials.this.getClass().getSimpleName();
}
};
}
}

private static abstract class TestCredentialsNameProvider extends CredentialsNameProvider<Credentials> {
@Override public String getName(Credentials credentials) {
return getClass().getSimpleName();
}
}

}

0 comments on commit 813ca86

Please sign in to comment.