Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ClassLoader assertions #2973

Draft
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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-2023 the original author or authors.
*/
package org.assertj.core.api;

/**
* Base class for all implementations of assertions for {@link ClassLoader class loaders}.
*
* @param <SELF> the "self" type of this assertion class. Please read &quot;<a
* href="http://bit.ly/1IZIRcY" target="_blank">Emulating 'self types' using Java
* Generics to simplify fluent API implementation</a>&quot; for more details.
* @author Ashley Scopes
* @since 3.24.0
*/
public abstract class AbstractClassLoaderAssert<SELF extends AbstractClassLoaderAssert<SELF>>
extends AbstractAssert<SELF, ClassLoader> {

protected AbstractClassLoaderAssert(ClassLoader classLoader, Class<?> selfType) {
super(classLoader, selfType);
}

}
11 changes: 11 additions & 0 deletions assertj-core/src/main/java/org/assertj/core/api/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,17 @@ public static ClassAssert assertThat(Class<?> actual) {
return AssertionsForClassTypes.assertThat(actual);
}

/**
* Creates a new instance of <code>{@link ClassLoaderAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.24.0
*/
public static AbstractClassLoaderAssert<?> assertThat(ClassLoader actual) {
return AssertionsForClassTypes.assertThat(actual);
}

/**
* Creates a new instance of <code>{@link DoubleAssert}</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,17 @@ public static ClassAssert assertThat(Class<?> actual) {
return new ClassAssert(actual);
}

/**
* Creates a new instance of <code>{@link ClassLoaderAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.24.0
*/
public static AbstractClassLoaderAssert<?> assertThat(ClassLoader actual) {
return new ClassLoaderAssert(actual);
}

/**
* Creates a new instance of <code>{@link DoubleAssert}</code>.
*
Expand Down
11 changes: 11 additions & 0 deletions assertj-core/src/main/java/org/assertj/core/api/Assumptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,17 @@ public static ClassAssert assumeThat(Class<?> actual) {
return asAssumption(ClassAssert.class, Class.class, actual);
}

/**
* Creates a new instance of <code>{@link ClassLoaderAssert}</code> assumption.
*
* @param actual the actual value.
* @return the created assumption for assertion object.
* @since 3.24.0
*/
public static AbstractClassLoaderAssert<?> assumeThat(ClassLoader actual) {
return asAssumption(ClassLoaderAssert.class, ClassLoader.class, actual);
}

/**
* Creates a new instance of <code>{@link DateAssert}</code> assumption.
*
Expand Down
11 changes: 11 additions & 0 deletions assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,17 @@ public static ClassAssert then(Class<?> actual) {
return assertThat(actual);
}

/**
* Creates a new instance of <code>{@link org.assertj.core.api.ClassLoaderAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.24.0
*/
public static AbstractClassLoaderAssert<?> then(ClassLoader actual) {
return assertThat(actual);
}

/**
* Creates a new instance of <code>{@link org.assertj.core.api.GenericComparableAssert}</code> with
* standard comparison semantics.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,17 @@ public static ClassAssert given(Class<?> actual) {
return assumeThat(actual);
}

/**
* Creates a new instance of <code>{@link ClassLoaderAssert}</code> assumption.
*
* @param actual the actual value.
* @return the created assumption for assertion object.
* @since 3.24.0
*/
public static AbstractClassLoaderAssert<?> given(ClassLoader actual) {
return assumeThat(actual);
}

/**
* Creates a new assumption's instance for an object value.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -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-2023 the original author or authors.
*/
package org.assertj.core.api;

