-
Notifications
You must be signed in to change notification settings - Fork 26.3k
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
yuluo-yx
wants to merge
4
commits into
apache:3.2
Choose a base branch
from
yuluo-yx:0413-yuluo/add-yaml-test
base: 3.2
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
test: add yaml loadAs test #14080
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/yaml/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "}"; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/yaml/YamlCodeCTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ | ||
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)); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/yaml/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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