1 /** 2 * Copyright (c) 2008, http://www.snakeyaml.org 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.yaml.snakeyaml.issues.issue306; 17 18 import org.junit.Test; 19 import org.yaml.snakeyaml.DumperOptions; 20 import org.yaml.snakeyaml.Util; 21 import org.yaml.snakeyaml.Yaml; 22 import org.yaml.snakeyaml.nodes.Tag; 23 24 import java.util.UUID; 25 import java.util.regex.Pattern; 26 27 import static org.junit.Assert.assertEquals; 28 import static org.junit.Assert.assertTrue; 29 30 public class UuidSupportTest { 31 32 public static final Pattern UUID_PATTERN = Pattern 33 .compile("^(?:\\p{XDigit}{8}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{12})$"); 34 public static final Tag UUID_TAG = new Tag(Tag.PREFIX + "java.util.UUID"); 35 36 @Test 37 public void pattern() { 38 assertTrue(UUID_PATTERN.matcher("7f511847-781a-45df-9c8d-1e32e028b9b3").matches()); 39 assertTrue(UUID_PATTERN.matcher("AC4877BE-0C31-4458-A86E-0272EFE1AAA8").matches()); 40 } 41 42 @Test 43 public void dumpAsString() { 44 UUID uuid = UUID.randomUUID(); 45 String str = uuid.toString(); 46 Yaml yaml = new Yaml(); 47 yaml.addImplicitResolver(UUID_TAG, UUID_PATTERN, null); 48 String output = yaml.dump(str); 49 assertEquals("'" + str + "'\n", output); 50 assertEquals(str + "\n", yaml.dump(uuid)); 51 } 52 53 @Test 54 public void loadAsUuid() { 55 Yaml yaml = new Yaml(); 56 yaml.addImplicitResolver(UUID_TAG, UUID_PATTERN, null); 57 UUID uuid = (UUID) yaml.load("7f511847-781a-45df-9c8d-1e32e028b9b3"); 58 assertEquals("7f511847-781a-45df-9c8d-1e32e028b9b3", uuid.toString()); 59 } 60 61 @Test 62 public void loadFromBean() { 63 String input = Util.getLocalResource("issues/issue306-1.yaml"); 64 Yaml yaml = new Yaml(); 65 BeanWithId bean = yaml.loadAs(input, BeanWithId.class); 66 assertEquals("7f511847-781a-45df-9c8d-1e32e028b9b3", bean.getId().toString()); 67 } 68 69 @Test 70 public void dumpUuid() { 71 UUID uuid = UUID.randomUUID(); 72 Yaml yaml = new Yaml(); 73 String output = yaml.dump(uuid); 74 assertEquals("!!java.util.UUID '" + uuid.toString() + "'\n", output); 75 } 76 77 @Test 78 public void dumpBean() { 79 BeanWithId bean = new BeanWithId(); 80 bean.setValue(3); 81 UUID uuid = UUID.fromString("ac4877be-0c31-4458-a86e-0272efe1aaa8"); 82 bean.setId(uuid); 83 Yaml yaml = new Yaml(); 84 String output = yaml.dumpAs(bean, Tag.MAP, DumperOptions.FlowStyle.BLOCK); 85 String expected = Util.getLocalResource("issues/issue306-2.yaml"); 86 assertEquals(expected, output); 87 } 88 } 89