下面列出了org.hamcrest.collection.IsEmptyCollection#org.hamcrest.core.IsNot 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void doesNotCloseTheStream() throws Exception {
final CloseableInputStream closeable = new CloseableInputStream(
new DeadInputStream()
);
new Assertion<>(
"must not be marked as closed before close is called",
closeable.wasClosed(),
new IsNot<>(new IsTrue())
).affirm();
closeable.close();
new Assertion<>(
"must be marked as closed after close is called",
closeable.wasClosed(),
new IsTrue()
).affirm();
}
@Test
public void testAllPermissionDeniesActionsWhenUserIsNotCorrectRole() {
SolrRequestHandler handler = new UpdateRequestHandler();
assertThat(handler, new IsInstanceOf(PermissionNameProvider.class));
setUserRole("dev", "dev");
setUserRole("admin", "admin");
addPermission("all", "admin");
checkRules(makeMap("resource", "/update",
"userPrincipal", "dev",
"requestType", RequestType.UNKNOWN,
"collectionRequests", "go",
"handler", new UpdateRequestHandler(),
"params", new MapSolrParams(singletonMap("key", "VAL2")))
, FORBIDDEN);
handler = new PropertiesRequestHandler();
assertThat(handler, new IsNot<>(new IsInstanceOf(PermissionNameProvider.class)));
checkRules(makeMap("resource", "/admin/info/properties",
"userPrincipal", "dev",
"requestType", RequestType.UNKNOWN,
"collectionRequests", "go",
"handler", handler,
"params", new MapSolrParams(emptyMap()))
, FORBIDDEN);
}
@Test
public void withServiceCreation() {
Configuration configuration = ConfigurationBuilder.newConfigurationBuilder()
.withCache("cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)))
.build();
//tag::withServiceCreation[]
Configuration withBoundedThreads = configuration.derive()
.withService(new PooledExecutionServiceConfiguration()
.addDefaultPool("default", 1, 16))
.build();
//end::withServiceCreation[]
Assert.assertThat(configuration.getServiceCreationConfigurations(), IsNot.not(IsCollectionContaining.hasItem(IsInstanceOf.instanceOf(PooledExecutionServiceConfiguration.class))));
PooledExecutionServiceConfiguration serviceCreationConfiguration = ServiceUtils.findSingletonAmongst(PooledExecutionServiceConfiguration.class, withBoundedThreads.getServiceCreationConfigurations());
Assert.assertThat(serviceCreationConfiguration.getDefaultPoolAlias(), Is.is("default"));
Assert.assertThat(serviceCreationConfiguration.getPoolConfigurations().keySet(), IsIterableContainingInAnyOrder.containsInAnyOrder("default"));
PooledExecutionServiceConfiguration.PoolConfiguration pool = serviceCreationConfiguration.getPoolConfigurations().get("default");
Assert.assertThat(pool.minSize(), Is.is(1));
Assert.assertThat(pool.maxSize(), Is.is(16));
}
@Test
public void hashCodesAreEqualForEquivalentObjects() {
// Arrange:
final Ed25519EncodedFieldElement encoded1 = MathUtils.getRandomEncodedFieldElement(32);
final Ed25519EncodedFieldElement encoded2 = encoded1.decode().encode();
final Ed25519EncodedFieldElement encoded3 = MathUtils.getRandomEncodedFieldElement(32);
final Ed25519EncodedFieldElement encoded4 = MathUtils.getRandomEncodedFieldElement(32);
// Assert:
MatcherAssert.assertThat(encoded1.hashCode(), IsEqual.equalTo(encoded2.hashCode()));
MatcherAssert
.assertThat(encoded1.hashCode(), IsNot.not(IsEqual.equalTo(encoded3.hashCode())));
MatcherAssert
.assertThat(encoded1.hashCode(), IsNot.not(IsEqual.equalTo(encoded4.hashCode())));
MatcherAssert
.assertThat(encoded3.hashCode(), IsNot.not(IsEqual.equalTo(encoded4.hashCode())));
}
@Test
public void equalsOnlyReturnsTrueForEquivalentObjects() {
// Arrange:
final Ed25519GroupElement g1 = MathUtils.getRandomGroupElement();
final Ed25519GroupElement g2 = MathUtils.toRepresentation(g1, CoordinateSystem.P2);
final Ed25519GroupElement g3 = MathUtils.toRepresentation(g1, CoordinateSystem.CACHED);
final Ed25519GroupElement g4 = MathUtils.toRepresentation(g1, CoordinateSystem.P1xP1);
final Ed25519GroupElement g5 = MathUtils.getRandomGroupElement();
// Assert
MatcherAssert.assertThat(g2, IsEqual.equalTo(g1));
MatcherAssert.assertThat(g3, IsEqual.equalTo(g1));
MatcherAssert.assertThat(g1, IsEqual.equalTo(g4));
MatcherAssert.assertThat(g1, IsNot.not(IsEqual.equalTo(g5)));
MatcherAssert.assertThat(g2, IsNot.not(IsEqual.equalTo(g5)));
MatcherAssert.assertThat(g3, IsNot.not(IsEqual.equalTo(g5)));
MatcherAssert.assertThat(g5, IsNot.not(IsEqual.equalTo(g4)));
}
@Test
public void sensesChangesInMap() throws Exception {
final AtomicInteger size = new AtomicInteger(2);
final PropertiesOf props = new PropertiesOf(
new MapOf<>(
() -> new Repeated<>(
size.incrementAndGet(), () -> new MapEntry<>(
new SecureRandom().nextInt(),
1
)
)
)
);
new Assertion<>(
"Must sense the changes in the underlying map",
props.value().size(),
new IsNot<>(new IsEqual<>(props.value().size()))
).affirm();
}
@Test
public void testSimpleScenario() throws Exception {
new Assertion<>(
"Futures returns Response",
new Futures(
(artifact, group) -> input -> new RsPage(
new RqFake(),
"wait",
() -> new IterableOf<>(
new XeAppend("group", group),
new XeAppend("artifact", artifact)
)
)
).apply("a", "g").get().apply("test"),
new IsNot<>(new IsEqual<>(null))
).affirm();
}
@Test
public void testAllPermissionAllowsActionsWhenUserHasCorrectRole() {
SolrRequestHandler handler = new UpdateRequestHandler();
assertThat(handler, new IsInstanceOf(PermissionNameProvider.class));
setUserRole("dev", "dev");
setUserRole("admin", "admin");
addPermission("all", "dev", "admin");
checkRules(makeMap("resource", "/update",
"userPrincipal", "dev",
"requestType", RequestType.UNKNOWN,
"collectionRequests", "go",
"handler", handler,
"params", new MapSolrParams(singletonMap("key", "VAL2")))
, STATUS_OK);
handler = new PropertiesRequestHandler();
assertThat(handler, new IsNot<>(new IsInstanceOf(PermissionNameProvider.class)));
checkRules(makeMap("resource", "/admin/info/properties",
"userPrincipal", "dev",
"requestType", RequestType.UNKNOWN,
"collectionRequests", "go",
"handler", handler,
"params", new MapSolrParams(emptyMap()))
, STATUS_OK);
}
@Test
void encryptedDataCanBeDecrypted() {
// Arrange:
final CryptoEngine engine = this.getCryptoEngine();
final KeyPair kp = KeyPair.random(engine);
final BlockCipher blockCipher = this.getBlockCipher(kp, kp);
final byte[] input = RandomUtils.generateRandomBytes();
// Act:
final byte[] encryptedBytes = blockCipher.encrypt(input);
final byte[] decryptedBytes = blockCipher.decrypt(encryptedBytes);
// Assert:
MatcherAssert.assertThat(encryptedBytes, IsNot.not(IsEqual.equalTo(decryptedBytes)));
MatcherAssert.assertThat(decryptedBytes, IsEqual.equalTo(input));
}
@Test
void dataCanBeEncryptedWithSenderPrivateKeyAndRecipientPublicKey() {
// Arrange:
final CryptoEngine engine = this.getCryptoEngine();
final KeyPair skp = KeyPair.random(engine);
final KeyPair rkp = KeyPair.random(engine);
final BlockCipher blockCipher =
this.getBlockCipher(skp, KeyPair.onlyPublic(rkp.getPublicKey(), engine));
final byte[] input = RandomUtils.generateRandomBytes();
// Act:
final byte[] encryptedBytes = blockCipher.encrypt(input);
// Assert:
MatcherAssert.assertThat(encryptedBytes, IsNot.not(IsEqual.equalTo(input)));
}
@Test
void dataEncryptedWithPrivateKeyCanOnlyBeDecryptedByMatchingPublicKey(
) {
// Arrange:
final CryptoEngine engine = this.getCryptoEngine();
final BlockCipher blockCipher1 =
this.getBlockCipher(KeyPair.random(engine), KeyPair.random(engine));
final BlockCipher blockCipher2 =
this.getBlockCipher(KeyPair.random(engine), KeyPair.random(engine));
final byte[] input = RandomUtils.generateRandomBytes();
// Act:
final byte[] encryptedBytes1 = blockCipher1.encrypt(input);
final byte[] encryptedBytes2 = blockCipher2.encrypt(input);
// Assert:
MatcherAssert.assertThat(blockCipher1.decrypt(encryptedBytes1), IsEqual.equalTo(input));
MatcherAssert
.assertThat(blockCipher1.decrypt(encryptedBytes2), IsNot.not(IsEqual.equalTo(input)));
MatcherAssert
.assertThat(blockCipher2.decrypt(encryptedBytes1), IsNot.not(IsEqual.equalTo(input)));
MatcherAssert.assertThat(blockCipher2.decrypt(encryptedBytes2), IsEqual.equalTo(input));
}
@Test
public void withService() {
Configuration configuration = ConfigurationBuilder.newConfigurationBuilder()
.withCache("cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)))
.build();
//tag::withService[]
Configuration withThrowingStrategy = configuration.derive()
.updateCache("cache", existing -> existing.withService(
new DefaultResilienceStrategyConfiguration(new ThrowingResilienceStrategy<>())
))
.build();
//end::withService[]
Assert.assertThat(configuration.getServiceCreationConfigurations(), IsNot.not(IsCollectionContaining.hasItem(
IsInstanceOf.instanceOf(DefaultResilienceStrategyConfiguration.class))));
DefaultResilienceStrategyConfiguration resilienceStrategyConfiguration =
ServiceUtils.findSingletonAmongst(DefaultResilienceStrategyConfiguration.class,
withThrowingStrategy.getCacheConfigurations().get("cache").getServiceConfigurations());
Assert.assertThat(resilienceStrategyConfiguration.getInstance(), IsInstanceOf.instanceOf(ThrowingResilienceStrategy.class));
}
@Test
public void sensesChangesInMap() throws Exception {
final AtomicInteger size = new AtomicInteger(2);
final Map<Integer, Integer> map = new MapOf<>(
() -> new Repeated<>(
size.incrementAndGet(), () -> new MapEntry<>(
new SecureRandom().nextInt(),
1
)
)
);
MatcherAssert.assertThat(
"Can't sense the changes in the underlying map",
map.size(),
new IsNot<>(new IsEqual<>(map.size()))
);
}
@Test
public void shouldNotEncodeUnitOfMeasurementForCountObservations() throws EncodingException, NoSuchElementException, OwsExceptionReport {
OmObservation omObservation = responseToEncode.getObservationCollection().next();
omObservation.getObservationConstellation().
setObservationType(OmConstants.OBS_TYPE_COUNT_OBSERVATION);
Time phenTime = new TimeInstant(new Date(UTC_TIMESTAMP_1));
omObservation.setValue(new SingleObservationValue<>(phenTime,
new CountValue(52)));
((OmObservableProperty)omObservation.getObservationConstellation()
.getObservableProperty()).setUnit(null);
responseToEncode.setObservationCollection(ObservationStream.of(omObservation));
final String[] actual = getResponseString();
final String expected = "$sb Mess-Einheit: " + unit;
assertThat(Arrays.asList(actual), IsNot.not(CoreMatchers.hasItems(expected)));
}
@Test
public void testIgnoresCrashes() throws Exception {
new Assertion<>(
"Futures don't crash",
new Futures(
(artifact, group) -> {
throw new UnsupportedOperationException("intended");
}
).apply("a1", "g1").get().apply("test-2"),
new IsNot<>(new IsEqual<>(null))
).affirm();
}
static Stream<Arguments> dataProvider()
{
// @formatter:off
return Stream.of(
Arguments.of(StringComparisonRule.IS_EQUAL_TO, IsEqual.class),
Arguments.of(StringComparisonRule.CONTAINS, StringContains.class),
Arguments.of(StringComparisonRule.DOES_NOT_CONTAIN, IsNot.class),
Arguments.of(StringComparisonRule.MATCHES, StringRegularExpression.class)
);
// @formatter:on
}
@Test
public void extractsJsonObjectFromDict() {
MatcherAssert.assertThat(
new XmlBody.Of(
new HashDict(
new KvpOf("body", "<test/>"),
new KvpOf("unknown", "")
)
).xml(),
new IsNot<>(new IsNull<>())
);
}
@Test
public void extractsHtmlBodyFromDict() {
MatcherAssert.assertThat(
new HtmlBody.Of(
new HashDict(
new KvpOf("body", "<html></html>"),
new KvpOf("unknown", "")
)
).html().html(),
new IsNot<>(new IsNull<>())
);
}
/**
* HmRsTextBody can test if body doesn't equal to text.
*/
@Test
public void testsBodyValueDoesNotContainsText() {
MatcherAssert.assertThat(
new RsWithBody("Some response"),
new IsNot<>(new HmRsTextBody("expected something else"))
);
}
@Test
public void extractsJsonArrayFromDict() {
MatcherAssert.assertThat(
new JsonBody.Of(
new HashDict(
new KvpOf("body", "[]"),
new KvpOf("unknown", "")
)
).jsonArray(),
new IsNot<>(new IsNull<>())
);
}
/**
* HmRqTextBody can test if body doesn't equal to text.
*/
@Test
public void testsBodyValueDoesNotContainsText() {
MatcherAssert.assertThat(
new RqFake(
Collections.<String>emptyList(),
"some"
),
new IsNot<>(new HmRqTextBody("other"))
);
}
@Test
public void appendsSslTrustedToClient() {
final HttpClientBuilder builder = HttpClients.custom();
MatcherAssert.assertThat(
new SslTrusted().apply(builder),
new IsEqual<>(builder)
);
MatcherAssert.assertThat(
builder.build(),
new IsNot<>(new IsNull<>())
);
}
@Test
public void appendsProxyToClient() {
final HttpClientBuilder builder = HttpClients.custom();
MatcherAssert.assertThat(
new Proxy("localhost", 8080).apply(builder),
new IsEqual<>(builder)
);
MatcherAssert.assertThat(
builder.build(),
new IsNot<>(new IsNull<>())
);
}
@Test
public void isNotEqualsToIterableWithMoreElements() {
new Assertion<>(
"Must compare iterables and second one is bigger",
new IterableOf<>("a", "b").equals(new IterableOf<>("a")),
new IsNot<>(new IsTrue())
).affirm();
}
@Test
public void scalarCanBeAddedToVector() {
// Arrange:
final ColumnVector a = new ColumnVector(2, -4, 1);
// Act:
final ColumnVector result = a.add(8);
// Assert:
MatcherAssert.assertThat(result, IsNot.not(IsEqual.equalTo(a)));
MatcherAssert.assertThat(result, IsEqual.equalTo(new ColumnVector(10, 4, 9)));
}
@Test
public void twoVectorsOfSameSizeCanBeAddedTogetherElementWise() {
// Arrange:
final ColumnVector a = new ColumnVector(7, 5, 11);
final ColumnVector b = new ColumnVector(2, -4, 1);
// Act:
final ColumnVector result = a.addElementWise(b);
// Assert:
MatcherAssert.assertThat(result, IsNot.not(IsEqual.equalTo(a)));
MatcherAssert.assertThat(result, IsNot.not(IsEqual.equalTo(b)));
MatcherAssert.assertThat(result, IsEqual.equalTo(new ColumnVector(9, 1, 12)));
}
@Test
public void vectorCanBeMultipliedByScalar() {
// Arrange:
final ColumnVector a = new ColumnVector(2, -4, 1);
// Act:
final ColumnVector result = a.multiply(8);
// Assert:
MatcherAssert.assertThat(result, IsNot.not(IsEqual.equalTo(a)));
MatcherAssert.assertThat(result, IsEqual.equalTo(new ColumnVector(16, -32, 8)));
}
@Test
public void vectorCanBeMultipliedByVectorElementWise() {
// Arrange:
final ColumnVector v1 = new ColumnVector(3, 7, 2);
final ColumnVector v2 = new ColumnVector(1, 5, 3);
// Act:
final ColumnVector result = v1.multiplyElementWise(v2);
// Assert:
MatcherAssert.assertThat(result, IsNot.not(IsEqual.equalTo(v1)));
MatcherAssert.assertThat(result, IsNot.not(IsEqual.equalTo(v2)));
MatcherAssert.assertThat(result, IsEqual.equalTo(new ColumnVector(3, 35, 6)));
}
@Test
public void vectorAbsoluteValueCanBeTaken() {
// Arrange:
final ColumnVector vector = new ColumnVector(12.4, -2.1, 7);
// Act:
final ColumnVector result = vector.abs();
// Assert:
MatcherAssert.assertThat(result, IsNot.not(IsEqual.equalTo(vector)));
MatcherAssert.assertThat(result, IsEqual.equalTo(new ColumnVector(12.4, 2.1, 7)));
}
@Test
public void equalsOnlyReturnsTrueForEquivalentObjects() {
// Arrange:
final ColumnVector vector = new ColumnVector(2, -4, 1);
// Assert:
MatcherAssert.assertThat(new ColumnVector(2, -4, 1), IsEqual.equalTo(vector));
MatcherAssert.assertThat(new ColumnVector(1, -4, 1), IsNot.not(IsEqual.equalTo(vector)));
MatcherAssert.assertThat(new ColumnVector(2, 8, 1), IsNot.not(IsEqual.equalTo(vector)));
MatcherAssert.assertThat(new ColumnVector(2, -4, 2), IsNot.not(IsEqual.equalTo(vector)));
MatcherAssert.assertThat(null, IsNot.not(IsEqual.equalTo(vector)));
MatcherAssert.assertThat(new double[]{2, -4, 1}, IsNot.not(IsEqual.equalTo(vector)));
}