Skip to content

Commit

Permalink
Merge pull request #63 from mercyblitz/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
mercyblitz authored Jan 6, 2025
2 parents 63d3d2a + b116b01 commit 47a0400
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@

import io.microsphere.util.BaseUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.FileUrlResource;
import org.springframework.core.io.Resource;

import static io.microsphere.util.ClassLoaderUtils.resolveClass;

/**
* The utilities class for {@link Resource}
*
Expand All @@ -31,28 +30,14 @@
*/
public abstract class ResourceUtils extends BaseUtils {

/**
* The class name of {@linkplain org.springframework.core.io.FileUrlResource}
*
* @since Spring Framework 5.0.2
*/
public static final String FILE_URL_RESOURCE_CLASS_NAME = "org.springframework.core.io.FileUrlResource";

/**
* The {@link Class} of {@linkplain org.springframework.core.io.FileUrlResource}
*
* @since Spring Framework 5.0.2
*/
public static final Class FILE_URL_RESOURCE_CLASS = resolveClass(FILE_URL_RESOURCE_CLASS_NAME, ResourceUtils.class.getClassLoader());

/**
* Determine whether the specified {@link Resource} is a {@linkplain org.springframework.core.io.FileUrlResource}
*
* @param resource {@link Resource}
* @return <code>true</code> if the specified {@link Resource} is a {@linkplain org.springframework.core.io.FileUrlResource}
*/
public static boolean isFileUrlResource(Resource resource) {
return FILE_URL_RESOURCE_CLASS != null && FILE_URL_RESOURCE_CLASS.isInstance(resource);
return resource instanceof FileUrlResource;
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,38 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.util.List;

import static io.microsphere.collection.SetUtils.ofSet;
import static io.microsphere.spring.beans.factory.BeanFactoryUtils.asAutowireCapableBeanFactory;
import static io.microsphere.spring.beans.factory.BeanFactoryUtils.asBeanDefinitionRegistry;
import static io.microsphere.spring.beans.factory.BeanFactoryUtils.asConfigurableBeanFactory;
import static io.microsphere.spring.beans.factory.BeanFactoryUtils.asConfigurableListableBeanFactory;
import static io.microsphere.spring.beans.factory.BeanFactoryUtils.asDefaultListableBeanFactory;
import static io.microsphere.spring.beans.factory.BeanFactoryUtils.asHierarchicalBeanFactory;
import static io.microsphere.spring.beans.factory.BeanFactoryUtils.asListableBeanFactory;
import static io.microsphere.spring.beans.factory.BeanFactoryUtils.getBeanPostProcessors;
import static io.microsphere.spring.beans.factory.BeanFactoryUtils.getBeans;
import static io.microsphere.spring.beans.factory.BeanFactoryUtils.getOptionalBean;
import static io.microsphere.spring.beans.factory.BeanFactoryUtils.getResolvableDependencyTypes;
import static io.microsphere.spring.beans.factory.BeanFactoryUtils.isBeanDefinitionRegistry;
import static io.microsphere.spring.beans.factory.BeanFactoryUtils.isDefaultListableBeanFactory;
import static io.microsphere.spring.context.ApplicationContextUtils.APPLICATION_CONTEXT_AWARE_PROCESSOR_CLASS_NAME;
import static io.microsphere.util.ArrayUtils.of;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
Expand All @@ -41,22 +62,25 @@ public class BeanFactoryUtilsTest {

private AnnotationConfigApplicationContext applicationContext;

private ConfigurableListableBeanFactory beanFactory;

@BeforeEach
public void init() {
applicationContext = new AnnotationConfigApplicationContext();
this.applicationContext = new AnnotationConfigApplicationContext();
this.beanFactory = this.applicationContext.getBeanFactory();
}

@AfterEach
public void afterTest() {
applicationContext.close();
this.applicationContext.close();
}

@Test
public void testGetOptionalBean() {

applicationContext.register(BaseTestBean.class);
this.applicationContext.register(BaseTestBean.class);

applicationContext.refresh();
this.applicationContext.refresh();

BaseTestBean testBean = getOptionalBean(applicationContext, "baseTestBean", BaseTestBean.class);

Expand All @@ -69,7 +93,7 @@ public void testGetOptionalBean() {
@Test
public void testGetOptionalBeanIfAbsent() {

applicationContext.refresh();
this.applicationContext.refresh();

BaseTestBean testBean = getOptionalBean(applicationContext, "baseTestBean", BaseTestBean.class);

Expand All @@ -87,9 +111,9 @@ public void testGetOptionalBeanIfAbsent() {
@Test
public void testGetBeans() {

applicationContext.register(BaseTestBean.class, BaseTestBean2.class);
this.applicationContext.register(BaseTestBean.class, BaseTestBean2.class);

applicationContext.refresh();
this.applicationContext.refresh();

List<BaseTestBean> testBeans = getBeans(applicationContext, new String[]{"baseTestBean"}, BaseTestBean.class);

Expand All @@ -113,14 +137,46 @@ public void testGetBeans() {
@Test
public void testGetBeansIfAbsent() {

applicationContext.refresh();
this.applicationContext.refresh();

List<BaseTestBean> testBeans = getBeans(applicationContext, new String[]{"baseTestBean"}, BaseTestBean.class);

assertTrue(testBeans.isEmpty());

}

@Test
public void testIsMethods() {
assertTrue(isDefaultListableBeanFactory(this.beanFactory));
assertTrue(isBeanDefinitionRegistry(this.beanFactory));
}

@Test
public void testAsMethods() {
assertSame(this.beanFactory, asBeanDefinitionRegistry(this.beanFactory));
assertSame(this.beanFactory, asListableBeanFactory(this.beanFactory));
assertSame(this.beanFactory, asHierarchicalBeanFactory(this.beanFactory));
assertSame(this.beanFactory, asConfigurableBeanFactory(this.beanFactory));
assertSame(this.beanFactory, asAutowireCapableBeanFactory(this.beanFactory));
assertSame(this.beanFactory, asConfigurableListableBeanFactory(this.beanFactory));
assertSame(this.beanFactory, asDefaultListableBeanFactory(this.beanFactory));
}

@Test
public void testGetResolvableDependencyTypes() {
this.applicationContext.refresh();
assertEquals(ofSet(BeanFactory.class, ResourceLoader.class, ApplicationEventPublisher.class, ApplicationContext.class),
getResolvableDependencyTypes(this.beanFactory));
}

@Test
public void testGetBeanPostProcessors() {
this.applicationContext.refresh();
List<BeanPostProcessor> beanPostProcessors = getBeanPostProcessors(this.beanFactory);
assertFalse(beanPostProcessors.isEmpty());
assertEquals(APPLICATION_CONTEXT_AWARE_PROCESSOR_CLASS_NAME, beanPostProcessors.get(0).getClass().getName());
}


@Component("baseTestBean2")
private static class BaseTestBean2 extends BaseTestBean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.function.Function;

import static io.microsphere.spring.core.io.ResourceLoaderUtils.getResourceLoader;
import static io.microsphere.spring.core.io.ResourceUtils.FILE_URL_RESOURCE_CLASS;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
Expand Down Expand Up @@ -55,13 +54,13 @@ public void init() throws IOException {
@Test
public void testIsFileUrlResource() {
assertResource(this.classPathResource, ResourceUtils::isFileUrlResource, false);
assertResource(this.urlResource, ResourceUtils::isFileUrlResource, FILE_URL_RESOURCE_CLASS != null);
assertResource(this.urlResource, ResourceUtils::isFileUrlResource, true);
}

@Test
public void testIsFileBasedResource() throws IOException {
assertResource(this.classPathResource, ResourceUtils::isFileBasedResource, false);
assertResource(this.urlResource, ResourceUtils::isFileBasedResource, FILE_URL_RESOURCE_CLASS != null);
assertResource(this.urlResource, ResourceUtils::isFileBasedResource, true);
assertResource(new FileSystemResource(this.urlResource.getFile()), ResourceUtils::isFileBasedResource, true);
}

Expand Down

This file was deleted.

Loading

0 comments on commit 47a0400

Please sign in to comment.