10fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov/**
20fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov * Copyright (c) 2008, http://www.snakeyaml.org
30fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov *
40fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov * Licensed under the Apache License, Version 2.0 (the "License");
50fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov * you may not use this file except in compliance with the License.
60fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov * You may obtain a copy of the License at
70fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov *
80fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov *     http://www.apache.org/licenses/LICENSE-2.0
90fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov *
100fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov * Unless required by applicable law or agreed to in writing, software
110fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov * distributed under the License is distributed on an "AS IS" BASIS,
120fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov * See the License for the specific language governing permissions and
140fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov * limitations under the License.
150fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov */
160fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somovpackage examples;
170fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov
180fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somovimport java.io.StringReader;
190fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov
200fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somovimport junit.framework.TestCase;
210fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov
220fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somovimport org.yaml.snakeyaml.Yaml;
230fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov
240fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somovpublic class CustomJavaObjectWithBinaryStringTest extends TestCase {
250fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov    public static class Pojo {
260fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        private String data;
270fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov
280fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        public Pojo() {
290fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        }
300fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov
310fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        public Pojo(String data) {
320fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov            this.data = data;
330fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        }
340fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov
350fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        public String getData() {
360fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov            return data;
370fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        }
380fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov
390fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        public void setData(String data) {
400fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov            this.data = data;
410fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        }
420fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov
430fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        @Override public int hashCode() {
440fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov            final int prime = 31;
450fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov            int result = 1;
460fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov            result = prime * result + ((data == null) ? 0 : data.hashCode());
470fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov            return result;
480fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        }
490fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov
500fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        @Override public boolean equals(Object obj) {
510fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov            if (this == obj)
520fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov                return true;
530fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov            if (obj == null)
540fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov                return false;
550fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov            if (getClass() != obj.getClass())
560fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov                return false;
570fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov            Pojo other = (Pojo) obj;
580fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov            if (data == null) {
590fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov                if (other.data != null)
600fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov                    return false;
610fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov            } else if (!data.equals(other.data))
620fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov                return false;
630fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov            return true;
640fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        }
650fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov
660fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov    }
670fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov
680fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov    public void testDump() {
690fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        Yaml yaml = new Yaml();
700fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        Pojo expected = new Pojo(new String(new byte[] { 13, 14, 15, 16 }));
710fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        String output = yaml.dump(expected);
720fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov
730fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        assertTrue(output.contains("data: !!binary |-"));
740fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        assertTrue(output.contains("DQ4PEA=="));
750fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov
760fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        Pojo actual = (Pojo) yaml.load(new StringReader(output));
770fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov        assertEquals(expected, actual);
780fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov    }
790fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov
800fca80757efbb4219edb6a0173e4f6365c8903f9Andrey Somov}
81