Skip to content

Commit

Permalink
Improve {Checked,Throwing}ProviderTest to allow exception types to be…
Browse files Browse the repository at this point in the history
… returned in any order from Method.getGenericExceptionTypes. Fixes #1603.

(This does not use the suggested code from the original PR, and does not attempt to change the `testManyMethods` method because we assume that repeated calls to `getDeclaredMethods` will always return the same order of methods. If that ever changes, we can deal with it then.)

PiperOrigin-RevId: 525220025
  • Loading branch information
sameb authored and Guice Team committed Apr 18, 2023
1 parent 15d95bb commit 22dca18
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package com.google.inject.throwingproviders;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.truth.Truth.assertThat;
import static com.google.inject.Asserts.assertContains;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
Expand Down Expand Up @@ -1257,28 +1259,28 @@ String foo()
fail();
} catch (CreationException ce) {
// The only two that should fail are Interrupted & TooManyListeners.. the rest are OK.
List<Message> errors = ImmutableList.copyOf(ce.getErrorMessages());
assertEquals(
// Allow the msgs to come in any order.
ImmutableList<String> errors =
ce.getErrorMessages().stream().map(Message::getMessage).collect(toImmutableList());
String msg1 =
InterruptedException.class.getName()
+ " is not compatible with the exceptions (["
+ RemoteException.class
+ ", "
+ BindException.class
+ "]) declared in the CheckedProvider interface ("
+ RemoteProvider.class.getName()
+ ")",
errors.get(0).getMessage());
assertEquals(
+ ")";
String msg2 =
TooManyListenersException.class.getName()
+ " is not compatible with the exceptions (["
+ RemoteException.class
+ ", "
+ BindException.class
+ "]) declared in the CheckedProvider interface ("
+ RemoteProvider.class.getName()
+ ")",
errors.get(1).getMessage());
assertEquals(2, errors.size());
+ ")";
assertThat(errors).containsExactly(msg1, msg2);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package com.google.inject.throwingproviders;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.truth.Truth.assertThat;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -851,24 +853,24 @@ String foo()
fail();
} catch (CreationException ce) {
// The only two that should fail are Interrupted & TooManyListeners.. the rest are OK.
List<Message> errors = ImmutableList.copyOf(ce.getErrorMessages());
assertEquals(
// Allow the msgs to come in any order.
ImmutableList<String> errors =
ce.getErrorMessages().stream().map(Message::getMessage).collect(toImmutableList());
String msg1 =
InterruptedException.class.getName()
+ " is not compatible with the exceptions (["
+ RemoteException.class
+ "]) declared in the CheckedProvider interface ("
+ RemoteProvider.class.getName()
+ ")",
errors.get(0).getMessage());
assertEquals(
+ ")";
String msg2 =
TooManyListenersException.class.getName()
+ " is not compatible with the exceptions (["
+ RemoteException.class
+ "]) declared in the CheckedProvider interface ("
+ RemoteProvider.class.getName()
+ ")",
errors.get(1).getMessage());
assertEquals(2, errors.size());
+ ")";
assertThat(errors).containsExactly(msg1, msg2);
}
}

Expand Down

0 comments on commit 22dca18

Please sign in to comment.