xref: /aosp_15_r20/external/snakeyaml/src/test/java/examples/CustomJavaObjectWithBinaryStringTest.java (revision ac2a7c1bf4e14d82f3bd566dcc2d76d5b42faf34)
1 /**
2  * Copyright (c) 2008, SnakeYAML
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package examples;
15 
16 import java.io.StringReader;
17 import junit.framework.TestCase;
18 import org.yaml.snakeyaml.Yaml;
19 
20 public class CustomJavaObjectWithBinaryStringTest extends TestCase {
21 
22   public static class Pojo {
23 
24     private String data;
25 
Pojo()26     public Pojo() {}
27 
Pojo(String data)28     public Pojo(String data) {
29       this.data = data;
30     }
31 
getData()32     public String getData() {
33       return data;
34     }
35 
setData(String data)36     public void setData(String data) {
37       this.data = data;
38     }
39 
40     @Override
hashCode()41     public int hashCode() {
42       final int prime = 31;
43       int result = 1;
44       result = prime * result + ((data == null) ? 0 : data.hashCode());
45       return result;
46     }
47 
48     @Override
equals(Object obj)49     public boolean equals(Object obj) {
50       if (this == obj) {
51         return true;
52       }
53       if (obj == null) {
54         return false;
55       }
56       if (getClass() != obj.getClass()) {
57         return false;
58       }
59       Pojo other = (Pojo) obj;
60       if (data == null) {
61         return other.data == null;
62       } else {
63         return data.equals(other.data);
64       }
65     }
66 
67   }
68 
testDump()69   public void testDump() {
70     Yaml yaml = new Yaml();
71     Pojo expected = new Pojo(new String(new byte[] {13, 14, 15, 16}));
72     String output = yaml.dump(expected);
73 
74     assertTrue(output.contains("data: !!binary |-"));
75     assertTrue(output.contains("DQ4PEA=="));
76 
77     Pojo actual = yaml.load(new StringReader(output));
78     assertEquals(expected, actual);
79   }
80 
81 }
82