Skip to content

Commit

Permalink
Fix vararags warings
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-costigliola committed Feb 21, 2015
1 parent 5bfe1e8 commit e311f58
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 21 deletions.
Expand Up @@ -131,12 +131,14 @@ public CharacterAssert then(Character actual) {

/**
* Creates a new instance of <code>{@link ClassAssert}</code>
* </p>
* We don't return {@link ClassAssert} as it has overriden methods to annotated with {@link SafeVarargs}.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public ClassAssert then(Class<?> actual) {
return proxy(ClassAssert.class, Class.class, actual);
public SoftAssertionClassAssert then(Class<?> actual) {
return proxy(SoftAssertionClassAssert.class, Class.class, actual);
}

/**
Expand Down Expand Up @@ -348,13 +350,15 @@ public <T> ObjectArrayAssert<T> then(T[] actual) {

/**
* Creates a new instance of <code>{@link MapAssert}</code>.
* </p>
* We don't return {@link MapAssert} as it has overriden methods to annotated with {@link SafeVarargs}.
*
* @param actual the actual value.
* @return the created assertion object.
*/
@SuppressWarnings("unchecked")
public <K, V> MapAssert<K, V> then(Map<K, V> actual) {
return proxy(MapAssert.class, Map.class, actual);
public <K, V> SoftAssertionMapAssert<K, V> then(Map<K, V> actual) {
return proxy(SoftAssertionMapAssert.class, Map.class, actual);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/assertj/core/api/AbstractClassAssert.java
Expand Up @@ -170,10 +170,10 @@ public S isNotAnnotation() {
* &#64;Hero &#64;Force
* class Jedi implements Jedi {}
*
* // Should pass if :
* assertThat(Jedi.class).containsAnnotations(Force.class, hero.class);
* // Should pass:
* assertThat(Jedi.class).containsAnnotations(Force.class, Hero.class);
*
* // Should fail if :
* // Should fail:
* assertThat(Jedi.class).containsAnnotations(Force.class, DarkSide.class);
* </code></pre>
*
Expand Down
Expand Up @@ -131,12 +131,14 @@ public CharacterAssert assertThat(Character actual) {

/**
* Creates a new instance of <code>{@link ClassAssert}</code>
* </p>
* We don't return {@link ClassAssert} as it has overriden methods to annotated with {@link SafeVarargs}.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public ClassAssert assertThat(Class<?> actual) {
return proxy(ClassAssert.class, Class.class, actual);
public SoftAssertionClassAssert assertThat(Class<?> actual) {
return proxy(SoftAssertionClassAssert.class, Class.class, actual);
}

/**
Expand Down Expand Up @@ -348,13 +350,15 @@ public <T> ObjectArrayAssert<T> assertThat(T[] actual) {

/**
* Creates a new instance of <code>{@link MapAssert}</code>.
* <p>
* We don't return {@link MapAssert} as it has overriden methods to annotated with {@link SafeVarargs}.
*
* @param actual the actual value.
* @return the created assertion object.
*/
@SuppressWarnings("unchecked")
public <K, V> MapAssert<K, V> assertThat(Map<K, V> actual) {
return proxy(MapAssert.class, Map.class, actual);
public <K, V> SoftAssertionMapAssert<K, V> assertThat(Map<K, V> actual) {
return proxy(SoftAssertionMapAssert.class, Map.class, actual);
}

/**
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/org/assertj/core/api/Assertions.java
Expand Up @@ -186,7 +186,7 @@ public static AbstractClassAssert<?> assertThat(Class<?> actual) {
* @return the created assertion object.
*/
public static <T extends Comparable<? super T>> AbstractComparableAssert<?, T> assertThat(T actual) {
return new GenericComparableAssert<>(actual);
return new GenericComparableAssert<>(actual);
}

/**
Expand All @@ -196,7 +196,7 @@ public static <T extends Comparable<? super T>> AbstractComparableAssert<?, T> a
* @return the created assertion object.
*/
public static <T> AbstractIterableAssert<?, ? extends Iterable<? extends T>, T> assertThat(Iterable<? extends T> actual) {
return new IterableAssert<>(actual);
return new IterableAssert<>(actual);
}

/**
Expand All @@ -210,7 +210,7 @@ public static <T extends Comparable<? super T>> AbstractComparableAssert<?, T> a
* @return the created assertion object.
*/
public static <T> AbstractIterableAssert<?, ? extends Iterable<? extends T>, T> assertThat(Iterator<? extends T> actual) {
return new IterableAssert<>(actual);
return new IterableAssert<>(actual);
}

/**
Expand Down Expand Up @@ -341,7 +341,7 @@ public static AbstractIntegerAssert<?> assertThat(Integer actual) {
* @return the created assertion object.
*/
public static <T> AbstractListAssert<?, ? extends List<? extends T>, T> assertThat(List<? extends T> actual) {
return new ListAssert<>(actual);
return new ListAssert<>(actual);
}

/**
Expand Down Expand Up @@ -381,7 +381,7 @@ public static AbstractLongArrayAssert<?> assertThat(long[] actual) {
* @return the created assertion object.
*/
public static <T> AbstractObjectAssert<?, T> assertThat(T actual) {
return new ObjectAssert<>(actual);
return new ObjectAssert<>(actual);
}

/**
Expand Down Expand Up @@ -467,17 +467,20 @@ public static <T> T assertThat(final AssertProvider<T> component) {
* @return the created assertion object.
*/
public static <T> AbstractObjectArrayAssert<?, T> assertThat(T[] actual) {
return new ObjectArrayAssert<>(actual);
return new ObjectArrayAssert<>(actual);
}

/**
* Creates a new instance of <code>{@link MapAssert}</code>.
*
* <p>
* Returned type is {@link MapAssert} as it overrides method to annotate them with {@link SafeVarargs} avoiding
* annoying warnings.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static <K, V> AbstractMapAssert<?, ? extends Map<K, V>, K, V> assertThat(Map<K, V> actual) {
return new MapAssert<>(actual);
public static <K, V> MapAssert<K, V> assertThat(Map<K, V> actual) {
return new MapAssert<>(actual);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/assertj/core/api/ClassAssert.java
Expand Up @@ -12,6 +12,8 @@
*/
package org.assertj.core.api;

import java.lang.annotation.Annotation;

/**
* Assertion methods for {@code Class}es.
* <p>
Expand All @@ -26,4 +28,14 @@ public class ClassAssert extends AbstractClassAssert<ClassAssert> {
protected ClassAssert(Class<?> actual) {
super(actual, ClassAssert.class);
}

// override method to annotate it with @SafeVarargs, we unfortunately can't do that in AbstractClassAssert as it is
// used in soft assertions which need to be able to proxy method - @SafeVarargs requiring method to be final prevents
// using proxies.

@SafeVarargs
@Override
public final ClassAssert hasAnnotations(Class<? extends Annotation>... annotations) {
return super.hasAnnotations(annotations);
}
}
54 changes: 54 additions & 0 deletions src/main/java/org/assertj/core/api/MapAssert.java
Expand Up @@ -14,6 +14,8 @@

import java.util.Map;

import org.assertj.core.data.MapEntry;

/**
* Assertions for {@link Map}s.
* <p>
Expand All @@ -31,4 +33,56 @@ public class MapAssert<K, V> extends AbstractMapAssert<MapAssert<K, V>, Map<K, V
protected MapAssert(Map<K, V> actual) {
super(actual, MapAssert.class);
}

// override methods to annotate them with @SafeVarargs, we unfortunately can't do that in AbstractMapAssert as it is
// used in soft assertions which need to be able to proxy method - @SafeVarargs requiring method to be final prevents
// using proxies.

@SafeVarargs
@Override
public final MapAssert<K, V> contains(MapEntry<? extends K, ? extends V>... entries) {
return super.contains(entries);
}

@SafeVarargs
@Override
public final MapAssert<K, V> containsOnly(MapEntry<? extends K, ? extends V>... entries) {
return super.containsOnly(entries);
}

@SafeVarargs
@Override
public final MapAssert<K, V> containsExactly(MapEntry<? extends K, ? extends V>... entries) {
return super.containsExactly(entries);
}

@SafeVarargs
@Override
public final MapAssert<K, V> containsKeys(K... keys) {
return super.containsKeys(keys);
}

@SafeVarargs
@Override
public final MapAssert<K, V> containsOnlyKeys(K... keys) {
return super.containsOnlyKeys(keys);
}

@SafeVarargs
@Override
public final MapAssert<K, V> containsValues(V... values) {
return super.containsValues(values);
}

@SafeVarargs
@Override
public final MapAssert<K, V> doesNotContainKeys(K... keys) {
return super.doesNotContainKeys(keys);
}

@SafeVarargs
@Override
public final MapAssert<K, V> doesNotContain(MapEntry<? extends K, ? extends V>... entries) {
return super.doesNotContain(entries);
}
}
35 changes: 35 additions & 0 deletions src/main/java/org/assertj/core/api/SoftAssertionClassAssert.java
@@ -0,0 +1,35 @@
/**
* Licensed 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.
*
* Copyright 2012-2014 the original author or authors.
*/
package org.assertj.core.api;

import java.lang.annotation.Annotation;

/**
* Concrete assertions for {@link Class}s without any final methods to allow proxying.
*/
public class SoftAssertionClassAssert extends AbstractClassAssert<SoftAssertionClassAssert> {

protected SoftAssertionClassAssert(Class<?> actual) {
super(actual, SoftAssertionClassAssert.class);
}

// override method to annotate it with @SafeVarargs, we unfortunately can't do that in AbstractClassAssert as it is
// used in soft assertions which need to be able to proxy method - @SafeVarargs requiring method to be final prevents
// using proxies.

@SafeVarargs
@Override
public final SoftAssertionClassAssert hasAnnotations(Class<? extends Annotation>... annotations) {
return super.hasAnnotations(annotations);
}
}
26 changes: 26 additions & 0 deletions src/main/java/org/assertj/core/api/SoftAssertionMapAssert.java
@@ -0,0 +1,26 @@
/**
* Licensed 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.
*
* Copyright 2012-2014 the original author or authors.
*/
package org.assertj.core.api;

import java.util.Map;

/**
* Concrete assertions for {@link Map}s without any final methods to allow proxying.
*/
public class SoftAssertionMapAssert<K, V> extends AbstractMapAssert<SoftAssertionMapAssert<K, V>, Map<K, V>, K, V> {

protected SoftAssertionMapAssert(Map<K, V> actual) {
super(actual, SoftAssertionMapAssert.class);
}

}
20 changes: 19 additions & 1 deletion src/test/java/org/assertj/core/api/SoftAssertionsTest.java
Expand Up @@ -73,6 +73,24 @@ public void all_assertions_should_pass() {
softly.assertAll();
}

@Test
public void should_be_able_to_catch_exceptions_thrown_by_map_assertions() {
try {
softly.assertThat(Maps.mapOf(MapEntry.entry("54", "55"))).contains(MapEntry.entry("1", "2"));
softly.assertAll();
fail("Should not reach here");
} catch (SoftAssertionError e) {
List<String> errors = e.getErrors( );
assertThat(errors).contains("\nExpecting:\n"
+ " <{\"54\"=\"55\"}>\n"
+ "to contain:\n"
+ " <[MapEntry[key='1', value='2']]>\n"
+ "but could not find:\n"
+ " <[MapEntry[key='1', value='2']]>\n");

}
}

@SuppressWarnings("unchecked")
@Test
public void should_be_able_to_catch_exceptions_thrown_by_all_proxied_methods() {
Expand Down Expand Up @@ -160,7 +178,7 @@ public String toString() {
("IllegalArgumentException message");
softly.assertThat(illegalArgumentException).hasMessage("NullPointerException message");
softly.assertThatThrownBy(new ThrowingCallable() {

@Override
public void call() throws Exception {
throw new Exception("something was wrong");
Expand Down

0 comments on commit e311f58

Please sign in to comment.