/**
* Assertion methods for {@link ClassLoader class loaders}.
* <p>
* To create a new instance of this class, invoke
* <code>{@link Assertions#assertThat(ClassLoader)}</code></p>
*
* @author Ashley Scopes
* @since 3.24.0
*/
public class ClassLoaderAssert extends AbstractClassLoaderAssert<ClassLoaderAssert> {

/**
* Initialize this assertion.
*
* @param actual the classloader to perform assertions upon.
*/
protected ClassLoaderAssert(ClassLoader actual) {
super(actual, ClassLoaderAssert.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@ static <VALUE> InstanceOfAssertFactory<Optional, OptionalAssert<VALUE>> optional
InstanceOfAssertFactory<Class, ClassAssert> CLASS = new InstanceOfAssertFactory<>(Class.class,
Assertions::assertThat);

/**
* {@link InstanceOfAssertFactory} for a {@link ClassLoader}.
*
* @since 3.24.0
*/
InstanceOfAssertFactory<ClassLoader, AbstractClassLoaderAssert<?>> CLASS_LOADER = new InstanceOfAssertFactory<>(ClassLoader.class,
Assertions::assertThat);

/**
* {@link InstanceOfAssertFactory} for a {@code double} or its corresponding boxed type {@link Double}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,17 @@ public static AbstractClassAssert<?> assertThat(Class<?> actual) {
return new ClassAssert(actual);
}

/**
* Creates a new instance of <code>{@link ClassLoaderAssert}</code>
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.24.0
*/
public static AbstractClassLoaderAssert<?> assertThat(ClassLoader actual) {
return new ClassLoaderAssert(actual);
}

/**
* Creates a new instance of <code>{@link GenericComparableAssert}</code> with
* standard comparison semantics.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,17 @@ public static AbstractClassAssert<?> then(Class<?> actual) {
return assertThat(actual);
}

/**
* Creates a new instance of <code>{@link org.assertj.core.api.ClassLoaderAssert}</code>
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.24.0
*/
public static AbstractClassLoaderAssert<?> then(ClassLoader actual) {
return assertThat(actual);
}

/**
* Creates a new instance of <code>{@link CollectionAssert}</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ default ClassAssert then(Class<?> actual) {
return proxy(ClassAssert.class, Class.class, actual);
}

/**
* Creates a new instance of <code>{@link ClassLoaderAssert}</code> assumption.
*
* @param actual the actual value.
* @return the created assumption for assertion object.
* @since 3.24.0
*/
default AbstractClassLoaderAssert<?> then(ClassLoader actual) {
return proxy(ClassLoaderAssert.class, ClassLoader.class, actual);
}

/**
* Creates a new instance of <code>{@link CollectionAssert}</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ default ClassAssert assertThat(Class<?> actual) {
return proxy(ClassAssert.class, Class.class, actual);
}

/**
* Creates a new instance of <code>{@link ClassLoaderAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.24.0
*/
default AbstractClassLoaderAssert<?> assertThat(ClassLoader actual) {
return proxy(ClassLoaderAssert.class, ClassLoader.class, actual);
}

/**
* Creates a new instance of <code>{@link CollectionAssert}</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,17 @@ default ClassAssert assertThat(final Class<?> actual) {
return Assertions.assertThat(actual);
}

/**
* Creates a new instance of <code>{@link ClassLoaderAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
* @since 3.24.0
*/
default AbstractClassLoaderAssert<?> assertThat(final ClassLoader actual) {
return Assertions.assertThat(actual);
}

/**
* Creates a new instance of <code>{@link CharacterAssert}</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,16 @@ default ClassAssert assumeThat(final Class<?> actual) {
return Assumptions.assumeThat(actual);
}

/**
* Creates a new instance of <code>{@link ClassLoaderAssert}</code> assumption.
*
* @param actual the actual value.
* @return the created assumption for assertion object.
*/
default AbstractClassLoaderAssert<?> assertThat(final ClassLoader actual) {
return Assumptions.assumeThat(actual);
}

/**
* Creates a new instance of <code>{@link CharacterAssert}</code> assumption.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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-2023 the original author or authors.
*/
package org.assertj.core.api;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import static org.mockito.Mockito.mock;

import org.junit.jupiter.api.Test;

/**
* @author Ashley Scopes
*/
class Assertions_assertThat_with_ClassLoader_Test {

private final ClassLoader actual = mock(ClassLoader.class);

@Test
void should_create_Assert() {
// WHEN
AbstractClassLoaderAssert<?> assertions = assertThat(actual);
// THEN
then(assertions).isNotNull();
}

@Test
void should_pass_actual() {
// WHEN
AbstractClassLoaderAssert<?> assertions = assertThat(actual);
// THEN
then(assertions).extracting("actual").isSameAs(actual);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import static org.assertj.core.api.InstanceOfAssertFactories.CHAR_ARRAY;
import static org.assertj.core.api.InstanceOfAssertFactories.CHAR_SEQUENCE;
import static org.assertj.core.api.InstanceOfAssertFactories.CLASS;
import static org.assertj.core.api.InstanceOfAssertFactories.CLASS_LOADER;
import static org.assertj.core.api.InstanceOfAssertFactories.COLLECTION;
import static org.assertj.core.api.InstanceOfAssertFactories.COMPLETABLE_FUTURE;
import static org.assertj.core.api.InstanceOfAssertFactories.COMPLETION_STAGE;
Expand Down Expand Up @@ -461,6 +462,17 @@ void class_factory_should_allow_class_assertions() {
result.hasAnnotations(FunctionalInterface.class);
}

@Test
void class_loader_factory_should_allow_class_loader_assertions() {
// GIVEN
Object value = Assertions.class.getClassLoader();
// WHEN
AbstractClassLoaderAssert<?> result = assertThat(value).asInstanceOf(CLASS_LOADER);
// THEN
// FIXME replace with class loader specific assertion
result.isSameAs(Assertions.class.getClassLoader());
}

@Test
void double_factory_should_allow_double_assertions() {
// GIVEN
Expand Down
Loading