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

test: add yaml loadAs test #14080

Open
wants to merge 4 commits into
base: 3.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 org.apache.dubbo.rpc.cluster.yaml;

/**
* @author yuluo
*/
public class Person {

private Integer age;

private String name;

public void setAge(Integer age) {

this.age = age;
}

public void setName(String name) {

this.name = name;
}

public Integer getAge() {

return age;
}

public String getName() {

return name;
}

@Override
public String toString() {

return "Person: {age: " + age + "}, " + "{name: " + name + "}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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 org.apache.dubbo.rpc.cluster.yaml;

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.error.YAMLException;

/**
* @author yuluo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the author tag. THX

*/
class YamlCodeCTest {

@Test
void testYamlLoadAs() {

Yaml yaml = new Yaml();

InputStream yamlResources = this.getClass().getClassLoader().getResourceAsStream("person.yaml");

Person person = yaml.loadAs(yamlResources, Person.class);

Assertions.assertNotNull(yamlResources);
Assertions.assertNotNull(person);

Assertions.assertEquals(20, person.getAge());
Assertions.assertEquals("test", person.getName());
}

@Test
void testInvalidYamlLoadAs() {

Yaml yaml = new Yaml();

InputStream invalidYamlStream = this.getClass().getClassLoader().getResourceAsStream("InvalidYamlFile.yaml");

Assertions.assertThrows(YAMLException.class, () -> yaml.loadAs(invalidYamlStream, Person.class));
}

@Test
void testStringLoadAs() throws IOException {

Yaml yaml = new Yaml();

InputStream resource = this.getClass().getClassLoader().getResourceAsStream("person.yaml");

Assertions.assertNotNull(resource);

Scanner scanner = new Scanner(resource).useDelimiter("\\A");
String yamlString = scanner.hasNext() ? scanner.next() : "";
resource.close();
scanner.close();

Person person = yaml.loadAs(yamlString, Person.class);

Assertions.assertNotNull(person);
Assertions.assertEquals(person.getAge(), 20);
Assertions.assertEquals(person.getName(), "test");

Assertions.assertThrows(YAMLException.class, () -> yaml.loadAs("invalied", Person.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.
*/

/**
* For yamlCodeC test. #13799
*/
package org.apache.dubbo.rpc.cluster.yaml;
21 changes: 21 additions & 0 deletions dubbo-cluster/src/test/resources/person.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
#
# 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.
#
#

age: 20
name: test