Skip to content

Commit

Permalink
[ISSUE nacos-group#315] Support spring 6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperZ1999 committed Sep 28, 2023
1 parent e8cae2c commit e2e0153
Show file tree
Hide file tree
Showing 16 changed files with 1,072 additions and 14 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor;
import com.alibaba.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor;
import com.alibaba.spring.util.BeanUtils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.Map;

import com.alibaba.nacos.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
Expand All @@ -48,7 +49,6 @@
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.spring.context.event.config.NacosConfigReceivedEvent;
import com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor;

/**
* Injected {@link NacosValue}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. See the NOTICE file distributed with
* * this work for additional information regarding copyright ownership.
* * The ASF licenses this file to You 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.
*
*/

package com.alibaba.nacos.spring.beans.factory.annotation;

import org.junit.Assert;
import org.junit.Test;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;

public class AbstractAnnotationBeanPostProcessorTest {
@Test
public void testPropertyDescriptor() throws IntrospectionException {
PropertyDescriptor pd = new PropertyDescriptor("name", Student.class);
Class<?> type = pd.getPropertyType();
Assert.assertEquals(type, String.class);
}

static class Student {
String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. See the NOTICE file distributed with
* * this work for additional information regarding copyright ownership.
* * The ASF licenses this file to You 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.
*
*/

package com.alibaba.nacos.spring.context.annotation.config;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.core.env.MapPropertySource;

import java.util.HashMap;

public class NacosPropertySourceBuilderTest {
@Test
public void test() {
MapPropertySource propertySource = new MapPropertySource("test", new HashMap<>());
Assert.assertNotNull(propertySource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. See the NOTICE file distributed with
* * this work for additional information regarding copyright ownership.
* * The ASF licenses this file to You 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.
*
*/

package com.alibaba.nacos.spring.context.annotation.config;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.TypeConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.MethodParameter;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.lang.reflect.Method;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = NacosValueAnnotationBeanPostProcessorTest.class)
public class NacosValueAnnotationBeanPostProcessorTest {

@Autowired
private ConfigurableListableBeanFactory beanFactory;

@Bean
public NacosValueAnnotationBeanPostProcessor nacosValueAnnotationBeanPostProcessor() {
return new NacosValueAnnotationBeanPostProcessor();
}

@Test
public void testConvertIfNecessary() throws NoSuchMethodException {
TypeConverter converter = beanFactory.getTypeConverter();
Method method = NacosValueAnnotationBeanPostProcessorTest.class.getMethod("testMethodParameter", Integer.class);
Integer integer = converter.convertIfNecessary("12", Integer.class, new MethodParameter(method, 0));
System.out.println(integer);
Assert.assertEquals(integer, Integer.valueOf(12));
}

public void testMethodParameter(Integer i) {
System.out.println(i);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. See the NOTICE file distributed with
* * this work for additional information regarding copyright ownership.
* * The ASF licenses this file to You 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.
*
*/

package com.alibaba.nacos.spring.context.config.xml;

import org.junit.Assert;
import org.junit.Test;

public class NacosPropertySourceXmlBeanDefinitionTest {
@Test
public void test() {
NacosPropertySourceXmlBeanDefinition beanDefinition = new NacosPropertySourceXmlBeanDefinition();
Assert.assertNotNull(beanDefinition);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. See the NOTICE file distributed with
* * this work for additional information regarding copyright ownership.
* * The ASF licenses this file to You 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.
*
*/

package com.alibaba.nacos.spring.context.properties.config;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.validation.BindException;
import org.springframework.validation.DataBinder;

public class DataBinderTest {
@Test
public void test() throws BindException {
People people = new People();
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("name", "SuperZ1999");
propertyValues.add("age", 24);
DataBinder dataBinder = new DataBinder(people);
dataBinder.setAutoGrowNestedPaths(false);
dataBinder.setIgnoreInvalidFields(false);
dataBinder.setIgnoreUnknownFields(true);
dataBinder.bind(propertyValues);
dataBinder.close();

Assert.assertEquals("SuperZ1999", people.getName());
Assert.assertEquals(24, people.getAge());
}

static class People {
private String name;
private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. See the NOTICE file distributed with
* * this work for additional information regarding copyright ownership.
* * The ASF licenses this file to You 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.
*
*/

package com.alibaba.nacos.spring.factory;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Service;
import com.alibaba.nacos.client.naming.NacosNamingMaintainService;
import org.junit.Assert;
import org.junit.Test;

public class DelegatingNamingMaintainServiceTest {
@Test
public void testNamingMaintainService() throws NacosException {
NacosNamingMaintainService namingMaintainService = new NacosNamingMaintainService("127.0.0.1:8848");
Service service = namingMaintainService.queryService("example");
Assert.assertNotNull(service);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. See the NOTICE file distributed with
* * this work for additional information regarding copyright ownership.
* * The ASF licenses this file to You 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.
*
*/

package com.alibaba.nacos.spring.factory;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.client.naming.NacosNamingService;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

public class DelegatingNamingServiceTest {
@Test
public void testNamingService() throws NacosException {
NacosNamingService nacosNamingService = new NacosNamingService("127.0.0.1:8848");
List<Instance> instances = nacosNamingService.getAllInstances("example");
System.out.println(instances);
Assert.assertNotNull(instances);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.alibaba.nacos.spring.util.parse.DefaultPropertiesConfigParse;
import com.alibaba.nacos.spring.util.parse.DefaultYamlConfigParse;
import org.springframework.core.io.ByteArrayResource;

/**
* @author liaochuntao
Expand Down Expand Up @@ -83,6 +84,13 @@ public void testPropertiesParser() {
System.out.println(p);
}

@Test
public void testResource() {
ByteArrayResource resource = new ByteArrayResource(new byte[]{1, 1, 1, 1, 1, 1});
String description = resource.getDescription();
Assert.assertNotNull(description);
}

@Test
public void testYamlParser() {
final String yaml = "students:\n" + " - {name: lct-1,num: 12}\n"
Expand Down
Loading

0 comments on commit e2e0153

Please sign in to comment.