diff options
Diffstat (limited to 'src/test/java/org/yaml/snakeyaml/issues')
218 files changed, 10746 insertions, 6845 deletions
diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue10/BasicDumpTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue10/BasicDumpTest.java index a16c4e27..1b88b499 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue10/BasicDumpTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue10/BasicDumpTest.java @@ -1,99 +1,95 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue10; import java.util.ArrayList; import java.util.Iterator; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; public class BasicDumpTest extends TestCase { - public void testTag() { - DataSource base = new DataSource(); - JDBCDataSource baseJDBC = new JDBCDataSource(); - baseJDBC.setParent(base); - - ArrayList<DataSource> dataSources = new ArrayList<DataSource>(); - // trying expected order first - dataSources.add(base); - dataSources.add(baseJDBC); - - DataSources ds = new DataSources(); - ds.setDataSources(dataSources); - - Yaml yaml = new Yaml(); - String output = yaml.dump(ds); - - String etalon = Util.getLocalResource("javabeans/issue10-1.yaml"); - assertEquals(etalon.trim(), output.trim()); - Object obj = yaml.load(output); - DataSources dsOut = (DataSources) obj; - Iterator<DataSource> iter = dsOut.getDataSources().iterator(); - assertFalse("Must be DataSource.", iter.next() instanceof JDBCDataSource); - assertTrue(iter.next() instanceof JDBCDataSource); - } - - public void testTag2() { - DataSource base = new DataSource(); - JDBCDataSource baseJDBC = new JDBCDataSource(); - baseJDBC.setParent(base); - - ArrayList<DataSource> dataSources = new ArrayList<DataSource>(); - dataSources.add(base); - dataSources.add(baseJDBC); - - DataSources ds = new DataSources(); - ds.setDataSources(dataSources); - - Yaml yaml = new Yaml(); - String output = yaml.dumpAsMap(ds); - - String etalon = Util.getLocalResource("javabeans/issue10-2.yaml"); - assertEquals(etalon.trim(), output.trim()); - } - - /** - * different order does not require the global tag - */ - public void testTag3() { - DataSource base = new DataSource(); - JDBCDataSource baseJDBC = new JDBCDataSource(); - baseJDBC.setParent(base); - - ArrayList<DataSource> dataSources = new ArrayList<DataSource>(); - dataSources.add(baseJDBC); - dataSources.add(base); - - DataSources ds = new DataSources(); - ds.setDataSources(dataSources); - - Yaml yaml = new Yaml(); - String output = yaml.dumpAsMap(ds); - - String etalon = Util.getLocalResource("javabeans/issue10-3.yaml"); - assertEquals(etalon.trim(), output.trim()); - // load - Yaml beanLoader = new Yaml(); - DataSources bean = beanLoader.loadAs(output, DataSources.class); - Iterator<DataSource> iter = bean.getDataSources().iterator(); - assertTrue(iter.next() instanceof JDBCDataSource); - assertFalse("Must be DataSource.", iter.next() instanceof JDBCDataSource); - } + public void testTag() { + DataSource base = new DataSource(); + JDBCDataSource baseJDBC = new JDBCDataSource(); + baseJDBC.setParent(base); + + ArrayList<DataSource> dataSources = new ArrayList<DataSource>(); + // trying expected order first + dataSources.add(base); + dataSources.add(baseJDBC); + + DataSources ds = new DataSources(); + ds.setDataSources(dataSources); + + Yaml yaml = new Yaml(); + String output = yaml.dump(ds); + + String etalon = Util.getLocalResource("javabeans/issue10-1.yaml"); + assertEquals(etalon.trim(), output.trim()); + Object obj = yaml.load(output); + DataSources dsOut = (DataSources) obj; + Iterator<DataSource> iter = dsOut.getDataSources().iterator(); + assertFalse("Must be DataSource.", iter.next() instanceof JDBCDataSource); + assertTrue(iter.next() instanceof JDBCDataSource); + } + + public void testTag2() { + DataSource base = new DataSource(); + JDBCDataSource baseJDBC = new JDBCDataSource(); + baseJDBC.setParent(base); + + ArrayList<DataSource> dataSources = new ArrayList<DataSource>(); + dataSources.add(base); + dataSources.add(baseJDBC); + + DataSources ds = new DataSources(); + ds.setDataSources(dataSources); + + Yaml yaml = new Yaml(); + String output = yaml.dumpAsMap(ds); + + String etalon = Util.getLocalResource("javabeans/issue10-2.yaml"); + assertEquals(etalon.trim(), output.trim()); + } + + /** + * different order does not require the global tag + */ + public void testTag3() { + DataSource base = new DataSource(); + JDBCDataSource baseJDBC = new JDBCDataSource(); + baseJDBC.setParent(base); + + ArrayList<DataSource> dataSources = new ArrayList<DataSource>(); + dataSources.add(baseJDBC); + dataSources.add(base); + + DataSources ds = new DataSources(); + ds.setDataSources(dataSources); + + Yaml yaml = new Yaml(); + String output = yaml.dumpAsMap(ds); + + String etalon = Util.getLocalResource("javabeans/issue10-3.yaml"); + assertEquals(etalon.trim(), output.trim()); + // load + Yaml beanLoader = new Yaml(); + DataSources bean = beanLoader.loadAs(output, DataSources.class); + Iterator<DataSource> iter = bean.getDataSources().iterator(); + assertTrue(iter.next() instanceof JDBCDataSource); + assertFalse("Must be DataSource.", iter.next() instanceof JDBCDataSource); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue10/DataSource.java b/src/test/java/org/yaml/snakeyaml/issues/issue10/DataSource.java index 3063b9ea..0874ac12 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue10/DataSource.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue10/DataSource.java @@ -1,28 +1,27 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue10; public class DataSource { - String name; - public String getName() { - return name; - } + String name; - public void setName(String name) { - this.name = name; - } + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue10/DataSources.java b/src/test/java/org/yaml/snakeyaml/issues/issue10/DataSources.java index 995e2ca4..ecdbee37 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue10/DataSources.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue10/DataSources.java @@ -1,30 +1,29 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue10; import java.util.List; public class DataSources { - List<DataSource> dataSources; - public List<DataSource> getDataSources() { - return dataSources; - } + List<DataSource> dataSources; - public void setDataSources(List<DataSource> dataSources) { - this.dataSources = dataSources; - } + public List<DataSource> getDataSources() { + return dataSources; + } + + public void setDataSources(List<DataSource> dataSources) { + this.dataSources = dataSources; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue10/JDBCDataSource.java b/src/test/java/org/yaml/snakeyaml/issues/issue10/JDBCDataSource.java index e80193ea..6f2e2482 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue10/JDBCDataSource.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue10/JDBCDataSource.java @@ -1,56 +1,55 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue10; public class JDBCDataSource extends DataSource { - String username; - String password; - String url; - DataSource parent; + String username; + String password; + String url; - public DataSource getParent() { - return parent; - } + DataSource parent; - public void setParent(DataSource parent) { - this.parent = parent; - } + public DataSource getParent() { + return parent; + } - public String getUsername() { - return username; - } + public void setParent(DataSource parent) { + this.parent = parent; + } - public void setUsername(String username) { - this.username = username; - } + public String getUsername() { + return username; + } - public String getPassword() { - return password; - } + public void setUsername(String username) { + this.username = username; + } - public void setPassword(String password) { - this.password = password; - } + public String getPassword() { + return password; + } - public String getUrl() { - return url; - } + public void setPassword(String password) { + this.password = password; + } - public void setUrl(String url) { - this.url = url; - } + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue100/Data.java b/src/test/java/org/yaml/snakeyaml/issues/issue100/Data.java index 1596802a..08d794f7 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue100/Data.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue100/Data.java @@ -1,65 +1,64 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue100; public class Data { - private String id; - private int age; - public Data() { - this.id = null; - this.age = 999; - } + private String id; + private int age; - public Data(String id, int age) { - this.id = id; - this.age = age; - } + public Data() { + this.id = null; + this.age = 999; + } - public String getId() { - return id; - } + public Data(String id, int age) { + this.id = id; + this.age = age; + } - public void setId(String id) { - this.id = id; - } + public String getId() { + return id; + } - public int getAge() { - return age; - } + public void setId(String id) { + this.id = id; + } - public void setAge(int age) { - this.age = age; - } + public int getAge() { + return age; + } - @Override - public String toString() { - return "Data [age=" + age + ", id=" + id + "]"; - } + public void setAge(int age) { + this.age = age; + } - @Override - public boolean equals(Object obj) { - if (obj instanceof Data) { - return toString().equals(obj.toString()); - } - return false; - } + @Override + public String toString() { + return "Data [age=" + age + ", id=" + id + "]"; + } - @Override - public int hashCode() { - return toString().hashCode(); + @Override + public boolean equals(Object obj) { + if (obj instanceof Data) { + return toString().equals(obj.toString()); } + return false; + } + + @Override + public int hashCode() { + return toString().hashCode(); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue100/DataBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue100/DataBean.java index cd311714..a60b25af 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue100/DataBean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue100/DataBean.java @@ -1,47 +1,46 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue100; public class DataBean { - private String id; - private Data data; - private DataMore more; - public String getId() { - return id; - } + private String id; + private Data data; + private DataMore more; - public void setId(String id) { - this.id = id; - } + public String getId() { + return id; + } - public Data getData() { - return data; - } + public void setId(String id) { + this.id = id; + } - public void setData(Data data) { - this.data = data; - } + public Data getData() { + return data; + } - public DataMore getMore() { - return more; - } + public void setData(Data data) { + this.data = data; + } - public void setMore(DataMore more) { - this.more = more; - } + public DataMore getMore() { + return more; + } + + public void setMore(DataMore more) { + this.more = more; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue100/DataMore.java b/src/test/java/org/yaml/snakeyaml/issues/issue100/DataMore.java index 4d4dff31..5ff96502 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue100/DataMore.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue100/DataMore.java @@ -1,43 +1,42 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue100; public class DataMore extends Data { - private boolean complete; - public DataMore() { - setId("-1"); - setAge(-1); - } + private boolean complete; - public DataMore(String id, int age, boolean complete) { - super(id, age); - this.complete = complete; - } + public DataMore() { + setId("-1"); + setAge(-1); + } - public boolean isComplete() { - return complete; - } + public DataMore(String id, int age, boolean complete) { + super(id, age); + this.complete = complete; + } - public void setComplete(boolean complete) { - this.complete = complete; - } + public boolean isComplete() { + return complete; + } - @Override - public String toString() { - return "DataMore [complete=" + complete + ", toString()=" + super.toString() + "]"; - } + public void setComplete(boolean complete) { + this.complete = complete; + } + + @Override + public String toString() { + return "DataMore [complete=" + complete + ", toString()=" + super.toString() + "]"; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue100/MergeJavaBeanTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue100/MergeJavaBeanTest.java index 21545574..431bd82d 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue100/MergeJavaBeanTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue100/MergeJavaBeanTest.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue100; @@ -19,180 +17,181 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; public class MergeJavaBeanTest extends TestCase { - @SuppressWarnings("unchecked") - public void testNoMerge() { - String input = "- &id001 !!org.yaml.snakeyaml.issues.issue100.Data {age: 11, id: id123}\n- *id001"; - // System.out.println(input); - Yaml yaml = new Yaml(); - List<Data> list = (List<Data>) yaml.load(input); - for (Data data : list) { - // System.out.println(data); - assertEquals("id123", data.getId()); - assertEquals(11, data.getAge()); - } - } - - public void testMergeWithTags() { - String input = Util.getLocalResource("issues/issue100-1.yaml"); - // System.out.println(input); - Yaml yaml = new Yaml(); - - List<?> list = (List<?>) yaml.load(input); - // First object: Data ( 11, "id123" ) - assertEquals(list.get(0).getClass(), Data.class); - assertEquals(11, ((Data) list.get(0)).getAge()); - assertEquals("id123", ((Data) list.get(0)).getId()); - - // Second object: Data ( 13, "id456" ) - assertEquals(list.get(1).getClass(), Data.class); - assertEquals(13, ((Data) list.get(1)).getAge()); - assertEquals("id456", ((Data) list.get(1)).getId()); - - // Third object: Data ( 11, "id789" ) - assertEquals(list.get(2).getClass(), Data.class); - assertEquals(11, ((Data) list.get(2)).getAge()); - assertEquals("id789", ((Data) list.get(2)).getId()); - - // 4th object: DataMore ( 30, "id123" ) - assertEquals(list.get(3).getClass(), DataMore.class); - assertEquals(30, ((DataMore) list.get(3)).getAge()); - assertEquals("id123", ((DataMore) list.get(3)).getId()); - assertTrue(((DataMore) list.get(3)).isComplete()); - - // 5th object: Map ( age: 100 ) - assertTrue(list.get(4) instanceof Map); - assertEquals(1, ((Map<?, ?>) list.get(4)).size()); - assertTrue(((Map<?, ?>) list.get(4)).containsKey("age")); - assertEquals(100, ((Map<?, ?>) list.get(4)).get("age")); - - // 6th object: DataMore ( 100, "id789" ) - assertEquals(list.get(5).getClass(), DataMore.class); - assertEquals(100, ((DataMore) list.get(5)).getAge()); - assertEquals("id789", ((DataMore) list.get(5)).getId()); - assertFalse(((DataMore) list.get(5)).isComplete()); - // All instances except the last Map must be different Data instances - Set<Data> dataSet = new HashSet<Data>(); - for (Object data : list) { - if (data instanceof Data) { - dataSet.add((Data) data); - } - } - assertEquals("Must be all but one Data instances.", list.size() - 1, dataSet.size()); + @SuppressWarnings("unchecked") + public void testNoMerge() { + String input = + "- &id001 !!org.yaml.snakeyaml.issues.issue100.Data {age: 11, id: id123}\n- *id001"; + // System.out.println(input); + Yaml yaml = new Yaml(); + List<Data> list = yaml.load(input); + for (Data data : list) { + // System.out.println(data); + assertEquals("id123", data.getId()); + assertEquals(11, data.getAge()); } - - /** - * Since to explicit tag is provided JavaBean properties are used to create - * a map - */ - @SuppressWarnings("unchecked") - public void testMergeBeanToMap() { - String input = "- &id001 !!org.yaml.snakeyaml.issues.issue100.Data {age: 11, id: id123}\n- << : *id001"; - // System.out.println(input); - Yaml yaml = new Yaml(); - List<Object> list = (List<Object>) yaml.load(input); - // First object: Data ( 11, "id123" ) - Data first = (Data) list.get(0); - assertEquals(11, first.getAge()); - assertEquals("id123", first.getId()); - // Second object is map - Map<?, ?> second = (Map<?, ?>) list.get(1); - assertEquals(11, second.get("age")); - assertEquals("id123", second.get("id")); - } - - @SuppressWarnings("unchecked") - public void testMergeAndDeviate() { - String input = "- &id001 !!org.yaml.snakeyaml.issues.issue100.Data {age: 11, id: id123}\n- <<: *id001\n id: id456"; - // System.out.println(input); - Yaml yaml = new Yaml(); - List<Object> list = (List<Object>) yaml.load(input); - // First object: Data ( 11, "id123" ) - Data first = (Data) list.get(0); - assertEquals(11, first.getAge()); - assertEquals("id123", first.getId()); - // Second object is map with a diffrent id - Map<?, ?> second = (Map<?, ?>) list.get(1); - assertEquals(11, second.get("age")); - assertEquals("id456", second.get("id")); - } - - /** - * <pre> - * Test that the merge-override works correctly in the case of custom classes / data types. - * Two base objects are specified: - * id001 refers to Data ( 11, "id123" ) - * id002 refers to Data ( 37, null ) with a literal null value - * The third object is specified as having the properties of id001, with the properties of - * id002 overriding where they conflict. - * Data ( 37, "id123" ) - * </pre> - */ - @SuppressWarnings("unchecked") - public void testMergeAndDeviateOverride() { - String input = "- &id001 !!org.yaml.snakeyaml.issues.issue100.Data {age: 11, id: id123}\n- &id002 !!org.yaml.snakeyaml.issues.issue100.Data {age: 37}\n- <<: [ *id002, *id001 ]"; - // System.out.println(input); - Yaml yaml = new Yaml(); - List<Data> list = (List<Data>) yaml.load(input); - - // First object: Data ( 11, "id123" ) - assertEquals(11, list.get(0).getAge()); - assertEquals("id123", list.get(0).getId()); - - // Second object: Data ( 37, null ) - assertEquals(37, list.get(1).getAge()); - assertEquals(null, list.get(1).getId()); - - // Third object: map - Map<?, ?> third = (Map<?, ?>) list.get(2); - assertEquals(37, third.get("age")); - assertEquals("id123", third.get("id")); - } - - /** - * When the merged JavaBean is itself a JavaBean property then explicit tag - * is not required - */ - public void testMergeBeanProperty() { - String input = Util.getLocalResource("issues/issue100-3.yaml"); - // System.out.println(input); - Yaml yaml = new Yaml(new Constructor(DataBean.class)); - DataBean bean = (DataBean) yaml.load(input); - assertEquals("id001", bean.getId()); - assertEquals("id002", bean.getData().getId()); - assertEquals(17, bean.getData().getAge()); - assertEquals(17, bean.getMore().getAge()); - assertEquals("more003", bean.getMore().getId()); - assertTrue(bean.getMore().isComplete()); - } - - /** - * Merge map to JavaBean - */ - @SuppressWarnings("unchecked") - public void testMergeMapToJavaBean() { - String input = "- &id001 { age: 11, id: id123 }\n- !!org.yaml.snakeyaml.issues.issue100.Data\n <<: *id001\n id: id456"; - // System.out.println(input); - Yaml yaml = new Yaml(new Constructor()); - List<Object> objects = (List<Object>) yaml.load(input); - assertEquals(2, objects.size()); - // Check first type - Object first = objects.get(0); - Map<Object, Object> firstMap = (Map<Object, Object>) first; - // Check first contents - assertEquals(11, firstMap.get("age")); - assertEquals("id123", firstMap.get("id")); - // Check second contents - Data secondData = (Data) objects.get(1); - assertEquals(11, secondData.getAge()); - assertEquals("id456", secondData.getId()); + } + + public void testMergeWithTags() { + String input = Util.getLocalResource("issues/issue100-1.yaml"); + // System.out.println(input); + Yaml yaml = new Yaml(); + + List<?> list = yaml.load(input); + // First object: Data ( 11, "id123" ) + assertEquals(list.get(0).getClass(), Data.class); + assertEquals(11, ((Data) list.get(0)).getAge()); + assertEquals("id123", ((Data) list.get(0)).getId()); + + // Second object: Data ( 13, "id456" ) + assertEquals(list.get(1).getClass(), Data.class); + assertEquals(13, ((Data) list.get(1)).getAge()); + assertEquals("id456", ((Data) list.get(1)).getId()); + + // Third object: Data ( 11, "id789" ) + assertEquals(list.get(2).getClass(), Data.class); + assertEquals(11, ((Data) list.get(2)).getAge()); + assertEquals("id789", ((Data) list.get(2)).getId()); + + // 4th object: DataMore ( 30, "id123" ) + assertEquals(list.get(3).getClass(), DataMore.class); + assertEquals(30, ((DataMore) list.get(3)).getAge()); + assertEquals("id123", ((DataMore) list.get(3)).getId()); + assertTrue(((DataMore) list.get(3)).isComplete()); + + // 5th object: Map ( age: 100 ) + assertTrue(list.get(4) instanceof Map); + assertEquals(1, ((Map<?, ?>) list.get(4)).size()); + assertTrue(((Map<?, ?>) list.get(4)).containsKey("age")); + assertEquals(100, ((Map<?, ?>) list.get(4)).get("age")); + + // 6th object: DataMore ( 100, "id789" ) + assertEquals(list.get(5).getClass(), DataMore.class); + assertEquals(100, ((DataMore) list.get(5)).getAge()); + assertEquals("id789", ((DataMore) list.get(5)).getId()); + assertFalse(((DataMore) list.get(5)).isComplete()); + // All instances except the last Map must be different Data instances + Set<Data> dataSet = new HashSet<Data>(); + for (Object data : list) { + if (data instanceof Data) { + dataSet.add((Data) data); + } } + assertEquals("Must be all but one Data instances.", list.size() - 1, dataSet.size()); + } + + /** + * Since to explicit tag is provided JavaBean properties are used to create a map + */ + @SuppressWarnings("unchecked") + public void testMergeBeanToMap() { + String input = + "- &id001 !!org.yaml.snakeyaml.issues.issue100.Data {age: 11, id: id123}\n- << : *id001"; + // System.out.println(input); + Yaml yaml = new Yaml(); + List<Object> list = yaml.load(input); + // First object: Data ( 11, "id123" ) + Data first = (Data) list.get(0); + assertEquals(11, first.getAge()); + assertEquals("id123", first.getId()); + // Second object is map + Map<?, ?> second = (Map<?, ?>) list.get(1); + assertEquals(11, second.get("age")); + assertEquals("id123", second.get("id")); + } + + @SuppressWarnings("unchecked") + public void testMergeAndDeviate() { + String input = + "- &id001 !!org.yaml.snakeyaml.issues.issue100.Data {age: 11, id: id123}\n- <<: *id001\n id: id456"; + // System.out.println(input); + Yaml yaml = new Yaml(); + List<Object> list = yaml.load(input); + // First object: Data ( 11, "id123" ) + Data first = (Data) list.get(0); + assertEquals(11, first.getAge()); + assertEquals("id123", first.getId()); + // Second object is map with a diffrent id + Map<?, ?> second = (Map<?, ?>) list.get(1); + assertEquals(11, second.get("age")); + assertEquals("id456", second.get("id")); + } + + /** + * <pre> + * Test that the merge-override works correctly in the case of custom classes / data types. + * Two base objects are specified: + * id001 refers to Data ( 11, "id123" ) + * id002 refers to Data ( 37, null ) with a literal null value + * The third object is specified as having the properties of id001, with the properties of + * id002 overriding where they conflict. + * Data ( 37, "id123" ) + * </pre> + */ + @SuppressWarnings("unchecked") + public void testMergeAndDeviateOverride() { + String input = + "- &id001 !!org.yaml.snakeyaml.issues.issue100.Data {age: 11, id: id123}\n- &id002 !!org.yaml.snakeyaml.issues.issue100.Data {age: 37}\n- <<: [ *id002, *id001 ]"; + // System.out.println(input); + Yaml yaml = new Yaml(); + List<Data> list = yaml.load(input); + + // First object: Data ( 11, "id123" ) + assertEquals(11, list.get(0).getAge()); + assertEquals("id123", list.get(0).getId()); + + // Second object: Data ( 37, null ) + assertEquals(37, list.get(1).getAge()); + assertNull(list.get(1).getId()); + + // Third object: map + Map<?, ?> third = (Map<?, ?>) list.get(2); + assertEquals(37, third.get("age")); + assertEquals("id123", third.get("id")); + } + + /** + * When the merged JavaBean is itself a JavaBean property then explicit tag is not required + */ + public void testMergeBeanProperty() { + String input = Util.getLocalResource("issues/issue100-3.yaml"); + // System.out.println(input); + Yaml yaml = new Yaml(new Constructor(DataBean.class)); + DataBean bean = yaml.load(input); + assertEquals("id001", bean.getId()); + assertEquals("id002", bean.getData().getId()); + assertEquals(17, bean.getData().getAge()); + assertEquals(17, bean.getMore().getAge()); + assertEquals("more003", bean.getMore().getId()); + assertTrue(bean.getMore().isComplete()); + } + + /** + * Merge map to JavaBean + */ + @SuppressWarnings("unchecked") + public void testMergeMapToJavaBean() { + String input = + "- &id001 { age: 11, id: id123 }\n- !!org.yaml.snakeyaml.issues.issue100.Data\n <<: *id001\n id: id456"; + // System.out.println(input); + Yaml yaml = new Yaml(new Constructor()); + List<Object> objects = yaml.load(input); + assertEquals(2, objects.size()); + // Check first type + Object first = objects.get(0); + Map<Object, Object> firstMap = (Map<Object, Object>) first; + // Check first contents + assertEquals(11, firstMap.get("age")); + assertEquals("id123", firstMap.get("id")); + // Check second contents + Data secondData = (Data) objects.get(1); + assertEquals(11, secondData.getAge()); + assertEquals("id456", secondData.getId()); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue100/MergeMapsTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue100/MergeMapsTest.java index c89bc3be..02a1bb25 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue100/MergeMapsTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue100/MergeMapsTest.java @@ -1,42 +1,38 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue100; import java.util.List; import java.util.Map; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; public class MergeMapsTest extends TestCase { - @SuppressWarnings("unchecked") - public void testExplicitMergeTag() { - String input = Util.getLocalResource("issues/issue100-2.yaml"); - // System.out.println(input); - Yaml yaml = new Yaml(); - Map<String, ?> list = (Map<String, ?>) yaml.load(input); - List<Map<?, ?>> result = (List<Map<?, ?>>) list.get("result"); - Map<?, ?> first = result.iterator().next(); - for (Map<?, ?> data : result) { - // System.out.println(data); - assertEquals("Size must coinside.", first.size(), data.size()); - assertEquals(first, data); - } + @SuppressWarnings("unchecked") + public void testExplicitMergeTag() { + String input = Util.getLocalResource("issues/issue100-2.yaml"); + // System.out.println(input); + Yaml yaml = new Yaml(); + Map<String, ?> list = yaml.load(input); + List<Map<?, ?>> result = (List<Map<?, ?>>) list.get("result"); + Map<?, ?> first = result.iterator().next(); + for (Map<?, ?> data : result) { + // System.out.println(data); + assertEquals("Size must coinside.", first.size(), data.size()); + assertEquals(first, data); } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue102/BigDataLoadTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue102/BigDataLoadTest.java index 7e4beca0..63f8956c 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue102/BigDataLoadTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue102/BigDataLoadTest.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue102; @@ -20,56 +18,60 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; - import junit.framework.TestCase; - +import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; public class BigDataLoadTest extends TestCase { - private static final int SIZE = 5000; - public void testBigStringData() { - Yaml yaml = new Yaml(); - List<?> loaded = (List<?>) yaml.load(getLongYamlDocument(SIZE)); - assertEquals(SIZE, loaded.size()); - } + private static final int SIZE = 5000; - public void testBigStreamData() { - Yaml yaml = new Yaml(); - StringReader buffer = new StringReader(getLongYamlDocument(SIZE)); - List<?> loaded = (List<?>) yaml.load(buffer); - assertEquals(SIZE, loaded.size()); - } + public void testBigStringData() { + LoaderOptions options = new LoaderOptions(); + options.setCodePointLimit(10000000); + Yaml yaml = new Yaml(options); + List<?> loaded = yaml.load(getLongYamlDocument(SIZE)); + assertEquals(SIZE, loaded.size()); + } + + public void testBigStreamData() { + LoaderOptions options = new LoaderOptions(); + options.setCodePointLimit(10000000); + Yaml yaml = new Yaml(options); + StringReader buffer = new StringReader(getLongYamlDocument(SIZE)); + List<?> loaded = yaml.load(buffer); + assertEquals(SIZE, loaded.size()); + } - private String getLongYamlDocument(int size) { - List<DataBean> beans = new ArrayList<DataBean>(); - Yaml yaml = new Yaml(); - StringBuilder builder = new StringBuilder(); - for (int i = 0; i < size; i++) { - List<String> list = new ArrayList<String>(); - for (int j = 0; j < 10; j++) { - list.add(String.valueOf(i + j)); - } - Map<String, Integer> map = new HashMap<String, Integer>(); - for (int j = 0; j < 10; j++) { - map.put(String.valueOf(j), i + j); - } - DataBean bean = new DataBean(); - bean.setId("id" + i); - bean.setList(list); - bean.setMap(map); - beans.add(bean); - String ooo = yaml.dumpAsMap(bean); - String[] lines = ooo.split("\n"); - builder.append("-\n"); - for (int j = 0; j < lines.length; j++) { - builder.append(" "); - builder.append(lines[j]); - builder.append("\n"); - } - } - String data = builder.toString(); - System.out.println("Long data size: " + data.length() / 1024 + " kBytes."); - return data; + private String getLongYamlDocument(int size) { + List<DataBean> beans = new ArrayList<DataBean>(); + Yaml yaml = new Yaml(); + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < size; i++) { + List<String> list = new ArrayList<String>(); + for (int j = 0; j < 10; j++) { + list.add(String.valueOf(i + j)); + } + Map<String, Integer> map = new HashMap<String, Integer>(); + for (int j = 0; j < 10; j++) { + map.put(String.valueOf(j), i + j); + } + DataBean bean = new DataBean(); + bean.setId("id" + i); + bean.setList(list); + bean.setMap(map); + beans.add(bean); + String ooo = yaml.dumpAsMap(bean); + String[] lines = ooo.split("\n"); + builder.append("-\n"); + for (int j = 0; j < lines.length; j++) { + builder.append(" "); + builder.append(lines[j]); + builder.append("\n"); + } } + String data = builder.toString(); + // System.out.println("Long data size: " + data.length() / 1024 + " kBytes."); + return data; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue102/DataBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue102/DataBean.java index 9b1eb387..f084d561 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue102/DataBean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue102/DataBean.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue102; @@ -19,31 +17,32 @@ import java.util.List; import java.util.Map; public class DataBean { - private String id; - private List<String> list; - private Map<String, Integer> map; - public String getId() { - return id; - } + private String id; + private List<String> list; + private Map<String, Integer> map; - public void setId(String id) { - this.id = id; - } + public String getId() { + return id; + } - public List<String> getList() { - return list; - } + public void setId(String id) { + this.id = id; + } - public void setList(List<String> list) { - this.list = list; - } + public List<String> getList() { + return list; + } - public Map<String, Integer> getMap() { - return map; - } + public void setList(List<String> list) { + this.list = list; + } - public void setMap(Map<String, Integer> map) { - this.map = map; - } + public Map<String, Integer> getMap() { + return map; + } + + public void setMap(Map<String, Integer> map) { + this.map = map; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue103/FakeMap.java b/src/test/java/org/yaml/snakeyaml/issues/issue103/FakeMap.java index bdced750..78d35c48 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue103/FakeMap.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue103/FakeMap.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue103; @@ -22,107 +20,109 @@ import java.util.Map; import java.util.Set; /** - * Implements map interface, but behaves like collection. It just collects - * whatever you put(...) in here. Needed to track duplications in merge - * procedure. - * + * Implements map interface, but behaves like collection. It just collects whatever you put(...) in + * here. Needed to track duplications in merge procedure. + * * issue100 & issue103 */ public class FakeMap<K, V> implements Map<K, V> { - class FakeEntry implements java.util.Map.Entry<K, V> { - - private final K key; - private V val; - - public FakeEntry(K key, V val) { - this.key = key; - this.val = val; - } - - public K getKey() { - return key; - } + class FakeEntry implements java.util.Map.Entry<K, V> { - public V getValue() { - return val; - } - - public V setValue(V newV) { - V old = val; - val = newV; - return old; - } + private final K key; + private V val; + public FakeEntry(K key, V val) { + this.key = key; + this.val = val; } - ArrayList<java.util.Map.Entry<K, V>> entries = new ArrayList<Map.Entry<K, V>>(); - - public void clear() { - entries.clear(); + public K getKey() { + return key; } - public boolean containsKey(Object arg0) { - for (java.util.Map.Entry<K, V> entry : entries) { - if (entry.getKey().equals(arg0)) - return true; - } - return false; + public V getValue() { + return val; } - public boolean containsValue(Object arg0) { - for (java.util.Map.Entry<K, V> entry : entries) { - if (entry.getValue().equals(arg0)) - return true; - } - return false; + public V setValue(V newV) { + V old = val; + val = newV; + return old; } - public Set<java.util.Map.Entry<K, V>> entrySet() { - throw new UnsupportedOperationException(); - } + } - public V get(Object arg0) { - for (java.util.Map.Entry<K, V> entry : entries) { - if (entry.getKey().equals(arg0)) - return entry.getValue(); - } - return null; - } + ArrayList<java.util.Map.Entry<K, V>> entries = new ArrayList<Map.Entry<K, V>>(); - public boolean isEmpty() { - return values().isEmpty(); - } + public void clear() { + entries.clear(); + } - public Set<K> keySet() { - throw new UnsupportedOperationException(); + public boolean containsKey(Object arg0) { + for (java.util.Map.Entry<K, V> entry : entries) { + if (entry.getKey().equals(arg0)) { + return true; + } } - - public V put(K key, V val) { - entries.add(new FakeEntry(key, val)); - return null; + return false; + } + + public boolean containsValue(Object arg0) { + for (java.util.Map.Entry<K, V> entry : entries) { + if (entry.getValue().equals(arg0)) { + return true; + } } - - public void putAll(Map<? extends K, ? extends V> arg0) { - throw new UnsupportedOperationException(); + return false; + } + + public Set<java.util.Map.Entry<K, V>> entrySet() { + throw new UnsupportedOperationException(); + } + + public V get(Object arg0) { + for (java.util.Map.Entry<K, V> entry : entries) { + if (entry.getKey().equals(arg0)) { + return entry.getValue(); + } } - - public V remove(Object arg0) { - for (Iterator<java.util.Map.Entry<K, V>> iter = entries.iterator(); iter.hasNext();) { - java.util.Map.Entry<K, V> entry = iter.next(); - if (entry.getKey().equals(arg0)) { - iter.remove(); - return entry.getValue(); - } - } - return null; + return null; + } + + public boolean isEmpty() { + return values().isEmpty(); + } + + public Set<K> keySet() { + throw new UnsupportedOperationException(); + } + + public V put(K key, V val) { + entries.add(new FakeEntry(key, val)); + return null; + } + + public void putAll(Map<? extends K, ? extends V> arg0) { + throw new UnsupportedOperationException(); + } + + public V remove(Object arg0) { + for (Iterator<java.util.Map.Entry<K, V>> iter = entries.iterator(); iter.hasNext();) { + java.util.Map.Entry<K, V> entry = iter.next(); + if (entry.getKey().equals(arg0)) { + iter.remove(); + return entry.getValue(); + } } + return null; + } - public int size() { - return entries.size(); - } + public int size() { + return entries.size(); + } - public Collection<V> values() { - throw new UnsupportedOperationException(); - } + public Collection<V> values() { + throw new UnsupportedOperationException(); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue103/MergingTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue103/MergingTest.java index b825e283..2d2a5a00 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue103/MergingTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue103/MergingTest.java @@ -1,120 +1,116 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue103; import java.util.List; import java.util.Map; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; public class MergingTest extends TestCase { - @SuppressWarnings({ "rawtypes", "unchecked" }) - public void testMergeWithDefaultMap() { - String input = Util.getLocalResource("issues/issue103.yaml"); - // System.out.println(input); - Yaml yaml = new Yaml(); - - check((Map) yaml.load(input)); - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - public void testMergeWithFakeMap() { - String input = Util.getLocalResource("issues/issue103.yaml"); - // System.out.println(input); - Constructor c = new Constructor() { - protected java.util.Map<Object, Object> createDefaultMap() { - return new FakeMap<Object, Object>(); - } - }; - - Yaml yaml = new Yaml(c); - - check((FakeMap) yaml.load(input)); - } - - private void check(Map<String, List<Map<Object, Object>>> map) { - - assertEquals(2, map.size()); - assertTrue(map.containsKey("input")); - assertTrue(map.containsKey("result")); - - // input: ... - List<Map<Object, Object>> inputList = map.get("input"); - assertEquals(4, inputList.size()); - - Map<Object, Object> center = inputList.get(0); - assertEquals(2, center.size()); - assertEquals(Integer.valueOf(1), center.get("x")); - assertEquals(Integer.valueOf(2), center.get("y")); - - Map<Object, Object> left = inputList.get(1); - assertEquals(2, left.size()); - assertEquals(Integer.valueOf(0), left.get("x")); - assertEquals(Integer.valueOf(2), left.get("y")); - - Map<Object, Object> big = inputList.get(2); - assertEquals(1, big.size()); - assertEquals(Integer.valueOf(10), big.get("r")); - - Map<Object, Object> small = inputList.get(3); - assertEquals(1, small.size()); - assertEquals(Integer.valueOf(1), small.get("r")); - - // result : ... - List<Map<Object, Object>> resultList = map.get("result"); - assertEquals(5, resultList.size()); - - Map<Object, Object> explicitKeys = resultList.get(0); - assertEquals(4, explicitKeys.size()); - assertEquals(Integer.valueOf(1), explicitKeys.get("x")); - assertEquals(Integer.valueOf(2), explicitKeys.get("y")); - assertEquals(Integer.valueOf(10), explicitKeys.get("r")); - assertEquals("center/big", explicitKeys.get("label")); - - Map<?, ?> merge_center = resultList.get(1); - assertEquals(4, merge_center.size()); - assertEquals(Integer.valueOf(1), merge_center.get("x")); - assertEquals(Integer.valueOf(2), merge_center.get("y")); - assertEquals(Integer.valueOf(10), merge_center.get("r")); - assertEquals("center/big", merge_center.get("label")); - - Map<?, ?> merge_left_override = resultList.get(2); - assertEquals(4, merge_left_override.size()); - assertEquals(Integer.valueOf(0), merge_left_override.get("x")); - assertEquals(Integer.valueOf(5), merge_left_override.get("y")); - assertEquals(Integer.valueOf(10), merge_left_override.get("r")); - assertEquals("center/big", merge_left_override.get("label")); - - Map<?, ?> merge_center_big = resultList.get(3); - assertEquals(4, merge_center_big.size()); - assertEquals(Integer.valueOf(1), merge_center_big.get("x")); - assertEquals(Integer.valueOf(2), merge_center_big.get("y")); - assertEquals(Integer.valueOf(10), merge_center_big.get("r")); - assertEquals("center/big", merge_center_big.get("label")); - - Map<?, ?> merge_big_left_small_override = resultList.get(4); - assertEquals(4, merge_big_left_small_override.size()); - assertEquals(Integer.valueOf(1), merge_big_left_small_override.get("x")); - assertEquals(Integer.valueOf(2), merge_big_left_small_override.get("y")); - assertEquals(Integer.valueOf(10), merge_big_left_small_override.get("r")); - assertEquals("center/big", merge_big_left_small_override.get("label")); - } + @SuppressWarnings({"rawtypes", "unchecked"}) + public void testMergeWithDefaultMap() { + String input = Util.getLocalResource("issues/issue103.yaml"); + // System.out.println(input); + Yaml yaml = new Yaml(); + + check((Map) yaml.load(input)); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + public void testMergeWithFakeMap() { + String input = Util.getLocalResource("issues/issue103.yaml"); + // System.out.println(input); + Constructor c = new Constructor() { + protected java.util.Map<Object, Object> createDefaultMap(int size) { + return new FakeMap<Object, Object>(); + } + }; + + Yaml yaml = new Yaml(c); + + check((FakeMap) yaml.load(input)); + } + + private void check(Map<String, List<Map<Object, Object>>> map) { + + assertEquals(2, map.size()); + assertTrue(map.containsKey("input")); + assertTrue(map.containsKey("result")); + + // input: ... + List<Map<Object, Object>> inputList = map.get("input"); + assertEquals(4, inputList.size()); + + Map<Object, Object> center = inputList.get(0); + assertEquals(2, center.size()); + assertEquals(Integer.valueOf(1), center.get("x")); + assertEquals(Integer.valueOf(2), center.get("y")); + + Map<Object, Object> left = inputList.get(1); + assertEquals(2, left.size()); + assertEquals(Integer.valueOf(0), left.get("x")); + assertEquals(Integer.valueOf(2), left.get("y")); + + Map<Object, Object> big = inputList.get(2); + assertEquals(1, big.size()); + assertEquals(Integer.valueOf(10), big.get("r")); + + Map<Object, Object> small = inputList.get(3); + assertEquals(1, small.size()); + assertEquals(Integer.valueOf(1), small.get("r")); + + // result : ... + List<Map<Object, Object>> resultList = map.get("result"); + assertEquals(5, resultList.size()); + + Map<Object, Object> explicitKeys = resultList.get(0); + assertEquals(4, explicitKeys.size()); + assertEquals(Integer.valueOf(1), explicitKeys.get("x")); + assertEquals(Integer.valueOf(2), explicitKeys.get("y")); + assertEquals(Integer.valueOf(10), explicitKeys.get("r")); + assertEquals("center/big", explicitKeys.get("label")); + + Map<?, ?> merge_center = resultList.get(1); + assertEquals(4, merge_center.size()); + assertEquals(Integer.valueOf(1), merge_center.get("x")); + assertEquals(Integer.valueOf(2), merge_center.get("y")); + assertEquals(Integer.valueOf(10), merge_center.get("r")); + assertEquals("center/big", merge_center.get("label")); + + Map<?, ?> merge_left_override = resultList.get(2); + assertEquals(4, merge_left_override.size()); + assertEquals(Integer.valueOf(0), merge_left_override.get("x")); + assertEquals(Integer.valueOf(5), merge_left_override.get("y")); + assertEquals(Integer.valueOf(10), merge_left_override.get("r")); + assertEquals("center/big", merge_left_override.get("label")); + + Map<?, ?> merge_center_big = resultList.get(3); + assertEquals(4, merge_center_big.size()); + assertEquals(Integer.valueOf(1), merge_center_big.get("x")); + assertEquals(Integer.valueOf(2), merge_center_big.get("y")); + assertEquals(Integer.valueOf(10), merge_center_big.get("r")); + assertEquals("center/big", merge_center_big.get("label")); + + Map<?, ?> merge_big_left_small_override = resultList.get(4); + assertEquals(4, merge_big_left_small_override.size()); + assertEquals(Integer.valueOf(1), merge_big_left_small_override.get("x")); + assertEquals(Integer.valueOf(2), merge_big_left_small_override.get("y")); + assertEquals(Integer.valueOf(10), merge_big_left_small_override.get("r")); + assertEquals("center/big", merge_big_left_small_override.get("label")); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue11/YamlMapTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue11/YamlMapTest.java index 65508757..8b6468aa 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue11/YamlMapTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue11/YamlMapTest.java @@ -1,25 +1,21 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue11; import java.util.Map; import java.util.TreeMap; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.AbstractConstruct; import org.yaml.snakeyaml.constructor.Constructor; @@ -30,116 +26,122 @@ import org.yaml.snakeyaml.representer.Represent; import org.yaml.snakeyaml.representer.Representer; public class YamlMapTest extends TestCase { - public void testYaml() { - Yaml yaml = new Yaml(new ExtendedConstructor(), new ExtendedRepresenter()); - String output = yaml.dump(new Custom(123)); - // System.out.println(output); - Custom o = (Custom) yaml.load(output); - assertEquals("123", o.getStr()); - } - @SuppressWarnings("unchecked") - public void testYamlMap() { - Map<String, Object> data = new TreeMap<String, Object>(); - data.put("customTag", new Custom(123)); - - Yaml yaml = new Yaml(new ExtendedConstructor(), new ExtendedRepresenter()); - String output = yaml.dump(data); - // System.out.println(output); - Object o = yaml.load(output); - - assertTrue(o instanceof Map); - Map<String, Object> m = (Map<String, Object>) o; - assertTrue(m.get("customTag") instanceof Custom); + public void testYaml() { + Yaml yaml = new Yaml(new ExtendedConstructor(), new ExtendedRepresenter()); + String output = yaml.dump(new Custom(123)); + // System.out.println(output); + Custom o = yaml.load(output); + assertEquals("123", o.getStr()); + } + + @SuppressWarnings("unchecked") + public void testYamlMap() { + Map<String, Object> data = new TreeMap<String, Object>(); + data.put("customTag", new Custom(123)); + + Yaml yaml = new Yaml(new ExtendedConstructor(), new ExtendedRepresenter()); + String output = yaml.dump(data); + // System.out.println(output); + Object o = yaml.load(output); + + assertTrue(o instanceof Map); + Map<String, Object> m = (Map<String, Object>) o; + assertTrue(m.get("customTag") instanceof Custom); + } + + @SuppressWarnings("unchecked") + public void testYamlMapBean() { + Map<String, Object> data = new TreeMap<String, Object>(); + data.put("knownClass", new Wrapper("test", new Custom(456))); + + Yaml yaml = new Yaml(new ExtendedConstructor(), new ExtendedRepresenter()); + String output = yaml.dump(data); + // System.out.println(output); + Object o = yaml.load(output); + + assertTrue(o instanceof Map); + Map<String, Object> m = (Map<String, Object>) o; + assertEquals(Wrapper.class, m.get("knownClass").getClass()); + } + + public static class Wrapper { + + private String a; + private Custom b; + + public Wrapper(String s, Custom bb) { + a = s; + b = bb; } - @SuppressWarnings("unchecked") - public void testYamlMapBean() { - Map<String, Object> data = new TreeMap<String, Object>(); - data.put("knownClass", new Wrapper("test", new Custom(456))); + public Wrapper() {} - Yaml yaml = new Yaml(new ExtendedConstructor(), new ExtendedRepresenter()); - String output = yaml.dump(data); - // System.out.println(output); - Object o = yaml.load(output); + public String getA() { + return a; + } - assertTrue(o instanceof Map); - Map<String, Object> m = (Map<String, Object>) o; - assertEquals(Wrapper.class, m.get("knownClass").getClass()); + public void setA(String s) { + a = s; } - public static class Wrapper { - private String a; - private Custom b; + public Custom getB() { + return b; + } - public Wrapper(String s, Custom bb) { - a = s; - b = bb; - } + public void setB(Custom bb) { + b = bb; + } + } - public Wrapper() { - } + public static class Custom { - public String getA() { - return a; - } + final private String str; - public void setA(String s) { - a = s; - } + public Custom(Integer i) { + str = i.toString(); + } - public Custom getB() { - return b; - } + public Custom(Custom c) { + str = c.str; + } - public void setB(Custom bb) { - b = bb; - } + public String toString() { + return str; } - public static class Custom { - final private String str; + public String getStr() { + return str; + } + } - public Custom(Integer i) { - str = i.toString(); - } + public static class ExtendedRepresenter extends Representer { - public Custom(Custom c) { - str = c.str; - } + public ExtendedRepresenter() { + this.representers.put(Custom.class, new RepresentCustom()); + } - public String toString() { - return str; - } + private class RepresentCustom implements Represent { - public String getStr() { - return str; - } + public Node representData(Object data) { + return representScalar(new Tag("!Custom"), data.toString()); + } } + } - public static class ExtendedRepresenter extends Representer { - public ExtendedRepresenter() { - this.representers.put(Custom.class, new RepresentCustom()); - } + public static class ExtendedConstructor extends Constructor { - private class RepresentCustom implements Represent { - public Node representData(Object data) { - return representScalar(new Tag("!Custom"), ((Custom) data).toString()); - } - } + public ExtendedConstructor() { + this.yamlConstructors.put(new Tag("!Custom"), new ConstructCustom()); } - public static class ExtendedConstructor extends Constructor { - public ExtendedConstructor() { - this.yamlConstructors.put(new Tag("!Custom"), new ConstructCustom()); - } + private class ConstructCustom extends AbstractConstruct { - private class ConstructCustom extends AbstractConstruct { - public Object construct(Node node) { - String str = (String) constructScalar((ScalarNode) node); - return new Custom(new Integer(str)); - } + public Object construct(Node node) { + String str = constructScalar((ScalarNode) node); + return new Custom(Integer.valueOf(str)); + } - } } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue111/LongUriTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue111/LongUriTest.java index acf00914..1c69a3d5 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue111/LongUriTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue111/LongUriTest.java @@ -1,50 +1,46 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue111; import java.io.StringReader; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.nodes.Node; import org.yaml.snakeyaml.nodes.ScalarNode; public class LongUriTest extends TestCase { - /** - * Try loading a tag with a very long escaped URI section (over 256 bytes' - * worth). - */ - public void testLongURIEscape() { - Yaml loader = new Yaml(); - // Create a long escaped string by exponential growth... - String longEscURI = "%41"; // capital A... - for (int i = 0; i < 10; ++i) { - longEscURI = longEscURI + longEscURI; - } - assertEquals(1024 * 3, longEscURI.length()); - String yaml = "!" + longEscURI + " www"; - Node node = loader.compose(new StringReader(yaml)); - ScalarNode scalar = (ScalarNode) node; - String etalon = "!"; - for (int i = 0; i < 1024; i++) { - etalon += "A"; - } - assertEquals(1025, etalon.length()); - assertEquals(etalon, scalar.getTag().toString()); + /** + * Try loading a tag with a very long escaped URI section (over 256 bytes' worth). + */ + public void testLongURIEscape() { + Yaml loader = new Yaml(); + // Create a long escaped string by exponential growth... + String longEscURI = "%41"; // capital A... + for (int i = 0; i < 10; ++i) { + longEscURI = longEscURI + longEscURI; + } + assertEquals(1024 * 3, longEscURI.length()); + String yaml = "!" + longEscURI + " www"; + + Node node = loader.compose(new StringReader(yaml)); + ScalarNode scalar = (ScalarNode) node; + String etalon = "!"; + for (int i = 0; i < 1024; i++) { + etalon += "A"; } + assertEquals(1025, etalon.length()); + assertEquals(etalon, scalar.getTag().toString()); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue112/MyClass.java b/src/test/java/org/yaml/snakeyaml/issues/issue112/MyClass.java index 2f7c1b6f..8aca29dc 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue112/MyClass.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue112/MyClass.java @@ -1,33 +1,32 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue112; public class MyClass<T extends Object> { - T name; - public void setName(T name) { - this.name = name; - } + T name; - public T getName() { - return this.name; - } + public void setName(T name) { + this.name = name; + } - @Override - public String toString() { - return name.toString(); - } + public T getName() { + return this.name; + } + + @Override + public String toString() { + return name.toString(); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue112/MyCompositeObject.java b/src/test/java/org/yaml/snakeyaml/issues/issue112/MyCompositeObject.java index 471e678e..48a48ab8 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue112/MyCompositeObject.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue112/MyCompositeObject.java @@ -1,30 +1,29 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue112; import java.util.Collection; public class MyCompositeObject { - Collection<MyClass<? extends Object>> things; - public Collection<MyClass<? extends Object>> getThings() { - return this.things; - } + Collection<MyClass<? extends Object>> things; - public void setThings(Collection<MyClass<? extends Object>> things) { - this.things = things; - } + public Collection<MyClass<? extends Object>> getThings() { + return this.things; + } + + public void setThings(Collection<MyClass<? extends Object>> things) { + this.things = things; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue112/ParameterisedTypeLoadingTestCase.java b/src/test/java/org/yaml/snakeyaml/issues/issue112/ParameterisedTypeLoadingTestCase.java index 6b7f94a8..c1e4e8d9 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue112/ParameterisedTypeLoadingTestCase.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue112/ParameterisedTypeLoadingTestCase.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue112; @@ -20,7 +18,6 @@ import static org.junit.Assert.assertNotNull; import java.io.IOException; import java.io.InputStream; - import org.junit.Test; import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; @@ -28,38 +25,38 @@ import org.yaml.snakeyaml.constructor.Constructor; public class ParameterisedTypeLoadingTestCase { - @Test - public void testParameterisedTypeLoading() throws IOException { - Yaml yamlParser = new Yaml(new Constructor(MyCompositeObject.class)); - MyCompositeObject obj = (MyCompositeObject) yamlParser.load(getInput()); - check(obj); - - // dump the object - Yaml yaml = new Yaml(); - String output = yaml.dumpAsMap(obj); - assertEquals(Util.getLocalResource("issues/issue112-2.yaml"), output); - } - - @Test - public void testJavaBeanLoader() throws IOException { - Yaml yamlParser = new Yaml(); - MyCompositeObject obj = yamlParser.loadAs(getInput(), MyCompositeObject.class); - check(obj); + @Test + public void testParameterisedTypeLoading() throws IOException { + Yaml yamlParser = new Yaml(new Constructor(MyCompositeObject.class)); + MyCompositeObject obj = yamlParser.load(getInput()); + check(obj); + + // dump the object + Yaml yaml = new Yaml(); + String output = yaml.dumpAsMap(obj); + assertEquals(Util.getLocalResource("issues/issue112-2.yaml"), output); + } + + @Test + public void testJavaBeanLoader() throws IOException { + Yaml yamlParser = new Yaml(); + MyCompositeObject obj = yamlParser.loadAs(getInput(), MyCompositeObject.class); + check(obj); + } + + private void check(MyCompositeObject obj) { + Object[] values = {1, "two", 3, "four", "!!!"}; + assertNotNull(obj); + assertEquals(5, obj.getThings().size()); + int i = 0; + for (MyClass<? extends Object> thing : obj.getThings()) { + assertEquals(MyClass.class, thing.getClass()); + assertNotNull("The 'name' property must be set.", thing.getName()); + assertEquals(values[i++], thing.getName()); } + } - private void check(MyCompositeObject obj) { - Object[] values = { 1, "two", 3, "four", "!!!" }; - assertNotNull(obj); - assertEquals(5, obj.getThings().size()); - int i = 0; - for (MyClass<? extends Object> thing : obj.getThings()) { - assertEquals(MyClass.class, thing.getClass()); - assertNotNull("The 'name' property must be set.", thing.getName()); - assertEquals(values[i++], thing.getName()); - } - } - - private InputStream getInput() throws IOException { - return this.getClass().getClassLoader().getResource("issues/issue112-1.yaml").openStream(); - } + private InputStream getInput() throws IOException { + return this.getClass().getClassLoader().getResource("issues/issue112-1.yaml").openStream(); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue114/PreserveTypeTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue114/PreserveTypeTest.java index fd69f9a4..87cd08b2 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue114/PreserveTypeTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue114/PreserveTypeTest.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue114; @@ -19,72 +17,70 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class PreserveTypeTest extends TestCase { - public static class MyBean { + public static class MyBean { - private int dummy; + private int dummy; - public int getDummy() { - return dummy; - } - - public void setDummy(int dummy) { - this.dummy = dummy; - } + public int getDummy() { + return dummy; } - public static class ReferencingBean { - - private List<MyBean> myBeans = new LinkedList<PreserveTypeTest.MyBean>(); - - public List<MyBean> getMyBeans() { - return myBeans; - } - - public void setMyBeans(List<MyBean> myBeans) { - this.myBeans = myBeans; - } + public void setDummy(int dummy) { + this.dummy = dummy; } + } - private Map<String, Object> createData(boolean collectionFirst) { - MyBean myBean = new MyBean(); - ReferencingBean referencingBean = new ReferencingBean(); - referencingBean.getMyBeans().add(myBean); + public static class ReferencingBean { - LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>(); - if (collectionFirst) { - map.put("referencingBean", referencingBean); - map.put("myBean", myBean); - } else { - map.put("myBean", myBean); - map.put("referencingBean", referencingBean); - } - return map; - } + private List<MyBean> myBeans = new LinkedList<PreserveTypeTest.MyBean>(); - private void check(String doc) { - Yaml yaml = new Yaml(); - @SuppressWarnings("unchecked") - Map<String, Object> loaded = (Map<String, Object>) yaml.load(doc); - Object myBean2 = loaded.get("myBean"); - assertTrue(myBean2.getClass().toString(), myBean2 instanceof MyBean); + public List<MyBean> getMyBeans() { + return myBeans; } - public void testPreserveType1() { - Yaml yaml = new Yaml(); - String s = yaml.dump(createData(true)); - check(s); + public void setMyBeans(List<MyBean> myBeans) { + this.myBeans = myBeans; } - - public void testPreserveType2() { - Yaml yaml = new Yaml(); - String s = yaml.dump(createData(false)); - check(s); + } + + private Map<String, Object> createData(boolean collectionFirst) { + MyBean myBean = new MyBean(); + ReferencingBean referencingBean = new ReferencingBean(); + referencingBean.getMyBeans().add(myBean); + + LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>(); + if (collectionFirst) { + map.put("referencingBean", referencingBean); + map.put("myBean", myBean); + } else { + map.put("myBean", myBean); + map.put("referencingBean", referencingBean); } + return map; + } + + private void check(String doc) { + Yaml yaml = new Yaml(); + @SuppressWarnings("unchecked") + Map<String, Object> loaded = yaml.load(doc); + Object myBean2 = loaded.get("myBean"); + assertTrue(myBean2.getClass().toString(), myBean2 instanceof MyBean); + } + + public void testPreserveType1() { + Yaml yaml = new Yaml(); + String s = yaml.dump(createData(true)); + check(s); + } + + public void testPreserveType2() { + Yaml yaml = new Yaml(); + String s = yaml.dump(createData(false)); + check(s); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue115/IssueBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue115/IssueBean.java index 572b94c7..0b2aa8ba 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue115/IssueBean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue115/IssueBean.java @@ -1,28 +1,27 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue115; public class IssueBean { - private ParameterizedBean<Integer, String> bean = new ParameterizedBean<Integer, String>(); - public ParameterizedBean<Integer, String> getBean() { - return bean; - } + private ParameterizedBean<Integer, String> bean = new ParameterizedBean<Integer, String>(); - public void setBean(ParameterizedBean<Integer, String> bean) { - this.bean = bean; - } -}
\ No newline at end of file + public ParameterizedBean<Integer, String> getBean() { + return bean; + } + + public void setBean(ParameterizedBean<Integer, String> bean) { + this.bean = bean; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue115/ParameterizedBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue115/ParameterizedBean.java index ad3376ad..e3bfec70 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue115/ParameterizedBean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue115/ParameterizedBean.java @@ -1,37 +1,36 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue115; public class ParameterizedBean<K, V> { - private K k; - private V v; - public K getK() { - return k; - } + private K k; + private V v; - public void setK(K k) { - this.k = k; - } + public K getK() { + return k; + } - public V getV() { - return v; - } + public void setK(K k) { + this.k = k; + } - public void setV(V v) { - this.v = v; - } -}
\ No newline at end of file + public V getV() { + return v; + } + + public void setV(V v) { + this.v = v; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue115/ParameterizedJavaBeanTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue115/ParameterizedJavaBeanTest.java index ca3ccb44..dcf1e4e7 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue115/ParameterizedJavaBeanTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue115/ParameterizedJavaBeanTest.java @@ -1,55 +1,51 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue115; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class ParameterizedJavaBeanTest extends TestCase { - public void testAsStandalone() { - ParameterizedBean<Integer, String> bean = new ParameterizedBean<Integer, String>(); - bean.setK(13); - bean.setV("ID47"); - Yaml yaml = new Yaml(); - String result = yaml.dump(bean); - assertEquals("!!org.yaml.snakeyaml.issues.issue115.ParameterizedBean {k: 13, v: ID47}\n", - result); - // load - @SuppressWarnings("unchecked") - ParameterizedBean<Integer, String> beanParsed = (ParameterizedBean<Integer, String>) yaml - .load(result); - assertEquals(new Integer(13), beanParsed.getK()); - assertEquals("ID47", beanParsed.getV()); - } + public void testAsStandalone() { + ParameterizedBean<Integer, String> bean = new ParameterizedBean<Integer, String>(); + bean.setK(13); + bean.setV("ID47"); + Yaml yaml = new Yaml(); + String result = yaml.dump(bean); + assertEquals("!!org.yaml.snakeyaml.issues.issue115.ParameterizedBean {k: 13, v: ID47}\n", + result); + // load + @SuppressWarnings("unchecked") + ParameterizedBean<Integer, String> beanParsed = yaml.load(result); + assertEquals(Integer.valueOf(13), beanParsed.getK()); + assertEquals("ID47", beanParsed.getV()); + } - public void testAsJavaBeanProperty() { - Yaml yaml = new Yaml(); - IssueBean issue = new IssueBean(); - ParameterizedBean<Integer, String> bean = new ParameterizedBean<Integer, String>(); - bean.setK(432); - bean.setV("Val432"); - issue.setBean(bean); - String result = yaml.dump(issue); - assertEquals("!!org.yaml.snakeyaml.issues.issue115.IssueBean\nbean: {k: 432, v: Val432}\n", - result); - // load - IssueBean issueParsed = (IssueBean) yaml.load(result); - assertEquals(new Integer(432), issueParsed.getBean().getK()); - assertEquals("Val432", issueParsed.getBean().getV()); - } -}
\ No newline at end of file + public void testAsJavaBeanProperty() { + Yaml yaml = new Yaml(); + IssueBean issue = new IssueBean(); + ParameterizedBean<Integer, String> bean = new ParameterizedBean<Integer, String>(); + bean.setK(432); + bean.setV("Val432"); + issue.setBean(bean); + String result = yaml.dump(issue); + assertEquals("!!org.yaml.snakeyaml.issues.issue115.IssueBean\nbean: {k: 432, v: Val432}\n", + result); + // load + IssueBean issueParsed = yaml.load(result); + assertEquals(Integer.valueOf(432), issueParsed.getBean().getK()); + assertEquals("Val432", issueParsed.getBean().getV()); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue115/ParameterizedTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue115/ParameterizedTest.java index c6c69d30..710c9e65 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue115/ParameterizedTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue115/ParameterizedTest.java @@ -1,56 +1,57 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue115; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class ParameterizedTest extends TestCase { - public void testAsStandalone() { - Parameterized<Integer> parm = new Parameterized<Integer>(); - parm.t = 3; - Yaml yaml = new Yaml(); - String result = yaml.dump(parm); - assertEquals("!!org.yaml.snakeyaml.issues.issue115.Parameterized {t: 3}\n", result); - @SuppressWarnings("unchecked") - // load - Parameterized<Integer> parmParsed = (Parameterized<Integer>) yaml.load(result); - assertEquals(new Integer(3), parmParsed.t); - } - - public void testAsJavaBeanProperty() { - Yaml yaml = new Yaml(); - Issue issue = new Issue(); - Parameterized<Integer> parm = new Parameterized<Integer>(); - parm.t = 555; - issue.parm = parm; - String result = yaml.dump(issue); - assertEquals("!!org.yaml.snakeyaml.issues.issue115.Issue\nparm: {t: 555}\n", result); - // load - Issue issueParsed = (Issue) yaml.load(result); - assertEquals(new Integer(555), issueParsed.parm.t); - } + public void testAsStandalone() { + Parameterized<Integer> parm = new Parameterized<Integer>(); + parm.t = 3; + Yaml yaml = new Yaml(); + String result = yaml.dump(parm); + assertEquals("!!org.yaml.snakeyaml.issues.issue115.Parameterized {t: 3}\n", result); + @SuppressWarnings("unchecked") + // load + Parameterized<Integer> parmParsed = yaml.load(result); + assertEquals(Integer.valueOf(3), parmParsed.t); + } + + public void testAsJavaBeanProperty() { + Yaml yaml = new Yaml(); + Issue issue = new Issue(); + Parameterized<Integer> parm = new Parameterized<Integer>(); + parm.t = 555; + issue.parm = parm; + String result = yaml.dump(issue); + assertEquals("!!org.yaml.snakeyaml.issues.issue115.Issue\nparm: {t: 555}\n", result); + // load + Issue issueParsed = yaml.load(result); + assertEquals(Integer.valueOf(555), issueParsed.parm.t); + } } + class Issue { - public Parameterized<Integer> parm = new Parameterized<Integer>(); + + public Parameterized<Integer> parm = new Parameterized<Integer>(); } + class Parameterized<T> { - public T t; -}
\ No newline at end of file + + public T t; +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue116/HiddenSpecial.java b/src/test/java/org/yaml/snakeyaml/issues/issue116/HiddenSpecial.java index 8fea9ff6..dad74d63 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue116/HiddenSpecial.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue116/HiddenSpecial.java @@ -1,28 +1,27 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue116; public class HiddenSpecial { - private int inaccessableField; - public HiddenSpecial(String something) { - this.inaccessableField = something.hashCode(); - } + private final int inaccessableField; - public int retrieveMyVerySpecialField() { - return inaccessableField; - } -}
\ No newline at end of file + public HiddenSpecial(String something) { + this.inaccessableField = something.hashCode(); + } + + public int retrieveMyVerySpecialField() { + return inaccessableField; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue116/NoFieldsTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue116/NoFieldsTest.java index 044cdb8d..adcc0d23 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue116/NoFieldsTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue116/NoFieldsTest.java @@ -1,70 +1,70 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue116; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.error.YAMLException; public class NoFieldsTest extends TestCase { - public void testEmptyClass() { - Empty empty = new Empty(); - Yaml yaml = new Yaml(); - String result = yaml.dump(empty); - assertEquals("!!org.yaml.snakeyaml.issues.issue116.Empty {}\n", result); - Object emptyParsed = yaml.load(result); - assertTrue(emptyParsed instanceof Empty); - } + public void testEmptyClass() { + Empty empty = new Empty(); + Yaml yaml = new Yaml(); + String result = yaml.dump(empty); + assertEquals("!!org.yaml.snakeyaml.issues.issue116.Empty {}\n", result); + Object emptyParsed = yaml.load(result); + assertTrue(emptyParsed instanceof Empty); + } - public void testHiddenParameter() { - Hidden hidden = new Hidden(); - Yaml yaml = new Yaml(); - try { - yaml.dump(hidden); - fail("an exception should have been thrown"); - } catch (YAMLException e) { - assertEquals(e.getMessage(), - "No JavaBean properties found in org.yaml.snakeyaml.issues.issue116.Hidden"); - } - Object hiddenParsed = yaml.load("!!org.yaml.snakeyaml.issues.issue116.Hidden {}\n"); - assertTrue(hiddenParsed instanceof Hidden); + public void testHiddenParameter() { + Hidden hidden = new Hidden(); + Yaml yaml = new Yaml(); + try { + yaml.dump(hidden); + fail("an exception should have been thrown"); + } catch (YAMLException e) { + assertEquals(e.getMessage(), + "No JavaBean properties found in org.yaml.snakeyaml.issues.issue116.Hidden"); } + Object hiddenParsed = yaml.load("!!org.yaml.snakeyaml.issues.issue116.Hidden {}\n"); + assertTrue(hiddenParsed instanceof Hidden); + } - public void testSpecialHiddenParameter() { - HiddenSpecial hidden = new HiddenSpecial("qwerty"); - Yaml yaml = new Yaml(); - try { - yaml.dump(hidden); - fail("an exception should have been thrown"); - } catch (YAMLException e) { - assertEquals(e.getMessage(), - "No JavaBean properties found in org.yaml.snakeyaml.issues.issue116.HiddenSpecial"); - } - HiddenSpecial hs = (HiddenSpecial) yaml - .load("!!org.yaml.snakeyaml.issues.issue116.HiddenSpecial foo\n"); - assertEquals("foo".hashCode(), hs.retrieveMyVerySpecialField()); + public void testSpecialHiddenParameter() { + HiddenSpecial hidden = new HiddenSpecial("qwerty"); + Yaml yaml = new Yaml(); + try { + yaml.dump(hidden); + fail("an exception should have been thrown"); + } catch (YAMLException e) { + assertEquals(e.getMessage(), + "No JavaBean properties found in org.yaml.snakeyaml.issues.issue116.HiddenSpecial"); } + HiddenSpecial hs = yaml.load("!!org.yaml.snakeyaml.issues.issue116.HiddenSpecial foo\n"); + assertEquals("foo".hashCode(), hs.retrieveMyVerySpecialField()); + } } + class Empty { + } + class Hidden { - @SuppressWarnings("unused") - private int inaccessableField; + + @SuppressWarnings("unused") + private int inaccessableField; } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue124/Bean124.java b/src/test/java/org/yaml/snakeyaml/issues/issue124/Bean124.java index 202e84ed..e686b665 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue124/Bean124.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue124/Bean124.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue124; @@ -19,36 +17,37 @@ import java.util.ArrayList; import java.util.List; public class Bean124 { - private String a; - private List<Integer> numbers; - - public Bean124() { - this.a = "aaa"; - this.numbers = new ArrayList<Integer>(3); - numbers.add(1); - numbers.add(2); - numbers.add(3); - } - - public Bean124(String a, List<Integer> numbers) { - super(); - this.a = a; - this.numbers = numbers; - } - - public String getA() { - return a; - } - - public void setA(String a) { - this.a = a; - } - - public List<Integer> getNumbers() { - return numbers; - } - - public void setNumbers(List<Integer> numbers) { - this.numbers = numbers; - } -}
\ No newline at end of file + + private String a; + private List<Integer> numbers; + + public Bean124() { + this.a = "aaa"; + this.numbers = new ArrayList<Integer>(3); + numbers.add(1); + numbers.add(2); + numbers.add(3); + } + + public Bean124(String a, List<Integer> numbers) { + super(); + this.a = a; + this.numbers = numbers; + } + + public String getA() { + return a; + } + + public void setA(String a) { + this.a = a; + } + + public List<Integer> getNumbers() { + return numbers; + } + + public void setNumbers(List<Integer> numbers) { + this.numbers = numbers; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue124/DumpTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue124/DumpTest.java index 7da50388..8f237e3e 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue124/DumpTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue124/DumpTest.java @@ -1,22 +1,19 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue124; import junit.framework.TestCase; - import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions.FlowStyle; import org.yaml.snakeyaml.Yaml; @@ -24,64 +21,63 @@ import org.yaml.snakeyaml.nodes.Tag; public class DumpTest extends TestCase { - public void testDumperOptionsSideEffect() { - Yaml yaml = new Yaml(); - Bean124 bean = new Bean124(); - String output0 = yaml.dump(bean); - // System.out.println(output0); - assertEquals("!!org.yaml.snakeyaml.issues.issue124.Bean124\na: aaa\nnumbers: [1, 2, 3]\n", - output0); - String output1 = yaml.dumpAsMap(bean); - // System.out.println(output1); - assertEquals("a: aaa\nnumbers:\n- 1\n- 2\n- 3\n", output1); - String output2 = yaml.dump(bean); - // System.out.println(output2); - assertEquals("Yaml.dumpAs() should not have any side effects.", output0, output2); - } + public void testDumperOptionsSideEffect() { + Yaml yaml = new Yaml(); + Bean124 bean = new Bean124(); + String output0 = yaml.dump(bean); + // System.out.println(output0); + assertEquals("!!org.yaml.snakeyaml.issues.issue124.Bean124\na: aaa\nnumbers: [1, 2, 3]\n", + output0); + String output1 = yaml.dumpAsMap(bean); + // System.out.println(output1); + assertEquals("a: aaa\nnumbers:\n- 1\n- 2\n- 3\n", output1); + String output2 = yaml.dump(bean); + // System.out.println(output2); + assertEquals("Yaml.dumpAs() should not have any side effects.", output0, output2); + } - public void testDumperDifferentTag() { - Yaml yaml = new Yaml(); - Bean124 bean = new Bean124(); - String output1 = yaml.dumpAs(bean, new Tag("!!foo.bar"), FlowStyle.BLOCK); - assertEquals("!!foo.bar\na: aaa\nnumbers:\n- 1\n- 2\n- 3\n", output1); - } + public void testDumperDifferentTag() { + Yaml yaml = new Yaml(); + Bean124 bean = new Bean124(); + String output1 = yaml.dumpAs(bean, new Tag("!!foo.bar"), FlowStyle.BLOCK); + assertEquals("!!foo.bar\na: aaa\nnumbers:\n- 1\n- 2\n- 3\n", output1); + } - public void testDumperFlowStyle() { - Yaml yaml = new Yaml(); - Bean124 bean = new Bean124(); - String output1 = yaml.dumpAs(bean, new Tag("!!foo.bar"), FlowStyle.FLOW); - assertEquals("!!foo.bar {a: aaa, numbers: [1, 2, 3]}\n", output1); - } + public void testDumperFlowStyle() { + Yaml yaml = new Yaml(); + Bean124 bean = new Bean124(); + String output1 = yaml.dumpAs(bean, new Tag("!!foo.bar"), FlowStyle.FLOW); + assertEquals("!!foo.bar {a: aaa, numbers: [1, 2, 3]}\n", output1); + } - public void testDumperAutoStyle() { - Yaml yaml = new Yaml(); - Bean124 bean = new Bean124(); - String output1 = yaml.dumpAs(bean, new Tag("!!foo.bar"), FlowStyle.AUTO); - assertEquals("!!foo.bar\na: aaa\nnumbers: [1, 2, 3]\n", output1); - } + public void testDumperAutoStyle() { + Yaml yaml = new Yaml(); + Bean124 bean = new Bean124(); + String output1 = yaml.dumpAs(bean, new Tag("!!foo.bar"), FlowStyle.AUTO); + assertEquals("!!foo.bar\na: aaa\nnumbers: [1, 2, 3]\n", output1); + } - public void testDumperNullStyle() { - Yaml yaml = new Yaml(); - Bean124 bean = new Bean124(); - String output1 = yaml.dumpAs(bean, new Tag("!!foo.bar"), null); - assertEquals("!!foo.bar\na: aaa\nnumbers: [1, 2, 3]\n", output1); - } + public void testDumperNullStyle() { + Yaml yaml = new Yaml(); + Bean124 bean = new Bean124(); + String output1 = yaml.dumpAs(bean, new Tag("!!foo.bar"), null); + assertEquals("!!foo.bar\na: aaa\nnumbers: [1, 2, 3]\n", output1); + } - public void testDumperNullStyle2() { - DumperOptions options = new DumperOptions(); - options.setDefaultFlowStyle(FlowStyle.BLOCK); - Yaml yaml = new Yaml(options); - Bean124 bean = new Bean124(); - String output1 = yaml.dumpAs(bean, new Tag("!!foo2.bar2"), null); - assertEquals("!!foo2.bar2\na: aaa\nnumbers:\n- 1\n- 2\n- 3\n", output1); - } + public void testDumperNullStyle2() { + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(FlowStyle.BLOCK); + Yaml yaml = new Yaml(options); + Bean124 bean = new Bean124(); + String output1 = yaml.dumpAs(bean, new Tag("!!foo2.bar2"), null); + assertEquals("!!foo2.bar2\na: aaa\nnumbers:\n- 1\n- 2\n- 3\n", output1); + } - public void testDumperNullTag() { - Yaml yaml = new Yaml(); - Bean124 bean = new Bean124(); - String output1 = yaml.dumpAs(bean, null, FlowStyle.BLOCK); - assertEquals( - "!!org.yaml.snakeyaml.issues.issue124.Bean124\na: aaa\nnumbers:\n- 1\n- 2\n- 3\n", - output1); - } -}
\ No newline at end of file + public void testDumperNullTag() { + Yaml yaml = new Yaml(); + Bean124 bean = new Bean124(); + String output1 = yaml.dumpAs(bean, null, FlowStyle.BLOCK); + assertEquals("!!org.yaml.snakeyaml.issues.issue124.Bean124\na: aaa\nnumbers:\n- 1\n- 2\n- 3\n", + output1); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue127/Bean.java b/src/test/java/org/yaml/snakeyaml/issues/issue127/Bean.java index d347249c..d8bfb5e1 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue127/Bean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue127/Bean.java @@ -1,37 +1,36 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue127; public class Bean { - private String a; - private String b; - public String getA() { - return a; - } + private String a; + private String b; - public void setA(String a) { - this.a = a; - } + public String getA() { + return a; + } - public String getB() { - return b; - } + public void setA(String a) { + this.a = a; + } - public void setB(String b) { - this.b = b; - } -}
\ No newline at end of file + public String getB() { + return b; + } + + public void setB(String b) { + this.b = b; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue127/NullAliasTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue127/NullAliasTest.java index bd3da2fa..73b37aef 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue127/NullAliasTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue127/NullAliasTest.java @@ -1,25 +1,22 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue127; import java.util.LinkedHashMap; import java.util.Map; - import junit.framework.TestCase; - +import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.nodes.Node; import org.yaml.snakeyaml.nodes.Tag; @@ -27,30 +24,33 @@ import org.yaml.snakeyaml.representer.Represent; import org.yaml.snakeyaml.representer.Representer; public class NullAliasTest extends TestCase { - private static final Tag MY_TAG = new Tag("tag:example.com,2011:bean"); - public void testRespresenter() { - Bean bean = new Bean(); + private static final Tag MY_TAG = new Tag("tag:example.com,2011:bean"); + + public void testRespresenter() { + Bean bean = new Bean(); - bean.setA("a"); // leave b null - Yaml yaml = new Yaml(new BeanRepresenter()); - String output = yaml.dump(bean); - assertEquals("!<tag:example.com,2011:bean>\na: a\nb: null\n", output); + bean.setA("a"); // leave b null + Yaml yaml = new Yaml(new BeanRepresenter()); + String output = yaml.dump(bean); + assertEquals("!<tag:example.com,2011:bean>\na: a\nb: null\n", output); + } + + class BeanRepresenter extends Representer { + + public BeanRepresenter() { + this.representers.put(Bean.class, new RepresentBean()); } - class BeanRepresenter extends Representer { - public BeanRepresenter() { - this.representers.put(Bean.class, new RepresentBean()); - } - - private class RepresentBean implements Represent { - public Node representData(Object data) { - Bean bean = (Bean) data; - Map<String, Object> fields = new LinkedHashMap<String, Object>(2); - fields.put("a", bean.getA()); - fields.put("b", bean.getB()); - return representMapping(MY_TAG, fields, false); - } - } + private class RepresentBean implements Represent { + + public Node representData(Object data) { + Bean bean = (Bean) data; + Map<String, Object> fields = new LinkedHashMap<String, Object>(2); + fields.put("a", bean.getA()); + fields.put("b", bean.getB()); + return representMapping(MY_TAG, fields, DumperOptions.FlowStyle.BLOCK); + } } -}
\ No newline at end of file + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue132/ScalarEventTagTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue132/ScalarEventTagTest.java index 52eb0bd7..75db4ccf 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue132/ScalarEventTagTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue132/ScalarEventTagTest.java @@ -1,26 +1,22 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue132; import java.io.StringReader; import java.util.ArrayList; import java.util.List; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.events.Event; import org.yaml.snakeyaml.events.ScalarEvent; @@ -30,30 +26,31 @@ import org.yaml.snakeyaml.nodes.Node; * to test http://code.google.com/p/snakeyaml/issues/detail?id=132 */ public class ScalarEventTagTest extends TestCase { - public void testLoad() { - Yaml yaml = new Yaml(); - Iterable<Event> parsed = yaml.parse(new StringReader("5")); - List<Event> events = new ArrayList<Event>(5); - for (Event event : parsed) { - events.add(event); - // System.out.println(event); - } - String tag = ((ScalarEvent) events.get(2)).getTag(); - assertNull("The tag should not be specified: " + tag, tag); - } - public void testDump() { - Yaml yaml = new Yaml(); - Node intNode = yaml.represent(7); - assertEquals("tag:yaml.org,2002:int", intNode.getTag().toString()); - // System.out.println(intNode); - List<Event> intEvents = yaml.serialize(intNode); - String tag = ((ScalarEvent) intEvents.get(2)).getTag(); - assertEquals("Without the tag emitter would not know how to emit '7'", - "tag:yaml.org,2002:int", tag); - // - Node strNode = yaml.represent("7"); - assertEquals("tag:yaml.org,2002:str", strNode.getTag().toString()); - // System.out.println(strNode); + public void testLoad() { + Yaml yaml = new Yaml(); + Iterable<Event> parsed = yaml.parse(new StringReader("5")); + List<Event> events = new ArrayList<Event>(5); + for (Event event : parsed) { + events.add(event); + // System.out.println(event); } + String tag = ((ScalarEvent) events.get(2)).getTag(); + assertNull("The tag should not be specified: " + tag, tag); + } + + public void testDump() { + Yaml yaml = new Yaml(); + Node intNode = yaml.represent(7); + assertEquals("tag:yaml.org,2002:int", intNode.getTag().toString()); + // System.out.println(intNode); + List<Event> intEvents = yaml.serialize(intNode); + String tag = ((ScalarEvent) intEvents.get(2)).getTag(); + assertEquals("Without the tag emitter would not know how to emit '7'", "tag:yaml.org,2002:int", + tag); + // + Node strNode = yaml.represent("7"); + assertEquals("tag:yaml.org,2002:str", strNode.getTag().toString()); + // System.out.println(strNode); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue133/StackOverflowTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue133/StackOverflowTest.java index 080081d7..85744adf 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue133/StackOverflowTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue133/StackOverflowTest.java @@ -1,24 +1,24 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue133; -import java.awt.Point; - -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.awt.Point; +import org.junit.Test; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.introspector.Property; import org.yaml.snakeyaml.nodes.NodeTuple; @@ -28,45 +28,49 @@ import org.yaml.snakeyaml.representer.Representer; /** * to test http://code.google.com/p/snakeyaml/issues/detail?id=133 */ -public class StackOverflowTest extends TestCase { - public void testDumpRecursiveObject() { - try { - Yaml yaml = new Yaml(); - // by default it must fail with StackOverflow - yaml.dump(new Point()); - fail("getLocation() is recursive."); - } catch (Throwable e) { - String message = e.getMessage(); - assertTrue("StackOverflow has no message: " + e.getMessage(), message == null - || message.contains("Unable to find getter for property 'location'")); - } - } +public class StackOverflowTest { - /** - * Since Point.getLocation() creates a new instance of Point class, - * SnakeYAML will fail to dump an instance of Point if 'getLocation()' is - * also included. - * - * Since Point is not really a JavaBean, we can safely skip the recursive - * property when we dump the instance of Point. - */ - private class PointRepresenter extends Representer { + @Test + public void testDumpRecursiveObjectOnJava6() { - @Override - protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, - Object propertyValue, Tag customTag) { - if (javaBean instanceof Point && "location".equals(property.getName())) { - return null; - } else { - return super - .representJavaBeanProperty(javaBean, property, propertyValue, customTag); - } - } + org.junit.Assume.assumeTrue(System.getProperty("java.version").startsWith("1.6")); + + try { + Yaml yaml = new Yaml(); + // by default it must fail with StackOverflow + yaml.dump(new Point()); + fail("getLocation() is recursive."); + } catch (Throwable e) { + String message = e.getMessage(); + assertTrue("StackOverflow has no message: " + e.getMessage(), + message == null || message.contains("Unable to find getter for property 'location'")); } + } + + /** + * Since Point.getLocation() creates a new instance of Point class, SnakeYAML will fail to dump an + * instance of Point if 'getLocation()' is also included. + * + * Since Point is not really a JavaBean, we can safely skip the recursive property when we dump + * the instance of Point. + */ + private class PointRepresenter extends Representer { - public void testDump() { - Yaml yaml = new Yaml(new PointRepresenter()); - String output = yaml.dump(new Point()); - assertEquals("!!java.awt.Point {x: 0, y: 0}\n", output); + @Override + protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, + Object propertyValue, Tag customTag) { + if (javaBean instanceof Point && "location".equals(property.getName())) { + return null; + } else { + return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag); + } } + } + + @Test + public void testDump() { + Yaml yaml = new Yaml(new PointRepresenter()); + String output = yaml.dump(new Point()); + assertEquals("!!java.awt.Point {x: 0, y: 0}\n", output); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue136/TabInScalarTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue136/TabInScalarTest.java index 09c3b081..c7fa5442 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue136/TabInScalarTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue136/TabInScalarTest.java @@ -1,51 +1,48 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue136; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class TabInScalarTest extends TestCase { - public void testTab() { - String data = (String) new Yaml().load("L\tD"); - assertEquals("L\tD", data); - } - - public void testNoTab() { - String data = (String) new Yaml().load("L D"); - assertEquals("L D", data); - } - - public void testTabDoubleQuotes() { - String data = (String) new Yaml().load("\"L\tD\""); - // System.out.println(data); - assertEquals("L\tD", data); - } - - public void testTabSingleQuotes() { - String data = (String) new Yaml().load("'L\tD'"); - // System.out.println(data); - assertEquals("L\tD", data); - } - - public void testDumpTab() { - String data = (String) new Yaml().dump("L\tD"); - // System.out.println(data); - assertEquals("\"L\\tD\"\n", data); - } + public void testTab() { + String data = new Yaml().load("L\tD"); + assertEquals("L\tD", data); + } + + public void testNoTab() { + String data = new Yaml().load("L D"); + assertEquals("L D", data); + } + + public void testTabDoubleQuotes() { + String data = new Yaml().load("\"L\tD\""); + // System.out.println(data); + assertEquals("L\tD", data); + } + + public void testTabSingleQuotes() { + String data = new Yaml().load("'L\tD'"); + // System.out.println(data); + assertEquals("L\tD", data); + } + + public void testDumpTab() { + String data = new Yaml().dump("L\tD"); + // System.out.println(data); + assertEquals("\"L\\tD\"\n", data); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue137/SupplementaryCharactersTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue137/SupplementaryCharactersTest.java index 389735d3..a52c6362 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue137/SupplementaryCharactersTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue137/SupplementaryCharactersTest.java @@ -1,24 +1,24 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue137; +import java.io.InputStream; import java.io.UnsupportedEncodingException; - +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; /** @@ -26,38 +26,99 @@ import org.yaml.snakeyaml.Yaml; */ public class SupplementaryCharactersTest extends TestCase { - public void testSupplementaryCharacter() { - Yaml yaml = new Yaml(); - String parsed = (String) yaml.load("\"\\U0001f648\""); - assertEquals("\ud83d\ude48", parsed); - // System.out.println(data); - } + public static class EmojiContainer { + + public Map<String, Map<String, Integer>> sizes; + public Map<String, Map<String, List<String>>> values; + } + + public void testSupplementaryCharacter() { + Yaml yaml = new Yaml(); + String parsed = yaml.load("\"\\U0001f648\""); + assertEquals("\ud83d\ude48", parsed); + // System.out.println(data); + } + + public void testBasicMultilingualPlane() { + Yaml yaml = new Yaml(); + String parsed = yaml.load("\"\\U00000041\""); + assertEquals("A", parsed); + } + + /** + * Supplementary code points are dumped normally + */ + public void testDumpSupplementaryCodePoint() throws UnsupportedEncodingException { + String supplementary = "\ud83d\ude48"; + Yaml yaml = new Yaml(); + String output = yaml.dump(supplementary); + assertEquals("\ud83d\ude48\n", output); + String binString = yaml.load(output); + assertEquals(supplementary, binString); + } + + /** + * Non-printable characters are escaped + */ + public void testDumpNonPrintableCharacter() throws UnsupportedEncodingException { + String supplementary = "\u0001"; + Yaml yaml = new Yaml(); + String output = yaml.dump(supplementary); + assertEquals("!!binary |-\n AQ==\n", output); + byte[] binary = yaml.load(output); + String binString = new String(binary, StandardCharsets.UTF_8); + assertEquals(supplementary, binString); + } - public void testBasicMultilingualPlane() { - Yaml yaml = new Yaml(); - String parsed = (String) yaml.load("\"\\U00000041\""); - assertEquals("A", parsed); + public void testDumpSurrogateCharacter() throws UnsupportedEncodingException { + String supplementary = "\ud83d"; + Yaml yaml = new Yaml(); + try { + yaml.dump(supplementary); + fail("dumping half code point without other half should fail"); + } catch (Exception e) { + assertEquals("invalid string value has occurred", e.getMessage()); } - /** - * Supplementary Characters are dumped as binary - */ - public void testDumpSupplementaryCharacter() throws UnsupportedEncodingException { - String supplementary = "\ud83d\ude48"; - Yaml yaml = new Yaml(); - String output = yaml.dump(supplementary); - assertEquals("!!binary |-\n 8J+ZiA==\n", output); - byte[] binary = (byte[]) yaml.load(output); - String binString = new String(binary, "UTF-8"); - assertEquals(supplementary, binString); + } + + public void testLoadSupplementaryCodePoint() { + new Yaml().load("\"\ud83d\ude48\"\n"); + } + + public void testLoadSurrogateCharacter() { + try { + new Yaml().load("\"\ud83d\"\n"); + fail("separate surrogate characters are not printable"); + } catch (Exception e) { + assertEquals("special characters are not allowed", e.getMessage()); } + } + + /* + * This method tests loading of the document with a lot of SupplementaryCharacters. Main purpose + * is to check that StreamReader actually reads document fully, but not in one read (since file is + * bigger than StreamReader buffer). + */ + public void testLoadingEmoji() { + InputStream input = this.getClass().getClassLoader().getResourceAsStream("issues/emoji.yaml"); + EmojiContainer emoji = new Yaml().loadAs(input, EmojiContainer.class); + + assertEquals(emoji.sizes.keySet(), emoji.values.keySet()); - public void testLoadSupplementaryCharacter() { - try { - new Yaml().load("\"\ud83d\ude48\"\n"); - fail("Are Supplementary Characters printable ?"); - } catch (Exception e) { - assertEquals("special characters are not allowed", e.getMessage()); - } + for (Map.Entry<String, Map<String, Integer>> mainTopic : emoji.sizes.entrySet()) { + String mainName = mainTopic.getKey(); + Map<String, Integer> subtopic2size = mainTopic.getValue(); + + Map<String, List<String>> subtopic2values = emoji.values.get(mainName); + assertEquals(subtopic2size.keySet(), subtopic2values.keySet()); + + for (Map.Entry<String, Integer> subTopic : subtopic2size.entrySet()) { + String subName = subTopic.getKey(); + + assertEquals(subTopic.getValue().intValue(), subtopic2values.get(subName).size()); + } } + + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue138/ReaderExceptionTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue138/ReaderExceptionTest.java index 851ab171..4084fda7 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue138/ReaderExceptionTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue138/ReaderExceptionTest.java @@ -1,35 +1,32 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue138; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.reader.ReaderException; public class ReaderExceptionTest extends TestCase { - public void testGetters() { - try { - new Yaml().load("012\u0019"); - fail(); - } catch (ReaderException e) { - assertEquals(3, e.getPosition()); - assertEquals("'string'", e.getName()); - assertEquals('\u0019', e.getCharacter()); - } + public void testGetters() { + try { + new Yaml().load("012\u0019"); + fail(); + } catch (ReaderException e) { + assertEquals(3, e.getPosition()); + assertEquals("'string'", e.getName()); + assertEquals(0x19, e.getCodePoint()); } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue139/MergeValueTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue139/MergeValueTest.java index debf99f6..7dc347cd 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue139/MergeValueTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue139/MergeValueTest.java @@ -1,70 +1,66 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue139; import java.util.Map; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; public class MergeValueTest extends TestCase { - public void testNotUniqueSimple() { - String simple = "{key: 1, key: 2}"; - Yaml yaml = new Yaml(); - @SuppressWarnings("unchecked") - Map<String, Integer> map = (Map<String, Integer>) yaml.load(simple); - assertEquals(1, map.size()); - assertEquals(new Integer(2), map.get("key")); - } - - public void testMerge() { - check("issues/issue139-1.yaml");// merge with unique keys - check("issues/issue139-2.yaml");// merge with same key - } + public void testNotUniqueSimple() { + String simple = "{key: 1, key: 2}"; + Yaml yaml = new Yaml(); + @SuppressWarnings("unchecked") + Map<String, Integer> map = yaml.load(simple); + assertEquals(1, map.size()); + assertEquals(Integer.valueOf(2), map.get("key")); + } - private void check(String name) { - String input = Util.getLocalResource(name); - // System.out.println(input); - Yaml yaml = new Yaml(); - @SuppressWarnings("unchecked") - Map<String, Object> map = (Map<String, Object>) yaml.load(input); - assertEquals(2, map.size()); - assertTrue(map.containsKey("common")); - assertTrue(map.containsKey("production")); - assertEquals(map.get("common"), map.get("production")); - } + public void testMerge() { + check("issues/issue139-1.yaml");// merge with unique keys + check("issues/issue139-2.yaml");// merge with same key + } - /** - * http://yaml.org/type/merge.html: If the value associated with the key is - * a single mapping node, each of its key/value pairs is inserted into the - * current mapping, <b>unless the key already exists in it</b>. - */ + private void check(String name) { + String input = Util.getLocalResource(name); + // System.out.println(input); + Yaml yaml = new Yaml(); @SuppressWarnings("unchecked") - public void testMergeUnlessAlreadyExists() { - String input = Util.getLocalResource("issues/issue139-3.yaml"); - // System.out.println(input); - Yaml yaml = new Yaml(); - Map<String, Object> map = (Map<String, Object>) yaml.load(input); - assertEquals(2, map.size()); - Map<String, Integer> common = (Map<String, Integer>) map.get("common"); - Map<String, Integer> production = (Map<String, Integer>) map.get("production"); - assertEquals(new Integer(2), common.get("key")); - assertEquals(new Integer(3), production.get("key")); - } + Map<String, Object> map = yaml.load(input); + assertEquals(2, map.size()); + assertTrue(map.containsKey("common")); + assertTrue(map.containsKey("production")); + assertEquals(map.get("common"), map.get("production")); + } + + /** + * http://yaml.org/type/merge.html: If the value associated with the key is a single mapping node, + * each of its key/value pairs is inserted into the current mapping, <b>unless the key already + * exists in it</b>. + */ + @SuppressWarnings("unchecked") + public void testMergeUnlessAlreadyExists() { + String input = Util.getLocalResource("issues/issue139-3.yaml"); + // System.out.println(input); + Yaml yaml = new Yaml(); + Map<String, Object> map = yaml.load(input); + assertEquals(2, map.size()); + Map<String, Integer> common = (Map<String, Integer>) map.get("common"); + Map<String, Integer> production = (Map<String, Integer>) map.get("production"); + assertEquals(Integer.valueOf(2), common.get("key")); + assertEquals(Integer.valueOf(3), production.get("key")); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue139/UniqueKeyTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue139/UniqueKeyTest.java index 4117b979..28488b02 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue139/UniqueKeyTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue139/UniqueKeyTest.java @@ -1,25 +1,21 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue139; import java.util.List; import java.util.Map; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; import org.yaml.snakeyaml.error.YAMLException; @@ -29,36 +25,36 @@ import org.yaml.snakeyaml.nodes.NodeTuple; public class UniqueKeyTest extends TestCase { - public void testNotUnique() { - String data = "{key: 1, key: 2}"; - Yaml yaml = new Yaml(new UniqueKeyConstructor()); - try { - yaml.load(data); - fail("The same key must be rejected"); - } catch (Exception e) { - assertEquals("The key is not unique key", e.getMessage()); - } + public void testNotUnique() { + String data = "{key: 1, key: 2}"; + Yaml yaml = new Yaml(new UniqueKeyConstructor()); + try { + yaml.load(data); + fail("The same key must be rejected"); + } catch (Exception e) { + assertEquals("The key is not unique key", e.getMessage()); } - - private class UniqueKeyConstructor extends Constructor { - - @Override - protected void constructMapping2ndStep(MappingNode node, Map<Object, Object> mapping) { - List<NodeTuple> nodeValue = (List<NodeTuple>) node.getValue(); - for (NodeTuple tuple : nodeValue) { - Node keyNode = tuple.getKeyNode(); - Node valueNode = tuple.getValueNode(); - Object key = constructObject(keyNode); - if (key != null) { - key.hashCode();// check circular dependencies - } - Object value = constructObject(valueNode); - Object old = mapping.put(key, value); - if (old != null) { - throw new YAMLException("The key is not unique " + key); - } - } + } + + private class UniqueKeyConstructor extends Constructor { + + @Override + protected void constructMapping2ndStep(MappingNode node, Map<Object, Object> mapping) { + List<NodeTuple> nodeValue = node.getValue(); + for (NodeTuple tuple : nodeValue) { + Node keyNode = tuple.getKeyNode(); + Node valueNode = tuple.getValueNode(); + Object key = constructObject(keyNode); + if (key != null) { + key.hashCode();// check circular dependencies + } + Object value = constructObject(valueNode); + Object old = mapping.put(key, value); + if (old != null) { + throw new YAMLException("The key is not unique " + key); } + } } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue141/ConfigurableTimezoneTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue141/ConfigurableTimezoneTest.java index 872af854..145e5784 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue141/ConfigurableTimezoneTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue141/ConfigurableTimezoneTest.java @@ -1,45 +1,41 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue141; import java.util.Date; import java.util.TimeZone; - import junit.framework.TestCase; - import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; public class ConfigurableTimezoneTest extends TestCase { - public void testNoTimezone() { - Yaml yaml = new Yaml(); - String output = yaml.dump(new Date()); - assertTrue(output, output.endsWith("Z\n")); - } + public void testNoTimezone() { + Yaml yaml = new Yaml(); + String output = yaml.dump(new Date()); + assertTrue(output, output.endsWith("Z\n")); + } - public void testTimezone() { - DumperOptions options = new DumperOptions(); - options.setTimeZone(TimeZone.getTimeZone("GMT+1:00")); - Yaml yaml = new Yaml(options); - Date date = new Date(); - String output = yaml.dump(date); - // System.out.println(output); - assertTrue(output, output.trim().endsWith("+1:00")); - Date parsed = (Date) yaml.load(output); - assertEquals(date, parsed); - } + public void testTimezone() { + DumperOptions options = new DumperOptions(); + options.setTimeZone(TimeZone.getTimeZone("GMT+1:00")); + Yaml yaml = new Yaml(options); + Date date = new Date(); + String output = yaml.dump(date); + // System.out.println(output); + assertTrue(output, output.trim().endsWith("+01:00")); + Date parsed = yaml.load(output); + assertEquals(date, parsed); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue142/DerivedMapTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue142/DerivedMapTest.java new file mode 100644 index 00000000..c78b94a1 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue142/DerivedMapTest.java @@ -0,0 +1,70 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue142; + +import java.util.LinkedHashMap; +import java.util.Map; +import junit.framework.TestCase; +import org.yaml.snakeyaml.Yaml; + +public class DerivedMapTest extends TestCase { + + public static class Features extends LinkedHashMap<String, Object> { + + } + + public static class MyObject { + + private String name; + private Map<String, Object> features = new LinkedHashMap<String, Object>(); + private Features moreFeatures = new Features(); + + public Map<String, Object> getFeatures() { + return features; + } + + public void setFeatures(Map<String, Object> features) { + this.features = features; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Features getMoreFeatures() { + return moreFeatures; + } + + public void setMoreFeatures(Features moreFeatures) { + this.moreFeatures = moreFeatures; + } + + } + + public void testDerivedMap() { + MyObject o = new MyObject(); + o.setName("Mickey"); + o.getFeatures().put("Address", "Disney"); + o.getMoreFeatures().put("Address", "Disney"); + Yaml yaml = new Yaml(); + String asYaml = yaml.dump(o); + // System.out.println(asYaml); + MyObject o2 = yaml.load(asYaml); + } + +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue143/GenericMapTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue143/GenericMapTest.java index c98449ec..8c7ec921 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue143/GenericMapTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue143/GenericMapTest.java @@ -1,45 +1,44 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue143; import junit.framework.TestCase; - import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; public class GenericMapTest extends TestCase { - public void testMap() throws Exception { - BeanWithMap fact = new BeanWithMap(); - GenericMap<Integer> shash = fact.getMap(); - shash.put("toto", new Integer(10)); - DumperOptions options = new DumperOptions(); - options.setAllowReadOnlyProperties(true); - Yaml yaml = new Yaml(options); - // String txt = yaml.dump(fact); - // assertTrue(txt.contains("org.yaml.snakeyaml.issues.issue143.GenericMapTest")); - } + public void testMap() throws Exception { + BeanWithMap fact = new BeanWithMap(); + GenericMap<Integer> shash = fact.getMap(); + shash.put("toto", Integer.valueOf(10)); + DumperOptions options = new DumperOptions(); + options.setAllowReadOnlyProperties(true); + Yaml yaml = new Yaml(options); + // String txt = yaml.dump(fact); + // assertTrue(txt.contains("org.yaml.snakeyaml.issues.issue143.GenericMapTest")); + } - public static class GenericMap<T> extends java.util.HashMap<String, T> { - private static final long serialVersionUID = -6833859369398863926L; - } + public static class GenericMap<T> extends java.util.HashMap<String, T> { + + private static final long serialVersionUID = -6833859369398863926L; + } + + public class BeanWithMap { - public class BeanWithMap { - public GenericMap<Integer> getMap() { - return new GenericMap<Integer>(); - } + public GenericMap<Integer> getMap() { + return new GenericMap<Integer>(); } -}
\ No newline at end of file + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue144/BeanData.java b/src/test/java/org/yaml/snakeyaml/issues/issue144/BeanData.java index 79880002..71b35192 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue144/BeanData.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue144/BeanData.java @@ -1,46 +1,45 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue144; public class BeanData { - private String id; - private float number; - public BeanData() { - this.id = "noid"; - } + private String id; + private float number; - public BeanData(String id) { - this.id = id; - } + public BeanData() { + this.id = "noid"; + } - public String getId() { - return id; - } + public BeanData(String id) { + this.id = id; + } - public void setId(String id) { - this.id = id; - } + public String getId() { + return id; + } - public float getNumber() { - return number; - } + public void setId(String id) { + this.id = id; + } - public void setNumber(float number) { - this.number = number; - } + public float getNumber() { + return number; + } -}
\ No newline at end of file + public void setNumber(float number) { + this.number = number; + } + +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue144/FloatPropertyTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue144/FloatPropertyTest.java index b9492aad..527aa2c4 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue144/FloatPropertyTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue144/FloatPropertyTest.java @@ -1,42 +1,38 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue144; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.extensions.compactnotation.CompactConstructor; public class FloatPropertyTest extends TestCase { - public void testFloatAsJavaBeanProperty() throws Exception { - BeanData bean = new BeanData(); - bean.setId("id1"); - bean.setNumber(3.5f); - Yaml yaml = new Yaml(); - String txt = yaml.dump(bean); - BeanData parsed = yaml.loadAs(txt, BeanData.class); - assertEquals(3.5f, parsed.getNumber()); - } + public void testFloatAsJavaBeanProperty() throws Exception { + BeanData bean = new BeanData(); + bean.setId("id1"); + bean.setNumber(3.5f); + Yaml yaml = new Yaml(); + String txt = yaml.dump(bean); + BeanData parsed = yaml.loadAs(txt, BeanData.class); + assertEquals(3.5f, parsed.getNumber()); + } - public void testCompact() { - Yaml yaml = new Yaml(new CompactConstructor()); - BeanData obj = (BeanData) yaml - .load("org.yaml.snakeyaml.issues.issue144.BeanData(id): { number: 123.4 }"); - assertEquals(123.4f, obj.getNumber()); - } + public void testCompact() { + Yaml yaml = new Yaml(new CompactConstructor()); + BeanData obj = yaml.load("org.yaml.snakeyaml.issues.issue144.BeanData(id): { number: 123.4 }"); + assertEquals(123.4f, obj.getNumber()); + } -}
\ No newline at end of file +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue145/AbstractThing.java b/src/test/java/org/yaml/snakeyaml/issues/issue145/AbstractThing.java index c169e387..516faac3 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue145/AbstractThing.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue145/AbstractThing.java @@ -1,28 +1,27 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue145; public abstract class AbstractThing { - protected String id; - public String getId() { - return id; - } + protected String id; - public void setId(String id) { - this.id = id; - } + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue145/CompleteThing.java b/src/test/java/org/yaml/snakeyaml/issues/issue145/CompleteThing.java index eca4a066..498b7f46 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue145/CompleteThing.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue145/CompleteThing.java @@ -1,24 +1,22 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue145; public class CompleteThing extends AbstractThing { - @Override - public String toString() { - return "CompleteThing-" + id; - } + @Override + public String toString() { + return "CompleteThing-" + id; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue145/LineNumberInExceptionTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue145/LineNumberInExceptionTest.java index 39c8c740..5218fea0 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue145/LineNumberInExceptionTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue145/LineNumberInExceptionTest.java @@ -1,55 +1,54 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue145; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class LineNumberInExceptionTest extends TestCase { - public void testLineReport() { - Yaml yaml = new Yaml(); - try { - yaml.load("---\n!!org.yaml.snakeyaml.issues.issue145.AbstractThing { id: QQQ }"); - fail("Instances for abstract classes cannot be created"); - } catch (Exception e) { - assertTrue(e.toString().contains("line 2, column 1")); - assertEquals( - "Can't construct a java object for tag:yaml.org,2002:org.yaml.snakeyaml.issues.issue145.AbstractThing; exception=java.lang.InstantiationException\n" - + " in 'string', line 2, column 1:\n" - + " !!org.yaml.snakeyaml.issues.issu ... \n" + " ^\n", - e.getMessage()); - } - } + public void testLineReport() { + Yaml yaml = new Yaml(); + try { + yaml.load("---\n!!org.yaml.snakeyaml.issues.issue145.AbstractThing { id: QQQ }"); + fail("Instances for abstract classes cannot be created"); + } catch (Exception e) { + assertTrue(e.toString().contains("line 2, column 1")); - public void testCompleteThing() { - Yaml yaml = new Yaml(); - CompleteThing thing = (CompleteThing) yaml - .load("---\n!!org.yaml.snakeyaml.issues.issue145.CompleteThing { id: QQQ }"); - assertEquals("QQQ", thing.getId()); + String tag = "tag:yaml.org,2002:org.yaml.snakeyaml.issues.issue145.AbstractThing"; + String expectedError = + String.format("Can't create an instance for %s\n" + " in 'string', line 2, column 1:\n" + + " !!org.yaml.snakeyaml.issues.issu ... \n" + " ^\n", tag); + + assertEquals(expectedError, e.getMessage()); } + } + + public void testCompleteThing() { + Yaml yaml = new Yaml(); + CompleteThing thing = + yaml.load("---\n!!org.yaml.snakeyaml.issues.issue145.CompleteThing { id: QQQ }"); + assertEquals("QQQ", thing.getId()); + } - public void testWrongParameter() { - Yaml yaml = new Yaml(); - try { - yaml.load("---\n!!org.yaml.snakeyaml.issues.issue145.CompleteThing { id2: QQQ }"); - fail("Invalid parameter"); - } catch (Exception e) { - assertTrue("The error should ponit to QQQ.", e.toString().contains("line 2, column 59")); - } + public void testWrongParameter() { + Yaml yaml = new Yaml(); + try { + yaml.load("---\n!!org.yaml.snakeyaml.issues.issue145.CompleteThing { id2: QQQ }"); + fail("Invalid parameter"); + } catch (Exception e) { + assertTrue("The error should ponit to QQQ.", e.toString().contains("line 2, column 59")); } -}
\ No newline at end of file + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue147/PrintableTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue147/PrintableTest.java index a534b495..b96d8efa 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue147/PrintableTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue147/PrintableTest.java @@ -1,29 +1,27 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue147; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class PrintableTest extends TestCase { - // http://code.google.com/p/snakeyaml/issues/detail?id=147 - public void testFFFD() { - Yaml yaml = new Yaml(); - String fffd = (String) yaml.load(yaml.dump("\uFFFD")); - assertEquals("\uFFFD", fffd); - } -}
\ No newline at end of file + + // http://code.google.com/p/snakeyaml/issues/detail?id=147 + public void testFFFD() { + Yaml yaml = new Yaml(); + String fffd = yaml.load(yaml.dump("\uFFFD")); + assertEquals("\uFFFD", fffd); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue148/PrintableUnicodeTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue148/PrintableUnicodeTest.java index 0fa9322b..9a0b7afd 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue148/PrintableUnicodeTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue148/PrintableUnicodeTest.java @@ -1,177 +1,180 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue148; import java.util.Formatter; - import junit.framework.TestCase; - import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions.ScalarStyle; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.reader.ReaderException; public class PrintableUnicodeTest extends TestCase { - public void testFFFD() { - Yaml yaml = createYaml(); - String fffd = yaml.dump("\uFFFD"); - assertEquals("\"\\ufffd\"\n", fffd); - } - public void testSerialization() { - // test serialization of all Unicode codepoints - Yaml yaml = createYaml(); - for (int c = Character.MIN_VALUE; c <= Character.MAX_VALUE; c++) { - String original = Character.toString((char) c); - String serialized = yaml.dump(original); + public void testFFFD() { + Yaml yaml = createYaml(); + String fffd = yaml.dump("\uFFFD"); + assertEquals("\"\\ufffd\"\n", fffd); + } - // "On output, a YAML processor must only produce these acceptable - // characters, - // and should also escape all non-printable Unicode characters." - for (int i = 0; i < serialized.length(); i++) { - int cp = (int) serialized.charAt(i); - if (!isAcceptable(cp)) - fail(String.format( - "U+%04x: Serialization produced result with unacceptable U+%04x\n", c, - cp)); - if (!isPrintable(cp)) - fail(String.format( - "U+%04x: Serialization produced result with nonprintable U+%04x\n", c, - cp)); - } - } - } + public void testSerialization() { + // test serialization of valid Unicode BMP codepoints + Yaml yaml = createYaml(); + for (int c = Character.MIN_VALUE; c <= Character.MAX_VALUE; c++) { + if (Character.isHighSurrogate((char) c) || Character.isLowSurrogate((char) c)) { + continue; + } - public void testDeserialization() { - // test deserialization of non-escaped codepoints - for (int c = Character.MIN_VALUE; c <= Character.MAX_VALUE; c++) { - // ignore breaks, which have special meaning - if (c == 0x0A || c == 0x0D || c == 0x85 || c == 0x2028 || c == 0x2029) - continue; - if (!isAcceptable(c) || c == 0x27) - continue; - String expected = Character.toString((char) c); - String serialized = "'" + expected + "'"; + String original = Character.toString((char) c); + String serialized = yaml.dump(original); - String result; - try { - result = new Yaml().load(serialized).toString(); - } catch (ReaderException e) { - fail(String - .format("U+%04x: Deserialization threw ReaderException for an acceptable character\n", - c)); - continue; - } - if (!result.equals(expected)) - fail(String.format("U+%04x: Deserialization incorrect: %s\n", c, hexdump(result))); + // "On output, a YAML processor must only produce these acceptable + // characters, + // and should also escape all non-printable Unicode characters." + for (int i = 0; i < serialized.length(); i++) { + int cp = serialized.charAt(i); + if (!isAcceptable(cp)) { + fail(String.format("U+%04x: Serialization produced result with unacceptable U+%04x\n", c, + cp)); + } + if (!isPrintable(cp)) { + fail(String.format("U+%04x: Serialization produced result with nonprintable U+%04x\n", c, + cp)); } + } } + } - public void testDeserialization2() { - // test deserialization of escaped codepoints - // "Any such characters must be presented using escape sequences." - for (int c = Character.MIN_VALUE; c <= Character.MAX_VALUE; c++) { - String expected = Character.toString((char) c); - String serialized = String.format("\"\\u%04x\"", c); + public void testDeserialization() { + // test deserialization of non-escaped codepoints + for (int c = Character.MIN_VALUE; c <= Character.MAX_VALUE; c++) { + // ignore breaks, which have special meaning + if (c == 0x0A || c == 0x0D || c == 0x85 || c == 0x2028 || c == 0x2029) { + continue; + } + if (!isAcceptable(c) || c == 0x27) { + continue; + } + String expected = Character.toString((char) c); + String serialized = "'" + expected + "'"; - String result; - try { - result = new Yaml().load(serialized).toString(); - } catch (ReaderException e) { - fail(String - .format("U+%04x: Deserialization threw ReaderException for an acceptable escaped character\n", - c)); - continue; - } - if (!result.equals(expected)) - fail(String.format("U+%04x: Deserialization of escaped character incorrect: %s\n", - c, hexdump(result))); - } + String result; + try { + result = new Yaml().load(serialized).toString(); + } catch (ReaderException e) { + fail(String.format( + "U+%04x: Deserialization threw ReaderException for an acceptable character\n", c)); + continue; + } + if (!result.equals(expected)) { + fail(String.format("U+%04x: Deserialization incorrect: %s\n", c, hexdump(result))); + } } + } - private Yaml createYaml() { - DumperOptions options = new DumperOptions(); - options.setAllowUnicode(false); - options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED); - return new Yaml(options); - } + public void testDeserialization2() { + // test deserialization of escaped codepoints + // "Any such characters must be presented using escape sequences." + for (int c = Character.MIN_VALUE; c <= Character.MAX_VALUE; c++) { + String expected = Character.toString((char) c); + String serialized = String.format("\"\\u%04x\"", c); - /** - * Test whether a character is printable, according to the YAML spec. - * ('c-printable') - */ - public static boolean isPrintable(int c) { - return c == 0x9 || c == 0xA || c == 0xD || (c >= 0x20 && c <= 0x7E) // 8 - // bit - || c == 0x85 || (c >= 0xA0 && c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFD) // 16 - // bit - || (c >= 0x10000 && c <= 0x10FFFF); // 32 bit + String result; + try { + result = new Yaml().load(serialized).toString(); + } catch (ReaderException e) { + fail(String.format( + "U+%04x: Deserialization threw ReaderException for an acceptable escaped character\n", + c)); + continue; + } + if (!result.equals(expected)) { + fail(String.format("U+%04x: Deserialization of escaped character incorrect: %s\n", c, + hexdump(result))); + } } + } + + private Yaml createYaml() { + DumperOptions options = new DumperOptions(); + options.setAllowUnicode(false); + options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED); + return new Yaml(options); + } - /** - * "On input, a YAML processor must accept all printable ASCII characters, - * the space, tab, line break, and all Unicode characters beyond #x9F. On - * output, a YAML processor must only produce these acceptable characters, - * and should also escape all non-printable Unicode characters. The allowed - * character range explicitly excludes the surrogate block #xD800-#xDFFF, - * DEL #x7F, the C0 control block #x0-#x1F (except for #x9, #xA, and #xD), - * the C1 control block #x80-#x9F, #xFFFE, and #xFFFF." - */ - public static boolean isAcceptable(int c) { - return (c >= 0x20 && c <= 0x7e // accept all printable ASCII characters, - // the space, - || c == 0x09 // tab, - || c == 0x0A || c == 0x0D || c == 0x85 || c == 0x2028 || c == 0x2029 // line - // break, + /** + * Test whether a character is printable, according to the YAML spec. ('c-printable') + */ + public static boolean isPrintable(int c) { + return c == 0x9 || c == 0xA || c == 0xD || (c >= 0x20 && c <= 0x7E) // 8 + // bit + || c == 0x85 || (c >= 0xA0 && c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFD) // 16 + // bit + || (c >= 0x10000 && c <= 0x10FFFF); // 32 bit + } + + /** + * "On input, a YAML processor must accept all printable ASCII characters, the space, tab, line + * break, and all Unicode characters beyond #x9F. On output, a YAML processor must only produce + * these acceptable characters, and should also escape all non-printable Unicode characters. The + * allowed character range explicitly excludes the surrogate block #xD800-#xDFFF, DEL #x7F, the C0 + * control block #x0-#x1F (except for #x9, #xA, and #xD), the C1 control block #x80-#x9F, #xFFFE, + * and #xFFFF." + */ + public static boolean isAcceptable(int c) { + return (c >= 0x20 && c <= 0x7e // accept all printable ASCII characters, + // the space, + || c == 0x09 // tab, + || c == 0x0A || c == 0x0D || c == 0x85 || c == 0x2028 || c == 0x2029 // line + // break, || isUnicodeCharacter(c) && c >= 0x9F // and all Unicode characters - // beyond #x9F - ) && !( // The allowed character range explicitly excludes - c >= 0xD800 && c <= 0xDFFF // the surrogate block #xD800-#xDFFF - || c == 0x7f // DEL #x7F, - || c <= 0x1F && !(c == 0x09 || c == 0x0A || c == 0x0D) // the - // C0 - // control - // block - // #x0-#x1F - // (except - // for - // #x9, - // #xA, - // and - // #xD), - || c >= 0x80 && c <= 0x9F // the C1 control block - // #x80-#x9F, - || c == 0xFFFE // #xFFFE, - || c == 0xFFFF // and #xFFFF. - ); - } + // beyond #x9F + ) && !( // The allowed character range explicitly excludes + c >= 0xD800 && c <= 0xDFFF // the surrogate block #xD800-#xDFFF + || c == 0x7f // DEL #x7F, + || c <= 0x1F && !(c == 0x09 || c == 0x0A || c == 0x0D) // the + // C0 + // control + // block + // #x0-#x1F + // (except + // for + // #x9, + // #xA, + // and + // #xD), + || c >= 0x80 && c <= 0x9F // the C1 control block + // #x80-#x9F, + || c == 0xFFFE // #xFFFE, + || c == 0xFFFF // and #xFFFF. + ); + } - /** - * Tests whether a codepoint is a designated Unicode noncharacter or not. - */ - public static boolean isUnicodeCharacter(int c) { - int plane = c / 0x10000; - return !(c >= 0xFDD0 && c <= 0xFDEF) && (plane <= 16 && (c & 0xFFFE) != 0xFFFE); - } + /** + * Tests whether a codepoint is a designated Unicode noncharacter or not. + */ + public static boolean isUnicodeCharacter(int c) { + int plane = c / 0x10000; + return !(c >= 0xFDD0 && c <= 0xFDEF) && (plane <= 16 && (c & 0xFFFE) != 0xFFFE); + } - public static String hexdump(String input) { - StringBuilder result = new StringBuilder(); - Formatter formatter = new Formatter(result); - for (int i = 0; i < input.length(); i++) - formatter.format("%02x ", (int) input.charAt(i)); - return result.toString(); + public static String hexdump(String input) { + StringBuilder result = new StringBuilder(); + Formatter formatter = new Formatter(result); + for (int i = 0; i < input.length(); i++) { + formatter.format("%02x ", (int) input.charAt(i)); } -}
\ No newline at end of file + return result.toString(); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue149/ComponentBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue149/ComponentBean.java index 97f7d1ed..54fa6f28 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue149/ComponentBean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue149/ComponentBean.java @@ -1,37 +1,36 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue149; public class ComponentBean { - private int property1; - private String property2; - public int getProperty1() { - return property1; - } + private int property1; + private String property2; - public void setProperty1(int property1) { - this.property1 = property1; - } + public int getProperty1() { + return property1; + } - public String getProperty2() { - return property2; - } + public void setProperty1(int property1) { + this.property1 = property1; + } - public void setProperty2(String property2) { - this.property2 = property2; - } + public String getProperty2() { + return property2; + } + + public void setProperty2(String property2) { + this.property2 = property2; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue149/GlobalDirectivesTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue149/GlobalDirectivesTest.java index 6c2b5f41..67ee4de3 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue149/GlobalDirectivesTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue149/GlobalDirectivesTest.java @@ -1,24 +1,20 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue149; import java.util.Iterator; - import junit.framework.TestCase; - import org.yaml.snakeyaml.TypeDescription; import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; @@ -26,54 +22,55 @@ import org.yaml.snakeyaml.constructor.Constructor; import org.yaml.snakeyaml.nodes.Tag; public class GlobalDirectivesTest extends TestCase { - public void testOneDocument() { - String input = Util.getLocalResource("issues/issue149-one-document.yaml"); - // System.out.println(input); - Constructor constr = new Constructor(); - TypeDescription description = new TypeDescription(ComponentBean.class, new Tag( - "tag:ualberta.ca,2012:29")); - constr.addTypeDescription(description); - Yaml yaml = new Yaml(constr); - Iterator<Object> parsed = yaml.loadAll(input).iterator(); - ComponentBean bean = (ComponentBean) parsed.next(); - assertEquals(0, bean.getProperty1()); - assertEquals("aaa", bean.getProperty2()); - assertFalse(parsed.hasNext()); - } - public void testDirectives() { - String input = Util.getLocalResource("issues/issue149-losing-directives.yaml"); - // System.out.println(input); - Constructor constr = new Constructor(); - TypeDescription description = new TypeDescription(ComponentBean.class, new Tag( - "tag:ualberta.ca,2012:" + 29)); - constr.addTypeDescription(description); - Yaml yaml = new Yaml(constr); - Iterator<Object> parsed = yaml.loadAll(input).iterator(); - ComponentBean bean1 = (ComponentBean) parsed.next(); - assertEquals(0, bean1.getProperty1()); - assertEquals("aaa", bean1.getProperty2()); - ComponentBean bean2 = (ComponentBean) parsed.next(); - assertEquals(3, bean2.getProperty1()); - assertEquals("bbb", bean2.getProperty2()); - assertFalse(parsed.hasNext()); - } + public void testOneDocument() { + String input = Util.getLocalResource("issues/issue149-one-document.yaml"); + // System.out.println(input); + Constructor constr = new Constructor(); + TypeDescription description = + new TypeDescription(ComponentBean.class, new Tag("tag:ualberta.ca,2012:29")); + constr.addTypeDescription(description); + Yaml yaml = new Yaml(constr); + Iterator<Object> parsed = yaml.loadAll(input).iterator(); + ComponentBean bean = (ComponentBean) parsed.next(); + assertEquals(0, bean.getProperty1()); + assertEquals("aaa", bean.getProperty2()); + assertFalse(parsed.hasNext()); + } + + public void testDirectives() { + String input = Util.getLocalResource("issues/issue149-losing-directives.yaml"); + // System.out.println(input); + Constructor constr = new Constructor(); + TypeDescription description = + new TypeDescription(ComponentBean.class, new Tag("tag:ualberta.ca,2012:" + 29)); + constr.addTypeDescription(description); + Yaml yaml = new Yaml(constr); + Iterator<Object> parsed = yaml.loadAll(input).iterator(); + ComponentBean bean1 = (ComponentBean) parsed.next(); + assertEquals(0, bean1.getProperty1()); + assertEquals("aaa", bean1.getProperty2()); + ComponentBean bean2 = (ComponentBean) parsed.next(); + assertEquals(3, bean2.getProperty1()); + assertEquals("bbb", bean2.getProperty2()); + assertFalse(parsed.hasNext()); + } - public void testDirectives2() { - String input = Util.getLocalResource("issues/issue149-losing-directives-2.yaml"); - // System.out.println(input); - Constructor constr = new Constructor(); - TypeDescription description = new TypeDescription(ComponentBean.class, new Tag( - "tag:ualberta.ca,2012:" + 29)); - constr.addTypeDescription(description); - Yaml yaml = new Yaml(constr); - Iterator<Object> parsed = yaml.loadAll(input).iterator(); - ComponentBean bean1 = (ComponentBean) parsed.next(); - assertEquals(0, bean1.getProperty1()); - assertEquals("aaa", bean1.getProperty2()); - ComponentBean bean2 = (ComponentBean) parsed.next(); - assertEquals(3, bean2.getProperty1()); - assertEquals("bbb", bean2.getProperty2()); - assertFalse(parsed.hasNext()); - } -}
\ No newline at end of file + public void testDirectives2() { + String input = Util.getLocalResource("issues/issue149-losing-directives-2.yaml"); + // System.out.println(input); + Constructor constr = new Constructor(); + TypeDescription description = + new TypeDescription(ComponentBean.class, new Tag("tag:ualberta.ca,2012:" + 29)); + constr.addTypeDescription(description); + Yaml yaml = new Yaml(constr); + Iterator<Object> parsed = yaml.loadAll(input).iterator(); + ComponentBean bean1 = (ComponentBean) parsed.next(); + assertEquals(0, bean1.getProperty1()); + assertEquals("aaa", bean1.getProperty2()); + ComponentBean bean2 = (ComponentBean) parsed.next(); + assertEquals(3, bean2.getProperty1()); + assertEquals("bbb", bean2.getProperty2()); + assertFalse(parsed.hasNext()); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue150/Car.java b/src/test/java/org/yaml/snakeyaml/issues/issue150/Car.java index 1973d9d1..b602bd98 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue150/Car.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue150/Car.java @@ -1,47 +1,45 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue150; import java.util.Collection; /** - * + * * @author tturki - * + * */ public class Car { - private String plate; - private Collection<Wheel> wheels; - public Car() { - } + private String plate; + private Collection<Wheel> wheels; + + public Car() {} - public String getPlate() { - return plate; - } + public String getPlate() { + return plate; + } - public void setPlate(String plate) { - this.plate = plate; - } + public void setPlate(String plate) { + this.plate = plate; + } - public Collection<Wheel> getWheels() { - return wheels; - } + public Collection<Wheel> getWheels() { + return wheels; + } - public void setWheels(Collection<Wheel> wheels) { - this.wheels = wheels; - } -}
\ No newline at end of file + public void setWheels(Collection<Wheel> wheels) { + this.wheels = wheels; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue150/Wheel.java b/src/test/java/org/yaml/snakeyaml/issues/issue150/Wheel.java index bae16372..dbd767f4 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue150/Wheel.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue150/Wheel.java @@ -1,31 +1,29 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue150; public class Wheel { - private Integer id; - public Wheel() { - } + private Integer id; - public Integer getId() { - return id; - } + public Wheel() {} - public void setId(Integer id) { - this.id = id; - } -}
\ No newline at end of file + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue150/YamlLoadAsIssueTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue150/YamlLoadAsIssueTest.java index f4002d46..ea2af791 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue150/YamlLoadAsIssueTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue150/YamlLoadAsIssueTest.java @@ -1,32 +1,26 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue150; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.Reader; import java.io.StringReader; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import java.util.regex.Pattern; - import org.junit.Before; import org.junit.Test; import org.yaml.snakeyaml.Yaml; @@ -41,101 +35,102 @@ import org.yaml.snakeyaml.nodes.Tag; public class YamlLoadAsIssueTest { - private StringBuilder doc; - - @Before - public void setUp() { - doc = new StringBuilder(); - line("!car"); - line("plate: 12-XP-F4"); - line("wheels:"); - line("- w#1"); - line("- w#2"); - line("- w#3"); - line("- w#4"); + private StringBuilder doc; + + @Before + public void setUp() { + doc = new StringBuilder(); + line("!car"); + line("plate: 12-XP-F4"); + line("wheels:"); + line("- w#1"); + line("- w#2"); + line("- w#3"); + line("- w#4"); + } + + private StringBuilder line(String text) { + return doc.append(text).append('\n'); + } + + @Test + public void loadConstructsDocumentCorrectly() { + Yaml yaml = yaml(); + Object result = yaml.load(reader()); + assertNotNull(result); + assertEquals(Car.class, result.getClass()); + assertEquals("12-XP-F4", ((Car) result).getPlate()); + assertEquals(4, ((Car) result).getWheels().size()); + } + + private Yaml yaml() { + Yaml yaml = new Yaml(new MyConstructor()); + yaml.addImplicitResolver(new Tag("!wheel"), Pattern.compile("w#\\d+"), "w"); + return yaml; + } + + @Test + public void ignoreImplicitTag() { + Yaml yaml = yaml(); + Car car = yaml.loadAs(reader(), Car.class); + assertNotNull(car); + assertEquals("12-XP-F4", car.getPlate()); + ArrayList<Wheel> wheels = new ArrayList<Wheel>(car.getWheels()); + assertEquals(4, wheels.size()); + for (int i = 0; i < wheels.size(); i++) { + assertEquals(wheels.get(i).getId(), Integer.valueOf(i + 1)); } + } - private StringBuilder line(String text) { - return doc.append(text).append('\n'); - } + private Reader reader() { + return new StringReader(doc.toString()); + } - @Test - public void loadConstructsDocumentCorrectly() { - Yaml yaml = yaml(); - Object result = yaml.load(reader()); - assertNotNull(result); - assertEquals(Car.class, result.getClass()); - assertEquals("12-XP-F4", ((Car) result).getPlate()); - assertEquals(4, ((Car) result).getWheels().size()); - } - - private Yaml yaml() { - Yaml yaml = new Yaml(new MyConstructor()); - yaml.addImplicitResolver(new Tag("!wheel"), Pattern.compile("w#\\d+"), "w"); - return yaml; - } + private class MyConstructor extends Constructor { - @Test - public void ignoreImplicitTag() { - Yaml yaml = yaml(); - Car car = yaml.loadAs(reader(), Car.class); - assertNotNull(car); - assertEquals("12-XP-F4", car.getPlate()); - ArrayList<Wheel> wheels = new ArrayList<Wheel>(car.getWheels()); - assertEquals(4, wheels.size()); - for (int i = 0; i < wheels.size(); i++) { - assertEquals(wheels.get(i).getId(), Integer.valueOf(i + 1)); - } + public MyConstructor() { + yamlConstructors.put(new Tag("!car"), new ConstructCar()); + yamlConstructors.put(new Tag("!wheel"), new ConstructWheel()); } - private Reader reader() { - return new StringReader(doc.toString()); + private String toScalarString(Node node) { + return constructScalar((ScalarNode) node); } - private class MyConstructor extends Constructor { - public MyConstructor() { - yamlConstructors.put(new Tag("!car"), new ConstructCar()); - yamlConstructors.put(new Tag("!wheel"), new ConstructWheel()); - } - - private String toScalarString(Node node) { - return (String) constructScalar((ScalarNode) node); + private class ConstructCar extends AbstractConstruct { + + @SuppressWarnings("unchecked") + public Car construct(Node node) { + Car car = new Car(); + MappingNode mapping = (MappingNode) node; + List<NodeTuple> list = mapping.getValue(); + for (NodeTuple tuple : list) { + String field = toScalarString(tuple.getKeyNode()); + if ("plate".equals(field)) { + car.setPlate(toScalarString(tuple.getValueNode())); + } + if ("wheels".equals(field)) { + SequenceNode snode = (SequenceNode) tuple.getValueNode(); + List<Wheel> wheels = (List<Wheel>) constructSequence(snode); + car.setWheels(wheels); + } } + return car; + } + } - private class ConstructCar extends AbstractConstruct { - - @SuppressWarnings("unchecked") - public Car construct(Node node) { - Car car = new Car(); - MappingNode mapping = (MappingNode) node; - List<NodeTuple> list = mapping.getValue(); - for (NodeTuple tuple : list) { - String field = toScalarString(tuple.getKeyNode()); - if ("plate".equals(field)) { - car.setPlate(toScalarString(tuple.getValueNode())); - } - if ("wheels".equals(field)) { - SequenceNode snode = (SequenceNode) tuple.getValueNode(); - List<Wheel> wheels = (List<Wheel>) constructSequence(snode); - car.setWheels(wheels); - } - } - return car; - } - } + private class ConstructWheel extends AbstractConstruct { - private class ConstructWheel extends AbstractConstruct { - - public Wheel construct(Node node) { - Wheel w = null; - String strValue = toScalarString(node); - if (strValue != null && strValue.length() > 2) { - strValue = strValue.trim().substring(2); - w = new Wheel(); - w.setId(Integer.valueOf(strValue)); - } - return w; - } + public Wheel construct(Node node) { + Wheel w = null; + String strValue = toScalarString(node); + if (strValue != null && strValue.length() > 2) { + strValue = strValue.trim().substring(2); + w = new Wheel(); + w.setId(Integer.valueOf(strValue)); } + return w; + } } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue151/EscapedUnicodeTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue151/EscapedUnicodeTest.java index 86a88fcd..e3500af3 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue151/EscapedUnicodeTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue151/EscapedUnicodeTest.java @@ -1,36 +1,33 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue151; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class EscapedUnicodeTest extends TestCase { - public void testUnicode() { - Yaml yaml = new Yaml(); - // http://www.tutorialspoint.com/html/ascii_table_lookup.htm - String str = (String) yaml.load("\"\\xC3\\xA4\""); - assertEquals("2 escape sequences must be converted to 2 characters.", "ä", str); - } + public void testUnicode() { + Yaml yaml = new Yaml(); + // http://www.tutorialspoint.com/html/ascii_table_lookup.htm + String str = yaml.load("\"\\xC3\\xA4\""); + assertEquals("2 escape sequences must be converted to 2 characters.", "ä", str); + } - public void testUnicode2() { - Yaml yaml = new Yaml(); - String str = (String) yaml.load("\"Acetylsalicyls\\xE4ure\""); - assertEquals("E4 -> ä", "Acetylsalicylsäure", str); - } + public void testUnicode2() { + Yaml yaml = new Yaml(); + String str = yaml.load("\"Acetylsalicyls\\xE4ure\""); + assertEquals("E4 -> ä", "Acetylsalicylsäure", str); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue154/MissingPropertyTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue154/MissingPropertyTest.java index 6a4c5146..431643a6 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue154/MissingPropertyTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue154/MissingPropertyTest.java @@ -1,22 +1,19 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue154; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; import org.yaml.snakeyaml.error.YAMLException; @@ -24,62 +21,62 @@ import org.yaml.snakeyaml.representer.Representer; public class MissingPropertyTest extends TestCase { - private Yaml yaml; + private Yaml yaml; - public void setUp() { - yaml = new Yaml(); - } + public void setUp() { + yaml = new Yaml(); + } - /** - * A normal scalar property should work fine. - */ - public void testPublicField() throws Exception { - String doc = "hello: 5"; - TestBean bean = yaml.loadAs(doc, TestBean.class); - assertNotNull(bean); - assertEquals(5, bean.hello); - } + /** + * A normal scalar property should work fine. + */ + public void testPublicField() throws Exception { + String doc = "hello: 5"; + TestBean bean = yaml.loadAs(doc, TestBean.class); + assertNotNull(bean); + assertEquals(5, bean.hello); + } - /** - * By default, unknown fields should throw a YAMLException. - */ - public void testUnknownField() throws Exception { - try { - String doc = "goodbye: 10"; - yaml.loadAs(doc, TestBean.class); - } catch (YAMLException e) { - assertTrue(e.getMessage().contains("Cannot create property=goodbye")); - } + /** + * By default, unknown fields should throw a YAMLException. + */ + public void testUnknownField() throws Exception { + try { + String doc = "goodbye: 10"; + yaml.loadAs(doc, TestBean.class); + } catch (YAMLException e) { + assertTrue(e.getMessage().contains("Cannot create property=goodbye")); } + } - /** - * A new method setSkipMissingProperties(boolean) was added to configure - * whether missing properties should throw a YAMLException (the default) or - * simply show a warning. The default is false. - */ - public void testSkipMissingProperties() throws Exception { - Representer representer = new Representer(); - representer.getPropertyUtils().setSkipMissingProperties(true); - yaml = new Yaml(new Constructor(), representer); - String doc = "goodbye: 10\nhello: 5\nfizz: [1]"; - TestBean bean = yaml.loadAs(doc, TestBean.class); - assertNotNull(bean); - assertEquals(5, bean.hello); - } + /** + * A new method setSkipMissingProperties(boolean) was added to configure whether missing + * properties should throw a YAMLException (the default) or simply show a warning. The default is + * false. + */ + public void testSkipMissingProperties() throws Exception { + Representer representer = new Representer(); + representer.getPropertyUtils().setSkipMissingProperties(true); + yaml = new Yaml(new Constructor(), representer); + String doc = "goodbye: 10\nhello: 5\nfizz: [1]"; + TestBean bean = yaml.loadAs(doc, TestBean.class); + assertNotNull(bean); + assertEquals(5, bean.hello); + } - /** - * The default for setSkipMissingProperties(boolean) is false; this just - * ensures it works if set manually. - */ - public void testNoSkipMissingProperties() throws Exception { - try { - Representer representer = new Representer(); - representer.getPropertyUtils().setSkipMissingProperties(false); - yaml = new Yaml(new Constructor(), representer); - String doc = "goodbye: 10"; - yaml.loadAs(doc, TestBean.class); - } catch (YAMLException e) { - assertTrue(e.getMessage().contains("Cannot create property=goodbye")); - } + /** + * The default for setSkipMissingProperties(boolean) is false; this just ensures it works if set + * manually. + */ + public void testNoSkipMissingProperties() throws Exception { + try { + Representer representer = new Representer(); + representer.getPropertyUtils().setSkipMissingProperties(false); + yaml = new Yaml(new Constructor(), representer); + String doc = "goodbye: 10"; + yaml.loadAs(doc, TestBean.class); + } catch (YAMLException e) { + assertTrue(e.getMessage().contains("Cannot create property=goodbye")); } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue154/TestBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue154/TestBean.java index 264b58b0..522d210a 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue154/TestBean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue154/TestBean.java @@ -1,20 +1,19 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue154; public class TestBean { - public int hello; + + public int hello; } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue155/BinaryTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue155/BinaryTest.java index f5c5ef29..21323f83 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue155/BinaryTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue155/BinaryTest.java @@ -1,34 +1,32 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue155; +import java.nio.charset.StandardCharsets; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class BinaryTest extends TestCase { - public void testBinaryString() throws Exception { - String data = "\u2666"; - byte[] bytes = data.getBytes("UTF-8"); - String inconsistentString = new String(bytes, "ISO-8859-1"); - Yaml yaml = new Yaml(); - String payload = yaml.dump(inconsistentString); - // System.out.println("payload: '" + payload + "'"); - String loaded = new String((byte[]) yaml.load(payload), "UTF-8"); - assertEquals(inconsistentString, loaded); - } + public void testBinaryString() throws Exception { + String data = "\u2666"; + byte[] bytes = data.getBytes(StandardCharsets.UTF_8); + String inconsistentString = new String(bytes, StandardCharsets.ISO_8859_1); + Yaml yaml = new Yaml(); + String payload = yaml.dump(inconsistentString); + // System.out.println("payload: '" + payload + "'"); + String loaded = new String((byte[]) yaml.load(payload), StandardCharsets.UTF_8); + assertEquals(inconsistentString, loaded); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue163/LinearScalaTralingTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue163/LinearScalaTralingTest.java index 6210f924..598a1ec8 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue163/LinearScalaTralingTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue163/LinearScalaTralingTest.java @@ -1,43 +1,39 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue163; import java.util.Map; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class LinearScalaTralingTest extends TestCase { - public void testClipChomping() throws Exception { - String data = "testnode: |\n This is line 1\n This is line 2\n"; - Yaml yaml = new Yaml(); - @SuppressWarnings("unchecked") - Map<String, String> payload = (Map<String, String>) yaml.load(data); - assertEquals("This is line 1\nThis is line 2\n", payload.get("testnode")); - } + public void testClipChomping() throws Exception { + String data = "testnode: |\n This is line 1\n This is line 2\n"; + Yaml yaml = new Yaml(); + @SuppressWarnings("unchecked") + Map<String, String> payload = yaml.load(data); + assertEquals("This is line 1\nThis is line 2\n", payload.get("testnode")); + } - public void testStripChomping() throws Exception { - // mind the trailing '|-' to indicate strip chomping - String data = "testnode: |-\n This is line 1\n This is line 2\n"; - Yaml yaml = new Yaml(); - @SuppressWarnings("unchecked") - Map<String, String> payload = (Map<String, String>) yaml.load(data); - assertEquals("No traling line break expected.", "This is line 1\nThis is line 2", - payload.get("testnode")); - } + public void testStripChomping() throws Exception { + // mind the trailing '|-' to indicate strip chomping + String data = "testnode: |-\n This is line 1\n This is line 2\n"; + Yaml yaml = new Yaml(); + @SuppressWarnings("unchecked") + Map<String, String> payload = yaml.load(data); + assertEquals("No traling line break expected.", "This is line 1\nThis is line 2", + payload.get("testnode")); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue171/ClassWithGenericMap.java b/src/test/java/org/yaml/snakeyaml/issues/issue171/ClassWithGenericMap.java index 5cfe7cc5..15ac3712 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue171/ClassWithGenericMap.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue171/ClassWithGenericMap.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue171; @@ -20,5 +18,7 @@ import java.util.HashMap; import java.util.Map; class ClassWithGenericMap { - public final Map<String, Collection<Integer>> services = new HashMap<String, Collection<Integer>>(); -}
\ No newline at end of file + + public final Map<String, Collection<Integer>> services = + new HashMap<String, Collection<Integer>>(); +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue171/CustomRepresenter.java b/src/test/java/org/yaml/snakeyaml/issues/issue171/CustomRepresenter.java index 093f90a9..976d9ed1 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue171/CustomRepresenter.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue171/CustomRepresenter.java @@ -1,34 +1,35 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue171; +import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.nodes.Node; import org.yaml.snakeyaml.nodes.Tag; import org.yaml.snakeyaml.representer.Represent; import org.yaml.snakeyaml.representer.Representer; class CustomRepresenter extends Representer { - public CustomRepresenter() { - this.representers.put(ClassWithGenericMap.class, new RepresentClassX()); - } - private class RepresentClassX implements Represent { - public Node representData(Object data) { - ClassWithGenericMap classX = (ClassWithGenericMap) data; - return representMapping(Tag.MAP, classX.services, false); - } + public CustomRepresenter() { + this.representers.put(ClassWithGenericMap.class, new RepresentClassX()); + } + + private class RepresentClassX implements Represent { + + public Node representData(Object data) { + ClassWithGenericMap classX = (ClassWithGenericMap) data; + return representMapping(Tag.MAP, classX.services, DumperOptions.FlowStyle.BLOCK); } -}
\ No newline at end of file + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue171/GenericExtendsObjectTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue171/GenericExtendsObjectTest.java index 4ff2a491..9dc67fad 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue171/GenericExtendsObjectTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue171/GenericExtendsObjectTest.java @@ -1,36 +1,32 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue171; import java.util.ArrayList; import java.util.List; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class GenericExtendsObjectTest extends TestCase { - public void testNoCompilationError() throws Exception { - Yaml yaml = new Yaml(new CustomRepresenter()); - ClassWithGenericMap map = new ClassWithGenericMap(); - List<Integer> list = new ArrayList<Integer>(); - list.add(7); - map.services.put("wow", list); - String output = yaml.dump(map); - assertEquals("wow: [7]\n", output); - } + public void testNoCompilationError() throws Exception { + Yaml yaml = new Yaml(new CustomRepresenter()); + ClassWithGenericMap map = new ClassWithGenericMap(); + List<Integer> list = new ArrayList<Integer>(); + list.add(7); + map.services.put("wow", list); + String output = yaml.dump(map); + assertEquals("wow: [7]\n", output); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue173/RecursiveAnchorTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue173/RecursiveAnchorTest.java index 4571d1ce..a712c169 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue173/RecursiveAnchorTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue173/RecursiveAnchorTest.java @@ -1,54 +1,50 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue173; import java.util.HashMap; import java.util.Map; - import junit.framework.TestCase; - import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.representer.Representer; public class RecursiveAnchorTest extends TestCase { - public void testWithoutCustomStyle() throws Exception { - Map<String, Map<String, Object>> rootMap = new HashMap<String, Map<String, Object>>(); - Map<String, Object> enclosedMap = new HashMap<String, Object>(); - enclosedMap.put("world", "test"); - rootMap.put("test", enclosedMap); - Yaml yaml = new Yaml(); - String output = yaml.dump(rootMap); - assertEquals("test: {world: test}\n", output); - } - - public void testWithBlockStyle() throws Exception { - Map<String, Map<String, Object>> rootMap = new HashMap<String, Map<String, Object>>(); - Map<String, Object> enclosedMap = new HashMap<String, Object>(); - enclosedMap.put("world", "test"); - rootMap.put("test", enclosedMap); - - DumperOptions yamlOptions = new DumperOptions(); - yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); - - Representer yamlRepresenter = new Representer(); - - Yaml yaml = new Yaml(yamlRepresenter, yamlOptions); - String output = yaml.dump(rootMap); - assertEquals("test:\n world: test\n", output); - } + public void testWithoutCustomStyle() throws Exception { + Map<String, Map<String, Object>> rootMap = new HashMap<String, Map<String, Object>>(); + Map<String, Object> enclosedMap = new HashMap<String, Object>(); + enclosedMap.put("world", "test"); + rootMap.put("test", enclosedMap); + Yaml yaml = new Yaml(); + String output = yaml.dump(rootMap); + assertEquals("test: {world: test}\n", output); + } + + public void testWithBlockStyle() throws Exception { + Map<String, Map<String, Object>> rootMap = new HashMap<String, Map<String, Object>>(); + Map<String, Object> enclosedMap = new HashMap<String, Object>(); + enclosedMap.put("world", "test"); + rootMap.put("test", enclosedMap); + + DumperOptions yamlOptions = new DumperOptions(); + yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + + Representer yamlRepresenter = new Representer(); + + Yaml yaml = new Yaml(yamlRepresenter, yamlOptions); + String output = yaml.dump(rootMap); + assertEquals("test:\n world: test\n", output); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue176/SingleQuoteTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue176/SingleQuoteTest.java index be5a0404..75297d07 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue176/SingleQuoteTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue176/SingleQuoteTest.java @@ -1,25 +1,21 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue176; import java.util.LinkedHashMap; import java.util.Map; - import junit.framework.TestCase; - import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions.FlowStyle; import org.yaml.snakeyaml.Yaml; @@ -28,36 +24,36 @@ import org.yaml.snakeyaml.representer.Representer; public class SingleQuoteTest extends TestCase { - public void testNoSingleQuoteForBlockStyle() throws Exception { - checkQuotes(true, "cows:\n steak:cow: '11'"); - } + public void testNoSingleQuoteForBlockStyle() throws Exception { + checkQuotes(true, "cows:\n steak:cow: '11'"); + } - public void testSingleQuoteForFlowStyle() throws Exception { - checkQuotes(false, "cows: {'steak:cow': '11'}"); - } + public void testSingleQuoteForFlowStyle() throws Exception { + checkQuotes(false, "cows: {'steak:cow': '11'}"); + } - private void checkQuotes(boolean isBlock, String expectation) { - DumperOptions options = new DumperOptions(); - options.setIndent(4); - if (isBlock) { - options.setDefaultFlowStyle(FlowStyle.BLOCK); - } - Representer representer = new Representer(); - - Yaml yaml = new Yaml(new SafeConstructor(), representer, options); - - LinkedHashMap<String, Object> lvl1 = new LinkedHashMap<String, Object>(); - lvl1.put("steak:cow", "11"); - LinkedHashMap<String, Object> root = new LinkedHashMap<String, Object>(); - root.put("cows", lvl1); - String output = yaml.dump(root); - assertEquals(expectation + "\n", output); - - // parse the value back - @SuppressWarnings("unchecked") - Map<String, Object> cows = (Map<String, Object>) yaml.load(output); - @SuppressWarnings("unchecked") - Map<String, String> cow = (Map<String, String>) cows.get("cows"); - assertEquals("11", cow.get("steak:cow")); + private void checkQuotes(boolean isBlock, String expectation) { + DumperOptions options = new DumperOptions(); + options.setIndent(4); + if (isBlock) { + options.setDefaultFlowStyle(FlowStyle.BLOCK); } + Representer representer = new Representer(); + + Yaml yaml = new Yaml(new SafeConstructor(), representer, options); + + LinkedHashMap<String, Object> lvl1 = new LinkedHashMap<String, Object>(); + lvl1.put("steak:cow", "11"); + LinkedHashMap<String, Object> root = new LinkedHashMap<String, Object>(); + root.put("cows", lvl1); + String output = yaml.dump(root); + assertEquals(expectation + "\n", output); + + // parse the value back + @SuppressWarnings("unchecked") + Map<String, Object> cows = yaml.load(output); + @SuppressWarnings("unchecked") + Map<String, String> cow = (Map<String, String>) cows.get("cows"); + assertEquals("11", cow.get("steak:cow")); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue177/PointBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue177/PointBean.java index ea46af97..a5c7e9c5 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue177/PointBean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue177/PointBean.java @@ -1,26 +1,25 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue177; public class PointBean { - public int x; - public int y; - @Override - public String toString() { - return "PointBean"; - } + public int x; + public int y; + + @Override + public String toString() { + return "PointBean"; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue177/PointBeanTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue177/PointBeanTest.java index 274cba3a..beff107d 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue177/PointBeanTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue177/PointBeanTest.java @@ -1,43 +1,38 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue177; import junit.framework.TestCase; - import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; public class PointBeanTest extends TestCase { - public void testNoSingleQuoteForBlockStyle() throws Exception { - String input = Util.getLocalResource("issues/issue177-1.yaml"); - try { - Yaml yaml = new Yaml(); - yaml.load(input); - fail(); - } catch (Exception e) { - assertEquals("Cannot create property=points for JavaBean=All Points\n" - + " in 'string', line 1, column 1:\n" - + " !!org.yaml.snakeyaml.issues.issu ... \n" + " ^\n" - + "Cannot create property=x for JavaBean=PointBean\n" - + " in 'string', line 7, column 5:\n" + " x: a\n" + " ^\n" - + "For input string: \"a\"\n" + " in 'string', line 7, column 8:\n" - + " x: a\n" + " ^\n" + "\n" - + " in 'string', line 3, column 3:\n" + " pt1:\n" + " ^\n", - e.getMessage()); - } + public void testNoSingleQuoteForBlockStyle() throws Exception { + String input = Util.getLocalResource("issues/issue177-1.yaml"); + try { + Yaml yaml = new Yaml(); + yaml.load(input); + fail(); + } catch (Exception e) { + assertEquals("Cannot create property=points for JavaBean=All Points\n" + + " in 'string', line 1, column 1:\n" + " !!org.yaml.snakeyaml.issues.issu ... \n" + + " ^\n" + "Cannot create property=x for JavaBean=PointBean\n" + + " in 'string', line 7, column 5:\n" + " x: a\n" + " ^\n" + + "For input string: \"a\"\n" + " in 'string', line 7, column 8:\n" + " x: a\n" + + " ^\n" + "\n" + " in 'string', line 3, column 3:\n" + " pt1:\n" + + " ^\n", e.getMessage()); } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue177/Points.java b/src/test/java/org/yaml/snakeyaml/issues/issue177/Points.java index e0e57c5a..6e6fa86b 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue177/Points.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue177/Points.java @@ -1,27 +1,26 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue177; import java.util.Map; public class Points { - public Map<String, PointBean> points; - @Override - public String toString() { - return "All Points"; - } + public Map<String, PointBean> points; + + @Override + public String toString() { + return "All Points"; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue182/InfinityFloatBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue182/InfinityFloatBean.java index 691959f4..6d72f2a7 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue182/InfinityFloatBean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue182/InfinityFloatBean.java @@ -1,21 +1,20 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue182; public class InfinityFloatBean { - public float infinityFloat; - public Float infinityFloatObject; + + public float infinityFloat; + public Float infinityFloatObject; } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue182/InfinityFloatBeanTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue182/InfinityFloatBeanTest.java index f589f03d..99c2a101 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue182/InfinityFloatBeanTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue182/InfinityFloatBeanTest.java @@ -1,36 +1,33 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue182; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class InfinityFloatBeanTest extends TestCase { - public void testInfinityFloatBean() throws Exception { + public void testInfinityFloatBean() throws Exception { - InfinityFloatBean bean = new InfinityFloatBean(); - bean.infinityFloat = Float.POSITIVE_INFINITY; - bean.infinityFloatObject = Float.POSITIVE_INFINITY; + InfinityFloatBean bean = new InfinityFloatBean(); + bean.infinityFloat = Float.POSITIVE_INFINITY; + bean.infinityFloatObject = Float.POSITIVE_INFINITY; - Yaml yaml = new Yaml(); - String yamled = yaml.dump(bean); - InfinityFloatBean loadedBean = yaml.loadAs(yamled, InfinityFloatBean.class); - assertEquals(Float.POSITIVE_INFINITY, loadedBean.infinityFloat); - assertEquals(Float.POSITIVE_INFINITY, loadedBean.infinityFloatObject); - } + Yaml yaml = new Yaml(); + String yamled = yaml.dump(bean); + InfinityFloatBean loadedBean = yaml.loadAs(yamled, InfinityFloatBean.class); + assertEquals(Float.POSITIVE_INFINITY, loadedBean.infinityFloat); + assertEquals(Float.POSITIVE_INFINITY, loadedBean.infinityFloatObject); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue183/NumberBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue183/NumberBean.java index 0d498468..5a093f70 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue183/NumberBean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue183/NumberBean.java @@ -1,20 +1,19 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue183; public class NumberBean { - public Number number; + + public Number number; } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue183/NumberBeanTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue183/NumberBeanTest.java index 8200a013..7a6a1444 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue183/NumberBeanTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue183/NumberBeanTest.java @@ -1,95 +1,104 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue183; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class NumberBeanTest extends TestCase { - public void testNumberBean() throws Exception { - - NumberBean number = new NumberBean(); - number.number = 1; - - Yaml yaml = new Yaml(); - String dump = yaml.dump(number); - NumberBean loaded = yaml.loadAs(dump, NumberBean.class); - assertEquals(new Long(1), loaded.number); - } - - public void testNumberAsFloatInfinity() throws Exception { - NumberBean number = new NumberBean(); - number.number = Float.POSITIVE_INFINITY; - - Yaml yaml = new Yaml(); - String dump = yaml.dump(number); - NumberBean loaded = yaml.loadAs(dump, NumberBean.class); - assertEquals(Float.POSITIVE_INFINITY, loaded.number.floatValue()); - } - - public void testNumberAsDoubleInfinity() throws Exception { - NumberBean number = new NumberBean(); - number.number = Double.POSITIVE_INFINITY; - - Yaml yaml = new Yaml(); - String dump = yaml.dump(number); - NumberBean loaded = yaml.loadAs(dump, NumberBean.class); - assertEquals(Double.POSITIVE_INFINITY, loaded.number.doubleValue()); - } - - public void testNumberAsNegativeFloatInfinity() throws Exception { - NumberBean number = new NumberBean(); - number.number = Float.NEGATIVE_INFINITY; - - Yaml yaml = new Yaml(); - String dump = yaml.dump(number); - NumberBean loaded = yaml.loadAs(dump, NumberBean.class); - assertEquals(Float.NEGATIVE_INFINITY, loaded.number.floatValue()); - } - - public void testNumberAsNegativeDoubleInfinity() throws Exception { - NumberBean number = new NumberBean(); - number.number = Double.NEGATIVE_INFINITY; - - Yaml yaml = new Yaml(); - String dump = yaml.dump(number); - NumberBean loaded = yaml.loadAs(dump, NumberBean.class); - assertEquals(Double.NEGATIVE_INFINITY, loaded.number.doubleValue()); - } - - public void testNumberAsFloatNaN() throws Exception { - NumberBean number = new NumberBean(); - number.number = Float.NaN; - - Yaml yaml = new Yaml(); - String dump = yaml.dump(number); - NumberBean loaded = yaml.loadAs(dump, NumberBean.class); - assertEquals(Float.NaN, loaded.number.floatValue()); - } - - public void testNumberAsDoubleNaN() throws Exception { - NumberBean number = new NumberBean(); - number.number = Double.NaN; - - Yaml yaml = new Yaml(); - String dump = yaml.dump(number); - NumberBean loaded = yaml.loadAs(dump, NumberBean.class); - assertEquals(Double.NaN, loaded.number.doubleValue()); - } + public void testNumberAsInteger() throws Exception { + + NumberBean number = new NumberBean(); + number.number = 1; + assertEquals(Integer.class, number.number.getClass()); + + Yaml yaml = new Yaml(); + String dump = yaml.dump(number); + NumberBean loaded = yaml.loadAs(dump, NumberBean.class); + assertEquals("Integer is converted to Double.", Double.valueOf(1), loaded.number); + } + + public void testNumberAsDouble() throws Exception { + + NumberBean number = new NumberBean(); + number.number = 1.0; + + Yaml yaml = new Yaml(); + String dump = yaml.dump(number); + NumberBean loaded = yaml.loadAs(dump, NumberBean.class); + assertEquals(number.number, loaded.number); + } + + public void testNumberAsFloatInfinity() throws Exception { + NumberBean number = new NumberBean(); + number.number = Float.POSITIVE_INFINITY; + + Yaml yaml = new Yaml(); + String dump = yaml.dump(number); + NumberBean loaded = yaml.loadAs(dump, NumberBean.class); + assertEquals(Float.POSITIVE_INFINITY, loaded.number.floatValue()); + } + + public void testNumberAsDoubleInfinity() throws Exception { + NumberBean number = new NumberBean(); + number.number = Double.POSITIVE_INFINITY; + + Yaml yaml = new Yaml(); + String dump = yaml.dump(number); + NumberBean loaded = yaml.loadAs(dump, NumberBean.class); + assertEquals(Double.POSITIVE_INFINITY, loaded.number.doubleValue()); + } + + public void testNumberAsNegativeFloatInfinity() throws Exception { + NumberBean number = new NumberBean(); + number.number = Float.NEGATIVE_INFINITY; + + Yaml yaml = new Yaml(); + String dump = yaml.dump(number); + NumberBean loaded = yaml.loadAs(dump, NumberBean.class); + assertEquals(Float.NEGATIVE_INFINITY, loaded.number.floatValue()); + } + + public void testNumberAsNegativeDoubleInfinity() throws Exception { + NumberBean number = new NumberBean(); + number.number = Double.NEGATIVE_INFINITY; + + Yaml yaml = new Yaml(); + String dump = yaml.dump(number); + NumberBean loaded = yaml.loadAs(dump, NumberBean.class); + assertEquals(Double.NEGATIVE_INFINITY, loaded.number.doubleValue()); + } + + public void testNumberAsFloatNaN() throws Exception { + NumberBean number = new NumberBean(); + number.number = Float.NaN; + + Yaml yaml = new Yaml(); + String dump = yaml.dump(number); + NumberBean loaded = yaml.loadAs(dump, NumberBean.class); + assertEquals(Float.NaN, loaded.number.floatValue()); + } + + public void testNumberAsDoubleNaN() throws Exception { + NumberBean number = new NumberBean(); + number.number = Double.NaN; + + Yaml yaml = new Yaml(); + String dump = yaml.dump(number); + NumberBean loaded = yaml.loadAs(dump, NumberBean.class); + assertEquals(Double.NaN, loaded.number.doubleValue()); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue188/ErrorMessageTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue188/ErrorMessageTest.java index fa17b680..39a941d4 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue188/ErrorMessageTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue188/ErrorMessageTest.java @@ -1,35 +1,32 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue188; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class ErrorMessageTest extends TestCase { - public void testErrorMessage() throws Exception { + public void testErrorMessage() throws Exception { - try { - Yaml yaml = new Yaml(); - String doc = "templates:\n" + " master:\n" + " type: Compute : invalid123"; - yaml.load(doc); - fail(); - } catch (Exception e) { - assertFalse(e.getMessage(), e.getMessage().contains("null")); - } + try { + Yaml yaml = new Yaml(); + String doc = "templates:\n" + " master:\n" + " type: Compute : invalid123"; + yaml.load(doc); + fail(); + } catch (Exception e) { + assertFalse(e.getMessage(), e.getMessage().contains("null")); } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue192/EqualsSignTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue192/EqualsSignTest.java index f48f7d14..a012d5a3 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue192/EqualsSignTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue192/EqualsSignTest.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue192; @@ -20,11 +18,11 @@ import org.yaml.snakeyaml.Yaml; public class EqualsSignTest extends TestCase { - public void testMappingNode() { - new Yaml().load("part1: ="); - } + public void testMappingNode() { + new Yaml().load("part1: ="); + } - public void testScalarNode() { - new Yaml().load("="); - } + public void testScalarNode() { + new Yaml().load("="); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue193/AbstractBeanTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue193/AbstractBeanTest.java index 8476a6a7..074b0076 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue193/AbstractBeanTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue193/AbstractBeanTest.java @@ -1,25 +1,20 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue193; import junit.framework.TestCase; - -import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.constructor.BaseConstructor; import org.yaml.snakeyaml.constructor.Constructor; import org.yaml.snakeyaml.introspector.BeanAccess; import org.yaml.snakeyaml.introspector.PropertyUtils; @@ -27,26 +22,26 @@ import org.yaml.snakeyaml.representer.Representer; public class AbstractBeanTest extends TestCase { - public void testErrorMessage() throws Exception { + public void testErrorMessage() throws Exception { - BeanA1 b = new BeanA1(); - b.setId(2l); - b.setName("name1"); + BeanA1 b = new BeanA1(); + b.setId(2L); + b.setName("name1"); - Constructor c = new Constructor(); - Representer r = new Representer(); + Constructor c = new Constructor(); + Representer r = new Representer(); - PropertyUtils pu = new PropertyUtils(); - c.setPropertyUtils(pu); - r.setPropertyUtils(pu); + PropertyUtils pu = new PropertyUtils(); + c.setPropertyUtils(pu); + r.setPropertyUtils(pu); - pu.getProperties(BeanA1.class, BeanAccess.FIELD); + pu.getProperties(BeanA1.class, BeanAccess.FIELD); - Yaml yaml = new Yaml(c, r); - // yaml.setBeanAccess(BeanAccess.FIELD); - String dump = yaml.dump(b); - BeanA1 b2 = (BeanA1) yaml.load(dump); - assertEquals(b.getId(), b2.getId()); - assertEquals(b.getName(), b2.getName()); - } + Yaml yaml = new Yaml(c, r); + // yaml.setBeanAccess(BeanAccess.FIELD); + String dump = yaml.dump(b); + BeanA1 b2 = yaml.load(dump); + assertEquals(b.getId(), b2.getId()); + assertEquals(b.getName(), b2.getName()); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue193/BeanA.java b/src/test/java/org/yaml/snakeyaml/issues/issue193/BeanA.java index e2ab61b9..a8f54009 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue193/BeanA.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue193/BeanA.java @@ -1,22 +1,20 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue193; public abstract class BeanA { - public abstract Object getId(); + public abstract Object getId(); } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue193/BeanA1.java b/src/test/java/org/yaml/snakeyaml/issues/issue193/BeanA1.java index f7b7719f..15323d05 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue193/BeanA1.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue193/BeanA1.java @@ -1,40 +1,38 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue193; public class BeanA1 extends BeanA { - private Long id; + private Long id; - public Long getId() { - return id; - } + public Long getId() { + return id; + } - public void setId(Long id) { - this.id = id; - } + public void setId(Long id) { + this.id = id; + } - private String name; + private String name; - public String getName() { - return name; - } + public String getName() { + return name; + } - public void setName(String name) { - this.name = name; - } + public void setName(String name) { + this.name = name; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue193/TestYaml.java b/src/test/java/org/yaml/snakeyaml/issues/issue193/TestYaml.java index 662c74bd..7b1c9cee 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue193/TestYaml.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue193/TestYaml.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue193; @@ -19,50 +17,47 @@ import org.yaml.snakeyaml.Yaml; public class TestYaml { - public static abstract class BeanA { + public static abstract class BeanA { - public abstract Object getId(); + public abstract Object getId(); - } - - public static class BeanA1 extends BeanA { + } - private Long id; + public static class BeanA1 extends BeanA { - // @Override - public Long getId() { - return id; - } + private Long id; - public void setId(Long id) { - this.id = id; - } + // @Override + public Long getId() { + return id; + } - private String name; + public void setId(Long id) { + this.id = id; + } - public String getName() { - return name; - } + private String name; - public void setName(String name) { - this.name = name; - } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; } - public static void main(String[] args) { + } - System.out.println("test..."); - BeanA1 b = new BeanA1(); - b.setId(2l); - b.setName("name1"); - Yaml yaml = new Yaml(); - String dump = yaml.dump(b); + public static void main(String[] args) { - System.out.println("dump:" + dump); + BeanA1 b = new BeanA1(); + b.setId(2L); + b.setName("name1"); + Yaml yaml = new Yaml(); + String dump = yaml.dump(b); - dump = "!!org.yaml.snakeyaml.issues.issue193.TestYaml$BeanA1 {id: 2, name: name1}"; + dump = "!!org.yaml.snakeyaml.issues.issue193.TestYaml$BeanA1 {id: 2, name: name1}"; - yaml.load(dump); - } + yaml.load(dump); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue203/ContentIdentifier.java b/src/test/java/org/yaml/snakeyaml/issues/issue203/ContentIdentifier.java index 0557412e..99190bc8 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue203/ContentIdentifier.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue203/ContentIdentifier.java @@ -1,19 +1,19 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue203; -public interface ContentIdentifier extends Identifiable<Long> {}; +public interface ContentIdentifier extends Identifiable<Long> { + +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue203/ContentIdentifierImpl.java b/src/test/java/org/yaml/snakeyaml/issues/issue203/ContentIdentifierImpl.java index b4c10f8c..d9419d21 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue203/ContentIdentifierImpl.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue203/ContentIdentifierImpl.java @@ -1,29 +1,27 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue203; -public class ContentIdentifierImpl implements ContentIdentifier { +public class ContentIdentifierImpl implements ContentIdentifier { - private Long id; + private final Long id; - public ContentIdentifierImpl(Long id) { - this.id = id; - } + public ContentIdentifierImpl(Long id) { + this.id = id; + } - public Long getId() { - return id; - } -}
\ No newline at end of file + public Long getId() { + return id; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue203/DataBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue203/DataBean.java index dc96d04c..68d319a7 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue203/DataBean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue203/DataBean.java @@ -1,39 +1,38 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue203; public class DataBean { - private String id; - private ContentIdentifier content; - public String getId() { - return id; - } + private String id; + private ContentIdentifier content; - public void setId(String id) { - this.id = id; - } + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } - public ContentIdentifier getContent() { - return content; - } - public void setContent(ContentIdentifier content) { - this.content = content; - } + public ContentIdentifier getContent() { + return content; + } + + public void setContent(ContentIdentifier content) { + this.content = content; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue203/GenericTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue203/GenericTest.java index 165fbe6b..ec2924b6 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue203/GenericTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue203/GenericTest.java @@ -1,32 +1,28 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue203; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class GenericTest extends TestCase { - public void testGenericInterface() { - Yaml yaml = new Yaml(); - String uuu = "!!org.yaml.snakeyaml.issues.issue203.DataBean\n" - + "content: !!org.yaml.snakeyaml.issues.issue203.ContentIdentifierImpl 33\n" - + "id: 555"; - DataBean obj = (DataBean) yaml.load(uuu); - assertEquals(33, obj.getContent().getId().intValue()); - } + public void testGenericInterface() { + Yaml yaml = new Yaml(); + String uuu = "!!org.yaml.snakeyaml.issues.issue203.DataBean\n" + + "content: !!org.yaml.snakeyaml.issues.issue203.ContentIdentifierImpl 33\n" + "id: 555"; + DataBean obj = yaml.load(uuu); + assertEquals(33, obj.getContent().getId().intValue()); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue203/Identifiable.java b/src/test/java/org/yaml/snakeyaml/issues/issue203/Identifiable.java index dbe1ffe2..6426925f 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue203/Identifiable.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue203/Identifiable.java @@ -1,22 +1,21 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue203; import java.io.Serializable; public interface Identifiable<ID extends Serializable> { - public ID getId(); -}
\ No newline at end of file + + ID getId(); +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue205/AppleSmileTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue205/AppleSmileTest.java index f17556ec..7141113c 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue205/AppleSmileTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue205/AppleSmileTest.java @@ -1,36 +1,33 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue205; +import java.io.InputStream; +import java.util.Map; import junit.framework.TestCase; import org.yaml.snakeyaml.Yaml; -import java.io.InputStream; - public class AppleSmileTest extends TestCase { - public void testEmoji() { - //http://support.apple.com/en-us/ht4976 - InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("issues/ios_emoji_surrogate.yaml"); - Yaml yaml = new Yaml(); - try { - yaml.load(input); - fail("Surrogate characters must not be accepted."); - } catch (Exception e) { - assertEquals("special characters are not allowed", e.getMessage()); - } - } + public void testEmoji() { + // http://support.apple.com/en-us/ht4976 + InputStream input = Thread.currentThread().getContextClassLoader() + .getResourceAsStream("issues/ios_emoji_surrogate.yaml"); + Yaml yaml = new Yaml(); + Map<String, String> map = yaml.load(input); + String ios_emoji = map.get("text"); + assertEquals("😷😊", ios_emoji); + System.out.println(ios_emoji); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue207/OctalNumberTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue207/OctalNumberTest.java index 82dd5d50..b3962708 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue207/OctalNumberTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue207/OctalNumberTest.java @@ -1,41 +1,38 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue207; -import junit.framework.TestCase; -import org.yaml.snakeyaml.Yaml; +import static org.junit.Assert.assertEquals; -import java.io.InputStream; import java.util.Map; +import org.junit.Test; +import org.yaml.snakeyaml.Yaml; -public class OctalNumberTest extends TestCase { +public class OctalNumberTest { - public void testOctalNumbersMoreThenSeven() { - Yaml yaml = new Yaml(); - assertEquals(7, yaml.load("07")); - assertEquals(63, yaml.load("077")); - assertEquals(0, yaml.load("0")); - assertEquals("0A", yaml.load("0A")); - assertEquals("09", yaml.load("!!str 09")); + @Test + public void testOctalNumbersMoreThenSeven() { + Yaml yaml = new Yaml(); + assertEquals(Integer.valueOf(7), yaml.load("07")); + assertEquals(Integer.valueOf(63), yaml.load("077")); + assertEquals(Integer.valueOf(0), yaml.load("0")); + assertEquals("0A", yaml.load("0A")); + assertEquals("09", yaml.load("!!str 09")); - /* TODO fix 207 and 130 - assertEquals("08", yaml.load("08")); - assertEquals("09", yaml.load("09")); - Map<String, String> parsed9 = (Map<String, String>) yaml.load("a: 09"); - assertEquals("09", parsed9.get("a")); - */ - } + assertEquals("08", yaml.load("08")); + assertEquals("09", yaml.load("09")); + Map<String, String> parsed9 = yaml.load("a: 09"); + assertEquals("09", parsed9.get("a")); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue24/LineNumberTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue24/LineNumberTest.java index 3ea8ead0..222cce67 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue24/LineNumberTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue24/LineNumberTest.java @@ -1,22 +1,19 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue24; import junit.framework.TestCase; - import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; @@ -24,16 +21,17 @@ import org.yaml.snakeyaml.Yaml; * to test http://code.google.com/p/snakeyaml/issues/detail?id=24 */ public class LineNumberTest extends TestCase { - public void test1() { - String resource = Util.getLocalResource("issues/issue24-1.yaml"); - // System.out.println(resource); - Yaml yaml = new Yaml(); - try { - yaml.load(resource); - fail(); - } catch (Exception e) { - assertTrue(e.toString(), e.toString().contains("line 3")); - assertTrue(e.toString(), e.toString().contains("column 12")); - } + + public void test1() { + String resource = Util.getLocalResource("issues/issue24-1.yaml"); + // System.out.println(resource); + Yaml yaml = new Yaml(); + try { + yaml.load(resource); + fail(); + } catch (Exception e) { + assertTrue(e.toString(), e.toString().contains("line 3")); + assertTrue(e.toString(), e.toString().contains("column 12")); } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue29/BigJavaBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue29/BigJavaBean.java index 5141662e..cb20be40 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue29/BigJavaBean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue29/BigJavaBean.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue29; @@ -19,109 +17,120 @@ import java.util.List; import java.util.Map; public class BigJavaBean { - private String name; - private String address; - private String description; - private int id; - private List<Integer> numbers; - private Map<String, String> data; - - public BigJavaBean() { - } - public BigJavaBean(int id, String name, String address, String description) { - super(); - this.name = name; - this.address = address; - this.description = description; - this.id = id; - } + private String name; + private String address; + private String description; + private int id; + private List<Integer> numbers; + private Map<String, String> data; - public String getName() { - return name; - } + public BigJavaBean() {} - public void setName(String name) { - this.name = name; - } + public BigJavaBean(int id, String name, String address, String description) { + super(); + this.name = name; + this.address = address; + this.description = description; + this.id = id; + } - public String getAddress() { - return address; - } + public String getName() { + return name; + } - public String getDescription() { - return description; - } + public void setName(String name) { + this.name = name; + } - public int getId() { - return id; - } + public String getAddress() { + return address; + } - public List<Integer> getNumbers() { - return numbers; - } + public String getDescription() { + return description; + } - public void setNumbers(List<Integer> numbers) { - this.numbers = numbers; - } + public int getId() { + return id; + } - public Map<String, String> getData() { - return data; - } + public List<Integer> getNumbers() { + return numbers; + } - public void setData(Map<String, String> data) { - this.data = data; - } + public void setNumbers(List<Integer> numbers) { + this.numbers = numbers; + } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((address == null) ? 0 : address.hashCode()); - result = prime * result + ((data == null) ? 0 : data.hashCode()); - result = prime * result + ((description == null) ? 0 : description.hashCode()); - result = prime * result + id; - result = prime * result + ((name == null) ? 0 : name.hashCode()); - result = prime * result + ((numbers == null) ? 0 : numbers.hashCode()); - return result; - } + public Map<String, String> getData() { + return data; + } + + public void setData(Map<String, String> data) { + this.data = data; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((address == null) ? 0 : address.hashCode()); + result = prime * result + ((data == null) ? 0 : data.hashCode()); + result = prime * result + ((description == null) ? 0 : description.hashCode()); + result = prime * result + id; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((numbers == null) ? 0 : numbers.hashCode()); + return result; + } - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - BigJavaBean other = (BigJavaBean) obj; - if (address == null) { - if (other.address != null) - return false; - } else if (!address.equals(other.address)) - return false; - if (data == null) { - if (other.data != null) - return false; - } else if (!data.equals(other.data)) - return false; - if (description == null) { - if (other.description != null) - return false; - } else if (!description.equals(other.description)) - return false; - if (id != other.id) - return false; - if (name == null) { - if (other.name != null) - return false; - } else if (!name.equals(other.name)) - return false; - if (numbers == null) { - if (other.numbers != null) - return false; - } else if (!numbers.equals(other.numbers)) - return false; - return true; + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + BigJavaBean other = (BigJavaBean) obj; + if (address == null) { + if (other.address != null) { + return false; + } + } else if (!address.equals(other.address)) { + return false; + } + if (data == null) { + if (other.data != null) { + return false; + } + } else if (!data.equals(other.data)) { + return false; + } + if (description == null) { + if (other.description != null) { + return false; + } + } else if (!description.equals(other.description)) { + return false; + } + if (id != other.id) { + return false; + } + if (name == null) { + if (other.name != null) { + return false; + } + } else if (!name.equals(other.name)) { + return false; + } + if (numbers == null) { + return other.numbers == null; + } else { + return numbers.equals(other.numbers); } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue29/FlexibleScalarStyleTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue29/FlexibleScalarStyleTest.java index 384b83d1..f5f1d236 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue29/FlexibleScalarStyleTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue29/FlexibleScalarStyleTest.java @@ -1,25 +1,21 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue29; import java.util.LinkedHashMap; import java.util.Map; - import junit.framework.TestCase; - import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions.ScalarStyle; import org.yaml.snakeyaml.Util; @@ -32,85 +28,87 @@ import org.yaml.snakeyaml.representer.Representer; * to test http://code.google.com/p/snakeyaml/issues/detail?id=29 */ public class FlexibleScalarStyleTest extends TestCase { - public void testLong() { - DumperOptions options = new DumperOptions(); - options.setDefaultScalarStyle(ScalarStyle.FOLDED); - Yaml yaml = new Yaml(options); - String result = yaml - .dump("qqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq " - + "qqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq " - + "qqqqqqqqqqqqqqqqqqqqqqqqq 111111111111111111111111\n " - + "qqqqqqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqqqq\n"); - // System.out.println(result); - assertTrue(result.startsWith(">\n")); - assertEquals( - ">\n qqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq\n qqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqq 111111111111111111111111\n qqqqqqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqqqq\n", - result); - } - public void testNoFoldedScalar() { - DumperOptions options = new DumperOptions(); - options.setWidth(30); - Yaml yaml = new Yaml(options); - String output = yaml.dump(getData()); - // System.out.println(output); - String etalon = Util.getLocalResource("representer/scalar-style1.yaml"); - assertEquals(etalon, output); - } + public void testLong() { + DumperOptions options = new DumperOptions(); + options.setDefaultScalarStyle(ScalarStyle.FOLDED); + Yaml yaml = new Yaml(options); + String result = + yaml.dump("qqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq " + + "qqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq " + + "qqqqqqqqqqqqqqqqqqqqqqqqq 111111111111111111111111\n " + + "qqqqqqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqqqq\n"); + // System.out.println(result); + assertTrue(result.startsWith(">\n")); + assertEquals( + ">\n qqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq\n qqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqq 111111111111111111111111\n qqqqqqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqqqq\n", + result); + } - public void testCustomScalarStyle() { - DumperOptions options = new DumperOptions(); - options.setWidth(30); - Yaml yaml = new Yaml(new MyRepresenter(), options); - String output = yaml.dump(getData()); - // System.out.println(output); - String etalon = Util.getLocalResource("representer/scalar-style2.yaml"); - assertEquals(etalon, output); - } + public void testDefaultScalarStyle() { + DumperOptions options = new DumperOptions(); + options.setWidth(30); + Yaml yaml = new Yaml(options); + String output = yaml.dump(getData()); + // System.out.println(output); + String etalon = Util.getLocalResource("representer/scalar-style1.yaml"); + assertEquals(etalon, output); + } - public void testCustomScalarStyleNoSplitLines() { - DumperOptions options = new DumperOptions(); - options.setWidth(30); - options.setSplitLines(false); - Yaml yaml = new Yaml(new MyRepresenter(), options); - String output = yaml.dump(getData()); - // System.out.println(output); - String etalon = Util.getLocalResource("representer/scalar-style3.yaml"); - assertEquals(etalon, output); - } + public void testCustomScalarStyle() { + DumperOptions options = new DumperOptions(); + options.setWidth(30); + Yaml yaml = new Yaml(new MyRepresenter(), options); + String output = yaml.dump(getData()); + // System.out.println(output); + String etalon = Util.getLocalResource("representer/scalar-style2.yaml"); + assertEquals(etalon, output); + } - private Map<String, String> getData() { - Map<String, String> map = new LinkedHashMap<String, String>(); - map.put("name", "Steve Jobs"); - map.put("address", "Name\nStreet Number\nCountry"); - map.put("description", - "1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000"); - return map; - } + public void testCustomScalarStyleNoSplitLines() { + DumperOptions options = new DumperOptions(); + options.setWidth(30); + options.setSplitLines(false); + Yaml yaml = new Yaml(new MyRepresenter(), options); + String output = yaml.dump(getData()); + // System.out.println(output); + String etalon = Util.getLocalResource("representer/scalar-style3.yaml"); + assertEquals(etalon, output); + } - private class MyRepresenter extends Representer { + private Map<String, String> getData() { + Map<String, String> map = new LinkedHashMap<String, String>(); + map.put("name", "Steve Jobs"); + map.put("address", "Name\nStreet Number\nCountry"); + map.put("description", + "1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000"); + return map; + } - public MyRepresenter() { - super(); - this.representers.put(String.class, new FlexibleRepresent()); - } + private class MyRepresenter extends Representer { + + public MyRepresenter() { + super(); + this.representers.put(String.class, new FlexibleRepresent()); + } + + private class FlexibleRepresent extends RepresentString { - private class FlexibleRepresent extends RepresentString { - public Node representData(Object data) { - ScalarNode node = (ScalarNode) super.representData(data); - if (node.getStyle() == null) { - // if Plain scalar style - if (node.getValue().length() < 25) { - return node; - } else { - // Folded scalar style - return new ScalarNode(node.getTag(), node.getValue(), node.getStartMark(), - node.getEndMark(), '>'); - } - } else { - return node; - } - } + public Node representData(Object data) { + ScalarNode node = (ScalarNode) super.representData(data); + if (node.isPlain()) { + // if Plain scalar style + if (node.getValue().length() < 25) { + return node; + } else { + // Folded scalar style + return new ScalarNode(node.getTag(), node.getValue(), node.getStartMark(), + node.getEndMark(), ScalarStyle.FOLDED); + } + } else { + return node; } + } } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue29/FlexibleScalarStylesInJavaBeanTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue29/FlexibleScalarStylesInJavaBeanTest.java index afddbc8e..06d0b665 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue29/FlexibleScalarStylesInJavaBeanTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue29/FlexibleScalarStylesInJavaBeanTest.java @@ -1,21 +1,18 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue29; -import java.beans.IntrospectionException; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; @@ -24,9 +21,8 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; - import junit.framework.TestCase; - +import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.introspector.BeanAccess; import org.yaml.snakeyaml.introspector.Property; @@ -37,111 +33,112 @@ import org.yaml.snakeyaml.nodes.Tag; import org.yaml.snakeyaml.representer.Representer; public class FlexibleScalarStylesInJavaBeanTest extends TestCase { - public void testDifferentStyles() { - BigJavaBean bean1 = new BigJavaBean(1, "simple", "line 1\nline2\nzipcode", "short text1"); - List<Integer> numbers1 = new ArrayList<Integer>(Arrays.asList(1, 2, 3)); - bean1.setNumbers(numbers1); - Map<String, String> data1 = new HashMap<String, String>(); - data1.put("key1", "value1"); - data1.put("key2", "value2"); - bean1.setData(data1); - // - BigJavaBean bean2 = new BigJavaBean(1, "second", "line 111\nline 222\nzipcode 12345\n\n", - "info: semicolon is used"); - List<Integer> numbers2 = new ArrayList<Integer>(Arrays.asList(4, 5, 6, 777, 888, 999, 1000)); - bean2.setNumbers(numbers2); - Map<String, String> data2 = new HashMap<String, String>(); - data2.put("key21", "value12"); - data2.put("key22", "value with\ntwo lines"); - bean2.setData(data2); - // - List<BigJavaBean> list = new ArrayList<BigJavaBean>(); - list.add(bean1); - list.add(bean2); - Yaml yaml = new Yaml(new MyRepresenter()); - yaml.setBeanAccess(BeanAccess.FIELD); - String output = yaml.dump(list); - // System.out.println(output); - // parse back - @SuppressWarnings("unchecked") - List<BigJavaBean> parsed = (List<BigJavaBean>) yaml.load(output); - assertEquals(2, parsed.size()); - assertEquals(bean1, parsed.get(0)); - assertEquals(bean2, parsed.get(1)); + public void testDifferentStyles() { + BigJavaBean bean1 = new BigJavaBean(1, "simple", "line 1\nline2\nzipcode", "short text1"); + List<Integer> numbers1 = new ArrayList<Integer>(Arrays.asList(1, 2, 3)); + bean1.setNumbers(numbers1); + Map<String, String> data1 = new HashMap<String, String>(); + data1.put("key1", "value1"); + data1.put("key2", "value2"); + bean1.setData(data1); + // + BigJavaBean bean2 = new BigJavaBean(1, "second", "line 111\nline 222\nzipcode 12345\n\n", + "info: semicolon is used"); + List<Integer> numbers2 = new ArrayList<Integer>(Arrays.asList(4, 5, 6, 777, 888, 999, 1000)); + bean2.setNumbers(numbers2); + Map<String, String> data2 = new HashMap<String, String>(); + data2.put("key21", "value12"); + data2.put("key22", "value with\ntwo lines"); + bean2.setData(data2); + // + List<BigJavaBean> list = new ArrayList<BigJavaBean>(); + list.add(bean1); + list.add(bean2); + Yaml yaml = new Yaml(new MyRepresenter()); + yaml.setBeanAccess(BeanAccess.FIELD); + String output = yaml.dump(list); + // System.out.println(output); + // parse back + @SuppressWarnings("unchecked") + List<BigJavaBean> parsed = yaml.load(output); + assertEquals(2, parsed.size()); + assertEquals(bean1, parsed.get(0)); + assertEquals(bean2, parsed.get(1)); + + } + + private class MyRepresenter extends Representer { + + /* + * Change the default order. Important data goes first. + */ + @Override + protected Set<Property> getProperties(Class<? extends Object> type) { + if (type.isAssignableFrom(BigJavaBean.class)) { + Set<Property> standard = super.getProperties(type); + Set<Property> sorted = new TreeSet<Property>(new PropertyComparator()); + sorted.addAll(standard); + return sorted; + } else { + return super.getProperties(type); + } } - private class MyRepresenter extends Representer { - /* - * Change the default order. Important data goes first. - */ - @Override - protected Set<Property> getProperties(Class<? extends Object> type) - throws IntrospectionException { - if (type.isAssignableFrom(BigJavaBean.class)) { - Set<Property> standard = super.getProperties(type); - Set<Property> sorted = new TreeSet<Property>(new PropertyComparator()); - sorted.addAll(standard); - return sorted; - } else { - return super.getProperties(type); - } - } + private class PropertyComparator implements Comparator<Property> { - private class PropertyComparator implements Comparator<Property> { - public int compare(Property o1, Property o2) { - // important go first - List<String> order = new ArrayList<String>(Arrays.asList("id", "name", - "description", "address")); - for (String name : order) { - int c = compareByName(o1, o2, name); - if (c != 0) { - return c; - } - } - // all the rest - return o1.compareTo(o2); - } + public int compare(Property o1, Property o2) { + // important go first + List<String> order = + new ArrayList<String>(Arrays.asList("id", "name", "description", "address")); + for (String name : order) { + int c = compareByName(o1, o2, name); + if (c != 0) { + return c; + } + } + // all the rest + return o1.compareTo(o2); + } - private int compareByName(Property o1, Property o2, String name) { - if (o1.getName().equals(name)) { - return -1; - } else if (o2.getName().equals(name)) { - return 1; - } - return 0;// compare further - } + private int compareByName(Property o1, Property o2, String name) { + if (o1.getName().equals(name)) { + return -1; + } else if (o2.getName().equals(name)) { + return 1; } + return 0;// compare further + } + } - @Override - protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, - Object propertyValue, Tag customTag) { - if (javaBean instanceof BigJavaBean) { - BigJavaBean bean = (BigJavaBean) javaBean; - NodeTuple standard = super.representJavaBeanProperty(javaBean, property, - propertyValue, customTag); - if (property.getName().equals("numbers")) { - // when the list is small, make it block collection style - if (bean.getNumbers().size() < 5) { - SequenceNode n = (SequenceNode) standard.getValueNode(); - return new NodeTuple(standard.getKeyNode(), new SequenceNode(n.getTag(), - true, n.getValue(), n.getStartMark(), n.getEndMark(), false)); - } - } - if (property.getName().equals("description")) { - // if description contains ':' use folded scalar style and - // not single quoted scalar style - if (bean.getDescription().indexOf(':') > 0) { - ScalarNode n = (ScalarNode) standard.getValueNode(); - return new NodeTuple(standard.getKeyNode(), new ScalarNode(n.getTag(), - n.getValue(), n.getStartMark(), n.getEndMark(), '>')); - } - } - return standard; - } else { - return super - .representJavaBeanProperty(javaBean, property, propertyValue, customTag); - } + @Override + protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, + Object propertyValue, Tag customTag) { + if (javaBean instanceof BigJavaBean) { + BigJavaBean bean = (BigJavaBean) javaBean; + NodeTuple standard = + super.representJavaBeanProperty(javaBean, property, propertyValue, customTag); + if (property.getName().equals("numbers")) { + // when the list is small, make it block collection style + if (bean.getNumbers().size() < 5) { + SequenceNode n = (SequenceNode) standard.getValueNode(); + return new NodeTuple(standard.getKeyNode(), new SequenceNode(n.getTag(), true, + n.getValue(), n.getStartMark(), n.getEndMark(), DumperOptions.FlowStyle.BLOCK)); + } + } + if (property.getName().equals("description")) { + // if description contains ':' use folded scalar style and + // not single quoted scalar style + if (bean.getDescription().indexOf(':') > 0) { + ScalarNode n = (ScalarNode) standard.getValueNode(); + return new NodeTuple(standard.getKeyNode(), new ScalarNode(n.getTag(), n.getValue(), + n.getStartMark(), n.getEndMark(), DumperOptions.ScalarStyle.FOLDED)); + } } + return standard; + } else { + return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag); + } } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue306/BeanWithId.java b/src/test/java/org/yaml/snakeyaml/issues/issue306/BeanWithId.java index 40f849e3..84221a98 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue306/BeanWithId.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue306/BeanWithId.java @@ -1,39 +1,38 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue306; import java.util.UUID; public class BeanWithId { - private int value; - private UUID id; - public int getValue() { - return value; - } + private int value; + private UUID id; - public void setValue(int value) { - this.value = value; - } + public int getValue() { + return value; + } - public UUID getId() { - return id; - } + public void setValue(int value) { + this.value = value; + } - public void setId(UUID id) { - this.id = id; - } + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue306/UuidSupportTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue306/UuidSupportTest.java index 36228688..bef675f1 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue306/UuidSupportTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue306/UuidSupportTest.java @@ -1,88 +1,85 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue306; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.UUID; +import java.util.regex.Pattern; import org.junit.Test; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.nodes.Tag; -import java.util.UUID; -import java.util.regex.Pattern; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - public class UuidSupportTest { - public static final Pattern UUID_PATTERN = Pattern - .compile("^(?:\\p{XDigit}{8}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{12})$"); - public static final Tag UUID_TAG = new Tag(Tag.PREFIX + "java.util.UUID"); + public static final Pattern UUID_PATTERN = Pattern + .compile("^(?:\\p{XDigit}{8}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{12})$"); + public static final Tag UUID_TAG = new Tag(Tag.PREFIX + "java.util.UUID"); - @Test - public void pattern() { - assertTrue(UUID_PATTERN.matcher("7f511847-781a-45df-9c8d-1e32e028b9b3").matches()); - assertTrue(UUID_PATTERN.matcher("AC4877BE-0C31-4458-A86E-0272EFE1AAA8").matches()); - } + @Test + public void pattern() { + assertTrue(UUID_PATTERN.matcher("7f511847-781a-45df-9c8d-1e32e028b9b3").matches()); + assertTrue(UUID_PATTERN.matcher("AC4877BE-0C31-4458-A86E-0272EFE1AAA8").matches()); + } - @Test - public void dumpAsString() { - UUID uuid = UUID.randomUUID(); - String str = uuid.toString(); - Yaml yaml = new Yaml(); - yaml.addImplicitResolver(UUID_TAG, UUID_PATTERN, null); - String output = yaml.dump(str); - assertEquals("'" + str + "'\n", output); - assertEquals(str + "\n", yaml.dump(uuid)); - } + @Test + public void dumpAsString() { + UUID uuid = UUID.randomUUID(); + String str = uuid.toString(); + Yaml yaml = new Yaml(); + yaml.addImplicitResolver(UUID_TAG, UUID_PATTERN, null); + String output = yaml.dump(str); + assertEquals("'" + str + "'\n", output); + assertEquals(str + "\n", yaml.dump(uuid)); + } - @Test - public void loadAsUuid() { - Yaml yaml = new Yaml(); - yaml.addImplicitResolver(UUID_TAG, UUID_PATTERN, null); - UUID uuid = (UUID) yaml.load("7f511847-781a-45df-9c8d-1e32e028b9b3"); - assertEquals("7f511847-781a-45df-9c8d-1e32e028b9b3", uuid.toString()); - } + @Test + public void loadAsUuid() { + Yaml yaml = new Yaml(); + yaml.addImplicitResolver(UUID_TAG, UUID_PATTERN, null); + UUID uuid = yaml.load("7f511847-781a-45df-9c8d-1e32e028b9b3"); + assertEquals("7f511847-781a-45df-9c8d-1e32e028b9b3", uuid.toString()); + } - @Test - public void loadFromBean() { - String input = Util.getLocalResource("issues/issue306-1.yaml"); - Yaml yaml = new Yaml(); - BeanWithId bean = yaml.loadAs(input, BeanWithId.class); - assertEquals("7f511847-781a-45df-9c8d-1e32e028b9b3", bean.getId().toString()); - } + @Test + public void loadFromBean() { + String input = Util.getLocalResource("issues/issue306-1.yaml"); + Yaml yaml = new Yaml(); + BeanWithId bean = yaml.loadAs(input, BeanWithId.class); + assertEquals("7f511847-781a-45df-9c8d-1e32e028b9b3", bean.getId().toString()); + } - @Test - public void dumpUuid() { - UUID uuid = UUID.randomUUID(); - Yaml yaml = new Yaml(); - String output = yaml.dump(uuid); - assertEquals("!!java.util.UUID '" + uuid.toString() + "'\n", output); - } + @Test + public void dumpUuid() { + UUID uuid = UUID.randomUUID(); + Yaml yaml = new Yaml(); + String output = yaml.dump(uuid); + assertEquals("!!java.util.UUID '" + uuid + "'\n", output); + } - @Test - public void dumpBean() { - BeanWithId bean = new BeanWithId(); - bean.setValue(3); - UUID uuid = UUID.fromString("ac4877be-0c31-4458-a86e-0272efe1aaa8"); - bean.setId(uuid); - Yaml yaml = new Yaml(); - String output = yaml.dumpAs(bean, Tag.MAP, DumperOptions.FlowStyle.BLOCK); - String expected = Util.getLocalResource("issues/issue306-2.yaml"); - assertEquals(expected, output); - } + @Test + public void dumpBean() { + BeanWithId bean = new BeanWithId(); + bean.setValue(3); + UUID uuid = UUID.fromString("ac4877be-0c31-4458-a86e-0272efe1aaa8"); + bean.setId(uuid); + Yaml yaml = new Yaml(); + String output = yaml.dumpAs(bean, Tag.MAP, DumperOptions.FlowStyle.BLOCK); + String expected = Util.getLocalResource("issues/issue306-2.yaml"); + assertEquals(expected, output); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue307/Blocks.java b/src/test/java/org/yaml/snakeyaml/issues/issue307/Blocks.java new file mode 100644 index 00000000..23fec53a --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue307/Blocks.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue307; + +public class Blocks { + + @OrderAnnotation(order = 3) + public int x; + + @OrderAnnotation(order = 2) + public int y; + + @OrderAnnotation(order = 1) + public int z; +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue307/CustomerBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue307/CustomerBean.java new file mode 100644 index 00000000..e30b177b --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue307/CustomerBean.java @@ -0,0 +1,28 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue307; + +public class CustomerBean extends OrderBean { + + @OrderAnnotation(order = -1) + String customerName; + + public String getCustomerName() { + return customerName; + } + + public void setCustomerName(String customerName) { + this.customerName = customerName; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue307/OrderAnnotation.java b/src/test/java/org/yaml/snakeyaml/issues/issue307/OrderAnnotation.java new file mode 100644 index 00000000..5d67d20b --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue307/OrderAnnotation.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue307; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface OrderAnnotation { + + int order() default 0; +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue307/OrderBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue307/OrderBean.java new file mode 100644 index 00000000..ddaa6193 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue307/OrderBean.java @@ -0,0 +1,60 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue307; + +public class OrderBean { + + public String name; + + @OrderAnnotation(order = 1) + public Blocks type; + + @OrderAnnotation(order = 2) + public Integer age; + + @OrderAnnotation(order = 3) + public String text; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Blocks getType() { + return type; + } + + public void setType(Blocks type) { + this.type = type; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue307/OrderTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue307/OrderTest.java new file mode 100644 index 00000000..0e0c6269 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue307/OrderTest.java @@ -0,0 +1,104 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue307; + +import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Comparator; +import java.util.Set; +import java.util.TreeSet; +import junit.framework.TestCase; +import org.yaml.snakeyaml.Util; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.introspector.BeanAccess; +import org.yaml.snakeyaml.introspector.Property; +import org.yaml.snakeyaml.introspector.PropertyUtils; +import org.yaml.snakeyaml.representer.Representer; + +public class OrderTest extends TestCase { + + public void test_order() { + Representer representer = new Representer(); + representer.setPropertyUtils(new OrderUtil()); + Yaml yaml = new Yaml(representer); + String s = Util.getLocalResource("issues/issue307-order.yaml"); + OrderBean orderBean = yaml.loadAs(s, OrderBean.class); + String dump = yaml.dump(orderBean); + // System.out.println(dump); + String str = "!!org.yaml.snakeyaml.issues.issue307.OrderBean\n" + "name: tian\n" + + "type: {z: 256, y: 255, x: 254}\n" + "age: 22\n" + "text: omit\n"; + assertEquals(str, dump); + } + + public void test_extend_order() { + Representer representer = new Representer(); + representer.setPropertyUtils(new OrderUtil()); + Yaml yaml = new Yaml(representer); + String s = Util.getLocalResource("issues/issue307-order.yaml"); + CustomerBean orderBean2 = yaml.loadAs(s, CustomerBean.class); + orderBean2.setCustomerName("customer"); + String dump = yaml.dump(orderBean2); + // System.out.println(dump); + String str = "!!org.yaml.snakeyaml.issues.issue307.CustomerBean\n" + "customerName: customer\n" + + "name: tian\n" + "type: {z: 256, y: 255, x: 254}\n" + "age: 22\n" + "text: omit\n"; + assertEquals(str, dump); + } + + public static class OrderUtil extends PropertyUtils { + + @Override + protected Set<Property> createPropertySet(final Class<?> type, BeanAccess bAccess) { + Set<Property> properties = new TreeSet<>(new Comparator<Property>() { + @Override + public int compare(Property prop1, Property prop2) { + Integer order1 = getValue(prop1.getName(), type); + Integer order2 = getValue(prop2.getName(), type); + if (order1 == null) { + order1 = 0; + } + if (order2 == null) { + order2 = 0; + } + if (order1 < order2) { + return -1; + } + if (order1 > order2) { + return 1; + } + return prop1.getName().compareTo(prop2.getName()); + } + }); + properties.addAll(super.createPropertySet(type, bAccess)); + return properties; + } + + public Integer getValue(String name, Class<?> type) { + PropertyUtils propertyUtils = new PropertyUtils(); + propertyUtils.setBeanAccess(BeanAccess.FIELD); + Property property = propertyUtils.getProperty(type, name); + Annotation annotation = property.getAnnotation(OrderAnnotation.class); + Integer invoke = null; + if (annotation != null) { + try { + Method method = annotation.annotationType().getDeclaredMethod("order"); + invoke = (Integer) method.invoke(annotation, null); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } + return invoke; + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue310/Option.java b/src/test/java/org/yaml/snakeyaml/issues/issue310/Option.java index 9c0cddb4..c78efe21 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue310/Option.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue310/Option.java @@ -1,34 +1,32 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue310; public final class Option<T> { - private final T value; + private final T value; - public static <T> Option<T> valueOf(T v) { - return new Option<T>(v); - } + public static <T> Option<T> valueOf(T v) { + return new Option<T>(v); + } - private Option(T v) { - this.value = v; - } + private Option(T v) { + this.value = v; + } - public T getValue() { - return value; - } + public T getValue() { + return value; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue310/Person.java b/src/test/java/org/yaml/snakeyaml/issues/issue310/Person.java index c18cde2c..5adf6299 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue310/Person.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue310/Person.java @@ -1,46 +1,44 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue310; public class Person { - private final int id; - private final String name; - private final Option<Double> income; + private final int id; + private final String name; + private final Option<Double> income; - public Person() { - this(0, "", Option.valueOf(0.)); - } + public Person() { + this(0, "", Option.valueOf(0.)); + } - public Person(int id, String name, Option<Double> income) { - this.id = id; - this.name = name; - this.income = income; - } + public Person(int id, String name, Option<Double> income) { + this.id = id; + this.name = name; + this.income = income; + } - public int getId() { - return id; - } + public int getId() { + return id; + } - public String getName() { - return name; - } + public String getName() { + return name; + } - public Option<Double> getIncome() { - return income; - } + public Option<Double> getIncome() { + return income; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue310/PropertyWithPrivateCostructorTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue310/PropertyWithPrivateCostructorTest.java index cb527624..d84db4e8 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue310/PropertyWithPrivateCostructorTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue310/PropertyWithPrivateCostructorTest.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue310; @@ -26,50 +24,51 @@ import org.yaml.snakeyaml.representer.Representer; public class PropertyWithPrivateCostructorTest { - public static class OptionRepresenter extends Representer { + public static class OptionRepresenter extends Representer { - public OptionRepresenter() { - this.representers.put(Option.class, new RepresentOption()); - } + public OptionRepresenter() { + this.representers.put(Option.class, new RepresentOption()); + } - private class RepresentOption implements Represent { - public Node representData(Object data) { - Option<?> opt = (Option<?>) data; - return represent(opt.getValue()); - } - } + private class RepresentOption implements Represent { + public Node representData(Object data) { + Option<?> opt = (Option<?>) data; + return represent(opt.getValue()); + } } - @Test - public void loadFromString() { + } - String yamlStr = "id: 123\n" + "income: 123456.78\n" + "name: Neo Anderson"; + @Test + public void loadFromString() { - Person loadedPerson = yaml().loadAs(yamlStr, Person.class); + String yamlStr = "id: 123\n" + "income: 123456.78\n" + "name: Neo Anderson"; - assertEquals("id", loadedPerson.getId(), 123); - assertEquals("name", loadedPerson.getName(), "Neo Anderson"); - assertEquals("income", loadedPerson.getIncome().getValue().doubleValue(), 123456.78, 0.); - } + Person loadedPerson = yaml().loadAs(yamlStr, Person.class); - @Test - public void dumpNload() { + assertEquals("id", loadedPerson.getId(), 123); + assertEquals("name", loadedPerson.getName(), "Neo Anderson"); + assertEquals("income", loadedPerson.getIncome().getValue().doubleValue(), 123456.78, 0.); + } - Person person = new Person(123, "Neo Anderson", Option.valueOf(123456.78)); + @Test + public void dumpNload() { - String dump = yaml().dumpAsMap(person); + Person person = new Person(123, "Neo Anderson", Option.valueOf(123456.78)); - Person loadedPerson = yaml().loadAs(dump, Person.class); + String dump = yaml().dumpAsMap(person); - assertEquals("id", loadedPerson.getId(), 123); - assertEquals("name", loadedPerson.getName(), "Neo Anderson"); - assertEquals("income", loadedPerson.getIncome().getValue().doubleValue(), 123456.78, 0.); - } + Person loadedPerson = yaml().loadAs(dump, Person.class); - private Yaml yaml() { - Yaml _yaml = new Yaml(new OptionRepresenter()); - _yaml.setBeanAccess(BeanAccess.FIELD); - return _yaml; - } + assertEquals("id", loadedPerson.getId(), 123); + assertEquals("name", loadedPerson.getName(), "Neo Anderson"); + assertEquals("income", loadedPerson.getIncome().getValue().doubleValue(), 123456.78, 0.); + } + + private Yaml yaml() { + Yaml _yaml = new Yaml(new OptionRepresenter()); + _yaml.setBeanAccess(BeanAccess.FIELD); + return _yaml; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue311/BeanWithEnum.java b/src/test/java/org/yaml/snakeyaml/issues/issue311/BeanWithEnum.java index 7bd5e2cf..18888336 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue311/BeanWithEnum.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue311/BeanWithEnum.java @@ -1,57 +1,55 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue311; public class BeanWithEnum { - private boolean boolField; - private String name; - private BooleanEnum enumField; + private boolean boolField; + private String name; + private BooleanEnum enumField; - public BeanWithEnum() { - this(true, "", BooleanEnum.UNKNOWN); - } + public BeanWithEnum() { + this(true, "", BooleanEnum.UNKNOWN); + } - public BeanWithEnum(boolean boolField, String name, BooleanEnum enumField) { - this.boolField = boolField; - this.name = name; - this.enumField = enumField; - } + public BeanWithEnum(boolean boolField, String name, BooleanEnum enumField) { + this.boolField = boolField; + this.name = name; + this.enumField = enumField; + } - public boolean isBoolField() { - return boolField; - } + public boolean isBoolField() { + return boolField; + } - public String getName() { - return name; - } + public String getName() { + return name; + } - public BooleanEnum getEnumField() { - return enumField; - } + public BooleanEnum getEnumField() { + return enumField; + } - public void setBoolField(boolean boolField) { - this.boolField = boolField; - } + public void setBoolField(boolean boolField) { + this.boolField = boolField; + } - public void setName(String name) { - this.name = name; - } + public void setName(String name) { + this.name = name; + } - public void setEnumField(BooleanEnum enumField) { - this.enumField = enumField; - } + public void setEnumField(BooleanEnum enumField) { + this.enumField = enumField; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue311/BooleanEnum.java b/src/test/java/org/yaml/snakeyaml/issues/issue311/BooleanEnum.java index f37aabd5..8b85ae29 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue311/BooleanEnum.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue311/BooleanEnum.java @@ -1,45 +1,44 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue311; public enum BooleanEnum { - TRUE(true), FALSE(false), UNKNOWN(); + TRUE(true), FALSE(false), UNKNOWN(); - private boolean boolValue; - private boolean defined; + private final boolean boolValue; + private final boolean defined; - BooleanEnum(boolean p) { - boolValue = p; - defined = true; - } + BooleanEnum(boolean p) { + boolValue = p; + defined = true; + } - BooleanEnum() { - boolValue = false; - defined = false; - } + BooleanEnum() { + boolValue = false; + defined = false; + } - boolean getBoolValue() { - if (!defined) - throw new IllegalArgumentException("Undefined has no value"); - else - return boolValue; + boolean getBoolValue() { + if (!defined) { + throw new IllegalArgumentException("Undefined has no value"); + } else { + return boolValue; } + } - boolean isDefined() { - return defined; - } -}
\ No newline at end of file + boolean isDefined() { + return defined; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue311/BooleanEnumTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue311/BooleanEnumTest.java index 3f0107ac..205dd060 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue311/BooleanEnumTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue311/BooleanEnumTest.java @@ -1,20 +1,20 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue311; +import static org.junit.Assert.assertEquals; + import org.junit.Test; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @@ -26,87 +26,92 @@ import org.yaml.snakeyaml.nodes.Tag; import org.yaml.snakeyaml.representer.Represent; import org.yaml.snakeyaml.representer.Representer; -import static org.junit.Assert.assertEquals; - public class BooleanEnumTest { - @Test - public void loadEnum() { - - Yaml yaml = new Yaml(new MyConstructor(), new MyRepresenter()); - BeanWithEnum parsed = yaml.loadAs("{boolField: true, enumField: true, name: '10'}", BeanWithEnum.class); - //System.out.println(parsed.getEnumField()); - assertEquals(BooleanEnum.TRUE, parsed.getEnumField()); - assertEquals("10", parsed.getName()); + @Test + public void loadEnum() { + + Yaml yaml = new Yaml(new MyConstructor(), new MyRepresenter()); + BeanWithEnum parsed = + yaml.loadAs("{boolField: true, enumField: true, name: '10'}", BeanWithEnum.class); + // System.out.println(parsed.getEnumField()); + assertEquals(BooleanEnum.TRUE, parsed.getEnumField()); + assertEquals("10", parsed.getName()); + } + + @Test + public void loadEnumUndefined() { + + Yaml yaml = new Yaml(new MyConstructor(), new MyRepresenter()); + BeanWithEnum parsed = + yaml.loadAs("{boolField: true, enumField: nonsense, name: bar}", BeanWithEnum.class); + // System.out.println(parsed.getEnumField()); + assertEquals(BooleanEnum.UNKNOWN, parsed.getEnumField()); + assertEquals("bar", parsed.getName()); + } + + @Test + public void dumpEnum() { + + BeanWithEnum bean = new BeanWithEnum(true, "10", BooleanEnum.TRUE); + Yaml yaml = new Yaml(new MyConstructor(), new MyRepresenter()); + String output = yaml.dumpAs(bean, Tag.MAP, DumperOptions.FlowStyle.FLOW); + assertEquals("{boolField: true, enumField: 'true', name: '10'}\n", output); + } + + class MyRepresenter extends Representer { + + public MyRepresenter() { + this.representers.put(BooleanEnum.class, new RepresentEnum()); } - @Test - public void loadEnumUndefined() { - - Yaml yaml = new Yaml(new MyConstructor(), new MyRepresenter()); - BeanWithEnum parsed = yaml.loadAs("{boolField: true, enumField: nonsense, name: bar}", BeanWithEnum.class); - //System.out.println(parsed.getEnumField()); - assertEquals(BooleanEnum.UNKNOWN, parsed.getEnumField()); - assertEquals("bar", parsed.getName()); - } + private class RepresentEnum implements Represent { - @Test - public void dumpEnum() { + public Node representData(Object data) { + BooleanEnum myEnum = (BooleanEnum) data; + String value; + switch (myEnum) { + case TRUE: + value = "true"; + break; - BeanWithEnum bean = new BeanWithEnum(true, "10", BooleanEnum.TRUE); - Yaml yaml = new Yaml(new MyConstructor(), new MyRepresenter()); - String output = yaml.dumpAs(bean, Tag.MAP, DumperOptions.FlowStyle.FLOW); - assertEquals("{boolField: true, enumField: 'true', name: '10'}\n", output); - } + case FALSE: + value = "false"; + break; - class MyRepresenter extends Representer { - public MyRepresenter() { - this.representers.put(BooleanEnum.class, new RepresentEnum()); - } + case UNKNOWN: + value = "unknown"; + break; - private class RepresentEnum implements Represent { - public Node representData(Object data) { - BooleanEnum myEnum = (BooleanEnum) data; - String value; - switch (myEnum) { - case TRUE: - value = "true"; - break; - - case FALSE: - value = "false"; - break; - - case UNKNOWN: - value = "unknown"; - break; - - default: - throw new IllegalArgumentException(); - } - return representScalar(Tag.STR, value); - } + default: + throw new IllegalArgumentException(); } + return representScalar(Tag.STR, value); + } } + } - class MyConstructor extends Constructor { - public MyConstructor() { - this.yamlClassConstructors.put(NodeId.scalar, new ConstructEnum()); - } + class MyConstructor extends Constructor { + + public MyConstructor() { + this.yamlClassConstructors.put(NodeId.scalar, new ConstructEnum()); + } - private class ConstructEnum extends ConstructScalar { - public Object construct(Node node) { - if (node.getType().equals(BooleanEnum.class)) { - String val = (String) constructScalar((ScalarNode) node); - if ("true".equals(val)) { - return BooleanEnum.TRUE; - } else if ("false".equals(val)) { - return BooleanEnum.FALSE; - } else - return BooleanEnum.UNKNOWN; - } - return super.construct(node); - } + private class ConstructEnum extends ConstructScalar { + + public Object construct(Node node) { + if (node.getType().equals(BooleanEnum.class)) { + String val = constructScalar((ScalarNode) node); + if ("true".equals(val)) { + return BooleanEnum.TRUE; + } else if ("false".equals(val)) { + return BooleanEnum.FALSE; + } else { + return BooleanEnum.UNKNOWN; + } } + return super.construct(node); + } } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue318/ContextClassLoaderTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue318/ContextClassLoaderTest.java index 82f29ee7..a9ae9dca 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue318/ContextClassLoaderTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue318/ContextClassLoaderTest.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue318; @@ -27,7 +25,6 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.Properties; - import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -35,102 +32,116 @@ import org.yaml.snakeyaml.Yaml; public class ContextClassLoaderTest { - static public class DomainBean { - - private int value = 0; - - public void setValue(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + value; - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - DomainBean other = (DomainBean) obj; - if (value != other.value) - return false; - return true; - } + static public class DomainBean { + + private int value = 0; + + public void setValue(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + value; + return result; } - private URLClassLoader yamlCL; + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + DomainBean other = (DomainBean) obj; + return value == other.value; + } - @Before - public void before() throws MalformedURLException { - Properties classpath = new Properties(); - InputStream cpProperties = getClass().getResourceAsStream("classpath.properties"); - try { - classpath.load(cpProperties); - } catch (IOException e2) { - fail(e2.getLocalizedMessage()); - } + } - File runtimeClassesDir = new File(classpath.getProperty("runtime_classes_dir")); + private URLClassLoader yamlCL; - yamlCL = new URLClassLoader(new URL[] { runtimeClassesDir.toURI().toURL() }, null); + @Before + public void before() throws MalformedURLException { + Properties classpath = new Properties(); + InputStream cpProperties = getClass().getResourceAsStream("classpath.properties"); + try { + classpath.load(cpProperties); + } catch (IOException e2) { + fail(e2.getLocalizedMessage()); } - @After - public void after() { - // URLClassLoader.close is @since 1.7 - // if (yamlCL != null) { - // try { - // yamlCL.close(); - // } catch (IOException e) { - // e.printStackTrace(); - // } finally { + File runtimeClassesDir = new File(classpath.getProperty("runtime_classes_dir")); + + ClassLoader noSnakeYAMLClassLoader = + new ClassLoader(Thread.currentThread().getContextClassLoader()) { + + @Override + protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { + if (!name.startsWith("org.yaml.snakeyaml")) { + return super.loadClass(name, resolve); + } + throw new ClassNotFoundException("Can't load SnakeYaml classes by this ClassLoader"); + } + + }; + + yamlCL = + new URLClassLoader(new URL[] {runtimeClassesDir.toURI().toURL()}, noSnakeYAMLClassLoader); + } + + @After + public void after() { + if (yamlCL != null) { + try { + yamlCL.close(); + } catch (IOException e) { + e.printStackTrace(); + } finally { yamlCL = null; - // } - // } + } } + } - @Test(expected = ClassNotFoundException.class) - public void expectNoDomainClassInYamlCL() throws ClassNotFoundException { - yamlCL.loadClass(DomainBean.class.getName()); - } + @Test(expected = ClassNotFoundException.class) + public void expectNoDomainClassInYamlCL() throws ClassNotFoundException { + yamlCL.loadClass(DomainBean.class.getName()); + } - @Test - public void yamlClassInYAMLCL() throws ClassNotFoundException { - yamlCL.loadClass(Yaml.class.getName()); - } + @Test + public void yamlClassInYAMLCL() throws ClassNotFoundException { + yamlCL.loadClass(Yaml.class.getName()); + } - @Test - public void domainInDifferentConstructor() throws ClassNotFoundException, - InstantiationException, IllegalAccessException, NoSuchMethodException, - SecurityException, IllegalArgumentException, InvocationTargetException { + @Test + public void domainInDifferentConstructor() throws ClassNotFoundException, InstantiationException, + IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, + InvocationTargetException { - Class<?> yamlClass = yamlCL.loadClass(Yaml.class.getName()); + Class<?> yamlClass = yamlCL.loadClass(Yaml.class.getName()); - DomainBean bean = new DomainBean(); - bean.setValue(13); + DomainBean bean = new DomainBean(); + bean.setValue(13); - Object yaml = yamlClass.newInstance(); + Object yaml = yamlClass.newInstance(); - Method dumpMethod = yaml.getClass().getMethod("dump", new Class<?>[] { Object.class }); - String dump = dumpMethod.invoke(yaml, bean).toString(); + Method dumpMethod = yaml.getClass().getMethod("dump", Object.class); + String dump = dumpMethod.invoke(yaml, bean).toString(); - Method loadMethod = yaml.getClass().getMethod("load", new Class<?>[] { String.class }); - DomainBean object = (DomainBean) loadMethod.invoke(yaml, dump); + Method loadMethod = yaml.getClass().getMethod("load", String.class); + DomainBean object = (DomainBean) loadMethod.invoke(yaml, dump); - assertEquals(bean, object); - } + assertEquals(bean, object); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue322_382/PropertyWithoutGetterTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue322_382/PropertyWithoutGetterTest.java new file mode 100644 index 00000000..51dca53e --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue322_382/PropertyWithoutGetterTest.java @@ -0,0 +1,111 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue322_382; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; +import org.yaml.snakeyaml.TypeDescription; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; + +public class PropertyWithoutGetterTest { + + @Test + public void testPublicFooWithPublicFields() { + Constructor constructor = new Constructor(); + constructor.addTypeDescription(new TypeDescription(PublicFooWithPublicFields.class, "!foo")); + Yaml yaml = new Yaml(constructor); + + PublicFooWithPublicFields foo = + yaml.loadAs("!foo\ncountryCodes: [NZ, NO]\nsome: NO", PublicFooWithPublicFields.class); + + assertEquals(2, foo.countryCodes.size()); + assertEquals("NZ", foo.countryCodes.get(0)); + assertEquals("The type (String) must be taken from the field.", "NO", foo.countryCodes.get(1)); + assertEquals("NO", foo.some); + } + + @Test + public void testStaticFooWithoutGetter() { + Constructor constructor = new Constructor(); + constructor.addTypeDescription(new TypeDescription(StaticFooWithoutGetter.class, "!foo")); + Yaml yaml = new Yaml(constructor); + + StaticFooWithoutGetter foo = + yaml.loadAs("!foo\ncountryCodes: [NZ, NO]\nsome: NO", StaticFooWithoutGetter.class); + + assertEquals(2, foo.countryCodes.size()); + assertEquals("NZ", foo.countryCodes.get(0)); + assertEquals("The type List(String) must be taken from the setter.", "NO", + foo.countryCodes.get(1)); + assertEquals("NO", foo.some); + } + + @Test + public void testStaticFooWithGetter() { + Constructor constructor = new Constructor(); + constructor.addTypeDescription(new TypeDescription(StaticFooWithGetter.class, "!foo")); + Yaml yaml = new Yaml(constructor); + + StaticFooWithGetter foo = + yaml.loadAs("!foo\ncountryCodes: [NZ, NO]\nsome: NO", StaticFooWithGetter.class); + + assertEquals(2, foo.countryCodes.size()); + assertEquals("NZ", foo.countryCodes.get(0)); + assertEquals("The type List(String) must be taken from the getter.", "NO", + foo.countryCodes.get(1)); + assertEquals("NO", foo.some); + } + + public static class StaticFooWithoutGetter { + + private List<String> countryCodes = new ArrayList<String>(); + private String some; + + public void setCountryCodes(List<String> countryCodes) { + for (Object countryCode : countryCodes) { + // System.out.println(countryCode.getClass().getName()); + } + this.countryCodes = countryCodes; + } + + public void setSome(String sime) { + this.some = sime; + } + } + + public static class StaticFooWithGetter { + + private List<String> countryCodes = new ArrayList<String>(); + private String some; + + public List<String> getCountryCodes() { + return countryCodes; + } + + public void setCountryCodes(List<String> countryCodes) { + for (Object countryCode : countryCodes) { + // System.out.println(countryCode.getClass().getName()); + } + this.countryCodes = countryCodes; + } + + public void setSome(String sime) { + this.some = sime; + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue322_382/PublicFooWithPublicFields.java b/src/test/java/org/yaml/snakeyaml/issues/issue322_382/PublicFooWithPublicFields.java new file mode 100644 index 00000000..9b2a7e5a --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue322_382/PublicFooWithPublicFields.java @@ -0,0 +1,34 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue322_382; + +import java.util.ArrayList; +import java.util.List; + +public class PublicFooWithPublicFields { + + public List<String> countryCodes = new ArrayList<String>(); + public String some; + + public void setCountryCodes(List<String> countryCodes) { + for (Object countryCode : countryCodes) { + System.out.println(countryCode.getClass().getName()); + } + this.countryCodes = countryCodes; + } + + public void setSome(String sime) { + this.some = sime; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue332/Data.java b/src/test/java/org/yaml/snakeyaml/issues/issue332/Data.java index 56eeb4c7..28cb08e8 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue332/Data.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue332/Data.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue332; @@ -19,52 +17,48 @@ import java.beans.ConstructorProperties; import java.math.BigDecimal; public class Data { - private String label; - private String unit; + private String label; - private BigDecimal value; + private String unit; - public BigDecimal getValue() { - return value; - } + private BigDecimal value; - public String getLabel() { - return label; - } + public BigDecimal getValue() { + return value; + } - @ConstructorProperties({"label", "value", "unit"}) - public Data(String label, BigDecimal value, String unit) { - this.label = label; - this.value = value; - this.unit = unit; - } + public String getLabel() { + return label; + } -// public void setLabel(String label) { -// this.label = label; -// } -// -// public void setUnit(String unit) { -// this.unit = unit; -// } -// -// public void setValue(BigDecimal value) { -// this.value = value; -// } + @ConstructorProperties({"label", "value", "unit"}) + public Data(String label, BigDecimal value, String unit) { + this.label = label; + this.value = value; + this.unit = unit; + } - public String getUnit() { - return unit; - } + // public void setLabel(String label) { + // this.label = label; + // } + // + // public void setUnit(String unit) { + // this.unit = unit; + // } + // + // public void setValue(BigDecimal value) { + // this.value = value; + // } - public Data() { - } + public String getUnit() { + return unit; + } - @Override - public String toString() { - return "Data{" + - "label='" + label + '\'' + - ", unit='" + unit + '\'' + - ", value=" + value + - '}'; - } + public Data() {} + + @Override + public String toString() { + return "Data{" + "label='" + label + '\'' + ", unit='" + unit + '\'' + ", value=" + value + '}'; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue332/DataTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue332/DataTest.java index 6b6098df..55a87fd0 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue332/DataTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue332/DataTest.java @@ -1,39 +1,38 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue332; -import org.junit.Test; -import org.yaml.snakeyaml.Yaml; +import static org.junit.Assert.assertEquals; import java.math.BigDecimal; - -import static org.junit.Assert.assertEquals; +import org.junit.Test; +import org.yaml.snakeyaml.Yaml; public class DataTest { - @Test - public void testGetUnit() throws Exception { - Data data = new Data("Voltage", BigDecimal.TEN, "V"); - assertEquals("!!org.yaml.snakeyaml.issues.issue332.Data {}", new Yaml().dump(data).trim()); - //TODO assertEquals("!!org.yaml.snakeyaml.issues.issue332.Data {label: Voltage, unit: V, value: !!float '10'}", new Yaml().dump(data).trim()); - } + @Test + public void testGetUnit() throws Exception { + Data data = new Data("Voltage", BigDecimal.TEN, "V"); + assertEquals("!!org.yaml.snakeyaml.issues.issue332.Data {}", new Yaml().dump(data).trim()); + // TODO assertEquals("!!org.yaml.snakeyaml.issues.issue332.Data {label: Voltage, unit: V, value: + // !!float '10'}", new Yaml().dump(data).trim()); + } - @Test - public void testLoad() throws Exception { - String doc = "!!org.yaml.snakeyaml.issues.issue332.Data [Voltage, 10, volts]"; - assertEquals("Data{label='Voltage', unit='volts', value=10}", (new Yaml().load(doc)).toString()); - } -}
\ No newline at end of file + @Test + public void testLoad() throws Exception { + String doc = "!!org.yaml.snakeyaml.issues.issue332.Data [Voltage, 10, volts]"; + assertEquals("Data{label='Voltage', unit='volts', value=10}", + (new Yaml().load(doc)).toString()); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue335/JavaLangObjectTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue335/JavaLangObjectTest.java new file mode 100644 index 00000000..0d97c6ef --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue335/JavaLangObjectTest.java @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue335; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.yaml.snakeyaml.Yaml; + +public class JavaLangObjectTest { + + @Test + public void testLoadObjectAsMapping() throws Exception { + Object obj = new Yaml().load("!!java.lang.Object {}"); + assertEquals(Object.class, obj.getClass()); + } + + @Test + public void testLoadObjectAsScalar() throws Exception { + try { + new Yaml().load("!!java.lang.Object"); + fail("Object has no single argument constructor"); + } catch (Exception e) { + assertTrue(e.getMessage().contains("No single argument constructor found")); + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue337/DuplicateKeyTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue337/DuplicateKeyTest.java new file mode 100644 index 00000000..c5d88945 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue337/DuplicateKeyTest.java @@ -0,0 +1,176 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue337; + +import static junit.framework.TestCase.assertTrue; +import static org.junit.Assert.assertEquals; + +import java.util.LinkedHashMap; +import java.util.Map; +import org.junit.Test; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Util; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.DuplicateKeyException; + +public class DuplicateKeyTest { + + public static class MapProvider<K, V> { + + private Map<K, V> map = new LinkedHashMap<K, V>(); + + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Map<K, V> getMap() { + return map; + } + + public void setMap(Map<K, V> map) { + this.map = map; + } + + @SuppressWarnings("rawtypes") + @Override + public boolean equals(Object obj) { + if (obj instanceof MapProvider) { + return map.equals(((MapProvider) obj).getMap()); + } else { + return false; + } + } + + @Override + public int hashCode() { + return map.hashCode(); + } + } + + // test guff + public static class FooEntry { + + private String id; + private String url; + + public FooEntry() {} + + public FooEntry(String id, String url) { + this.id = id; + this.url = url; + } + + public String getUrl() { + return this.url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getId() { + return this.id; + } + + public void setId(String id) { + this.id = id; + } + + } + + @Test + public void defaultConfigurationNoErrorsWithDuplicates() { + String input = Util.getLocalResource("issues/issue337-duplicate-keys.yaml"); + Yaml yaml = new Yaml(); + MapProvider<String, FooEntry> testdata = yaml.loadAs(input, MapProvider.class); + assertEquals("has-dup-keys", testdata.getName()); + assertEquals(1, testdata.getMap().size()); + assertEquals("daaf8911-36e4-4e92-86ea-eb77ac2c1e91", testdata.getMap().get("someitem").getId()); + } + + @Test + public void errorOnDuplicateKeys() { + String input = Util.getLocalResource("issues/issue337-duplicate-keys.yaml"); + LoaderOptions lc = new LoaderOptions(); + lc.setAllowDuplicateKeys(false); + Yaml yaml = new Yaml(lc); + try { + yaml.loadAs(input, MapProvider.class); + } catch (DuplicateKeyException e) { + assertTrue(e.getMessage(), e.getMessage().contains("found duplicate key someitem")); + assertTrue(e.getMessage(), e.getMessage().contains("line 3, column 3")); + } + } + + @Test + public void errorOnDuplicateKeysInJavaBeanProperty() { + String input = Util.getLocalResource("issues/issue337-duplicate-keys-javabean-property.yaml"); + LoaderOptions lc = new LoaderOptions(); + lc.setAllowDuplicateKeys(false); + Yaml yaml = new Yaml(lc); + try { + MapProvider<String, FooEntry> testdata = yaml.loadAs(input, MapProvider.class); + assertEquals("has-dup-keys", testdata.getName()); + } catch (DuplicateKeyException e) { + assertTrue(e.getMessage(), e.getMessage().contains("found duplicate key name")); + assertTrue(e.getMessage(), e.getMessage().contains("line 9, column 1")); + } + } + + @Test + public void acceptDuplicateKeysInJavaBeanProperty() { + String input = Util.getLocalResource("issues/issue337-duplicate-keys-javabean-property.yaml"); + LoaderOptions lc = new LoaderOptions(); + lc.setAllowDuplicateKeys(true); + Yaml yaml = new Yaml(lc); + MapProvider<String, FooEntry> testdata = yaml.loadAs(input, MapProvider.class); + assertEquals("has-dup-keys", testdata.getName()); + } + + @Test + public void defaultConfigUniqueKeysWorks() { + String input = Util.getLocalResource("issues/issue337-duplicate-keys-no-dups.yaml"); + Yaml yaml = new Yaml(); + MapProvider<String, FooEntry> testdata = yaml.loadAs(input, MapProvider.class); + assertEquals("no-dups-test", testdata.getName()); + assertEquals(3, testdata.getMap().size()); + assertEquals("aead4b16-4b61-4eff-b241-6eff26eaa778", + testdata.getMap().get("someitem1").getId()); + assertEquals("daaf8911-36e4-4e92-86ea-eb77ac2c1e91", + testdata.getMap().get("someitem3").getId()); + } + + @Test + public void noDuplicatesConfigMutablePostChange() { + String input = Util.getLocalResource("issues/issue337-duplicate-keys-no-dups.yaml"); + LoaderOptions lc = new LoaderOptions(); + lc.setAllowDuplicateKeys(false); + Yaml yaml = new Yaml(lc); + MapProvider<String, FooEntry> testdata = yaml.loadAs(input, MapProvider.class); + assertEquals("no-dups-test", testdata.getName()); + assertEquals(3, testdata.getMap().size()); + assertEquals("aead4b16-4b61-4eff-b241-6eff26eaa778", + testdata.getMap().get("someitem1").getId()); + assertEquals("daaf8911-36e4-4e92-86ea-eb77ac2c1e91", + testdata.getMap().get("someitem3").getId()); + testdata.getMap().put("someitem1", new FooEntry("AnotherEntry", "AnotherURL")); + } + +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue341/MultiBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue341/MultiBean.java new file mode 100644 index 00000000..131ef5a4 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue341/MultiBean.java @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue341; + +public class MultiBean { + + private Long id; + private String name; + + public MultiBean(Long id, String name) { + this.id = id; + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue341/MultiRepresenter.java b/src/test/java/org/yaml/snakeyaml/issues/issue341/MultiRepresenter.java new file mode 100644 index 00000000..06efd876 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue341/MultiRepresenter.java @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue341; + +import org.yaml.snakeyaml.nodes.Node; +import org.yaml.snakeyaml.representer.Representer; + +class MultiRepresenter extends Representer { + + public MultiRepresenter() { + multiRepresenters.put(null, new RepresentAsSring()); + } + + private class RepresentAsSring extends RepresentString { + + public Node representData(Object data) { + return super.representData(data.toString()); + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue341/MultiRepresenterTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue341/MultiRepresenterTest.java new file mode 100644 index 00000000..60202c09 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue341/MultiRepresenterTest.java @@ -0,0 +1,28 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue341; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.yaml.snakeyaml.Yaml; + +public class MultiRepresenterTest { + + @Test + public void testLoadObjectAsMapping() throws Exception { + String dump = new Yaml(new MultiRepresenter()).dump(new MultiBean(17L, "foo")); + assertTrue(dump.startsWith("org.yaml.snakeyaml.issues.issue341.MultiBean@")); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue348/MultiLevelImmutableTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue348/MultiLevelImmutableTest.java new file mode 100644 index 00000000..accc8f75 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue348/MultiLevelImmutableTest.java @@ -0,0 +1,52 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue348; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import java.util.List; +import java.util.Map; +import org.junit.Test; +import org.yaml.snakeyaml.Util; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.issues.issue348.model.Bar; +import org.yaml.snakeyaml.issues.issue348.model.Baz; +import org.yaml.snakeyaml.issues.issue348.model.Foo; + +public class MultiLevelImmutableTest { + + @Test + public void testUnexpectedRecursive() { + Yaml yaml = new Yaml(); + String data = Util.getLocalResource("issues/issue348.yaml"); + Map<?, ?> loadedMap = yaml.loadAs(data, Map.class); + + for (Map.Entry<?, ?> entry : loadedMap.entrySet()) { + assertThat(entry.getValue(), instanceOf(List.class)); + } + + Object foo = ((List) loadedMap.get("foo")).get(0); + Object bar = ((List) loadedMap.get("bar")).get(0); + Object baz = ((List) loadedMap.get("baz")).get(0); + assertThat(foo, instanceOf(Foo.class)); + assertThat(bar, instanceOf(Bar.class)); + assertThat(baz, instanceOf(Baz.class)); + + assertEquals(foo, ((Bar) bar).getFoo()); + assertEquals(bar, ((Baz) baz).getBar()); + assertEquals("foo", ((Foo) foo).getFoo()); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue348/model/Bar.java b/src/test/java/org/yaml/snakeyaml/issues/issue348/model/Bar.java new file mode 100644 index 00000000..dd6db14a --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue348/model/Bar.java @@ -0,0 +1,29 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue348.model; + +public class Bar { + + private Foo foo; + + public Bar() {} + + public Bar(Foo foo) { + this.foo = foo; + } + + public Foo getFoo() { + return foo; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue348/model/Baz.java b/src/test/java/org/yaml/snakeyaml/issues/issue348/model/Baz.java new file mode 100644 index 00000000..8ddade2a --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue348/model/Baz.java @@ -0,0 +1,30 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue348.model; + +public class Baz { + + private Bar bar; + + public Baz() {} + + public Baz(Bar bar) { + this.bar = bar; + } + + public Bar getBar() { + return bar; + } + +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue348/model/Foo.java b/src/test/java/org/yaml/snakeyaml/issues/issue348/model/Foo.java new file mode 100644 index 00000000..f95b348a --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue348/model/Foo.java @@ -0,0 +1,29 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue348.model; + +public class Foo { + + private String foo; + + public Foo() {} + + public Foo(String str) { + this.foo = str; + } + + public String getFoo() { + return foo; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue349/YamlBase64BinaryTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue349/YamlBase64BinaryTest.java new file mode 100644 index 00000000..3185cb0c --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue349/YamlBase64BinaryTest.java @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue349; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; +import junit.framework.TestCase; +import org.yaml.snakeyaml.Yaml; + +public class YamlBase64BinaryTest extends TestCase { + + public void testLocalBinaryTags() throws IOException { + String[] names = {"1", "2", "3"}; + for (String name : names) { + toBeTested(name); + } + } + + public void toBeTested(String name) throws IOException { + Yaml yaml = new Yaml(); + InputStream inputStream = + YamlBase64BinaryTest.class.getResourceAsStream("/issues/issue349-" + name + ".yaml"); + Map<String, Object> bean = yaml.load(inputStream); + byte[] jpeg = (byte[]) bean.get("picture"); + assertEquals(65, jpeg.length); + inputStream.close(); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue351/MergedOrderTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue351/MergedOrderTest.java new file mode 100644 index 00000000..b9400757 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue351/MergedOrderTest.java @@ -0,0 +1,45 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue351; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; +import org.junit.Test; +import org.yaml.snakeyaml.Yaml; + +public class MergedOrderTest { + + @SuppressWarnings("rawtypes") + @Test + public void mergedLinkedMapOrder() throws IOException { + Yaml yaml = new Yaml(); + InputStream inputStream = MergedOrderTest.class.getResourceAsStream("/issues/issue351_1.yaml"); + Map<?, ?> bean = yaml.loadAs(inputStream, Map.class); + + Object first = bean.get("prize_cashBack_fixture"); + Object second = bean.get("prize_cashBack_sendEmail_fixture"); + + assertThat(first, instanceOf(Map.class)); + assertThat(second, instanceOf(Map.class)); + + assertArrayEquals(((Map) first).entrySet().toArray(), ((Map) second).entrySet().toArray()); + + inputStream.close(); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue354/PunctuationInTheBeginningTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue354/PunctuationInTheBeginningTest.java new file mode 100644 index 00000000..6aaa7269 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue354/PunctuationInTheBeginningTest.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue354; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.util.Map; +import org.junit.Test; +import org.yaml.snakeyaml.Util; +import org.yaml.snakeyaml.Yaml; + +/** + * The test does not fix anything. It just proves that SnakeYAML works as it should according to the + * spec 1.1 + */ +public class PunctuationInTheBeginningTest { + + @Test + public void testBacktickAndAtSign() throws IOException { + String input = Util.getLocalResource("issues/issue354.yaml"); + Yaml yaml = new Yaml(); + Map<String, Object> bean = yaml.load(input); + assertEquals("This is\n`a literal\n", bean.get("foo")); + assertEquals("And\n@this\n", bean.get("bar")); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue358/UnexpectedIndentTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue358/UnexpectedIndentTest.java new file mode 100644 index 00000000..74cc4104 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue358/UnexpectedIndentTest.java @@ -0,0 +1,74 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue358; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Test; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.error.YAMLException; + +public class UnexpectedIndentTest { + + @Test + public void testIndicatorIndentMuchSmaller() { + check(4, 6); // standard + check(4, 5); // ugly, but acceptable, because the YAML will be valid + try { + check(4, 4); + fail("Invalid indent may cause invalid YAML"); + } catch (YAMLException e) { + assertEquals("Indicator indent must be smaller then indent.", e.getMessage()); + } + try { + check(6, 4); + fail("Invalid indent may cause invalid YAML"); + } catch (YAMLException e) { + assertEquals("Indicator indent must be smaller then indent.", e.getMessage()); + } + } + + private void check(int indicatorIndent, int indent) { + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + options.setIndicatorIndent(indicatorIndent); + options.setIndent(indent); + Map<String, Object> map = create(); + String dumped = new Yaml(options).dump(map); + // System.out.println(dumped); + Map<String, Object> parsed = new Yaml().load(dumped); + assertEquals(map, parsed); + } + + private Map<String, Object> create() { + Map<String, Object> map = new HashMap<String, Object>(); + List<Map<String, String>> list = new ArrayList<Map<String, String>>(); + Map<String, String> member1 = new HashMap<String, String>(); + member1.put("db_1", "ds"); + member1.put("name", "asd"); + list.add(member1); + Map<String, String> member2 = new HashMap<String, String>(); + member2.put("db_2", "daas"); + member2.put("name", "adas"); + list.add(member2); + map.put("some", list); + return map; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue373/ModelMapIntegerBigdecimal.java b/src/test/java/org/yaml/snakeyaml/issues/issue373/ModelMapIntegerBigdecimal.java new file mode 100644 index 00000000..ab882359 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue373/ModelMapIntegerBigdecimal.java @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue373; + +import java.math.BigDecimal; +import java.util.Map; + +public class ModelMapIntegerBigdecimal { + + private Map<Integer, BigDecimal> mapIntegerBigDecimal; + + public ModelMapIntegerBigdecimal() {} + + public Map<Integer, BigDecimal> getMapIntegerBigDecimal() { + return mapIntegerBigDecimal; + } + + public void setMapIntegerBigDecimal(Map<Integer, BigDecimal> mapIntegerBigDecimal) { + this.mapIntegerBigDecimal = mapIntegerBigDecimal; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue373/UnknownRepresenterTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue373/UnknownRepresenterTest.java new file mode 100644 index 00000000..0820b116 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue373/UnknownRepresenterTest.java @@ -0,0 +1,45 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue373; + +import static junit.framework.TestCase.assertTrue; + +import java.math.BigDecimal; +import java.util.HashMap; +import java.util.Map; +import org.junit.Test; +import org.yaml.snakeyaml.Yaml; + +/** + * Redundant test - it does not test anything. It is here only to prove that SnakeYAML does what it + * should. + */ +public class UnknownRepresenterTest { + + @Test + public void testIndicatorIndentMuchSmaller() { + ModelMapIntegerBigdecimal mv = new ModelMapIntegerBigdecimal(); + + Map<Integer, BigDecimal> m = new HashMap<Integer, BigDecimal>(); + Integer a = 1; + BigDecimal b = new BigDecimal("0.01"); + m.put(a, b); + mv.setMapIntegerBigDecimal(m); + + Yaml yaml = new Yaml(); + String str = yaml.dump(mv); + // System.out.println(str); + assertTrue(str.contains("mapIntegerBigDecimal: {1: 0.01")); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue374/NumberAsJavaBeanPropertyTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue374/NumberAsJavaBeanPropertyTest.java new file mode 100644 index 00000000..a15725f1 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue374/NumberAsJavaBeanPropertyTest.java @@ -0,0 +1,86 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue374; + +import static org.junit.Assert.assertEquals; + +import java.text.NumberFormat; +import java.text.ParseException; +import java.util.Locale; +import org.junit.Test; +import org.yaml.snakeyaml.Yaml; + + +public class NumberAsJavaBeanPropertyTest { + + @Test + public void testNumberAsDouble() { + Locale originalLocale = Locale.getDefault(); + + Locale.setDefault(Locale.CANADA_FRENCH); + + AmbiguousNumberType original = new AmbiguousNumberType(); + original.number = 1.1; + assertEquals(Double.class, original.number.getClass()); + + Yaml yaml = new Yaml(); + String str = yaml.dump(original); + + AmbiguousNumberType interpreted = yaml.load(str); + + Locale.setDefault(originalLocale); + assertEquals(original.number, interpreted.number); + } + + public static class AmbiguousNumberType { + + public Number number; + } + + @Test + public void testNumberAsInteger() { + AmbiguousNumberType original = new AmbiguousNumberType(); + original.number = 1; + assertEquals(Integer.class, original.number.getClass()); + + Yaml yaml = new Yaml(); + String str = yaml.dump(original); + + AmbiguousNumberType interpreted = yaml.load(str); + + assertEquals(Double.valueOf(original.number.intValue()), interpreted.number); + } + + @Test + public void testNumberAsLong() { + AmbiguousNumberType original = new AmbiguousNumberType(); + original.number = 1L; + assertEquals(Long.class, original.number.getClass()); + + Yaml yaml = new Yaml(); + String str = yaml.dump(original); + + AmbiguousNumberType interpreted = yaml.load(str); + + assertEquals(Double.valueOf(original.number.intValue()), interpreted.number); + } + + @Test + public void testNumberFormatParse() throws ParseException { + NumberFormat nf = NumberFormat.getInstance(Locale.US); + assertEquals(Long.valueOf(1), nf.parse("1")); + assertEquals("NumberFormat converts 1.0 to 1 - this is against the specification.", + Long.valueOf(1), nf.parse("1.0")); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue375/EmptyDocumentTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue375/EmptyDocumentTest.java new file mode 100644 index 00000000..3671e60d --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue375/EmptyDocumentTest.java @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue375; + +import org.junit.Assert; +import org.junit.Test; +import org.yaml.snakeyaml.Yaml; + +public class EmptyDocumentTest { + + @Test + public void returnNullForEmptyDocument() { + Yaml yaml = new Yaml(); + Assert.assertNull(yaml.loadAs("", TestObject.class)); + Assert.assertNull(yaml.loadAs("\n \n", TestObject.class)); + Assert.assertNull(yaml.loadAs("---\n", TestObject.class)); + Assert.assertNull(yaml.loadAs("---\n#comment\n...\n", TestObject.class)); + } + + public static class TestObject { + + private int attribute1; + + public int getAttribute1() { + return attribute1; + } + + public void setAttribute1(int attribute1) { + this.attribute1 = attribute1; + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue377/BillionLaughsAttackTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue377/BillionLaughsAttackTest.java new file mode 100644 index 00000000..ea2e3ce8 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue377/BillionLaughsAttackTest.java @@ -0,0 +1,89 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue377; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Map; +import org.junit.Test; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.error.YAMLException; + +/** + * https://en.wikipedia.org/wiki/Billion_laughs_attack#Variations + */ +public class BillionLaughsAttackTest { + + public static final String data = + "a: &a [\"lol\",\"lol\",\"lol\",\"lol\",\"lol\",\"lol\",\"lol\",\"lol\",\"lol\"]\n" + + "b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a]\n" + "c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b]\n" + + "d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c]\n" + "e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d]\n" + + "f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e]\n" + "g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]\n" + + "h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g]\n" + "i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h]"; + + public static final String scalarAliasesData = + "a: &a foo\n" + "b: *a\n" + "c: *a\n" + "d: *a\n" + "e: *a\n" + "f: *a\n" + "g: *a\n"; + + @Test + public void billionLaughsAttackLoaded() { + LoaderOptions settings = new LoaderOptions(); + settings.setMaxAliasesForCollections(72); + Yaml yaml = new Yaml(settings); + Map map = yaml.load(data); + assertNotNull(map); + } + + @Test + public void billionLaughsAttackExpanded() { + LoaderOptions settings = new LoaderOptions(); + settings.setMaxAliasesForCollections(100); + Yaml yaml = new Yaml(settings); + Map map = yaml.load(data); + assertNotNull(map); + try { + map.toString(); + fail("Expected overflow"); + } catch (Throwable e) { + assertTrue("Catched exception " + e + " is not an instance of OutOfMemoryError", + e instanceof OutOfMemoryError); + } + } + + @Test + public void billionLaughsAttackWithRestrictedAliases() { + LoaderOptions settings = new LoaderOptions(); + Yaml yaml = new Yaml(settings); + try { + yaml.load(data); + fail(); + } catch (YAMLException e) { + assertEquals("Number of aliases for non-scalar nodes exceeds the specified max=50", + e.getMessage()); + } + } + + @Test + public void doNotRestrictScalarAliases() { + LoaderOptions settings = new LoaderOptions(); + settings.setMaxAliasesForCollections(5); // smaller than number of aliases for scalars + Yaml yaml = new Yaml(settings); + Map map = yaml.load(scalarAliasesData); + assertNotNull(map); + } + +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue377/ReferencesTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue377/ReferencesTest.java new file mode 100644 index 00000000..cb17cb8a --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue377/ReferencesTest.java @@ -0,0 +1,131 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue377; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.HashMap; +import org.junit.Test; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.SafeConstructor; + +public class ReferencesTest { + + /** + * Create data which is difficult to parse. + * + * @param size - size of the map, defines the complexity + * @return YAML to parse + */ + private String createDump(int size) { + HashMap root = new HashMap(); + HashMap s1, s2, t1, t2; + s1 = root; + s2 = new HashMap(); + /* + * the time to parse grows very quickly SIZE -> time to parse in seconds 25 -> 1 26 -> 2 27 -> 3 + * 28 -> 8 29 -> 13 30 -> 28 31 -> 52 32 -> 113 33 -> 245 34 -> 500 + */ + for (int i = 0; i < size; i++) { + + t1 = new HashMap(); + t2 = new HashMap(); + t1.put("foo", "1"); + t2.put("bar", "2"); + + s1.put("a", t1); + s1.put("b", t2); + s2.put("a", t1); + s2.put("b", t2); + + s1 = t1; + s2 = t2; + } + + // this is VERY BAD code + // the map has itself as a key (no idea why it may be used except of a DoS attack) + HashMap f = new HashMap(); + f.put(f, "a"); + f.put("g", root); + + Yaml yaml = new Yaml(new SafeConstructor()); + String output = yaml.dump(f); + return output; + } + + @Test + public void referencesWithRecursiveKeysNotAllowedByDefault() { + String output = createDump(30); + // System.out.println(output); + long time1 = System.currentTimeMillis(); + // Load + LoaderOptions settings = new LoaderOptions(); + settings.setMaxAliasesForCollections(150); + Yaml yaml = new Yaml(settings); + try { + yaml.load(output); + fail(); + } catch (Exception e) { + assertEquals("Recursive key for mapping is detected but it is not configured to be allowed.", + e.getMessage()); + } + long time2 = System.currentTimeMillis(); + float duration = (time2 - time1) / 1000; + assertTrue("It should fail quickly. Time was " + duration + " seconds.", duration < 1.0); + } + + @Test + public void parseManyAliasesForCollections() { + String output = createDump(25); + // Load + // long time1 = System.currentTimeMillis(); + LoaderOptions settings = new LoaderOptions(); + settings.setMaxAliasesForCollections(50); + settings.setAllowRecursiveKeys(true); + Yaml yaml = new Yaml(settings); + yaml.load(output); + // Disabling this as it runs slower than 0.9 on my machine + // long time2 = System.currentTimeMillis(); + // double duration = (time2 - time1) / 1000.0; + // assertTrue("It should take time. Time was " + duration + " seconds.", duration > 0.9); + // assertTrue("Time was " + duration + " seconds.", duration < 5.0); + } + + @Test + public void referencesWithRestrictedNesting() { + // without alias restriction this size should occupy tons of CPU, memory and time to parse + int depth = 35; + String bigYAML = createDump(depth); + // Load + long time1 = System.currentTimeMillis(); + LoaderOptions settings = new LoaderOptions(); + settings.setMaxAliasesForCollections(1000); + settings.setAllowRecursiveKeys(true); + settings.setNestingDepthLimit(depth); + Yaml yaml = new Yaml(settings); + try { + yaml.load(bigYAML); + fail(); + } catch (Exception e) { + assertEquals("Nesting Depth exceeded max 35", e.getMessage()); + } + long time2 = System.currentTimeMillis(); + float duration = (time2 - time1) / 1000; + assertTrue("It should fail quickly. Time was " + duration + " seconds.", duration < 1.0); + } + +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue377/ReferencesWithListsTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue377/ReferencesWithListsTest.java new file mode 100644 index 00000000..d1b9dd40 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue377/ReferencesWithListsTest.java @@ -0,0 +1,73 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue377; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.List; +import java.util.Map; +import org.junit.Test; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Yaml; + +/** + * Recursive keys setting has no influence on sequences + */ +public class ReferencesWithListsTest { + + @Test + public void referencesWithRecursiveKeysInListAllowedByDefault() { + String output = "a: &test\n" + "- *test"; + LoaderOptions settings = new LoaderOptions(); + settings.setAllowRecursiveKeys(true); + Yaml yaml = new Yaml(settings); + // System.out.println(output); + Map<String, Object> parsed = yaml.load(output); + assertNotNull(output, parsed); + assertEquals(1, parsed.size()); + } + + @Test + public void referencesWithRecursiveSequences() { + String output = "&test\n" + "- *test"; + LoaderOptions settings = new LoaderOptions(); + settings.setAllowRecursiveKeys(true); + Yaml yaml = new Yaml(settings); + // System.out.println(output); + List<Object> parsed = yaml.load(output); + assertNotNull(output, parsed); + assertEquals(1, parsed.size()); + } + + @Test + public void referencesWithRecursiveKeysInList() { + String output = "a: &test\n" + "- *test"; + LoaderOptions settings = new LoaderOptions(); + settings.setAllowRecursiveKeys(false); + Yaml yaml = new Yaml(settings); + // System.out.println(output); + Map<String, Object> parsed = yaml.load(output); + assertNotNull(output, parsed); + assertEquals(1, parsed.size()); + // try { + // yaml.load(output); + // fail("Should not have been reached, expected an exception because recursive keys are not + // allowed."); + // } catch (Exception e) { + // assertEquals("Recursive key for mapping is detected but it is not configured to be allowed.", + // e.getMessage()); + // } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue38/Bean.java b/src/test/java/org/yaml/snakeyaml/issues/issue38/Bean.java index d644561e..1ac04482 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue38/Bean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue38/Bean.java @@ -1,50 +1,48 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue38; public class Bean { - int value; - - public Bean() { - } - - public Bean(int value) { - this.value = value; - } - - @Override - public boolean equals(Object obj) { - return toString().equals(obj.toString()); - } - - @Override - public int hashCode() { - return value; - } - - @Override - public String toString() { - return "Bean " + String.valueOf(value); - } - - public int getValue() { - return value; - } - - public void setValue(int value) { - this.value = value; - } -}
\ No newline at end of file + + int value; + + public Bean() {} + + public Bean(int value) { + this.value = value; + } + + @Override + public boolean equals(Object obj) { + return toString().equals(obj.toString()); + } + + @Override + public int hashCode() { + return value; + } + + @Override + public String toString() { + return "Bean " + value; + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue38/BigNumberIdTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue38/BigNumberIdTest.java index d80168ac..22ac5989 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue38/BigNumberIdTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue38/BigNumberIdTest.java @@ -1,46 +1,46 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue38; import java.util.ArrayList; import java.util.List; - import junit.framework.TestCase; - +import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; /** * to test http://code.google.com/p/snakeyaml/issues/detail?id=38 */ public class BigNumberIdTest extends TestCase { - @SuppressWarnings("unchecked") - public void testBigNumberFormat() { - List<Bean> list = new ArrayList<Bean>(2000); - for (int i = 1; i < 1010; i++) { - Bean value = new Bean(i); - list.add(value); - list.add(value); - } - Yaml yaml = new Yaml(); - String output = yaml.dump(list); - // System.out.println(output); - // - List<Bean> list2 = (List<Bean>) yaml.load(output); - for (Bean bean : list2) { - assertTrue(bean.getValue() > 0); - } + + @SuppressWarnings("unchecked") + public void testBigNumberFormat() { + List<Bean> list = new ArrayList<Bean>(2000); + for (int i = 1; i < 1010; i++) { + Bean value = new Bean(i); + list.add(value); + list.add(value); + } + LoaderOptions options = new LoaderOptions(); + options.setMaxAliasesForCollections(1500); + Yaml yaml = new Yaml(options); + String output = yaml.dump(list); + // System.out.println(output); + // + List<Bean> list2 = yaml.load(output); + for (Bean bean : list2) { + assertTrue(bean.getValue() > 0); } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue383/RepresenterConfigurationTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue383/RepresenterConfigurationTest.java new file mode 100644 index 00000000..a30ae49c --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue383/RepresenterConfigurationTest.java @@ -0,0 +1,197 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue383; + +import static org.yaml.snakeyaml.DumperOptions.FlowStyle.BLOCK; +import static org.yaml.snakeyaml.DumperOptions.FlowStyle.FLOW; +import static org.yaml.snakeyaml.DumperOptions.ScalarStyle.FOLDED; +import static org.yaml.snakeyaml.DumperOptions.ScalarStyle.PLAIN; + +import java.util.Calendar; +import java.util.TimeZone; +import org.junit.Assert; +import org.junit.Test; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.introspector.PropertyUtils; +import org.yaml.snakeyaml.nodes.MappingNode; +import org.yaml.snakeyaml.nodes.ScalarNode; +import org.yaml.snakeyaml.representer.Representer; + +public class RepresenterConfigurationTest { + + @Test + public void testDefaultFlowStyleNotOverridden() { + Representer representer = new Representer(); + representer.setDefaultFlowStyle(BLOCK); + Yaml yaml = new Yaml(representer); + + MappingNode node = (MappingNode) yaml.represent(new TestObject(27, "test")); + Assert.assertEquals(DumperOptions.FlowStyle.BLOCK, node.getFlowStyle()); + } + + @Test + public void testDefaultFlowStyleIsOverridden() { + Representer representer = new Representer(); + representer.setDefaultFlowStyle(BLOCK); + + DumperOptions dumperOptions = new DumperOptions(); + dumperOptions.setDefaultFlowStyle(FLOW); + + Yaml yaml = new Yaml(representer, dumperOptions); + + MappingNode node = (MappingNode) yaml.represent(new TestObject(27, "test")); + Assert.assertEquals(DumperOptions.FlowStyle.FLOW, node.getFlowStyle()); + } + + @Test + public void testDefaultScalarStyleNotOverridden() { + Representer representer = new Representer(); + representer.setDefaultScalarStyle(FOLDED); + + Yaml yaml = new Yaml(representer); + + ScalarNode node = (ScalarNode) yaml.represent("test"); + Assert.assertEquals(FOLDED, node.getScalarStyle()); + Assert.assertEquals(FOLDED.getChar(), node.getStyle()); + } + + @Test + public void testDefaultScalarStyleOverridden() { + Representer representer = new Representer(); + representer.setDefaultScalarStyle(FOLDED); + + DumperOptions dumperOptions = new DumperOptions(); + dumperOptions.setDefaultScalarStyle(PLAIN); + + Yaml yaml = new Yaml(representer, dumperOptions); + + ScalarNode node = (ScalarNode) yaml.represent("test"); + Assert.assertEquals(node.getScalarStyle(), PLAIN); + Assert.assertEquals(node.getStyle(), PLAIN.getChar()); + } + + @Test + public void testPlainStyleByDefault() { + Yaml yaml = new Yaml(); + ScalarNode node = (ScalarNode) yaml.represent("test"); + Assert.assertEquals(PLAIN, node.getScalarStyle()); + Assert.assertEquals(PLAIN.getChar(), node.getStyle()); + } + + @Test + public void testTimeZoneNotOverridden() { + Representer representer = new Representer(); + representer.setTimeZone(TimeZone.getTimeZone("Europe/Kiev")); + + Yaml yaml = new Yaml(representer); + + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(0); + + ScalarNode node = (ScalarNode) yaml.represent(calendar.getTime()); + + Assert.assertEquals("1970-01-01T03:00:00+03:00", node.getValue()); + } + + @Test + public void testTimeZoneOverridden() { + Representer representer = new Representer(); + representer.setTimeZone(TimeZone.getTimeZone("Europe/Kiev")); + + DumperOptions dumperOptions = new DumperOptions(); + dumperOptions.setTimeZone(TimeZone.getTimeZone("UTC")); + + Yaml yaml = new Yaml(representer, dumperOptions); + + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(0); + + ScalarNode node = (ScalarNode) yaml.represent(calendar.getTime()); + + Assert.assertEquals("1970-01-01T00:00:00Z", node.getValue()); + } + + @Test + public void testDefaultTimeZone() { + Yaml yaml = new Yaml(); + + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(0); + + ScalarNode node = (ScalarNode) yaml.represent(calendar.getTime()); + + Assert.assertEquals("1970-01-01T00:00:00Z", node.getValue()); + } + + @Test + public void testAllowReadOnlyPropertiesNotOverridden() { + PropertyUtils propertyUtils = new PropertyUtils(); + propertyUtils.setAllowReadOnlyProperties(true); + Representer representer = new Representer(); + representer.setPropertyUtils(propertyUtils); + + Yaml yaml = new Yaml(representer); + MappingNode mappingNode = (MappingNode) yaml.represent(new TestObject(27, "test")); + Assert.assertEquals(2, mappingNode.getValue().size()); + } + + @Test + public void testAllowReadOnlyPropertiesOverridden() { + PropertyUtils propertyUtils = new PropertyUtils(); + propertyUtils.setAllowReadOnlyProperties(true); + Representer representer = new Representer(); + representer.setPropertyUtils(propertyUtils); + + DumperOptions dumperOptions = new DumperOptions(); + dumperOptions.setAllowReadOnlyProperties(false); + + Yaml yaml = new Yaml(representer, dumperOptions); + MappingNode mappingNode = (MappingNode) yaml.represent(new TestObject(27, "test")); + Assert.assertEquals(1, mappingNode.getValue().size()); + } + + @Test + public void testReadOnlyPropertiesNotAllowedByDefault() { + Yaml yaml = new Yaml(); + MappingNode mappingNode = (MappingNode) yaml.represent(new TestObject(27, "test")); + Assert.assertEquals(1, mappingNode.getValue().size()); + } + + + public static class TestObject { + + private int id; + + private final String value; + + public TestObject(int id, String value) { + this.id = id; + this.value = value; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getValue() { + return value; + } + } + +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue386/DuplicateKeysOrderTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue386/DuplicateKeysOrderTest.java new file mode 100644 index 00000000..a791f76b --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue386/DuplicateKeysOrderTest.java @@ -0,0 +1,42 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue386; + +import static org.hamcrest.CoreMatchers.hasItems; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.util.Map; +import org.junit.Test; +import org.yaml.snakeyaml.Util; +import org.yaml.snakeyaml.Yaml; + +public class DuplicateKeysOrderTest { + + @Test + public void deleteDuplicatKeysInCorrectOrder() { + String input = Util.getLocalResource("issues/issue386-duplicate-keys-order.yaml"); + Yaml yaml = new Yaml(); + Map<String, String> testMap = yaml.load(input); + + assertThat("Number of keys in map", testMap.size(), is(4)); + assertThat(testMap.keySet(), hasItems("key1", "key2", "key3", "lostone")); + + assertThat(testMap.get("key1"), is("1st-2")); + assertThat(testMap.get("key2"), is("2nd-2")); + assertThat(testMap.get("key3"), is("3rd-3")); + assertThat(testMap.get("lostone"), is("Not meeee!!!")); + } + +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue387/YamlExecuteProcessContextTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue387/YamlExecuteProcessContextTest.java new file mode 100644 index 00000000..c4bd63d5 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue387/YamlExecuteProcessContextTest.java @@ -0,0 +1,72 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue387; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; + +import java.util.Collection; +import java.util.Set; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.junit.Test; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; + +public class YamlExecuteProcessContextTest { + + public enum ExecuteProcessConstants { + EXECUTE_ID, EXECUTE_STATUS_START, EXECUTE_STATUS_DONE + } + + @NoArgsConstructor + @AllArgsConstructor + @Data + public static class YamlExecuteProcessContext { + + private String executionID; + private String schemaName; + private String username; + private String hostname; + private String sql; + private Collection<YamlExecuteProcessUnit> unitStatuses; + private Long startTimeMillis; + } + + @NoArgsConstructor + @AllArgsConstructor + @Data + public static class YamlExecuteProcessUnit { + + private String unitID; + private ExecuteProcessConstants status; + } + + @Test + public void parameterizedCollectionTest() { + String marshal = "unitStatuses: !!set\n" + " ? status: EXECUTE_STATUS_DONE\n" + + " unitID: '159917166'\n" + " : null\n"; + Constructor constructor = new Constructor(YamlExecuteProcessContext.class); + YamlExecuteProcessContext unmarshal = + new Yaml(constructor).loadAs(marshal, YamlExecuteProcessContext.class); + + assertThat(unmarshal.getUnitStatuses(), instanceOf(Set.class)); + + for (YamlExecuteProcessUnit unit : unmarshal.getUnitStatuses()) { + assertEquals(unit.getStatus(), ExecuteProcessConstants.EXECUTE_STATUS_DONE); + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue395/ReportPositionForDuplicateKeyTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue395/ReportPositionForDuplicateKeyTest.java new file mode 100644 index 00000000..f62eaff0 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue395/ReportPositionForDuplicateKeyTest.java @@ -0,0 +1,42 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue395; + +import static junit.framework.TestCase.assertTrue; + +import org.junit.Test; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; +import org.yaml.snakeyaml.constructor.DuplicateKeyException; +import org.yaml.snakeyaml.representer.Representer; +import org.yaml.snakeyaml.resolver.Resolver; + +public class ReportPositionForDuplicateKeyTest { + + @Test + public void deleteDuplicatKeysInCorrectOrder() { + LoaderOptions loaderOptions = new LoaderOptions(); + loaderOptions.setAllowDuplicateKeys(false); + Yaml yaml = new Yaml(new Constructor(), new Representer(), new DumperOptions(), loaderOptions, + new Resolver()); + try { + yaml.load("key1: a\nkey1: b"); + } catch (DuplicateKeyException e) { + assertTrue(e.getMessage(), e.getMessage().contains("found duplicate key key1")); + assertTrue(e.getMessage(), e.getMessage().contains("line 1, column 1")); + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue397/ColonInFlowContextInListTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue397/ColonInFlowContextInListTest.java new file mode 100644 index 00000000..9071f903 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue397/ColonInFlowContextInListTest.java @@ -0,0 +1,39 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue397; + +import java.util.List; +import junit.framework.TestCase; +import org.yaml.snakeyaml.Yaml; + +public class ColonInFlowContextInListTest extends TestCase { + + private final Yaml loader = new Yaml(); + + public void testList() { + List<String> list = loader.load("[ http://foo ]"); + assertTrue(list.contains("http://foo")); + } + + public void testListNoSpaces() { + List<String> list = loader.load("[http://foo]"); + assertTrue(list.contains("http://foo")); + } + + public void testList2() { + List<String> list = loader.load("[ http://foo,http://bar ]"); + assertTrue(list.contains("http://foo")); + assertTrue(list.contains("http://bar")); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue397/ColonInFlowContextInMapTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue397/ColonInFlowContextInMapTest.java new file mode 100644 index 00000000..568f6ffd --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue397/ColonInFlowContextInMapTest.java @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue397; + +import java.util.Map; +import junit.framework.TestCase; +import org.yaml.snakeyaml.Yaml; + +public class ColonInFlowContextInMapTest extends TestCase { + + private final Yaml loader = new Yaml(); + + public void test1() { + Map<String, Integer> map = loader.load("{a: 1}"); + assertEquals(Integer.valueOf(1), map.get("a")); + } + + public void test2() { + Map<String, Integer> map = loader.load("{a:}"); + assertTrue(map.containsKey("a")); + } + + public void test3() { + Map<String, Integer> map = loader.load("{a}"); + assertTrue(map.containsKey("a")); + } + + public void testTheOnlyCounterIntuitiveCase() { + Map<String, Integer> map = loader.load("{a:1}"); + assertTrue(map.containsKey("a:1")); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue397/ColonInFlowContextInSetTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue397/ColonInFlowContextInSetTest.java new file mode 100644 index 00000000..acc12f75 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue397/ColonInFlowContextInSetTest.java @@ -0,0 +1,39 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue397; + +import java.util.Set; +import junit.framework.TestCase; +import org.yaml.snakeyaml.Yaml; + +public class ColonInFlowContextInSetTest extends TestCase { + + private final Yaml loader = new Yaml(); + + public void testSet() { + Set<String> set = loader.load("!!set { http://foo }"); + assertTrue(set.contains("http://foo")); + } + + public void testSetNoSpaces() { + Set<String> set = loader.load("!!set {http://foo}"); + assertTrue(set.contains("http://foo")); + } + + public void testSet2() { + Set<String> set = loader.load("!!set { http://foo,http://bar }"); + assertTrue(set.contains("http://foo")); + assertTrue(set.contains("http://bar")); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue40/DogFoodBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue40/DogFoodBean.java index e0c7dabc..94956ae9 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue40/DogFoodBean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue40/DogFoodBean.java @@ -1,34 +1,33 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue40; import java.math.BigDecimal; public class DogFoodBean { - BigDecimal decimal; - public DogFoodBean() { - decimal = BigDecimal.ZERO; - } + BigDecimal decimal; - public BigDecimal getDecimal() { - return decimal; - } + public DogFoodBean() { + decimal = BigDecimal.ZERO; + } - public void setDecimal(BigDecimal decimal) { - this.decimal = decimal; - } + public BigDecimal getDecimal() { + return decimal; + } + + public void setDecimal(BigDecimal decimal) { + this.decimal = decimal; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue40/DogFoodBeanTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue40/DogFoodBeanTest.java index f0891e15..650cad4b 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue40/DogFoodBeanTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue40/DogFoodBeanTest.java @@ -1,72 +1,67 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue40; import java.math.BigDecimal; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class DogFoodBeanTest extends TestCase { - public void testOwnBigDecimal() { - DogFoodBean input = new DogFoodBean(); - input.setDecimal(new BigDecimal("5")); - Yaml yaml = new Yaml(); - String text = yaml.dump(input); - // System.out.println(text); - assertEquals("!!org.yaml.snakeyaml.issues.issue40.DogFoodBean {decimal: !!float '5'}\n", - text); - DogFoodBean output = (DogFoodBean) yaml.load(text); - assertEquals(output.getDecimal(), input.getDecimal()); - } + public void testOwnBigDecimal() { + DogFoodBean input = new DogFoodBean(); + input.setDecimal(new BigDecimal("5")); + Yaml yaml = new Yaml(); + String text = yaml.dump(input); + // System.out.println(text); + assertEquals("!!org.yaml.snakeyaml.issues.issue40.DogFoodBean {decimal: !!float '5'}\n", text); + DogFoodBean output = yaml.load(text); + assertEquals(output.getDecimal(), input.getDecimal()); + } - public void testBigDecimalPrecision() { - DogFoodBean input = new DogFoodBean(); - input.setDecimal(new BigDecimal("5.123")); - Yaml yaml = new Yaml(); - String text = yaml.dump(input); - // System.out.println(text); - assertEquals("!!org.yaml.snakeyaml.issues.issue40.DogFoodBean {decimal: 5.123}\n", text); - DogFoodBean output = (DogFoodBean) yaml.load(text); - assertEquals(input.getDecimal(), output.getDecimal()); - } + public void testBigDecimalPrecision() { + DogFoodBean input = new DogFoodBean(); + input.setDecimal(new BigDecimal("5.123")); + Yaml yaml = new Yaml(); + String text = yaml.dump(input); + // System.out.println(text); + assertEquals("!!org.yaml.snakeyaml.issues.issue40.DogFoodBean {decimal: 5.123}\n", text); + DogFoodBean output = yaml.load(text); + assertEquals(input.getDecimal(), output.getDecimal()); + } - public void testBigDecimalNoRootTag() { - DogFoodBean input = new DogFoodBean(); - input.setDecimal(new BigDecimal("5.123")); - Yaml yaml = new Yaml(); - String text = yaml.dumpAsMap(input); - // System.out.println(text); - assertEquals("decimal: 5.123\n", text); - Yaml loader = new Yaml(); - DogFoodBean output = loader.loadAs(text, DogFoodBean.class); - assertEquals(input.getDecimal(), output.getDecimal()); - } + public void testBigDecimalNoRootTag() { + DogFoodBean input = new DogFoodBean(); + input.setDecimal(new BigDecimal("5.123")); + Yaml yaml = new Yaml(); + String text = yaml.dumpAsMap(input); + // System.out.println(text); + assertEquals("decimal: 5.123\n", text); + Yaml loader = new Yaml(); + DogFoodBean output = loader.loadAs(text, DogFoodBean.class); + assertEquals(input.getDecimal(), output.getDecimal()); + } - public void testBigDecimal1() { - Yaml yaml = new Yaml(); - String text = yaml.dump(new BigDecimal("5")); - assertEquals("!!float '5'\n", text); - } + public void testBigDecimal1() { + Yaml yaml = new Yaml(); + String text = yaml.dump(new BigDecimal("5")); + assertEquals("!!float '5'\n", text); + } - public void testBigDecimal2() { - Yaml yaml = new Yaml(); - String text = yaml.dump(new BigDecimal("5.123")); - assertEquals("5.123\n", text); - } + public void testBigDecimal2() { + Yaml yaml = new Yaml(); + String text = yaml.dump(new BigDecimal("5.123")); + assertEquals("5.123\n", text); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue409/DumpEnumAsJavabeanPropertyTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue409/DumpEnumAsJavabeanPropertyTest.java new file mode 100644 index 00000000..05819395 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue409/DumpEnumAsJavabeanPropertyTest.java @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue409; + +import junit.framework.TestCase; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.nodes.Tag; + +public class DumpEnumAsJavabeanPropertyTest extends TestCase { + + public static class Bean { + + public ExtendedEnum myEnum = ExtendedEnum.B; + } + + public enum ExtendedEnum { + A { + public String toGreek() { + return "alpha"; + } + }, + B { + public String toGreek() { + return "beta"; + } + } // issue 409 + } + + public void testDumpExtendedEnum() { + Yaml yaml = new Yaml(); + String text = yaml.dumpAs(new Bean(), Tag.MAP, DumperOptions.FlowStyle.AUTO); + assertEquals("{myEnum: B}\n", text); + Bean actual = yaml.loadAs(text, Bean.class); + assertEquals(ExtendedEnum.B, actual.myEnum); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue416/IndentWithIndicatorTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue416/IndentWithIndicatorTest.java new file mode 100644 index 00000000..5f4769bb --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue416/IndentWithIndicatorTest.java @@ -0,0 +1,95 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue416; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import junit.framework.TestCase; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Util; +import org.yaml.snakeyaml.Yaml; + +public class IndentWithIndicatorTest extends TestCase { + + public void testIndentWithIndicator1() { + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + options.setIndentWithIndicator(true); + options.setIndent(2); + options.setIndicatorIndent(1); + + Yaml yaml = new Yaml(options); + String output = yaml.dump(createData()); + + String doc = Util.getLocalResource("issues/issue416-1.yaml"); + + assertEquals(doc, output); + } + + public void testIndentWithIndicator2() { + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + options.setIndentWithIndicator(true); + options.setIndent(2); + options.setIndicatorIndent(2); + + Yaml yaml = new Yaml(options); + String output = yaml.dump(createData()); + + String doc = Util.getLocalResource("issues/issue416-2.yaml"); + + assertEquals(doc, output); + } + + public void testIndentWithIndicator3() { + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + options.setIndentWithIndicator(false); + options.setIndent(4); + options.setIndicatorIndent(2); + + Yaml yaml = new Yaml(options); + String output = yaml.dump(createData()); + + String doc = Util.getLocalResource("issues/issue416_3.yml"); + + assertEquals(doc, output); + } + + private Map<String, Object> createData() { + Map<String, String> fred = new LinkedHashMap<>(); + fred.put("name", "Fred"); + fred.put("role", "creator"); + + Map<String, String> john = new LinkedHashMap<>(); + john.put("name", "John"); + john.put("role", "committer"); + + List<Map<String, String>> developers = new ArrayList<>(); + developers.add(fred); + developers.add(john); + + Map<String, Object> company = new LinkedHashMap<>(); + company.put("developers", developers); + company.put("name", "Yet Another Company"); + company.put("location", "Maastricht"); + + Map<String, Object> data = new LinkedHashMap<>(); + data.put("company", company); + + return data; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue423/DaylightSavingsTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue423/DaylightSavingsTest.java new file mode 100644 index 00000000..6eb68dfd --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue423/DaylightSavingsTest.java @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue423; + +import java.util.Calendar; +import java.util.TimeZone; +import junit.framework.TestCase; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; + +public class DaylightSavingsTest extends TestCase { + + public void testDaylightSavings20181104() { + + // Daylight savings ends on 2018-11-04 and 2 am America/Denver Timezone falls back from -06:00 + // to -07:00 + + TimeZone denverTimeZone = TimeZone.getTimeZone("America/Denver"); + + DumperOptions dumperOptions = new DumperOptions(); + dumperOptions.setTimeZone(TimeZone.getTimeZone("America/Denver")); + + Yaml yaml = new Yaml(dumperOptions); + + Calendar midnightBeforeFallback = Calendar.getInstance(denverTimeZone); + midnightBeforeFallback.set(2018, Calendar.NOVEMBER, 4, 0, 0); + + String dateString = yaml.dump(midnightBeforeFallback).trim(); // Trim to remove the new line + // character + + assertTrue("Timezone value should be -06:00", dateString.endsWith("-06:00")); + + Calendar oneHourAfterFallback = Calendar.getInstance(denverTimeZone); + oneHourAfterFallback.set(2018, Calendar.NOVEMBER, 4, 3, 0); + + dateString = yaml.dump(oneHourAfterFallback).trim(); + + assertTrue("Timezone value should be -07:00", dateString.endsWith("-07:00")); + + } + +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue426/NullDuplicateKeyTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue426/NullDuplicateKeyTest.java new file mode 100644 index 00000000..fe1d30a2 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue426/NullDuplicateKeyTest.java @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue426; + +import java.util.Map; +import junit.framework.TestCase; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.DuplicateKeyException; + +public class NullDuplicateKeyTest extends TestCase { + + public void testDuplicateKeyIsAllowed() { + Yaml loader = new Yaml(); + Map<String, String> duplicateMap = loader.load("~: foo\n" + "~: bar"); + assertEquals(1, duplicateMap.size()); + assertEquals("bar", duplicateMap.get(null)); + } + + public void testDuplicateKeyIsNotAlowed() { + LoaderOptions options = new LoaderOptions(); + options.setAllowDuplicateKeys(false); + Yaml loader = new Yaml(options); + try { + loader.load("~: foo\n~: bar"); + } catch (DuplicateKeyException e) { + assertTrue(e.getMessage(), e.getMessage().contains("found duplicate key null")); + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue431/FlexSimleKeyTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue431/FlexSimleKeyTest.java new file mode 100644 index 00000000..918361e9 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue431/FlexSimleKeyTest.java @@ -0,0 +1,79 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue431; + +import java.util.HashMap; +import java.util.Map; +import junit.framework.TestCase; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.error.YAMLException; + +public class FlexSimleKeyTest extends TestCase { + + private final int len = 130; + + public void testLongKey() { + Yaml dumper = new Yaml(createOptions(len)); + Map<String, Object> root = new HashMap(); + Map<String, String> map = new HashMap<>(); + String key = createKey(len); + map.put(key, "v1"); + root.put("data", map); + assertEquals("data: {? '" + key + "'\n : v1}\n", dumper.dump(root)); + } + + public void testForceLongKeyToBeImplicit() { + Yaml dumper = new Yaml(createOptions(len + 10)); + Map<String, Object> root = new HashMap(); + Map<String, String> map = new HashMap<>(); + String key = createKey(len); + map.put(key, "v1"); + root.put("data", map); + assertEquals("data: {'" + key + "': v1}\n", dumper.dump(root)); + } + + public void testTooLongKeyLength() { + try { + createOptions(1024 + 1); + fail("Length must be restricted to 1024 chars"); + } catch (YAMLException e) { + assertEquals( + "The simple key must not span more than 1024 stream characters. See https://yaml.org/spec/1.1/#id934537", + e.getMessage()); + } + } + + private DumperOptions createOptions(int len) { + DumperOptions dumperOptions = new DumperOptions(); + dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.AUTO); + dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN); + dumperOptions.setMaxSimpleKeyLength(len); + return dumperOptions; + } + + private String createKey(int length) { + StringBuffer outputBuffer = new StringBuffer(length); + for (int i = 0; i < length; i++) { + outputBuffer.append("" + (i + 1) % 10); + } + String prefix = String.valueOf(length); + String result = + prefix + "_" + outputBuffer.toString().substring(0, length - prefix.length() - 1); + if (result.length() != length) { + throw new RuntimeException("It was: " + result.length()); + } + return result; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue437/BinaryRoundTripTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue437/BinaryRoundTripTest.java new file mode 100644 index 00000000..87786066 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue437/BinaryRoundTripTest.java @@ -0,0 +1,89 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue437; + +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import junit.framework.TestCase; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.events.Event; +import org.yaml.snakeyaml.events.ImplicitTuple; +import org.yaml.snakeyaml.events.ScalarEvent; +import org.yaml.snakeyaml.nodes.Node; +import org.yaml.snakeyaml.nodes.NodeId; +import org.yaml.snakeyaml.nodes.ScalarNode; +import org.yaml.snakeyaml.nodes.Tag; + +public class BinaryRoundTripTest extends TestCase { + + public void testBinary() throws UnsupportedEncodingException { + Yaml underTest = new Yaml(); + String source = "\u0096"; + String serialized = underTest.dump(source); + assertEquals("!!binary |-\n" + " wpY=\n", serialized); + // parse back to bytes + byte[] deserialized = underTest.load(serialized); + assertEquals(source, new String(deserialized, StandardCharsets.UTF_8)); + } + + public void testBinaryNode() { + Yaml underTest = new Yaml(); + String source = "\u0096"; + Node node = underTest.represent(source); + // check Node + assertEquals(Tag.BINARY, node.getTag()); + assertEquals(NodeId.scalar, node.getNodeId()); + ScalarNode scalar = (ScalarNode) node; + assertEquals("wpY=", scalar.getValue()); + // check Event + List<Event> events = underTest.serialize(node); + assertEquals(5, events.size()); + ScalarEvent data = (ScalarEvent) events.get(2); + assertEquals(Tag.BINARY.toString(), data.getTag()); + assertEquals(DumperOptions.ScalarStyle.LITERAL, data.getScalarStyle()); + assertEquals("wpY=", data.getValue()); + ImplicitTuple implicit = data.getImplicit(); + assertFalse(implicit.canOmitTagInPlainScalar()); + assertFalse(implicit.canOmitTagInNonPlainScalar()); + } + + public void testStrNode() { + DumperOptions options = new DumperOptions(); + options.setNonPrintableStyle(DumperOptions.NonPrintableStyle.ESCAPE); + Yaml underTest = new Yaml(options); + String source = "\u0096"; + Node node = underTest.represent(source); + assertEquals(Tag.STR, node.getTag()); + assertEquals(NodeId.scalar, node.getNodeId()); + ScalarNode scalar = (ScalarNode) node; + assertEquals("\u0096", scalar.getValue()); + } + + public void testRoundTripBinary() { + DumperOptions options = new DumperOptions(); + options.setNonPrintableStyle(DumperOptions.NonPrintableStyle.ESCAPE); + Yaml underTest = new Yaml(options); + Map<String, String> toSerialized = new HashMap<>(); + toSerialized.put("key", "a\u0096b"); + String output = underTest.dump(toSerialized); + assertEquals("{key: \"a\\x96b\"}\n", output); + Map<String, String> parsed = underTest.load(output); + assertEquals(toSerialized.get("key"), parsed.get("key")); + assertEquals(toSerialized, parsed); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue440/LiteralWithTrailingSpacesTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue440/LiteralWithTrailingSpacesTest.java new file mode 100644 index 00000000..0f5386e3 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue440/LiteralWithTrailingSpacesTest.java @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue440; + +import junit.framework.TestCase; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; + +public class LiteralWithTrailingSpacesTest extends TestCase { + + public void testLiteral() { + DumperOptions options = new DumperOptions(); + options.setDefaultScalarStyle(DumperOptions.ScalarStyle.LITERAL); + Yaml yaml = new Yaml(options); + String correct = "this is some text with \"quotes\" and\nline breaks"; + String correctYaml = yaml.dump(correct); + assertEquals("|-\n" + " this is some text with \"quotes\" and\n" + " line breaks\n", + correctYaml); + } + + public void testTrimTrailingWhiteSpace() { + Yaml yaml = new Yaml(); + assertEquals("trailing", yaml.load("trailing ")); + assertEquals("trailing", yaml.load("trailing\r")); + assertEquals("trailing", yaml.load("trailing\n")); + assertEquals("trailing", yaml.load("trailing\r\n")); + assertEquals("trailing", yaml.load("trailing\t\n")); + assertEquals("trailing", yaml.load("trailing\n\n")); + assertEquals("trailing", yaml.load("trailing\n \r \n")); + } + + public void testLiteralWithNewLine() { + DumperOptions options = new DumperOptions(); + options.setDefaultScalarStyle(DumperOptions.ScalarStyle.LITERAL); + Yaml yaml = new Yaml(options); + String inCorrect = "this is some text with \"quotes\" and \nline breaks"; + String inCorrectYaml = yaml.dump(inCorrect); + // System.out.println(inCorrectYaml); + assertEquals("\"this is some text with \\\"quotes\\\" and \\nline breaks\"\n", inCorrectYaml); + // TODO assertTrue(inCorrectYaml.contains("|")); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue449/LeadingZeroStringTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue449/LeadingZeroStringTest.java new file mode 100644 index 00000000..5eeec69f --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue449/LeadingZeroStringTest.java @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue449; + +import java.util.regex.Pattern; +import junit.framework.TestCase; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.resolver.Resolver; + +public class LeadingZeroStringTest extends TestCase { + + public void testString() { + Yaml loader = new Yaml(); + // this almost looks like an octal, but it contains digits greater than 7, so it's a string + Object result = loader.load("0123456789"); + assertEquals("0123456789", result); + } + + public void testLeadingZeroForIntIsAccepted() { + Pattern regexp = Resolver.INT; + assertTrue("Valid octal must be recognised.", regexp.matcher("07").matches()); + } + + public void testOctalNumberCannotHave8() { + Pattern regexp = Resolver.INT; + assertFalse(regexp.matcher("08").matches()); + assertFalse(regexp.matcher("0123456789").matches()); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue46/FileTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue46/FileTest.java index f80f3ad8..daef2174 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue46/FileTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue46/FileTest.java @@ -1,26 +1,22 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue46; import java.io.File; import java.util.HashMap; import java.util.Map; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.nodes.Node; import org.yaml.snakeyaml.nodes.Tag; @@ -31,33 +27,36 @@ import org.yaml.snakeyaml.representer.Representer; * Issue 46: Dump a java.io.File object */ public class FileTest extends TestCase { - @SuppressWarnings("unchecked") - public void test() { - File file = new File("src/test/resources/examples/list-bean-1.yaml"); - assertTrue(file.exists()); - Yaml yaml = new Yaml(new MyRepresenter()); - Map<String, File> map = new HashMap<String, File>(); - map.put("one", file); - String output = yaml.dump(map); - // System.out.println(output); - assertTrue(output, output.startsWith("{one: !!java.io.File '")); - assertTrue(output, output.endsWith("list-bean-1.yaml'}\n")); - Map<String, File> parsed = (Map<String, File>) yaml.load(output); - File file2 = parsed.get("one"); - assertTrue(file2.getAbsolutePath(), file2.getAbsolutePath().endsWith("list-bean-1.yaml")); + + @SuppressWarnings("unchecked") + public void test() { + File file = new File("src/test/resources/examples/list-bean-1.yaml"); + assertTrue(file.exists()); + Yaml yaml = new Yaml(new MyRepresenter()); + Map<String, File> map = new HashMap<String, File>(); + map.put("one", file); + String output = yaml.dump(map); + // System.out.println(output); + assertTrue(output, output.startsWith("{one: !!java.io.File '")); + assertTrue(output, output.endsWith("list-bean-1.yaml'}\n")); + Map<String, File> parsed = yaml.load(output); + File file2 = parsed.get("one"); + assertTrue(file2.getAbsolutePath(), file2.getAbsolutePath().endsWith("list-bean-1.yaml")); + } + + public class MyRepresenter extends Representer { + + public MyRepresenter() { + this.representers.put(File.class, new FileRepresenter()); } - public class MyRepresenter extends Representer { - public MyRepresenter() { - this.representers.put(File.class, new FileRepresenter()); - } + public class FileRepresenter implements Represent { - public class FileRepresenter implements Represent { - public Node representData(Object data) { - File file = (File) data; - Node scalar = representScalar(new Tag("!!java.io.File"), file.getAbsolutePath()); - return scalar; - } - } + public Node representData(Object data) { + File file = (File) data; + Node scalar = representScalar(new Tag("!!java.io.File"), file.getAbsolutePath()); + return scalar; + } } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue467/MergeTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue467/MergeTest.java new file mode 100644 index 00000000..7631337c --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue467/MergeTest.java @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue467; + +import junit.framework.TestCase; +import org.yaml.snakeyaml.Yaml; + +public class MergeTest extends TestCase { + + public void testMergeAsScalar() { + Yaml loader = new Yaml(); + String data = + "test-list:\n" + " - &1\n" + " a: 1\n" + " b: 2\n" + " - &2 <<: *1\n" + " - <<: *2"; + try { + loader.load(data); + fail(); + } catch (Exception e) { + assertTrue(e.getMessage() + .contains("expected a mapping or list of mappings for merging, but found scalar")); + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue468/NonAsciiAnchorTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue468/NonAsciiAnchorTest.java new file mode 100644 index 00000000..7374c303 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue468/NonAsciiAnchorTest.java @@ -0,0 +1,65 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue468; + +import junit.framework.TestCase; +import org.yaml.snakeyaml.Yaml; + +public class NonAsciiAnchorTest extends TestCase { + + private final String NON_ANCHORS = ":,[]{}*&./"; + + public void testNonAsciiAnchor() { + Yaml loader = new Yaml(); + String value = loader.load("&something_タスク タスク"); + assertEquals("タスク", value); + } + + public void testUnderscore() { + Yaml loader = new Yaml(); + String value = loader.load("&_ タスク"); + assertEquals("タスク", value); + } + + public void testSmile() { + Yaml loader = new Yaml(); + String value = loader.load("&\uD83D\uDE01 v1"); + // System.out.println("&\uD83D\uDE01 v1"); + assertEquals("v1", value); + } + + public void testAlpha() { + Yaml loader = new Yaml(); + String value = loader.load("&kääk v1"); + assertEquals("v1", value); + } + + public void testNonAllowedAnchor() { + for (int i = 0; i < NON_ANCHORS.length(); i++) { + try { + Character c = NON_ANCHORS.charAt(i); + loadWith(c); + fail("Special chars should not be allowed in anchor name: " + c); + } catch (Exception e) { + assertTrue(e.getMessage(), e.getMessage().contains("while scanning an anchor")); + assertTrue(e.getMessage(), e.getMessage().contains("unexpected character found")); + } + } + } + + private void loadWith(char c) { + Yaml loader = new Yaml(); + loader.load("&" + c + " value"); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue47/IncompleteBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue47/IncompleteBean.java index 197946f2..5ce2f2f3 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue47/IncompleteBean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue47/IncompleteBean.java @@ -1,37 +1,36 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue47; public class IncompleteBean { - private int id; - private String name; - public IncompleteBean() { - id = 10; - } + private final int id; + private String name; - public String getName() { - return name; - } + public IncompleteBean() { + id = 10; + } - public void setName(String name) { - this.name = name; - } + public String getName() { + return name; + } - public int getId() { - return id; - } -}
\ No newline at end of file + public void setName(String name) { + this.name = name; + } + + public int getId() { + return id; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue47/ReadOnlyPropertiesTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue47/ReadOnlyPropertiesTest.java index a99f63f6..bfb7bd44 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue47/ReadOnlyPropertiesTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue47/ReadOnlyPropertiesTest.java @@ -1,59 +1,56 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue47; import junit.framework.TestCase; - import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.error.YAMLException; public class ReadOnlyPropertiesTest extends TestCase { - public void testBean1() { - IncompleteBean bean = new IncompleteBean(); - bean.setName("lunch"); - Yaml yaml = new Yaml(); - String output = yaml.dumpAsMap(bean); - // System.out.println(output); - assertEquals("name: lunch\n", output); - // - Yaml loader = new Yaml(); - IncompleteBean parsed = loader.loadAs(output, IncompleteBean.class); - assertEquals(bean.getName(), parsed.getName()); - } - public void testBean2() { - IncompleteBean bean = new IncompleteBean(); - bean.setName("lunch"); - DumperOptions options = new DumperOptions(); - options.setAllowReadOnlyProperties(true); - Yaml yaml = new Yaml(options); - String output = yaml.dumpAsMap(bean); - // System.out.println(output); - assertEquals("id: 10\nname: lunch\n", output); - // - Yaml loader = new Yaml(); - try { - loader.loadAs(output, IncompleteBean.class); - fail("Setter is missing."); - } catch (YAMLException e) { - String message = e.getMessage(); - assertTrue( - message, - message.contains("Unable to find property 'id' on class: org.yaml.snakeyaml.issues.issue47.IncompleteBean")); - } + public void testBean1() { + IncompleteBean bean = new IncompleteBean(); + bean.setName("lunch"); + Yaml yaml = new Yaml(); + String output = yaml.dumpAsMap(bean); + // System.out.println(output); + assertEquals("name: lunch\n", output); + // + Yaml loader = new Yaml(); + IncompleteBean parsed = loader.loadAs(output, IncompleteBean.class); + assertEquals(bean.getName(), parsed.getName()); + } + + public void testBean2() { + IncompleteBean bean = new IncompleteBean(); + bean.setName("lunch"); + DumperOptions options = new DumperOptions(); + options.setAllowReadOnlyProperties(true); + Yaml yaml = new Yaml(options); + String output = yaml.dumpAsMap(bean); + // System.out.println(output); + assertEquals("id: 10\nname: lunch\n", output); + // + Yaml loader = new Yaml(); + try { + loader.loadAs(output, IncompleteBean.class); + fail("Setter is missing."); + } catch (YAMLException e) { + String message = e.getMessage(); + assertTrue(message, message.contains( + "No writable property 'id' on class: org.yaml.snakeyaml.issues.issue47.IncompleteBean")); } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue474/ByteParseTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue474/ByteParseTest.java new file mode 100644 index 00000000..74ba36d3 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue474/ByteParseTest.java @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue474; + +import junit.framework.TestCase; +import org.yaml.snakeyaml.Yaml; + +public class ByteParseTest extends TestCase { + + public void testParseBytes() { + Yaml yamlProcessor = new Yaml(); + Byte[] lb = yamlProcessor.loadAs("[0x01,0x02,0xff,0x7f_ee_00_11]", Byte[].class); + + assertEquals(4, lb.length); + assertEquals(Byte.valueOf((byte) 1), lb[0]); + assertEquals(Byte.valueOf((byte) 2), lb[1]); + assertEquals(Byte.valueOf((byte) -1), lb[2]); + assertEquals(Byte.valueOf((byte) 255), lb[2]); // narrow + assertEquals(Byte.valueOf((byte) 17), lb[3]); + } + + public void testParseShorts() { + Yaml yamlProcessor = new Yaml(); + Short[] lb = yamlProcessor.loadAs("[0x0102,0x7ffe,33000,0x8fff,65000]", Short[].class); + + assertEquals(5, lb.length); + assertEquals(Short.valueOf((short) 258), lb[0]); + assertEquals(Short.valueOf((short) 32766), lb[1]); + assertEquals(Short.valueOf((short) -32536), lb[2]); + assertEquals(Short.valueOf((short) -28673), lb[3]); + assertEquals(Short.valueOf((short) -536), lb[4]); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue478/Location.java b/src/test/java/org/yaml/snakeyaml/issues/issue478/Location.java new file mode 100644 index 00000000..f30ce26e --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue478/Location.java @@ -0,0 +1,45 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue478; + +public class Location { + + int x; + int y; + int z; + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + + public int getZ() { + return z; + } + + public void setZ(int z) { + this.z = z; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue478/PropertyOrderTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue478/PropertyOrderTest.java new file mode 100644 index 00000000..a2a2d256 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue478/PropertyOrderTest.java @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue478; + +import java.util.Iterator; +import java.util.Set; +import java.util.TreeSet; +import junit.framework.TestCase; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.introspector.Property; + +public class PropertyOrderTest extends TestCase { + + Set<Property> reverse = new TreeSet<>(); + + public void testParseBytes() { + Yaml y = new Yaml(new org.yaml.snakeyaml.representer.Representer() { + @Override + protected Set<Property> getProperties(Class<? extends Object> type) { + // System.out.println("getProperties: reverse order"); + Iterator<Property> itr = ((TreeSet) super.getProperties(type)).descendingIterator(); + while (itr.hasNext()) { + Property a = itr.next(); + // System.out.println("> " + a.getName()); + reverse.add(a); + } + return reverse; + } + }); + String v = "x: 1\ny: 2\nz: 3\n"; + Location location = y.loadAs(v, Location.class); + + String output = y.dumpAsMap(location); + assertEquals("x", reverse.iterator().next().getName()); + assertEquals(v, output); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue479/CredentialAppConfig.java b/src/test/java/org/yaml/snakeyaml/issues/issue479/CredentialAppConfig.java new file mode 100644 index 00000000..86f1e240 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue479/CredentialAppConfig.java @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue479; + +public class CredentialAppConfig { + + private Mysql mysql; + private String test1; + + public String getTest1() { + return test1; + } + + public void setTest1(String test1) { + this.test1 = test1; + } + + public Mysql getMysql() { + return mysql; + } + + public void setMysql(Mysql mysql) { + this.mysql = mysql; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue479/DemoProperty.java b/src/test/java/org/yaml/snakeyaml/issues/issue479/DemoProperty.java new file mode 100644 index 00000000..b1043a9e --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue479/DemoProperty.java @@ -0,0 +1,29 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue479; + +import java.util.Map; + +public class DemoProperty { + + private Map<String, CredentialAppConfig> system; + + public Map<String, CredentialAppConfig> getSystem() { + return system; + } + + public void setSystem(Map<String, CredentialAppConfig> system) { + this.system = system; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue479/MergeKeyDeepMergeTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue479/MergeKeyDeepMergeTest.java new file mode 100644 index 00000000..d9ddadf5 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue479/MergeKeyDeepMergeTest.java @@ -0,0 +1,78 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue479; + +import java.io.InputStream; +import junit.framework.TestCase; +import org.yaml.snakeyaml.Util; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; +import org.yaml.snakeyaml.representer.Representer; + +public class MergeKeyDeepMergeTest extends TestCase { + + public void testOnlyTheCurrentMappingIsMerged() { + Constructor constructor = new Constructor(DemoProperty.class); + Representer representer = new Representer(); + representer.getPropertyUtils().setSkipMissingProperties(true); + Yaml yaml = new Yaml(constructor, representer); + + InputStream inputStream = + MergeKeyDeepMergeTest.class.getResourceAsStream("/issues/issue479.yaml"); + DemoProperty property = yaml.load(inputStream); + assertEquals("2", property.getSystem().get("erp").getTest1()); + assertNull(property.getSystem().get("erp").getMysql().getDb_host()); + assertNull(property.getSystem().get("erp").getMysql().getDb_port()); + assertEquals("erp", property.getSystem().get("erp").getMysql().getDb_name()); + assertNull(property.getSystem().get("erp").getMysql().getDb_user()); + assertNull(property.getSystem().get("erp").getMysql().getDb_password()); + } + + public void testMergeKeyDeepMerge() { + Yaml yaml = new Yaml(); + + InputStream inputStream = + MergeKeyDeepMergeTest.class.getResourceAsStream("/issues/issue479.yaml"); + Object property = yaml.load(inputStream); + String output = yaml.dump(property); + // System.out.println(output); + String expected = Util.getLocalResource("issues/issue479-output.yaml"); + assertEquals(expected, output); + } + + public void testMergeAsJavabean() { + Constructor constructor = new Constructor(DemoProperty.class); + Representer representer = new Representer(); + representer.getPropertyUtils().setSkipMissingProperties(true); + Yaml yaml = new Yaml(constructor, representer); + + InputStream inputStream = + MergeKeyDeepMergeTest.class.getResourceAsStream("/issues/issue479-1.yaml"); + DemoProperty property = yaml.load(inputStream); + String output = yaml.dump(property); + // System.out.println(output); + + String v = "!!org.yaml.snakeyaml.issues.issue479.DemoProperty\n" + "system:\n" + " erp:\n" + + " mysql: {db_host: mysql.avatar2.test, db_name: erp, db_password: secret, db_port: 3306,\n" + + " db_user: default}\n" + " test1: '2'\n"; + assertEquals(v, output); + + assertEquals("2", property.getSystem().get("erp").getTest1()); + assertEquals("mysql.avatar2.test", property.getSystem().get("erp").getMysql().getDb_host()); + assertEquals(3306, (int) property.getSystem().get("erp").getMysql().getDb_port()); + assertEquals("erp", property.getSystem().get("erp").getMysql().getDb_name()); + assertEquals("default", property.getSystem().get("erp").getMysql().getDb_user()); + assertEquals("secret", property.getSystem().get("erp").getMysql().getDb_password()); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue479/Mysql.java b/src/test/java/org/yaml/snakeyaml/issues/issue479/Mysql.java new file mode 100644 index 00000000..cd778e55 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue479/Mysql.java @@ -0,0 +1,63 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue479; + +public class Mysql { + + private String db_host; + private String db_name; + private Integer db_port; + private String db_user; + private String db_password; + + public String getDb_host() { + return db_host; + } + + public void setDb_host(String db_host) { + this.db_host = db_host; + } + + public Integer getDb_port() { + return db_port; + } + + public void setDb_port(Integer db_port) { + this.db_port = db_port; + } + + public String getDb_user() { + return db_user; + } + + public void setDb_user(String db_user) { + this.db_user = db_user; + } + + public String getDb_password() { + return db_password; + } + + public void setDb_password(String db_password) { + this.db_password = db_password; + } + + public String getDb_name() { + return db_name; + } + + public void setDb_name(String db_name) { + this.db_name = db_name; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue48/Bean.java b/src/test/java/org/yaml/snakeyaml/issues/issue48/Bean.java index cd8ef8a8..ded596a4 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue48/Bean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue48/Bean.java @@ -1,55 +1,53 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue48; public class Bean { - private int value; - private String name; - - public Bean() { - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @Override - public boolean equals(Object obj) { - return toString().equals(obj.toString()); - } - - @Override - public int hashCode() { - return value; - } - - @Override - public String toString() { - return "Bean " + String.valueOf(value); - } - - public int getValue() { - return value; - } - - public void setValue(int value) { - this.value = value; - } -}
\ No newline at end of file + + private int value; + private String name; + + public Bean() {} + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object obj) { + return toString().equals(obj.toString()); + } + + @Override + public int hashCode() { + return value; + } + + @Override + public String toString() { + return "Bean " + value; + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue48/SkipJavaBeanPropertyTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue48/SkipJavaBeanPropertyTest.java index d5374545..2142e6ac 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue48/SkipJavaBeanPropertyTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue48/SkipJavaBeanPropertyTest.java @@ -1,22 +1,19 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue48; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.introspector.Property; import org.yaml.snakeyaml.nodes.NodeTuple; @@ -24,33 +21,35 @@ import org.yaml.snakeyaml.nodes.Tag; import org.yaml.snakeyaml.representer.Representer; public class SkipJavaBeanPropertyTest extends TestCase { - public void testWithNull() { - Bean bean = new Bean(); - bean.setValue(3); - Yaml yaml = new Yaml(); - String output = yaml.dumpAsMap(bean); - // System.out.println(output); - assertEquals("name: null\nvalue: 3\n", output); - } - public void testWithoutNull() { - Bean bean = new Bean(); - bean.setValue(5); - Yaml yaml = new Yaml(new MyRepresenter()); - String output = yaml.dumpAsMap(bean); - // System.out.println(output); - assertEquals("value: 5\n", output); - } + public void testWithNull() { + Bean bean = new Bean(); + bean.setValue(3); + Yaml yaml = new Yaml(); + String output = yaml.dumpAsMap(bean); + // System.out.println(output); + assertEquals("name: null\nvalue: 3\n", output); + } + + public void testWithoutNull() { + Bean bean = new Bean(); + bean.setValue(5); + Yaml yaml = new Yaml(new MyRepresenter()); + String output = yaml.dumpAsMap(bean); + // System.out.println(output); + assertEquals("value: 5\n", output); + } + + private class MyRepresenter extends Representer { - private class MyRepresenter extends Representer { - @Override - protected NodeTuple representJavaBeanProperty(Object bean, Property property, Object value, - Tag customTag) { - if (value != null) { - return super.representJavaBeanProperty(bean, property, value, customTag); - } else { - return null; - } - } + @Override + protected NodeTuple representJavaBeanProperty(Object bean, Property property, Object value, + Tag customTag) { + if (value != null) { + return super.representJavaBeanProperty(bean, property, value, customTag); + } else { + return null; + } } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue480/AnchorUnicodeTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue480/AnchorUnicodeTest.java new file mode 100644 index 00000000..5c195f66 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue480/AnchorUnicodeTest.java @@ -0,0 +1,92 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue480; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import junit.framework.TestCase; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.emitter.EmitterException; +import org.yaml.snakeyaml.nodes.Node; +import org.yaml.snakeyaml.serializer.AnchorGenerator; + +public class AnchorUnicodeTest extends TestCase { + + private static final Set<Character> INVALID_ANCHOR = new HashSet(); + + static { + INVALID_ANCHOR.add('['); + INVALID_ANCHOR.add(']'); + INVALID_ANCHOR.add('{'); + INVALID_ANCHOR.add('}'); + INVALID_ANCHOR.add(','); + INVALID_ANCHOR.add('*'); + INVALID_ANCHOR.add('&'); + } + + public void testUnicodeAnchor() { + DumperOptions options = new DumperOptions(); + options.setAnchorGenerator(new AnchorGenerator() { + int id = 0; + + @Override + public String nextAnchor(Node node) { + return "タスク" + id++; + } + }); + + Yaml yaml = new Yaml(options); + + List<String> list = new ArrayList<>(); + list.add("abc"); + + List<List<String>> toExport = new ArrayList<>(); + toExport.add(list); + toExport.add(list); + + String output = yaml.dump(toExport); + assertEquals("- &タスク0 [abc]\n- *タスク0\n", output); + } + + public void testInvalidAnchor() { + for (Character ch : INVALID_ANCHOR) { + Yaml yaml = new Yaml(createSettings(ch)); + List<String> list = new ArrayList<>(); + list.add("abc"); + List<List<String>> toExport = new ArrayList<>(); + toExport.add(list); + toExport.add(list); + try { + yaml.dump(toExport); + fail(); + } catch (EmitterException e) { + assertTrue(e.getMessage().startsWith("Invalid character")); + } + } + } + + private DumperOptions createSettings(final Character invalid) { + DumperOptions options = new DumperOptions(); + options.setAnchorGenerator(new AnchorGenerator() { + @Override + public String nextAnchor(Node node) { + return "anchor" + invalid; + } + }); + return options; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue481/DumpAnchorTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue481/DumpAnchorTest.java new file mode 100644 index 00000000..13136292 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue481/DumpAnchorTest.java @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue481; + +import java.io.StringReader; +import java.io.StringWriter; +import junit.framework.TestCase; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Util; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.nodes.Node; +import org.yaml.snakeyaml.serializer.AnchorGenerator; + +public class DumpAnchorTest extends TestCase { + + public void test_anchor_test() { + String str = Util.getLocalResource("issues/issue481.yaml"); + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + options.setAnchorGenerator(new AnchorGenerator() { + @Override + public String nextAnchor(Node node) { + return node.getAnchor(); + } + }); + Yaml yaml = new Yaml(options); + + Node node = yaml.compose(new StringReader(str)); + StringWriter out = new StringWriter(); + yaml.serialize(node, out); + assertEquals(str, out.toString()); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue484/Base60FloatTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue484/Base60FloatTest.java new file mode 100644 index 00000000..c9897e14 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue484/Base60FloatTest.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue484; + +import junit.framework.TestCase; +import org.yaml.snakeyaml.Yaml; + +public class Base60FloatTest extends TestCase { + + public void test60Float() { + Yaml yamlProcessor = new Yaml(); + Double base60 = yamlProcessor.load("86:00.0"); + assertEquals(5160.0, base60); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue485/RestrictAliasNamesTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue485/RestrictAliasNamesTest.java new file mode 100644 index 00000000..d835ab03 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue485/RestrictAliasNamesTest.java @@ -0,0 +1,30 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue485; + +import junit.framework.TestCase; +import org.yaml.snakeyaml.Yaml; + +public class RestrictAliasNamesTest extends TestCase { + + public void testAliasFromRuby() { + try { + Yaml yamlProcessor = new Yaml(); + yamlProcessor.load("Exclude: **/*_old.rb"); + fail("Should not accept Alias **/*_old.rb"); + } catch (Exception e) { + assertTrue(e.getMessage().contains("unexpected character found *(42)")); + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue49/CalendarBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue49/CalendarBean.java index 50587eb7..728f441c 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue49/CalendarBean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue49/CalendarBean.java @@ -1,39 +1,38 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue49; import java.util.Calendar; public class CalendarBean { - private Calendar calendar; - private String name; - public String getName() { - return name; - } + private Calendar calendar; + private String name; - public void setName(String name) { - this.name = name; - } + public String getName() { + return name; + } - public Calendar getCalendar() { - return calendar; - } + public void setName(String name) { + this.name = name; + } - public void setCalendar(Calendar calendar) { - this.calendar = calendar; - } -}
\ No newline at end of file + public Calendar getCalendar() { + return calendar; + } + + public void setCalendar(Calendar calendar) { + this.calendar = calendar; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue49/CalendarTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue49/CalendarTest.java index 09af4a81..d9f10f11 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue49/CalendarTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue49/CalendarTest.java @@ -1,123 +1,120 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue49; +import static org.junit.Assert.assertNotEquals; + import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class CalendarTest extends TestCase { - /** - * Daylight Saving Time is not taken into account - */ - public void testDumpDstIgnored() { - CalendarBean bean = new CalendarBean(); - bean.setName("lunch"); - Calendar cal = Calendar.getInstance(); - cal.setTime(new Date(1000000000000L)); - cal.setTimeZone(TimeZone.getTimeZone("GMT-8:00")); - bean.setCalendar(cal); - Yaml yaml = new Yaml(); - String output = yaml.dumpAsMap(bean); - // System.out.println(output); - assertEquals("calendar: 2001-09-08T17:46:40-8:00\nname: lunch\n", output); - // - Yaml loader = new Yaml(); - CalendarBean parsed = loader.loadAs(output, CalendarBean.class); - assertEquals(bean.getCalendar(), parsed.getCalendar()); - } - /** - * Daylight Saving Time is in effect on this date/time in - * America/Los_Angeles Daylight<br/> - * Saving Time is not in effect on this date/time in GMT - */ - public void testDumpDstIsNotTheSame() { - check(1000000000000L, "America/Los_Angeles", "Must be 7 hours difference.", - "2001-09-08T18:46:40-7:00"); - } + /** + * Daylight Saving Time is not taken into account + */ + public void testDumpDstIgnored() { + CalendarBean bean = new CalendarBean(); + bean.setName("lunch"); + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date(1000000000000L)); + cal.setTimeZone(TimeZone.getTimeZone("GMT-8:00")); + bean.setCalendar(cal); + Yaml yaml = new Yaml(); + String output = yaml.dumpAsMap(bean); + // System.out.println(output); + assertEquals("calendar: 2001-09-08T17:46:40-08:00\nname: lunch\n", output); + // + Yaml loader = new Yaml(); + CalendarBean parsed = loader.loadAs(output, CalendarBean.class); + assertEquals(bean.getCalendar(), parsed.getCalendar()); + } + + /** + * Daylight Saving Time is in effect on this date/time in America/Los_Angeles Daylight<br/> + * Saving Time is not in effect on this date/time in GMT + */ + public void testDumpDstIsNotTheSame() { + check(1000000000000L, "America/Los_Angeles", "Must be 7 hours difference.", + "2001-09-08T18:46:40-07:00"); + } - /** - * Daylight Saving Time is not in effect on this date/time in - * America/Los_Angeles Daylight<br/> - * Saving Time is not in effect on this date/time in GMT - */ - public void testDumpDstIsTheSame() { - check(1266833741374L, "America/Los_Angeles", "Must be 8 hours difference.", - "2010-02-22T02:15:41.374-8:00"); - } + /** + * Daylight Saving Time is not in effect on this date/time in America/Los_Angeles Daylight<br/> + * Saving Time is not in effect on this date/time in GMT + */ + public void testDumpDstIsTheSame() { + check(1266833741374L, "America/Los_Angeles", "Must be 8 hours difference.", + "2010-02-22T02:15:41.374-08:00"); + } - /** - * Test odd time zone - */ - public void testNepal() { - check(1266833741374L, "Asia/Katmandu", "Must be 5:45 hours difference.", - "2010-02-22T16:00:41.374+5:45"); - } + /** + * Test odd time zone + */ + public void testNepal() { + check(1266833741374L, "Asia/Katmandu", "Must be 5:45 hours difference.", + "2010-02-22T16:00:41.374+05:45"); + } - public void testMoreThen10hours() { - check(1266833741374L, "Asia/Kamchatka", "Must be 12 hours difference.", - "2010-02-22T22:15:41.374+12:00"); - } + public void testMoreThen10hours() { + check(1266833741374L, "Asia/Kamchatka", "Must be 12 hours difference.", + "2010-02-22T22:15:41.374+12:00"); + } - private void check(long time, String timeZone, String warning, String etalon) { - CalendarBean bean = new CalendarBean(); - bean.setName("lunch"); - Calendar cal = Calendar.getInstance(); - cal.setTime(new Date(time)); - cal.setTimeZone(TimeZone.getTimeZone(timeZone)); - bean.setCalendar(cal); - Yaml yaml = new Yaml(); - String output = yaml.dumpAsMap(bean); - // System.out.println(output); - assertEquals(warning, "calendar: " + etalon + "\nname: lunch\n", output); - // - Yaml loader = new Yaml(); - CalendarBean parsed = loader.loadAs(output, CalendarBean.class); - assertFalse("TimeZone must deviate.", bean.getCalendar().equals(parsed.getCalendar())); - assertEquals(bean.getCalendar().getTimeInMillis(), parsed.getCalendar().getTimeInMillis()); - } + private void check(long time, String timeZone, String warning, String etalon) { + CalendarBean bean = new CalendarBean(); + bean.setName("lunch"); + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date(time)); + cal.setTimeZone(TimeZone.getTimeZone(timeZone)); + bean.setCalendar(cal); + Yaml yaml = new Yaml(); + String output = yaml.dumpAsMap(bean); + // System.out.println(output); + assertEquals(warning, "calendar: " + etalon + "\nname: lunch\n", output); + // + Yaml loader = new Yaml(); + CalendarBean parsed = loader.loadAs(output, CalendarBean.class); + assertNotEquals("TimeZone must deviate.", bean.getCalendar(), parsed.getCalendar()); + assertEquals(bean.getCalendar().getTimeInMillis(), parsed.getCalendar().getTimeInMillis()); + } - public void testLoadBean() { - Yaml beanLoader = new Yaml(); - CalendarBean bean = beanLoader.loadAs( - "calendar: 2001-12-14t21:59:43.10-05:00\nname: dinner", CalendarBean.class); - assertEquals("dinner", bean.getName()); - Calendar calendar = bean.getCalendar(); - assertEquals(TimeZone.getTimeZone("GMT-5:00").getOffset(calendar.getTime().getTime()), - calendar.getTimeZone().getOffset(calendar.getTime().getTime())); - // - Yaml yaml = new Yaml(); - Date date = (Date) yaml.load("2001-12-14t21:59:43.10-05:00"); - assertEquals(date, calendar.getTime()); - } + public void testLoadBean() { + Yaml beanLoader = new Yaml(); + CalendarBean bean = beanLoader.loadAs("calendar: 2001-12-14t21:59:43.10-05:00\nname: dinner", + CalendarBean.class); + assertEquals("dinner", bean.getName()); + Calendar calendar = bean.getCalendar(); + assertEquals(TimeZone.getTimeZone("GMT-5:00").getOffset(calendar.getTime().getTime()), + calendar.getTimeZone().getOffset(calendar.getTime().getTime())); + // + Yaml yaml = new Yaml(); + Date date = yaml.load("2001-12-14t21:59:43.10-05:00"); + assertEquals(date, calendar.getTime()); + } - public void testLoadWithTag() { - Yaml yaml = new Yaml(); - GregorianCalendar calendar = (GregorianCalendar) yaml - .load("!!java.util.GregorianCalendar 2001-12-14t21:59:43.10-05:00"); - assertEquals(TimeZone.getTimeZone("GMT-5:00").getOffset(calendar.getTime().getTime()), - calendar.getTimeZone().getOffset(calendar.getTime().getTime())); - // - Date date = (Date) yaml.load("2001-12-14t21:59:43.10-05:00"); - assertEquals(date, calendar.getTime()); - } + public void testLoadWithTag() { + Yaml yaml = new Yaml(); + GregorianCalendar calendar = + yaml.load("!!java.util.GregorianCalendar 2001-12-14t21:59:43.10-05:00"); + assertEquals(TimeZone.getTimeZone("GMT-5:00").getOffset(calendar.getTime().getTime()), + calendar.getTimeZone().getOffset(calendar.getTime().getTime())); + // + Date date = yaml.load("2001-12-14t21:59:43.10-05:00"); + assertEquals(date, calendar.getTime()); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue50/SnakeyamlTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue50/SnakeyamlTest.java index b288f80a..141d93cf 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue50/SnakeyamlTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue50/SnakeyamlTest.java @@ -1,75 +1,76 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue50; import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; /** * test issue 50. */ public class SnakeyamlTest extends TestCase { - public static interface SomeBean { - String getAttribute1(); - String getAttribute2(); - } + public interface SomeBean { + + String getAttribute1(); - /* public */static abstract class BaseSomeBean implements SomeBean { - private String attribute1; + String getAttribute2(); + } - public String getAttribute1() { - return attribute1; - } + /* public */static abstract class BaseSomeBean implements SomeBean { - public void setAttribute1(String attribute1) { - this.attribute1 = attribute1; - } + private String attribute1; + + public String getAttribute1() { + return attribute1; } - public static final class SomeBeanImpl extends BaseSomeBean { - private String attribute2; + public void setAttribute1(String attribute1) { + this.attribute1 = attribute1; + } + } - public SomeBeanImpl(final String attribute1, final String attribute2) { - setAttribute1(attribute1); - setAttribute2(attribute2); - } + public static final class SomeBeanImpl extends BaseSomeBean { - public String getAttribute2() { - return attribute2; - } + private String attribute2; - public void setAttribute2(String attribute2) { - this.attribute2 = attribute2; - } + public SomeBeanImpl(final String attribute1, final String attribute2) { + setAttribute1(attribute1); + setAttribute2(attribute2); + } + + public String getAttribute2() { + return attribute2; + } - @Override - public String toString() { - return "SomeBeanImpl"; - } + public void setAttribute2(String attribute2) { + this.attribute2 = attribute2; } - public void testIntrospector() { - SomeBean someBean = new SomeBeanImpl("value1", "value2"); - Yaml dumper = new Yaml(); - String output = dumper.dump(someBean); - // System.out.println(output); - assertEquals( - "!!org.yaml.snakeyaml.issues.issue50.SnakeyamlTest$SomeBeanImpl {attribute1: value1,\n attribute2: value2}\n", - output); + @Override + public String toString() { + return "SomeBeanImpl"; } + } + + public void testIntrospector() { + SomeBean someBean = new SomeBeanImpl("value1", "value2"); + Yaml dumper = new Yaml(); + String output = dumper.dump(someBean); + // System.out.println(output); + assertEquals( + "!!org.yaml.snakeyaml.issues.issue50.SnakeyamlTest$SomeBeanImpl {attribute1: value1,\n attribute2: value2}\n", + output); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue500/FailIteratorOnlyWhenErrorFoundTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue500/FailIteratorOnlyWhenErrorFoundTest.java new file mode 100644 index 00000000..ed4485a2 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue500/FailIteratorOnlyWhenErrorFoundTest.java @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue500; + +import java.util.Iterator; +import junit.framework.TestCase; +import org.yaml.snakeyaml.Yaml; + +public class FailIteratorOnlyWhenErrorFoundTest extends TestCase { + + public void testFailure() { + try { + Yaml yamlProcessor = new Yaml(); + String data = + "a: 1\n" + "---\n" + "Some comment \n" + "\n" + "\n" + "b: 2\n" + "\n" + "c: 3\n" + "---"; + Iterable<Object> parsed = yamlProcessor.loadAll(data); + Iterator<Object> iterator = parsed.iterator(); + assertNotNull(iterator.next()); // no failure, first document id valid + iterator.next(); + // for (Object obj : parsed) { + // assertNotNull(obj); + // System.out.println(obj); + // } + fail("Should not accept the second document"); + } catch (Exception e) { + assertTrue(e.getMessage(), e.getMessage().contains("in 'reader', line 6, column 2:")); + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue506/ParseStringsThatAlmostLookLikeInts.java b/src/test/java/org/yaml/snakeyaml/issues/issue506/ParseStringsThatAlmostLookLikeInts.java new file mode 100644 index 00000000..ed55cd61 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue506/ParseStringsThatAlmostLookLikeInts.java @@ -0,0 +1,106 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue506; + +import java.util.regex.Pattern; +import junit.framework.TestCase; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.resolver.Resolver; + +public class ParseStringsThatAlmostLookLikeInts extends TestCase { + + public void testHexPrefixedString() { + String in = "0x_"; + String expected = "0x_"; + Object out = new Yaml().load(in); + assertEquals(expected, out); + } + + public void testHexInt() { + String in = "0x_AB"; + Integer expected = 171; + Object out = new Yaml().load(in); + assertEquals(expected, out); + } + + public void testOctalPrefixedString() { + String in = "0_"; + String expected = "0_"; + Object out = new Yaml().load(in); + assertEquals(expected, out); + } + + public void testOctalInt() { + String in = "0_123"; + Integer expected = 83; + Object out = new Yaml().load(in); + assertEquals(expected, out); + } + + public void testBinaryPrefixedString() { + String in = "0b_"; + String expected = "0b_"; + Object out = new Yaml().load(in); + assertEquals(expected, out); + } + + public void testBinaryInt() { + String in = "0b_101"; + Integer expected = 5; + Object out = new Yaml().load(in); + assertEquals(expected, out); + } + + public void testDecimalish() { + String in = "-_"; + String expected = "-_"; + Object out = new Yaml().load(in); + assertEquals(expected, out); + } + + public void testIntPattern() { + Pattern regexp = Resolver.INT; + assertTrue(regexp.matcher("0xabc").matches()); + assertTrue(regexp.matcher("0x_abc").matches()); + assertFalse(regexp.matcher("0x_").matches()); + assertTrue(regexp.matcher("0b_01_00").matches()); + assertFalse(regexp.matcher("0b_").matches()); + assertTrue(regexp.matcher("0_77").matches()); + assertFalse(regexp.matcher("0_").matches()); + } + + public void testFloatPattern() { + Pattern regexp = Resolver.FLOAT; + assertFalse(regexp.matcher("0123456789").matches()); + assertFalse(regexp.matcher("123456789").matches()); + + assertTrue(regexp.matcher("00.3").matches()); + assertTrue(regexp.matcher("00.003").matches()); + assertTrue(regexp.matcher("02.003").matches()); + assertTrue(regexp.matcher("-02.003").matches()); + assertTrue(regexp.matcher("-02.003_001").matches()); + assertTrue(regexp.matcher("-2_000.003_001").matches()); + assertTrue(regexp.matcher(".3").matches()); + assertTrue(regexp.matcher("-.3").matches()); + assertTrue(regexp.matcher("+0.3").matches()); + assertTrue(regexp.matcher("8.1e-06").matches()); + assertTrue(regexp.matcher("8e-06").matches()); + assertTrue(regexp.matcher("8e06").matches()); + assertTrue(regexp.matcher("8e6").matches()); + assertTrue(regexp.matcher("8E6").matches()); + assertTrue(regexp.matcher("8E+06").matches()); + assertTrue(regexp.matcher("8e+6").matches()); + assertTrue(regexp.matcher("+8e+6").matches()); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue51/UnicodeStyleTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue51/UnicodeStyleTest.java index 45706652..cdd0a631 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue51/UnicodeStyleTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue51/UnicodeStyleTest.java @@ -1,44 +1,41 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue51; import junit.framework.TestCase; - import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions.ScalarStyle; import org.yaml.snakeyaml.Yaml; /** - * @see <a - * href="http://code.google.com/p/snakeyaml/issues/detail?id=51">Issue</a> + * @see <a href="http://code.google.com/p/snakeyaml/issues/detail?id=51">Issue</a> */ public class UnicodeStyleTest extends TestCase { - public void testFoldedStyle() { - Yaml yaml = new Yaml(); - String output = yaml.dump("í"); - // System.out.println(output); - assertEquals("í\n", output); - } - public void testDoubleQuotedStyle() { - DumperOptions options = new DumperOptions(); - options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED); - Yaml yaml = new Yaml(options); - String output = yaml.dump("í"); - // System.out.println(output); - assertEquals("\"í\"\n", output); - } + public void testFoldedStyle() { + Yaml yaml = new Yaml(); + String output = yaml.dump("í"); + // System.out.println(output); + assertEquals("í\n", output); + } + + public void testDoubleQuotedStyle() { + DumperOptions options = new DumperOptions(); + options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED); + Yaml yaml = new Yaml(options); + String output = yaml.dump("í"); + // System.out.println(output); + assertEquals("\"í\"\n", output); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue512/ListWithCommentTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue512/ListWithCommentTest.java new file mode 100644 index 00000000..cb8fc345 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue512/ListWithCommentTest.java @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue512; + +import java.util.List; +import junit.framework.TestCase; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Util; +import org.yaml.snakeyaml.Yaml; + +public class ListWithCommentTest extends TestCase { + + public void testList() { + String str = Util.getLocalResource("issues/issue512.yaml"); + LoaderOptions options = new LoaderOptions(); + options.setProcessComments(true); + Yaml yaml = new Yaml(options); + List<String> obj = yaml.load(str); + assertEquals(2, obj.size()); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue517/TrailingTabTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue517/TrailingTabTest.java new file mode 100644 index 00000000..722fd145 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue517/TrailingTabTest.java @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue517; + +import junit.framework.TestCase; +import org.yaml.snakeyaml.Yaml; + +public class TrailingTabTest extends TestCase { + + public void testTrailingTab() { + String str = "'bar'\t"; + Yaml yaml = new Yaml(); + try { + Object obj = yaml.load(str); + fail("Issue 517"); // TODO FIXME trailing TAB should be ignored + assertNotNull(obj); + } catch (Exception e) { + assertTrue(e.getMessage(), + e.getMessage().contains("found character '\\t(TAB)' that cannot start any token.")); + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue52/LineBreakDooubleQuotedTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue52/LineBreakDooubleQuotedTest.java index 8682fdde..0bbf58c7 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue52/LineBreakDooubleQuotedTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue52/LineBreakDooubleQuotedTest.java @@ -1,57 +1,54 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue52; import junit.framework.TestCase; - import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions.ScalarStyle; import org.yaml.snakeyaml.Yaml; /** - * @see <a - * href="http://code.google.com/p/snakeyaml/issues/detail?id=52">Issue</a> + * @see <a href="http://code.google.com/p/snakeyaml/issues/detail?id=52">Issue</a> */ public class LineBreakDooubleQuotedTest extends TestCase { - public void testDoubleQuotedStyle() { - DumperOptions options = new DumperOptions(); - options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED); - options.setWidth(20); - options.setIndent(4); - Yaml yaml = new Yaml(options); - String etalon = "12345678901234567890\n\n123 456"; - String output = yaml.dump(etalon); - // System.out.println(output); - assertEquals("\"12345678901234567890\\n\\\n \\n123 456\"\n", output); - String parsed = (String) yaml.load(output); - assertEquals(etalon, parsed); - } - public void testDoubleQuotedStyleNoLineSplit() { - DumperOptions options = new DumperOptions(); - options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED); - options.setWidth(20); - options.setSplitLines(false); - options.setIndent(4); - Yaml yaml = new Yaml(options); - String etalon = "12345678901234567890\n\n123 456"; - String output = yaml.dump(etalon); - // System.out.println(output); - assertEquals("\"12345678901234567890\\n\\n123 456\"\n", output); - String parsed = (String) yaml.load(output); - assertEquals(etalon, parsed); - } + public void testDoubleQuotedStyle() { + DumperOptions options = new DumperOptions(); + options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED); + options.setWidth(20); + options.setIndent(4); + Yaml yaml = new Yaml(options); + String etalon = "12345678901234567890\n\n123 456"; + String output = yaml.dump(etalon); + // System.out.println(output); + assertEquals("\"12345678901234567890\\n\\\n \\n123 456\"\n", output); + String parsed = yaml.load(output); + assertEquals(etalon, parsed); + } + + public void testDoubleQuotedStyleNoLineSplit() { + DumperOptions options = new DumperOptions(); + options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED); + options.setWidth(20); + options.setSplitLines(false); + options.setIndent(4); + Yaml yaml = new Yaml(options); + String etalon = "12345678901234567890\n\n123 456"; + String output = yaml.dump(etalon); + // System.out.println(output); + assertEquals("\"12345678901234567890\\n\\n123 456\"\n", output); + String parsed = yaml.load(output); + assertEquals(etalon, parsed); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue520/ChompingTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue520/ChompingTest.java new file mode 100644 index 00000000..1b4e9384 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue520/ChompingTest.java @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue520; + +import java.util.Map; +import junit.framework.TestCase; +import org.yaml.snakeyaml.Yaml; + +public class ChompingTest extends TestCase { + + public void testChomp() { + String input = "description: |+\n" + " line\n\n"; + Yaml yaml = new Yaml(); + Map<String, String> obj = yaml.load(input); + assertEquals("line\n\n", obj.get("description")); + + String output = yaml.dump(obj); + assertEquals(input, output); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue522/JavaBeanWithPropertiesLikeBooleanValuesTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue522/JavaBeanWithPropertiesLikeBooleanValuesTest.java new file mode 100644 index 00000000..0a980b5f --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue522/JavaBeanWithPropertiesLikeBooleanValuesTest.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue522; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.yaml.snakeyaml.Yaml; + +public class JavaBeanWithPropertiesLikeBooleanValuesTest { + + public static class Workflow { + + public String on; + public String off; + public String y; + public String n; + public String yes; + public String no; + } + + @Test + public void deserialize() { + Yaml yaml = new Yaml(); + + String strYaml = "on: This is on\n" + "off: This is off\n" + "y: This is y\n" + "n: This is n\n" + + "yes: This is yes\n" + "no: This is no\n"; + + Workflow wf = yaml.loadAs(strYaml, Workflow.class); + + assertEquals(wf.on, "This is on"); + assertEquals(wf.off, "This is off"); + assertEquals(wf.y, "This is y"); + assertEquals(wf.n, "This is n"); + assertEquals(wf.yes, "This is yes"); + assertEquals(wf.no, "This is no"); + + } + +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue525/FuzzyStackOverflowTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue525/FuzzyStackOverflowTest.java new file mode 100644 index 00000000..6ab42499 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue525/FuzzyStackOverflowTest.java @@ -0,0 +1,52 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue525; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Util; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.error.YAMLException; + +public class FuzzyStackOverflowTest { + + @Test + public void parseOpenUnmatchedMappings() { + try { + Yaml yaml = new Yaml(); + String strYaml = Util.getLocalResource("fuzzer/YamlFuzzer-4626423186325504"); + yaml.load(strYaml); + fail("Should report invalid YAML"); + } catch (YAMLException e) { + assertEquals("Nesting Depth exceeded max 50", e.getMessage()); + } + } + + @Test + public void parseOpenUnmatchedMappingsWithCustomLimit() { + try { + LoaderOptions options = new LoaderOptions(); + options.setNestingDepthLimit(1000); + Yaml yaml = new Yaml(options); + String strYaml = Util.getLocalResource("fuzzer/YamlFuzzer-4626423186325504"); + yaml.load(strYaml); + fail("Should report invalid YAML"); + } catch (YAMLException e) { + assertEquals("Nesting Depth exceeded max 1000", e.getMessage()); + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue526/Fuzzy47027Test.java b/src/test/java/org/yaml/snakeyaml/issues/issue526/Fuzzy47027Test.java new file mode 100644 index 00000000..bca42147 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue526/Fuzzy47027Test.java @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue526; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Util; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.error.YAMLException; + +// OSS-Fuzz - 47027 +public class Fuzzy47027Test { + + @Test + public void parseOpenUnmatchedSequences_47027() { + try { + Yaml yaml = new Yaml(); + String strYaml = Util.getLocalResource("fuzzer/YamlFuzzer-5427149240139776"); + yaml.load(strYaml); + fail("Should report invalid YAML"); + } catch (YAMLException e) { + assertEquals("Nesting Depth exceeded max 50", e.getMessage()); + } + } + + @Test + public void setCustomLimit100() { + try { + LoaderOptions options = new LoaderOptions(); + options.setNestingDepthLimit(100); + Yaml yaml = new Yaml(options); + String strYaml = Util.getLocalResource("fuzzer/YamlFuzzer-5427149240139776"); + yaml.load(strYaml); + fail("Should report invalid YAML"); + } catch (YAMLException e) { + assertEquals("Nesting Depth exceeded max 100", e.getMessage()); + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue527/Fuzzy47047Test.java b/src/test/java/org/yaml/snakeyaml/issues/issue527/Fuzzy47047Test.java new file mode 100644 index 00000000..49848d5b --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue527/Fuzzy47047Test.java @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue527; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Util; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.error.YAMLException; + +// Stackoverflow [OSS-Fuzz - 47047] +public class Fuzzy47047Test { + + @Test + public void parseKeyIndicators_47047() { + try { + LoaderOptions options = new LoaderOptions(); + options.setNestingDepthLimit(30); + Yaml yaml = new Yaml(options); + String strYaml = Util.getLocalResource("fuzzer/YamlFuzzer-5868638424399872"); + yaml.load(strYaml); + fail("Should report invalid YAML"); + } catch (YAMLException e) { + assertEquals("Nesting Depth exceeded max 30", e.getMessage()); + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue529/Fuzzy47028Test.java b/src/test/java/org/yaml/snakeyaml/issues/issue529/Fuzzy47028Test.java new file mode 100644 index 00000000..02c46878 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue529/Fuzzy47028Test.java @@ -0,0 +1,52 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue529; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.error.YAMLException; + +// StringIndexOutOfBoundsException [OSS-Fuzz 47028] +public class Fuzzy47028Test { + + @Test + public void parseEmptyFloat_47028() { + try { + LoaderOptions options = new LoaderOptions(); + Yaml yaml = new Yaml(options); + yaml.load("- !!float"); + fail("Should report invalid YAML"); + } catch (YAMLException e) { + assertTrue(e.getMessage().contains("while constructing a float")); + assertTrue(e.getMessage().contains("found empty value")); + } + } + + @Test + public void parseEmptyInt_47028() { + try { + LoaderOptions options = new LoaderOptions(); + Yaml yaml = new Yaml(options); + yaml.load("- !!int"); + fail("Should report invalid YAML"); + } catch (YAMLException e) { + assertTrue(e.getMessage().contains("while constructing an int")); + assertTrue(e.getMessage().contains("found empty value")); + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue530/Fuzzy47039Test.java b/src/test/java/org/yaml/snakeyaml/issues/issue530/Fuzzy47039Test.java new file mode 100644 index 00000000..9c09e1df --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue530/Fuzzy47039Test.java @@ -0,0 +1,34 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue530; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Util; +import org.yaml.snakeyaml.Yaml; + +// Stackoverflow [OSS-Fuzz - 47039] +public class Fuzzy47039Test { + + @Test + public void parseKeyIndicators_47039() { + LoaderOptions options = new LoaderOptions(); + Yaml yaml = new Yaml(options); + String strYaml = Util.getLocalResource("fuzzer/YamlFuzzer-5110034188599296"); + String parsed = yaml.load(strYaml); + assertEquals(strYaml.trim(), parsed); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue531/Fuzzy47081Test.java b/src/test/java/org/yaml/snakeyaml/issues/issue531/Fuzzy47081Test.java new file mode 100644 index 00000000..3410e5f4 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue531/Fuzzy47081Test.java @@ -0,0 +1,84 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue531; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.List; +import org.junit.Test; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.error.YAMLException; + +// Stackoverflow [OSS-Fuzz - 47081] + +/** + * A proof that this issue is a FALSE POSITIVE https://nvd.nist.gov/vuln/detail/CVE-2022-38752 + */ +public class Fuzzy47081Test { + + /** + * Recursive key should NOT be used for untrusted data to avoid + */ + @Test + public void parse47081_no_recursion_allowed() { + LoaderOptions options = new LoaderOptions(); + options.setAllowRecursiveKeys(false); // must be set to false for untrusted source + Yaml yaml = new Yaml(options); + String strYaml = " &a\n" + "- *a\n" + "- *a\n" + "- *a:\n" + "- *a\n" + "- *a\n" + "- *a"; + try { + yaml.load(strYaml); + fail("Should report invalid YAML: " + strYaml); + } catch (YAMLException e) { + assertEquals("Recursive key for mapping is detected but it is not configured to be allowed.", + e.getMessage()); + } + } + + /** + * Recursive list fails (with StackOverflowError) because it is used as a key Recursive key should + * NOT be used for untrusted data + */ + @Test + public void parse47081_allow_recursion() { + try { + LoaderOptions options = new LoaderOptions(); + options.setAllowRecursiveKeys(true); + Yaml yaml = new Yaml(options); + String strYaml = "&a\n" + "- *a\n" // if this line is removed, the test properly complains + // about the recursive keys in map -> Recursive key for + // mapping is detected, but it is not configured to be + // allowed. + + "- *a:\n"; // when the colon is removed, the test is Ok, because the recursive list is + // not a key + // System.out.println(strYaml); + yaml.load(strYaml); + fail("Should report invalid YAML: " + strYaml); + } catch (StackOverflowError e) { + assertTrue(true); + } + } + + @Test + public void parse47081_no_colon() { + LoaderOptions options = new LoaderOptions(); + options.setAllowRecursiveKeys(true); + Yaml yaml = new Yaml(options); + String strYaml = "&a\n" + "- *a\n" + "- *a\n"; + List<Object> parsed = yaml.load(strYaml); + assertEquals(strYaml, 2, parsed.size()); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue543/Fuzzer50355Test.java b/src/test/java/org/yaml/snakeyaml/issues/issue543/Fuzzer50355Test.java new file mode 100644 index 00000000..a919d08b --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue543/Fuzzer50355Test.java @@ -0,0 +1,42 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue543; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Util; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.error.YAMLException; + +// Stackoverflow [OSS-Fuzz - 50355] +public class Fuzzer50355Test { + + @Test + public void parse_50355() { + LoaderOptions options = new LoaderOptions(); + options.setAllowRecursiveKeys(false); + Yaml yaml = new Yaml(options); + String strYaml = Util.getLocalResource("fuzzer/YamlFuzzer-5167495132086272"); + try { + yaml.load(strYaml); + fail("Recursive keys should not be accepted"); + } catch (YAMLException e) { + assertEquals("Recursive key for mapping is detected but it is not configured to be allowed.", + e.getMessage()); + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue544/DoubleQuoteTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue544/DoubleQuoteTest.java new file mode 100644 index 00000000..b9a756a1 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue544/DoubleQuoteTest.java @@ -0,0 +1,97 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue544; + +import static org.junit.Assert.assertEquals; + +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.DumperOptions.FlowStyle; +import org.yaml.snakeyaml.DumperOptions.ScalarStyle; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.nodes.MappingNode; +import org.yaml.snakeyaml.nodes.NodeTuple; +import org.yaml.snakeyaml.nodes.ScalarNode; +import org.yaml.snakeyaml.nodes.Tag; + +public class DoubleQuoteTest { + + private MappingNode create() { + String content = "🔐This process is simple and secure."; + + ScalarNode doubleQuotedKey = + new ScalarNode(Tag.STR, "double_quoted", null, null, ScalarStyle.PLAIN); + ScalarNode doubleQuotedValue = + new ScalarNode(Tag.STR, content, null, null, ScalarStyle.DOUBLE_QUOTED); + NodeTuple doubleQuotedTuple = new NodeTuple(doubleQuotedKey, doubleQuotedValue); + + ScalarNode singleQuotedKey = + new ScalarNode(Tag.STR, "single_quoted", null, null, ScalarStyle.PLAIN); + ScalarNode singleQuotedValue = + new ScalarNode(Tag.STR, content, null, null, ScalarStyle.SINGLE_QUOTED); + NodeTuple singleQuotedTuple = new NodeTuple(singleQuotedKey, singleQuotedValue); + + List<NodeTuple> nodeTuples = new ArrayList<>(); + nodeTuples.add(doubleQuotedTuple); + nodeTuples.add(singleQuotedTuple); + + MappingNode mappingNode = new MappingNode(Tag.MAP, nodeTuples, FlowStyle.BLOCK); + + return mappingNode; + } + + private String emit(DumperOptions dumperOptions) { + Yaml yaml = new Yaml(dumperOptions); + + StringWriter writer = new StringWriter(); + yaml.serialize(create(), writer); + + return writer.toString(); + } + + @Test + public void testUnicode() { + DumperOptions dumperOptions = new DumperOptions(); + dumperOptions.setAllowUnicode(true); // use as is + String output = emit(dumperOptions); + String expectedOutput = "double_quoted: \"🔐This process is simple and secure.\"\n" + + "single_quoted: '🔐This process is simple and secure.'\n"; + + assertEquals(expectedOutput, output); + } + + @Test + public void testSubstitution() { + DumperOptions dumperOptions = new DumperOptions(); + dumperOptions.setAllowUnicode(false); // substitute with U notation + String output = emit(dumperOptions); + String expectedOutput = "double_quoted: \"\\U0001f510This process is simple and secure.\"\n" + + "single_quoted: \"\\U0001f510This process is simple and secure.\"\n"; + + assertEquals(expectedOutput, output); + } + + @Test + public void testDefault() { + DumperOptions dumperOptions = new DumperOptions(); + String output = emit(dumperOptions); + String expectedOutput = "double_quoted: \"🔐This process is simple and secure.\"\n" + + "single_quoted: '🔐This process is simple and secure.'\n"; + + assertEquals(expectedOutput, output); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue547/ByteLimitTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue547/ByteLimitTest.java new file mode 100644 index 00000000..e6eae1e5 --- /dev/null +++ b/src/test/java/org/yaml/snakeyaml/issues/issue547/ByteLimitTest.java @@ -0,0 +1,37 @@ +/** + * Copyright (c) 2008, SnakeYAML + * + * Licensed 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.yaml.snakeyaml.issues.issue547; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Yaml; + +public class ByteLimitTest { + + @Test + public void testUnicode() { + LoaderOptions options = new LoaderOptions(); + options.setCodePointLimit(15); + Yaml yaml = new Yaml(options); + try { + yaml.load("12345678901234567890"); + fail("Long input should not be accepted"); + } catch (Exception e) { + assertEquals("The incoming YAML document exceeds the limit: 15 code points.", e.getMessage()); + } + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue55/Blog.java b/src/test/java/org/yaml/snakeyaml/issues/issue55/Blog.java index 228b35b9..9c8eb7a5 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue55/Blog.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue55/Blog.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue55; @@ -19,19 +17,19 @@ import java.util.LinkedList; import java.util.List; class Blog { - private List<Post> posts = new LinkedList<Post>(); - public Blog() { - } + private final List<Post> posts = new LinkedList<Post>(); - public void addPost(Post p) { - // do some business logic here - posts.add(p); - } + public Blog() {} - public List<Post> getPosts() { - // in production code do not return the original set but a wrapped - // unmodifiable set - return posts; - } -}
\ No newline at end of file + public void addPost(Post p) { + // do some business logic here + posts.add(p); + } + + public List<Post> getPosts() { + // in production code do not return the original set but a wrapped + // unmodifiable set + return posts; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue55/FieldListTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue55/FieldListTest.java index c339ef41..9d06a6b8 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue55/FieldListTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue55/FieldListTest.java @@ -1,58 +1,55 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue55; import java.util.LinkedList; import java.util.List; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.introspector.BeanAccess; public class FieldListTest extends TestCase { - public void testYaml() { - Yaml beanLoader = new Yaml(); - beanLoader.setBeanAccess(BeanAccess.FIELD); - BlogField rehydrated = beanLoader.loadAs(Util.getLocalResource("issues/issue55_2.txt"), - BlogField.class); - assertEquals(4, rehydrated.getPosts().size()); + public void testYaml() { + Yaml beanLoader = new Yaml(); + beanLoader.setBeanAccess(BeanAccess.FIELD); + BlogField rehydrated = + beanLoader.loadAs(Util.getLocalResource("issues/issue55_2.txt"), BlogField.class); + assertEquals(4, rehydrated.getPosts().size()); + } + + public void testFailureWithoutFieldAccess() { + Yaml beanLoader = new Yaml(); + try { + beanLoader.loadAs(Util.getLocalResource("issues/issue55_2.txt"), BlogField.class); + fail("Private field must not be available"); + } catch (Exception e) { + assertTrue(e.getMessage().contains("No writable property 'posts'")); } + } - public void testFailureWithoutFieldAccess() { - Yaml beanLoader = new Yaml(); - try { - beanLoader.loadAs(Util.getLocalResource("issues/issue55_2.txt"), BlogField.class); - fail("Private field must not be available"); - } catch (Exception e) { - assertTrue(e.getMessage().contains("Unable to find property 'posts'")); - } - } + public static class BlogField { - public static class BlogField { - private List<Integer> posts; + private final List<Integer> posts; - public BlogField() { - posts = new LinkedList<Integer>(); - } + public BlogField() { + posts = new LinkedList<Integer>(); + } - public List<Integer> getPosts() { - return posts; - } + public List<Integer> getPosts() { + return posts; } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue55/JavaBeanListTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue55/JavaBeanListTest.java index 1e8966f7..6637b0f8 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue55/JavaBeanListTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue55/JavaBeanListTest.java @@ -1,58 +1,55 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue55; import java.util.LinkedList; import java.util.List; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.introspector.BeanAccess; public class JavaBeanListTest extends TestCase { - public void testYaml() { - Yaml beanLoader = new Yaml(); - beanLoader.setBeanAccess(BeanAccess.FIELD); - BlogBean rehydrated = (BlogBean) beanLoader.loadAs( - Util.getLocalResource("issues/issue55_2.txt"), BlogBean.class); - assertEquals(4, rehydrated.getPosts().size()); + public void testYaml() { + Yaml beanLoader = new Yaml(); + beanLoader.setBeanAccess(BeanAccess.FIELD); + BlogBean rehydrated = + beanLoader.loadAs(Util.getLocalResource("issues/issue55_2.txt"), BlogBean.class); + assertEquals(4, rehydrated.getPosts().size()); + } + + public void testFailureWithoutFieldAccess() { + Yaml beanLoader = new Yaml(); + try { + beanLoader.loadAs(Util.getLocalResource("issues/issue55_2.txt"), BlogBean.class); + fail("Private field must not be available"); + } catch (Exception e) { + assertTrue(e.getMessage().contains("No writable property 'posts'")); } + } - public void testFailureWithoutFieldAccess() { - Yaml beanLoader = new Yaml(); - try { - beanLoader.loadAs(Util.getLocalResource("issues/issue55_2.txt"), BlogBean.class); - fail("Private field must not be available"); - } catch (Exception e) { - assertTrue(e.getMessage().contains("Unable to find property 'posts'")); - } - } + public static class BlogBean { - public static class BlogBean { - private List<Integer> posts; + private final List<Integer> posts; - public BlogBean() { - posts = new LinkedList<Integer>(); - } + public BlogBean() { + posts = new LinkedList<Integer>(); + } - public List<Integer> getPosts() { - return posts; - } + public List<Integer> getPosts() { + return posts; } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue55/Post.java b/src/test/java/org/yaml/snakeyaml/issues/issue55/Post.java index d9e60cf3..9f3e3055 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue55/Post.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue55/Post.java @@ -1,42 +1,41 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue55; class Post { - private String title; - private String text; - public static String description; - public transient String cache; - // TODO empty constructor is required to support 2 step construction - protected Post() { - description = "I should not be dumped."; - cache = "Q34598723SDW234"; - } + private String title; + private String text; + public static String description; + public transient String cache; - public Post(String title, String text) { - this.title = title; - this.text = text; - } + // empty constructor is required to support 2 step construction + protected Post() { + description = "I should not be dumped."; + cache = "Q34598723SDW234"; + } - public String getTitle() { - return title; - } + public Post(String title, String text) { + this.title = title; + this.text = text; + } - public String getText() { - return text; - } -}
\ No newline at end of file + public String getTitle() { + return title; + } + + public String getText() { + return text; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue55/YamlFieldAccessCollectionTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue55/YamlFieldAccessCollectionTest.java index 64c24d48..470067ed 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue55/YamlFieldAccessCollectionTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue55/YamlFieldAccessCollectionTest.java @@ -1,25 +1,21 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue55; import java.util.Collection; import java.util.Map; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.introspector.BeanAccess; @@ -27,70 +23,69 @@ import org.yaml.snakeyaml.representer.Representer; public class YamlFieldAccessCollectionTest extends TestCase { - public void testYaml() { - Blog original = createTestBlog(); - Yaml yamlDumper = constructYamlDumper(); - String serialized = yamlDumper.dumpAsMap(original); - // System.out.println(serialized); - assertEquals(Util.getLocalResource("issues/issue55_1.txt"), serialized); - Yaml blogLoader = new Yaml(); - blogLoader.setBeanAccess(BeanAccess.FIELD); - Blog rehydrated = blogLoader.loadAs(serialized, Blog.class); - checkTestBlog(rehydrated); - } + public void testYaml() { + Blog original = createTestBlog(); + Yaml yamlDumper = constructYamlDumper(); + String serialized = yamlDumper.dumpAsMap(original); + // System.out.println(serialized); + assertEquals(Util.getLocalResource("issues/issue55_1.txt"), serialized); + Yaml blogLoader = new Yaml(); + blogLoader.setBeanAccess(BeanAccess.FIELD); + Blog rehydrated = blogLoader.loadAs(serialized, Blog.class); + checkTestBlog(rehydrated); + } - @SuppressWarnings("unchecked") - public void testYamlWithoutConfiguration() { - Yaml yaml = new Yaml(); - Map<String, Object> map = (Map<String, Object>) yaml.load(Util - .getLocalResource("issues/issue55_1.txt")); - assertEquals(1, map.size()); - } + @SuppressWarnings("unchecked") + public void testYamlWithoutConfiguration() { + Yaml yaml = new Yaml(); + Map<String, Object> map = yaml.load(Util.getLocalResource("issues/issue55_1.txt")); + assertEquals(1, map.size()); + } - public void testYamlFailure() { - Yaml beanLoader = new Yaml(); - try { - beanLoader.loadAs(Util.getLocalResource("issues/issue55_1.txt"), Blog.class); - fail("BeanAccess.FIELD is required."); - } catch (Exception e) { - assertTrue(e.getMessage(), e.getMessage().contains("Unable to find property 'posts'")); - } + public void testYamlFailure() { + Yaml beanLoader = new Yaml(); + try { + beanLoader.loadAs(Util.getLocalResource("issues/issue55_1.txt"), Blog.class); + fail("BeanAccess.FIELD is required."); + } catch (Exception e) { + assertTrue(e.getMessage(), e.getMessage().contains("No writable property 'posts'")); } + } - public void testYamlDefaultWithFeildAccess() { - Yaml yaml = new Yaml(); - yaml.setBeanAccess(BeanAccess.FIELD); - Blog original = createTestBlog(); - String serialized = yaml.dump(original); - assertEquals(Util.getLocalResource("issues/issue55_1_rootTag.txt"), serialized); - Blog rehydrated = (Blog) yaml.load(serialized); - checkTestBlog(rehydrated); - } + public void testYamlDefaultWithFeildAccess() { + Yaml yaml = new Yaml(); + yaml.setBeanAccess(BeanAccess.FIELD); + Blog original = createTestBlog(); + String serialized = yaml.dump(original); + assertEquals(Util.getLocalResource("issues/issue55_1_rootTag.txt"), serialized); + Blog rehydrated = yaml.load(serialized); + checkTestBlog(rehydrated); + } - protected Yaml constructYamlDumper() { - Representer representer = new Representer(); - representer.getPropertyUtils().setBeanAccess(BeanAccess.FIELD); - Yaml yaml = new Yaml(representer); - return yaml; - } + protected Yaml constructYamlDumper() { + Representer representer = new Representer(); + representer.getPropertyUtils().setBeanAccess(BeanAccess.FIELD); + Yaml yaml = new Yaml(representer); + return yaml; + } - protected Yaml constructYamlParser() { - Yaml yaml = new Yaml(); - yaml.setBeanAccess(BeanAccess.FIELD); - return yaml; - } + protected Yaml constructYamlParser() { + Yaml yaml = new Yaml(); + yaml.setBeanAccess(BeanAccess.FIELD); + return yaml; + } - protected Blog createTestBlog() { - Post post1 = new Post("Test", "Dummy"); - Post post2 = new Post("Highly", "Creative"); - Blog blog = new Blog(); - blog.addPost(post1); - blog.addPost(post2); - return blog; - } + protected Blog createTestBlog() { + Post post1 = new Post("Test", "Dummy"); + Post post2 = new Post("Highly", "Creative"); + Blog blog = new Blog(); + blog.addPost(post1); + blog.addPost(post2); + return blog; + } - protected void checkTestBlog(Blog blog) { - Collection<Post> posts = blog.getPosts(); - assertEquals("Blog contains 2 posts", 2, posts.size()); - } + protected void checkTestBlog(Blog blog) { + Collection<Post> posts = blog.getPosts(); + assertEquals("Blog contains 2 posts", 2, posts.size()); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue56/CodeBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue56/CodeBean.java index 00043633..b800b0b1 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue56/CodeBean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue56/CodeBean.java @@ -1,56 +1,55 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue56; public class CodeBean { - private String country; - private String type; - private String value; - public static int counter; - - public CodeBean() { - counter++; - } - - public String getCountry() { - return country; - } - - public void setCountry(String country) { - this.country = country; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - - @Override - public String toString() { - return "CodeBean: " + getValue(); - } -}
\ No newline at end of file + + private String country; + private String type; + private String value; + public static int counter; + + public CodeBean() { + counter++; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + public String toString() { + return "CodeBean: " + getValue(); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue56/PerlTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue56/PerlTest.java index 1f04b843..3ea9f6d1 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue56/PerlTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue56/PerlTest.java @@ -1,26 +1,22 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue56; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map; - import junit.framework.TestCase; - import org.yaml.snakeyaml.TypeDescription; import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; @@ -32,95 +28,97 @@ import org.yaml.snakeyaml.nodes.Tag; public class PerlTest extends TestCase { - @SuppressWarnings({ "unchecked", "rawtypes" }) - public void testMaps() { - Yaml yaml = new Yaml(new CustomConstructor()); - String input = Util.getLocalResource("issues/issue56-1.yaml"); - int counter = 0; - for (Object obj : yaml.loadAll(input)) { - // System.out.println(obj); - Map<String, Object> map = (Map<String, Object>) obj; - Integer oid = (Integer) map.get("oid"); - if (oid == 123058) { - ArrayList a = (ArrayList) map.get("sequences"); - LinkedHashMap b = (LinkedHashMap) a.get(0); - LinkedHashMap c = (LinkedHashMap) b.get("atc"); - LinkedHashMap d = (LinkedHashMap) c.get("name"); - LinkedHashMap e = (LinkedHashMap) d.get("canonical"); - String acidNameDe = e.entrySet().toArray()[1].toString(); - assertEquals("Unicode escaped sequence must be decoded.", - ":de=Acetylsalicylsäure", acidNameDe); - } - assertTrue(oid > 10000); - counter++; - } - assertEquals(4, counter); - assertEquals(0, CodeBean.counter); + @SuppressWarnings({"unchecked", "rawtypes"}) + public void testMaps() { + Yaml yaml = new Yaml(new CustomConstructor()); + String input = Util.getLocalResource("issues/issue56-1.yaml"); + int counter = 0; + for (Object obj : yaml.loadAll(input)) { + // System.out.println(obj); + Map<String, Object> map = (Map<String, Object>) obj; + Integer oid = (Integer) map.get("oid"); + if (oid == 123058) { + ArrayList a = (ArrayList) map.get("sequences"); + LinkedHashMap b = (LinkedHashMap) a.get(0); + LinkedHashMap c = (LinkedHashMap) b.get("atc"); + LinkedHashMap d = (LinkedHashMap) c.get("name"); + LinkedHashMap e = (LinkedHashMap) d.get("canonical"); + String acidNameDe = e.entrySet().toArray()[1].toString(); + assertEquals("Unicode escaped sequence must be decoded.", ":de=Acetylsalicylsäure", + acidNameDe); + } + assertTrue(oid > 10000); + counter++; } + assertEquals(4, counter); + assertEquals(0, CodeBean.counter); + } - private class CustomConstructor extends SafeConstructor { - public CustomConstructor() { - // define tags which begin with !org.yaml. - String prefix = "!de.oddb.org,2007/ODDB"; - this.yamlMultiConstructors.put(prefix, new ConstructYamlMap()); - } + private class CustomConstructor extends SafeConstructor { + + public CustomConstructor() { + // define tags which begin with !org.yaml. + String prefix = "!de.oddb.org,2007/ODDB"; + this.yamlMultiConstructors.put(prefix, new ConstructYamlMap()); } + } - @SuppressWarnings("unchecked") - public void testJavaBeanWithTypeDescription() { - Constructor c = new CustomBeanConstructor(); - TypeDescription descr = new TypeDescription(CodeBean.class, new Tag( - "!de.oddb.org,2007/ODDB::Util::Code")); - c.addTypeDescription(descr); - Yaml yaml = new Yaml(c); - String input = Util.getLocalResource("issues/issue56-1.yaml"); - int counter = 0; - for (Object obj : yaml.loadAll(input)) { - // System.out.println(obj); - Map<String, Object> map = (Map<String, Object>) obj; - Integer oid = (Integer) map.get("oid"); - assertTrue(oid > 10000); - counter++; - } - assertEquals(4, counter); - assertEquals(55, CodeBean.counter); + @SuppressWarnings("unchecked") + public void testJavaBeanWithTypeDescription() { + Constructor c = new CustomBeanConstructor(); + TypeDescription descr = + new TypeDescription(CodeBean.class, new Tag("!de.oddb.org,2007/ODDB::Util::Code")); + c.addTypeDescription(descr); + Yaml yaml = new Yaml(c); + String input = Util.getLocalResource("issues/issue56-1.yaml"); + int counter = 0; + for (Object obj : yaml.loadAll(input)) { + // System.out.println(obj); + Map<String, Object> map = (Map<String, Object>) obj; + Integer oid = (Integer) map.get("oid"); + assertTrue(oid > 10000); + counter++; } + assertEquals(4, counter); + assertEquals(55, CodeBean.counter); + } - @SuppressWarnings("unchecked") - public void testJavaBean() { - Constructor c = new CustomBeanConstructor(); - Yaml yaml = new Yaml(c); - String input = Util.getLocalResource("issues/issue56-1.yaml"); - int counter = 0; - for (Object obj : yaml.loadAll(input)) { - // System.out.println(obj); - Map<String, Object> map = (Map<String, Object>) obj; - Integer oid = (Integer) map.get("oid"); - assertTrue(oid > 10000); - counter++; - } - assertEquals(4, counter); - assertEquals(55, CodeBean.counter); + @SuppressWarnings("unchecked") + public void testJavaBean() { + Constructor c = new CustomBeanConstructor(); + Yaml yaml = new Yaml(c); + String input = Util.getLocalResource("issues/issue56-1.yaml"); + int counter = 0; + for (Object obj : yaml.loadAll(input)) { + // System.out.println(obj); + Map<String, Object> map = (Map<String, Object>) obj; + Integer oid = (Integer) map.get("oid"); + assertTrue(oid > 10000); + counter++; } + assertEquals(4, counter); + assertEquals(55, CodeBean.counter); + } - private class CustomBeanConstructor extends Constructor { - public CustomBeanConstructor() { - // define tags which begin with !org.yaml. - String prefix = "!de.oddb.org,2007/ODDB"; - this.yamlMultiConstructors.put(prefix, new ConstructYamlMap()); - } + private class CustomBeanConstructor extends Constructor { - protected Construct getConstructor(Node node) { - if (node.getTag().equals(new Tag("!de.oddb.org,2007/ODDB::Util::Code"))) { - node.setUseClassConstructor(true); - node.setType(CodeBean.class); - } - return super.getConstructor(node); - } + public CustomBeanConstructor() { + // define tags which begin with !org.yaml. + String prefix = "!de.oddb.org,2007/ODDB"; + this.yamlMultiConstructors.put(prefix, new ConstructYamlMap()); } - @Override - protected void setUp() { - CodeBean.counter = 0; + protected Construct getConstructor(Node node) { + if (node.getTag().equals(new Tag("!de.oddb.org,2007/ODDB::Util::Code"))) { + node.setUseClassConstructor(true); + node.setType(CodeBean.class); + } + return super.getConstructor(node); } + } + + @Override + protected void setUp() { + CodeBean.counter = 0; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue58/NullValueDumperTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue58/NullValueDumperTest.java index 9321c020..5d2b561d 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue58/NullValueDumperTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue58/NullValueDumperTest.java @@ -1,60 +1,57 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue58; import java.util.ArrayList; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class NullValueDumperTest extends TestCase { - public static class Foo { - private ArrayList<Object> bar = new ArrayList<Object>(); + public static class Foo { - public ArrayList<Object> getBar() { - return bar; - } - - public void setBar(ArrayList<Object> bar) { - this.bar = bar; - } - } + private ArrayList<Object> bar = new ArrayList<Object>(); - public void testListElement() { - final Foo foo = new Foo(); - foo.bar.add(1); - foo.bar.add("A"); - foo.bar.add(3.14); - Yaml yaml = new Yaml(); - assertEquals("bar:\n- 1\n- A\n- 3.14\n", yaml.dumpAsMap(foo)); + public ArrayList<Object> getBar() { + return bar; } - public void testNullListElement() { - final Foo foo = new Foo(); - - foo.bar.add(1); - foo.bar.add("A"); - foo.bar.add(null); - foo.bar.add(3.14); - Yaml yaml = new Yaml(); - assertEquals("bar:\n- 1\n- A\n- null\n- 3.14\n", yaml.dumpAsMap(foo)); - assertEquals( - "!!org.yaml.snakeyaml.issues.issue58.NullValueDumperTest$Foo\nbar: [1, A, null, 3.14]\n", - new Yaml().dump(foo)); + public void setBar(ArrayList<Object> bar) { + this.bar = bar; } + } + + public void testListElement() { + final Foo foo = new Foo(); + foo.bar.add(1); + foo.bar.add("A"); + foo.bar.add(3.14); + Yaml yaml = new Yaml(); + assertEquals("bar:\n- 1\n- A\n- 3.14\n", yaml.dumpAsMap(foo)); + } + + public void testNullListElement() { + final Foo foo = new Foo(); + + foo.bar.add(1); + foo.bar.add("A"); + foo.bar.add(null); + foo.bar.add(3.14); + Yaml yaml = new Yaml(); + assertEquals("bar:\n- 1\n- A\n- null\n- 3.14\n", yaml.dumpAsMap(foo)); + assertEquals( + "!!org.yaml.snakeyaml.issues.issue58.NullValueDumperTest$Foo\nbar: [1, A, null, 3.14]\n", + new Yaml().dump(foo)); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue60/CustomOrderTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue60/CustomOrderTest.java index 0942b53d..6e93d057 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue60/CustomOrderTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue60/CustomOrderTest.java @@ -1,29 +1,24 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue60; -import java.beans.IntrospectionException; import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; import java.util.TreeSet; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.introspector.BeanAccess; @@ -31,54 +26,54 @@ import org.yaml.snakeyaml.introspector.Property; import org.yaml.snakeyaml.introspector.PropertyUtils; import org.yaml.snakeyaml.representer.Representer; -//issue 59 +// issue 59 public class CustomOrderTest extends TestCase { - public void testReversedOrder() { - Representer repr = new Representer(); - repr.setPropertyUtils(new ReversedPropertyUtils()); - Yaml yaml = new Yaml(repr); - String output = yaml.dump(getBean()); - // System.out.println(output); - assertEquals(Util.getLocalResource("issues/issue59-1.yaml"), output); - } + public void testReversedOrder() { + Representer repr = new Representer(); + repr.setPropertyUtils(new ReversedPropertyUtils()); + Yaml yaml = new Yaml(repr); + String output = yaml.dump(getBean()); + // System.out.println(output); + assertEquals(Util.getLocalResource("issues/issue59-1.yaml"), output); + } - private class ReversedPropertyUtils extends PropertyUtils { - @Override - protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess bAccess) - throws IntrospectionException { - Set<Property> result = new TreeSet<Property>(Collections.reverseOrder()); - result.addAll(super.createPropertySet(type, bAccess)); - return result; - } - } + private class ReversedPropertyUtils extends PropertyUtils { - public void testUnsorted() { - Representer repr = new Representer(); - repr.setPropertyUtils(new UnsortedPropertyUtils()); - Yaml yaml = new Yaml(repr); - String output = yaml.dump(getBean()); - // System.out.println(output); - assertEquals(Util.getLocalResource("issues/issue59-2.yaml"), output); + @Override + protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess bAccess) { + Set<Property> result = new TreeSet<Property>(Collections.reverseOrder()); + result.addAll(super.createPropertySet(type, bAccess)); + return result; } + } - private class UnsortedPropertyUtils extends PropertyUtils { - @Override - protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess bAccess) - throws IntrospectionException { - Set<Property> result = new LinkedHashSet<Property>(getPropertiesMap(type, - BeanAccess.FIELD).values()); - result.remove(result.iterator().next());// drop 'listInt' property - return result; - } - } + public void testUnsorted() { + Representer repr = new Representer(); + repr.setPropertyUtils(new UnsortedPropertyUtils()); + Yaml yaml = new Yaml(repr); + String output = yaml.dump(getBean()); + // System.out.println(output); + assertEquals(Util.getLocalResource("issues/issue59-2.yaml"), output); + } + + private class UnsortedPropertyUtils extends PropertyUtils { - private SkipBean getBean() { - SkipBean bean = new SkipBean(); - bean.setText("foo"); - bean.setListDate(null); - bean.setListInt(Arrays.asList(new Integer[] { null, 1, 2, 3 })); - bean.setListStr(Arrays.asList(new String[] { "bar", null, "foo", null })); - return bean; + @Override + protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess bAccess) { + Set<Property> result = + new LinkedHashSet<Property>(getPropertiesMap(type, BeanAccess.FIELD).values()); + result.remove(result.iterator().next());// drop 'listInt' property + return result; } + } + + private SkipBean getBean() { + SkipBean bean = new SkipBean(); + bean.setText("foo"); + bean.setListDate(null); + bean.setListInt(Arrays.asList(null, 1, 2, 3)); + bean.setListStr(Arrays.asList("bar", null, "foo", null)); + return bean; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue60/SkipBean.java b/src/test/java/org/yaml/snakeyaml/issues/issue60/SkipBean.java index 388feedb..6337afce 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue60/SkipBean.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue60/SkipBean.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue60; @@ -24,67 +22,67 @@ import java.util.Map; public class SkipBean { - private List<Integer> listInt; - private List<String> listStr; - private List<Date> listDate; - private List<File> empty = new ArrayList<File>(0); - private Map<String, Integer> map = new HashMap<String, Integer>(0); - private String text; - private Integer number; - - public List<Integer> getListInt() { - return listInt; - } - - public void setListInt(List<Integer> listInt) { - this.listInt = listInt; - } - - public List<String> getListStr() { - return listStr; - } - - public void setListStr(List<String> listStr) { - this.listStr = listStr; - } - - public List<Date> getListDate() { - return listDate; - } - - public void setListDate(List<Date> listDate) { - this.listDate = listDate; - } - - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } - - public Integer getNumber() { - return number; - } - - public void setNumber(Integer number) { - this.number = number; - } - - public List<File> getEmpty() { - return empty; - } - - public void setEmpty(List<File> empty) { - this.empty = empty; - } - - public Map<String, Integer> getMap() { - return map; - } - - public void setMap(Map<String, Integer> map) { - this.map = map; - } + private List<Integer> listInt; + private List<String> listStr; + private List<Date> listDate; + private List<File> empty = new ArrayList<File>(0); + private Map<String, Integer> map = new HashMap<String, Integer>(0); + private String text; + private Integer number; + + public List<Integer> getListInt() { + return listInt; + } + + public void setListInt(List<Integer> listInt) { + this.listInt = listInt; + } + + public List<String> getListStr() { + return listStr; + } + + public void setListStr(List<String> listStr) { + this.listStr = listStr; + } + + public List<Date> getListDate() { + return listDate; + } + + public void setListDate(List<Date> listDate) { + this.listDate = listDate; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public Integer getNumber() { + return number; + } + + public void setNumber(Integer number) { + this.number = number; + } + + public List<File> getEmpty() { + return empty; + } + + public void setEmpty(List<File> empty) { + this.empty = empty; + } + + public Map<String, Integer> getMap() { + return map; + } + + public void setMap(Map<String, Integer> map) { + this.map = map; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue60/SkipBeanTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue60/SkipBeanTest.java index 8cb6e0ef..208dfc91 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue60/SkipBeanTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue60/SkipBeanTest.java @@ -1,24 +1,20 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue60; import java.util.Arrays; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.introspector.Property; @@ -32,67 +28,68 @@ import org.yaml.snakeyaml.representer.Representer; public class SkipBeanTest extends TestCase { - public void testSkipNull() { - Yaml yaml = new Yaml(new SkipNullRepresenter()); - String output = yaml.dump(getBean()); - // System.out.println(output); - assertEquals(Util.getLocalResource("issues/issue60-1.yaml"), output); - } + public void testSkipNull() { + Yaml yaml = new Yaml(new SkipNullRepresenter()); + String output = yaml.dump(getBean()); + // System.out.println(output); + assertEquals(Util.getLocalResource("issues/issue60-1.yaml"), output); + } - private class SkipNullRepresenter extends Representer { - @Override - protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, - Object propertyValue, Tag customTag) { - if (propertyValue == null) { - return null; - } else { - return super - .representJavaBeanProperty(javaBean, property, propertyValue, customTag); - } - } - } + private class SkipNullRepresenter extends Representer { - public void testSkipEmptyCollections() { - Yaml yaml = new Yaml(new SkipEmptyRepresenter()); - String output = yaml.dump(getBean()); - // System.out.println(output); - assertEquals(Util.getLocalResource("issues/issue60-2.yaml"), output); + @Override + protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, + Object propertyValue, Tag customTag) { + if (propertyValue == null) { + return null; + } else { + return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag); + } } + } - private class SkipEmptyRepresenter extends Representer { - @Override - protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, - Object propertyValue, Tag customTag) { - NodeTuple tuple = super.representJavaBeanProperty(javaBean, property, propertyValue, - customTag); - Node valueNode = tuple.getValueNode(); - if (Tag.NULL.equals(valueNode.getTag())) { - return null;// skip 'null' values - } - if (valueNode instanceof CollectionNode) { - if (Tag.SEQ.equals(valueNode.getTag())) { - SequenceNode seq = (SequenceNode) valueNode; - if (seq.getValue().isEmpty()) { - return null;// skip empty lists - } - } - if (Tag.MAP.equals(valueNode.getTag())) { - MappingNode seq = (MappingNode) valueNode; - if (seq.getValue().isEmpty()) { - return null;// skip empty maps - } - } - } - return tuple; + public void testSkipEmptyCollections() { + Yaml yaml = new Yaml(new SkipEmptyRepresenter()); + String output = yaml.dump(getBean()); + // System.out.println(output); + assertEquals(Util.getLocalResource("issues/issue60-2.yaml"), output); + } + + private class SkipEmptyRepresenter extends Representer { + + @Override + protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, + Object propertyValue, Tag customTag) { + NodeTuple tuple = + super.representJavaBeanProperty(javaBean, property, propertyValue, customTag); + Node valueNode = tuple.getValueNode(); + if (Tag.NULL.equals(valueNode.getTag())) { + return null;// skip 'null' values + } + if (valueNode instanceof CollectionNode) { + if (Tag.SEQ.equals(valueNode.getTag())) { + SequenceNode seq = (SequenceNode) valueNode; + if (seq.getValue().isEmpty()) { + return null;// skip empty lists + } } + if (Tag.MAP.equals(valueNode.getTag())) { + MappingNode seq = (MappingNode) valueNode; + if (seq.getValue().isEmpty()) { + return null;// skip empty maps + } + } + } + return tuple; } + } - private SkipBean getBean() { - SkipBean bean = new SkipBean(); - bean.setText("foo"); - bean.setListDate(null); - bean.setListInt(Arrays.asList(new Integer[] { null, 1, 2, 3 })); - bean.setListStr(Arrays.asList(new String[] { "bar", null, "foo", null })); - return bean; - } + private SkipBean getBean() { + SkipBean bean = new SkipBean(); + bean.setText("foo"); + bean.setListDate(null); + bean.setListInt(Arrays.asList(null, 1, 2, 3)); + bean.setListStr(Arrays.asList("bar", null, "foo", null)); + return bean; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue61/GenericListBeanTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue61/GenericListBeanTest.java index f872aae6..97ae7624 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue61/GenericListBeanTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue61/GenericListBeanTest.java @@ -1,117 +1,116 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue61; import java.util.ArrayList; import java.util.List; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; public class GenericListBeanTest extends TestCase { - @SuppressWarnings("unchecked") - public void testGenericList() { - Yaml yaml = new Yaml(); - ListProvider<String> listProvider = new ListProvider<String>(); - listProvider.getList().add("foo"); - listProvider.getList().add("bar"); - String s = yaml.dumpAsMap(listProvider); - // System.out.println(s); - assertEquals("list:\n- foo\n- bar\n", s); - // parse - Yaml loader = new Yaml(); - ListProvider<String> listProvider2 = loader.loadAs(s, ListProvider.class); - assertEquals("foo", listProvider2.getList().get(0)); - assertEquals("bar", listProvider2.getList().get(1)); - assertEquals(listProvider, listProvider2); + + @SuppressWarnings("unchecked") + public void testGenericList() { + Yaml yaml = new Yaml(); + ListProvider<String> listProvider = new ListProvider<String>(); + listProvider.getList().add("foo"); + listProvider.getList().add("bar"); + String s = yaml.dumpAsMap(listProvider); + // System.out.println(s); + assertEquals("list:\n- foo\n- bar\n", s); + // parse + Yaml loader = new Yaml(); + ListProvider<String> listProvider2 = loader.loadAs(s, ListProvider.class); + assertEquals("foo", listProvider2.getList().get(0)); + assertEquals("bar", listProvider2.getList().get(1)); + assertEquals(listProvider, listProvider2); + } + + @SuppressWarnings("rawtypes") + public void testGenericBean() { + Yaml yaml = new Yaml(); + ListProvider<Bean> listProvider = new ListProvider<Bean>(); + Bean foo = new Bean(); + foo.setName("foo"); + listProvider.getList().add(foo); + Bean bar = new Bean(); + bar.setName("bar"); + bar.setNumber(3); + listProvider.getList().add(bar); + String s = yaml.dumpAsMap(listProvider); + // System.out.println(s); + String etalon = Util.getLocalResource("issues/issue61-1.yaml"); + assertEquals(etalon, s); + // parse + Yaml loader = new Yaml(); + ListProvider listProvider2 = loader.loadAs(s, ListProvider.class); + Bean foo2 = (Bean) listProvider2.getList().get(0); + assertEquals("foo", foo2.getName()); + assertEquals(0, foo2.getNumber()); + Bean bar2 = (Bean) listProvider2.getList().get(1); + assertEquals("bar", bar2.getName()); + assertEquals(3, bar2.getNumber()); + } + + public static class ListProvider<T> { + + private List<T> list = new ArrayList<T>(); + + public List<T> getList() { + return list; + } + + public void setList(List<T> list) { + this.list = list; } @SuppressWarnings("rawtypes") - public void testGenericBean() { - Yaml yaml = new Yaml(); - ListProvider<Bean> listProvider = new ListProvider<Bean>(); - Bean foo = new Bean(); - foo.setName("foo"); - listProvider.getList().add(foo); - Bean bar = new Bean(); - bar.setName("bar"); - bar.setNumber(3); - listProvider.getList().add(bar); - String s = yaml.dumpAsMap(listProvider); - // System.out.println(s); - String etalon = Util.getLocalResource("issues/issue61-1.yaml"); - assertEquals(etalon, s); - // parse - Yaml loader = new Yaml(); - ListProvider listProvider2 = loader.loadAs(s, ListProvider.class); - Bean foo2 = (Bean) listProvider2.getList().get(0); - assertEquals("foo", foo2.getName()); - assertEquals(0, foo2.getNumber()); - Bean bar2 = (Bean) listProvider2.getList().get(1); - assertEquals("bar", bar2.getName()); - assertEquals(3, bar2.getNumber()); + @Override + public boolean equals(Object obj) { + if (obj instanceof ListProvider) { + return list.equals(((ListProvider) obj).getList()); + } else { + return false; + } } - public static class ListProvider<T> { - private List<T> list = new ArrayList<T>(); - - public List<T> getList() { - return list; - } - - public void setList(List<T> list) { - this.list = list; - } - - @SuppressWarnings("rawtypes") - @Override - public boolean equals(Object obj) { - if (obj instanceof ListProvider) { - return list.equals(((ListProvider) obj).getList()); - } else { - return false; - } - } - - @Override - public int hashCode() { - return list.hashCode(); - } + @Override + public int hashCode() { + return list.hashCode(); } + } + + public static class Bean { - public static class Bean { - private String name; - private int number; + private String name; + private int number; - public String getName() { - return name; - } + public String getName() { + return name; + } - public void setName(String name) { - this.name = name; - } + public void setName(String name) { + this.name = name; + } - public int getNumber() { - return number; - } + public int getNumber() { + return number; + } - public void setNumber(int number) { - this.number = number; - } + public void setNumber(int number) { + this.number = number; } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue61/GenericMapBeanTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue61/GenericMapBeanTest.java index fd1eff27..4fdca222 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue61/GenericMapBeanTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue61/GenericMapBeanTest.java @@ -1,118 +1,116 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue61; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; public class GenericMapBeanTest extends TestCase { - @SuppressWarnings("unchecked") - public void testGenericMap() { - Yaml yaml = new Yaml(); - MapProvider<String, Integer> listProvider = new MapProvider<String, Integer>(); - listProvider.getMap().put("foo", 17); - listProvider.getMap().put("bar", 19); - String s = yaml.dumpAsMap(listProvider); - // System.out.println(s); - assertEquals("map:\n foo: 17\n bar: 19\n", s); - // parse - Yaml loader = new Yaml(); - MapProvider<String, Integer> listProvider2 = loader.loadAs(s, MapProvider.class); - assertEquals(new Integer(17), listProvider2.getMap().get("foo")); - assertEquals(new Integer(19), listProvider2.getMap().get("bar")); - assertEquals(listProvider, listProvider2); + + @SuppressWarnings("unchecked") + public void testGenericMap() { + Yaml yaml = new Yaml(); + MapProvider<String, Integer> listProvider = new MapProvider<String, Integer>(); + listProvider.getMap().put("foo", 17); + listProvider.getMap().put("bar", 19); + String s = yaml.dumpAsMap(listProvider); + // System.out.println(s); + assertEquals("map:\n foo: 17\n bar: 19\n", s); + // parse + Yaml loader = new Yaml(); + MapProvider<String, Integer> listProvider2 = loader.loadAs(s, MapProvider.class); + assertEquals(Integer.valueOf(17), listProvider2.getMap().get("foo")); + assertEquals(Integer.valueOf(19), listProvider2.getMap().get("bar")); + assertEquals(listProvider, listProvider2); + } + + @SuppressWarnings("rawtypes") + public void testGenericBean() { + Yaml yaml = new Yaml(); + MapProvider<String, Bean> listProvider = new MapProvider<String, Bean>(); + Bean foo = new Bean(); + foo.setName("foo"); + listProvider.getMap().put("foo", foo); + Bean bar = new Bean(); + bar.setName("bar"); + bar.setNumber(3); + listProvider.getMap().put("bar", bar); + String s = yaml.dumpAsMap(listProvider); + // System.out.println(s); + String etalon = Util.getLocalResource("issues/issue61-2.yaml"); + assertEquals(etalon, s); + // parse + Yaml loader = new Yaml(); + MapProvider listProvider2 = loader.loadAs(s, MapProvider.class); + Bean foo2 = (Bean) listProvider2.getMap().get("foo"); + assertEquals("foo", foo2.getName()); + assertEquals(0, foo2.getNumber()); + Bean bar2 = (Bean) listProvider2.getMap().get("bar"); + assertEquals("bar", bar2.getName()); + assertEquals(3, bar2.getNumber()); + } + + public static class MapProvider<K, V> { + + private Map<K, V> map = new LinkedHashMap<K, V>(); + + public Map<K, V> getMap() { + return map; + } + + public void setMap(Map<K, V> map) { + this.map = map; } @SuppressWarnings("rawtypes") - public void testGenericBean() { - Yaml yaml = new Yaml(); - MapProvider<String, Bean> listProvider = new MapProvider<String, Bean>(); - Bean foo = new Bean(); - foo.setName("foo"); - listProvider.getMap().put("foo", foo); - Bean bar = new Bean(); - bar.setName("bar"); - bar.setNumber(3); - listProvider.getMap().put("bar", bar); - String s = yaml.dumpAsMap(listProvider); - // System.out.println(s); - String etalon = Util.getLocalResource("issues/issue61-2.yaml"); - assertEquals(etalon, s); - // parse - Yaml loader = new Yaml(); - MapProvider listProvider2 = loader.loadAs(s, MapProvider.class); - Bean foo2 = (Bean) listProvider2.getMap().get("foo"); - assertEquals("foo", foo2.getName()); - assertEquals(0, foo2.getNumber()); - Bean bar2 = (Bean) listProvider2.getMap().get("bar"); - assertEquals("bar", bar2.getName()); - assertEquals(3, bar2.getNumber()); + @Override + public boolean equals(Object obj) { + if (obj instanceof MapProvider) { + return map.equals(((MapProvider) obj).getMap()); + } else { + return false; + } } - public static class MapProvider<K, V> { - private Map<K, V> map = new LinkedHashMap<K, V>(); - - public Map<K, V> getMap() { - return map; - } - - public void setMap(Map<K, V> map) { - this.map = map; - } - - @SuppressWarnings("rawtypes") - @Override - public boolean equals(Object obj) { - if (obj instanceof MapProvider) { - return map.equals(((MapProvider) obj).getMap()); - } else { - return false; - } - } - - @Override - public int hashCode() { - return map.hashCode(); - } + @Override + public int hashCode() { + return map.hashCode(); } + } + + public static class Bean { - public static class Bean { - private String name; - private int number; + private String name; + private int number; - public String getName() { - return name; - } + public String getName() { + return name; + } - public void setName(String name) { - this.name = name; - } + public void setName(String name) { + this.name = name; + } - public int getNumber() { - return number; - } + public int getNumber() { + return number; + } - public void setNumber(int number) { - this.number = number; - } + public void setNumber(int number) { + this.number = number; } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue64/MethodDesc.java b/src/test/java/org/yaml/snakeyaml/issues/issue64/MethodDesc.java index 74ea7f51..26e710cb 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue64/MethodDesc.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue64/MethodDesc.java @@ -1,47 +1,45 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue64; import java.util.List; public class MethodDesc { - private String name; - private List<Class<?>> argTypes; - public MethodDesc() { - } + private String name; + private List<Class<?>> argTypes; - public MethodDesc(String name, List<Class<?>> argTypes) { - this.name = name; - this.argTypes = argTypes; - } + public MethodDesc() {} - public String getName() { - return name; - } + public MethodDesc(String name, List<Class<?>> argTypes) { + this.name = name; + this.argTypes = argTypes; + } - public void setName(String name) { - this.name = name; - } + public String getName() { + return name; + } - public List<Class<?>> getArgTypes() { - return argTypes; - } + public void setName(String name) { + this.name = name; + } - public void setArgTypes(List<Class<?>> argTypes) { - this.argTypes = argTypes; - } + public List<Class<?>> getArgTypes() { + return argTypes; + } + + public void setArgTypes(List<Class<?>> argTypes) { + this.argTypes = argTypes; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue64/ParameterizedTypeTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue64/ParameterizedTypeTest.java index 50c7bc80..828e8c6d 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue64/ParameterizedTypeTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue64/ParameterizedTypeTest.java @@ -1,25 +1,21 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue64; import java.util.LinkedList; import java.util.List; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.AbstractConstruct; import org.yaml.snakeyaml.constructor.Constructor; @@ -31,56 +27,59 @@ import org.yaml.snakeyaml.representer.Representer; public class ParameterizedTypeTest extends TestCase { - public void testRepresenter() { - Yaml yaml = new Yaml(new ClassConstructor(), new ClassRepresenter()); + public void testRepresenter() { + Yaml yaml = new Yaml(new ClassConstructor(), new ClassRepresenter()); + + String methodName = "testMethod"; + List<Class<?>> argTypes = new LinkedList<Class<?>>(); + argTypes.add(String.class); + argTypes.add(Integer.class); + argTypes.add(Boolean.class); + MethodDesc methodDesc = new MethodDesc(methodName, argTypes); - String methodName = "testMethod"; - List<Class<?>> argTypes = new LinkedList<Class<?>>(); - argTypes.add(String.class); - argTypes.add(Integer.class); - argTypes.add(Boolean.class); - MethodDesc methodDesc = new MethodDesc(methodName, argTypes); + String out = yaml.dump(methodDesc); + // System.out.println(out); + assertEquals( + "!!org.yaml.snakeyaml.issues.issue64.MethodDesc\nargTypes: [!clazz 'String', !clazz 'Integer', !clazz 'Boolean']\nname: testMethod\n", + out); + MethodDesc parsed = yaml.load(out); + assertEquals(methodName, parsed.getName()); + List<Class<?>> argTypes2 = parsed.getArgTypes(); + assertEquals(3, argTypes2.size()); + assertEquals(argTypes, argTypes2); + } - String out = yaml.dump(methodDesc); - // System.out.println(out); - assertEquals( - "!!org.yaml.snakeyaml.issues.issue64.MethodDesc\nargTypes: [!clazz 'String', !clazz 'Integer', !clazz 'Boolean']\nname: testMethod\n", - out); - MethodDesc parsed = (MethodDesc) yaml.load(out); - assertEquals(methodName, parsed.getName()); - List<Class<?>> argTypes2 = parsed.getArgTypes(); - assertEquals(3, argTypes2.size()); - assertEquals(argTypes, argTypes2); + static class ClassRepresenter extends Representer { + + public ClassRepresenter() { + this.representers.put(Class.class, new RepresentClass()); } - static class ClassRepresenter extends Representer { - public ClassRepresenter() { - this.representers.put(Class.class, new RepresentClass()); - } + private class RepresentClass implements Represent { - private class RepresentClass implements Represent { - public Node representData(Object data) { - Class<?> clazz = (Class<?>) data; - return representScalar(new Tag("!clazz"), clazz.getSimpleName()); - } - } + public Node representData(Object data) { + Class<?> clazz = (Class<?>) data; + return representScalar(new Tag("!clazz"), clazz.getSimpleName()); + } } + } - static class ClassConstructor extends Constructor { - public ClassConstructor() { - this.yamlConstructors.put(new Tag("!clazz"), new ConstructClass()); - } + static class ClassConstructor extends Constructor { + + public ClassConstructor() { + this.yamlConstructors.put(new Tag("!clazz"), new ConstructClass()); + } - private class ConstructClass extends AbstractConstruct { + private class ConstructClass extends AbstractConstruct { - public Object construct(Node node) { - String clazz = (String) constructScalar((ScalarNode) node); - try { - return Class.forName("java.lang." + clazz); - } catch (ClassNotFoundException e) { - throw new RuntimeException(e); - } - } + public Object construct(Node node) { + String clazz = constructScalar((ScalarNode) node); + try { + return Class.forName("java.lang." + clazz); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); } + } } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue67/NonAsciiCharsInClassNameTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue67/NonAsciiCharsInClassNameTest.java index 12191736..b9321ebf 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue67/NonAsciiCharsInClassNameTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue67/NonAsciiCharsInClassNameTest.java @@ -1,110 +1,104 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue67; import junit.framework.TestCase; - -import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.nodes.Tag; import org.yaml.snakeyaml.representer.Representer; -import org.yaml.snakeyaml.scanner.ScannerException; public class NonAsciiCharsInClassNameTest extends TestCase { - private String PREFIX = "!!org.yaml.snakeyaml.issues.issue67.NonAsciiCharsInClassNameTest$"; - - public void testDump() { - Académico obj = new Académico(); - obj.setId(1); - obj.setName("Foo bar baz"); - Yaml yaml = new Yaml(); - String result = yaml.dump(obj); - assertEquals(PREFIX + "Acad%C3%A9mico {\n id: 1, name: Foo bar baz}\n", result); - } - public void testLoad() { - Yaml yaml = new Yaml(); - Académico obj = (Académico) yaml.load(PREFIX + "Acad%C3%A9mico {id: 3, name: Foo bar}"); - assertEquals(3, obj.getId()); - assertEquals("Foo bar", obj.getName()); - } - - public void testLoadInvalidPattern() { - try { - Yaml yaml = new Yaml(); - yaml.load(PREFIX + "Acad%WZ%A9mico {id: 3, name: Foo bar}"); - fail("Illegal hex characters in escape (%) pattern must not be accepted."); - } catch (Exception e) { - assertEquals( - "while scanning a tag\n" - + " in 'string', line 1, column 1:\n" - + " !!org.yaml.snakeyaml.issues.issu ... \n" - + " ^\n" - + "expected URI escape sequence of 2 hexadecimal numbers, but found W(87) and Z(90)\n" - + " in 'string', line 1, column 71:\n" - + " ... nAsciiCharsInClassNameTest$Acad%WZ%A9mico {id: 3, name: Foo bar}\n" - + " ^\n", e.getMessage()); - } - } + private final String PREFIX = "!!org.yaml.snakeyaml.issues.issue67.NonAsciiCharsInClassNameTest$"; - public static class Académico { - public int getId() { - return id; - } + public void testDump() { + Académico obj = new Académico(); + obj.setId(1); + obj.setName("Foo bar baz"); + Yaml yaml = new Yaml(); + String result = yaml.dump(obj); + assertEquals(PREFIX + "Acad%C3%A9mico {\n id: 1, name: Foo bar baz}\n", result); + } - public void setId(int id) { - this.id = id; - } + public void testLoad() { + Yaml yaml = new Yaml(); + Académico obj = yaml.load(PREFIX + "Acad%C3%A9mico {id: 3, name: Foo bar}"); + assertEquals(3, obj.getId()); + assertEquals("Foo bar", obj.getName()); + } - public String getName() { - return name; - } + public void testLoadInvalidPattern() { + try { + Yaml yaml = new Yaml(); + yaml.load(PREFIX + "Acad%WZ%A9mico {id: 3, name: Foo bar}"); + fail("Illegal hex characters in escape (%) pattern must not be accepted."); + } catch (Exception e) { + assertEquals("while scanning a tag\n" + " in 'string', line 1, column 1:\n" + + " !!org.yaml.snakeyaml.issues.issu ... \n" + " ^\n" + + "expected URI escape sequence of 2 hexadecimal numbers, but found W(87) and Z(90)\n" + + " in 'string', line 1, column 71:\n" + + " ... nAsciiCharsInClassNameTest$Acad%WZ%A9mico {id: 3, name: Foo bar}\n" + + " ^\n", e.getMessage()); + } + } - public void setName(String name) { - this.name = name; - } + public static class Académico { - private int id; - private String name; + public int getId() { + return id; } - public void testDumpCustomTag() { - Académico obj = new Académico(); - obj.setId(123); - obj.setName("Foo bar 123"); - Representer repr = new Representer(); - repr.addClassTag(Académico.class, new Tag("!foo")); - Yaml yaml = new Yaml(repr); - String result = yaml.dump(obj); - assertEquals("!foo {id: 123, name: Foo bar 123}\n", result); + public void setId(int id) { + this.id = id; } - public void testDumpEscapedTag() { - Académico obj = new Académico(); - obj.setId(123); - obj.setName("Foo bar 123"); - Representer repr = new Representer(); - repr.addClassTag(Académico.class, new Tag("!Académico")); - Yaml yaml = new Yaml(repr); - String result = yaml.dump(obj); - assertEquals("!Acad%C3%A9mico {id: 123, name: Foo bar 123}\n", result); + public String getName() { + return name; } - public void testTag() { - Tag tag = new Tag("!java/javabean:foo.Bar"); - assertEquals("!java/javabean:foo.Bar", tag.getValue()); + public void setName(String name) { + this.name = name; } + + private int id; + private String name; + } + + public void testDumpCustomTag() { + Académico obj = new Académico(); + obj.setId(123); + obj.setName("Foo bar 123"); + Representer repr = new Representer(); + repr.addClassTag(Académico.class, new Tag("!foo")); + Yaml yaml = new Yaml(repr); + String result = yaml.dump(obj); + assertEquals("!foo {id: 123, name: Foo bar 123}\n", result); + } + + public void testDumpEscapedTag() { + Académico obj = new Académico(); + obj.setId(123); + obj.setName("Foo bar 123"); + Representer repr = new Representer(); + repr.addClassTag(Académico.class, new Tag("!Académico")); + Yaml yaml = new Yaml(repr); + String result = yaml.dump(obj); + assertEquals("!Acad%C3%A9mico {id: 123, name: Foo bar 123}\n", result); + } + + public void testTag() { + Tag tag = new Tag("!java/javabean:foo.Bar"); + assertEquals("!java/javabean:foo.Bar", tag.getValue()); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue68/NonAsciiCharacterTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue68/NonAsciiCharacterTest.java index 9915abc4..79e6ca45 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue68/NonAsciiCharacterTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue68/NonAsciiCharacterTest.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue68; @@ -24,54 +22,52 @@ import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CodingErrorAction; +import java.nio.charset.StandardCharsets; import java.util.Map; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.YamlDocument; public class NonAsciiCharacterTest extends TestCase { - @SuppressWarnings("unchecked") - public void testLoad() { - Yaml yaml = new Yaml(); - Map<String, Map<String, String>> obj = (Map<String, Map<String, String>>) yaml - .load("test.string: {en: И}"); - assertEquals(1, obj.size()); - assertEquals("Map: " + obj.toString(), "И", obj.get("test.string").get("en")); - } + @SuppressWarnings("unchecked") + public void testLoad() { + Yaml yaml = new Yaml(); + Map<String, Map<String, String>> obj = yaml.load("test.string: {en: И}"); + assertEquals(1, obj.size()); + assertEquals("Map: " + obj, "И", obj.get("test.string").get("en")); + } - public void testLoadFromFileWithWrongEncoding() { - try { - Yaml yaml = new Yaml(); - InputStream input = new FileInputStream("src/test/resources/issues/issue68.txt"); - CharsetDecoder decoder = Charset.forName("Cp1252").newDecoder(); - decoder.onUnmappableCharacter(CodingErrorAction.REPORT); - Object text = yaml.load(new InputStreamReader(input, decoder)); - input.close(); - fail("Invalid UTF-8 must not be accepted: " + text.toString()); - } catch (Exception e) { - assertTrue(e.getMessage().endsWith("Exception: Input length = 1")); - } + public void testLoadFromFileWithWrongEncoding() { + try { + Yaml yaml = new Yaml(); + InputStream input = new FileInputStream("src/test/resources/issues/issue68.txt"); + CharsetDecoder decoder = Charset.forName("Cp1252").newDecoder(); + decoder.onUnmappableCharacter(CodingErrorAction.REPORT); + Object text = yaml.load(new InputStreamReader(input, decoder)); + input.close(); + fail("Invalid UTF-8 must not be accepted: " + text.toString()); + } catch (Exception e) { + assertTrue(e.getMessage().endsWith("Exception: Input length = 1")); } + } - public void testLoadFromFile() throws UnsupportedEncodingException, FileNotFoundException { - Yaml yaml = new Yaml(); - InputStream input = new FileInputStream("src/test/resources/issues/issue68.txt"); - String text = (String) yaml.load(new InputStreamReader(input, "UTF-8")); - assertEquals("И жить торопится и чувствовать спешит...", text); - } + public void testLoadFromFile() throws UnsupportedEncodingException, FileNotFoundException { + Yaml yaml = new Yaml(); + InputStream input = new FileInputStream("src/test/resources/issues/issue68.txt"); + String text = yaml.load(new InputStreamReader(input, StandardCharsets.UTF_8)); + assertEquals("И жить торопится и чувствовать спешит...", text); + } - public void testLoadFromInputStream() throws IOException { - InputStream input; - input = YamlDocument.class.getClassLoader().getResourceAsStream("issues/issue68.txt"); - if (input == null) { - throw new RuntimeException("Can not find issues/issue68.txt"); - } - Yaml yaml = new Yaml(); - String text = (String) yaml.load(input);// UTF-8 by default - assertEquals("И жить торопится и чувствовать спешит...", text); - input.close(); + public void testLoadFromInputStream() throws IOException { + InputStream input; + input = YamlDocument.class.getClassLoader().getResourceAsStream("issues/issue68.txt"); + if (input == null) { + throw new RuntimeException("Can not find issues/issue68.txt"); } + Yaml yaml = new Yaml(); + String text = yaml.load(input);// UTF-8 by default + assertEquals("И жить торопится и чувствовать спешит...", text); + input.close(); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue72/CollectionTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue72/CollectionTest.java index ca817e2e..92ee6909 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue72/CollectionTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue72/CollectionTest.java @@ -1,87 +1,85 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue72; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; public class CollectionTest extends TestCase { - public void testCollectionList() { - CollectionList bean = new CollectionList(); - Yaml yaml = new Yaml(); - String doc = yaml.dumpAsMap(bean); - // System.out.println(doc); - Yaml beanLoader = new Yaml(); - CollectionList parsed = beanLoader.loadAs(doc, CollectionList.class); - assertTrue(parsed.getNames().contains("aaa")); - assertTrue(parsed.getNames().contains("bbb")); - assertEquals(2, parsed.getNames().size()); - } + public void testCollectionList() { + CollectionList bean = new CollectionList(); + Yaml yaml = new Yaml(); + String doc = yaml.dumpAsMap(bean); + // System.out.println(doc); + Yaml beanLoader = new Yaml(); + CollectionList parsed = beanLoader.loadAs(doc, CollectionList.class); + assertTrue(parsed.getNames().contains("aaa")); + assertTrue(parsed.getNames().contains("bbb")); + assertEquals(2, parsed.getNames().size()); + } - public static class CollectionList { - private Collection<String> names; + public static class CollectionList { - public CollectionList() { - names = new ArrayList<String>(); - names.add("aaa"); - names.add("bbb"); - } + private Collection<String> names; - public Collection<String> getNames() { - return names; - } + public CollectionList() { + names = new ArrayList<String>(); + names.add("aaa"); + names.add("bbb"); + } - public void setNames(Collection<String> names) { - this.names = names; - } + public Collection<String> getNames() { + return names; } - public void testCollectionSet() { - CollectionSet bean = new CollectionSet(); - Yaml yaml = new Yaml(); - String doc = yaml.dumpAsMap(bean); - // System.out.println(doc); - Yaml beanLoader = new Yaml(); - CollectionSet parsed = beanLoader.loadAs(doc, CollectionSet.class); - assertTrue(parsed.getRoles().contains(11)); - assertTrue(parsed.getRoles().contains(13)); - assertEquals(2, parsed.getRoles().size()); + public void setNames(Collection<String> names) { + this.names = names; } + } - public static class CollectionSet { - private Collection<Integer> roles; + public void testCollectionSet() { + CollectionSet bean = new CollectionSet(); + Yaml yaml = new Yaml(); + String doc = yaml.dumpAsMap(bean); + // System.out.println(doc); + Yaml beanLoader = new Yaml(); + CollectionSet parsed = beanLoader.loadAs(doc, CollectionSet.class); + assertTrue(parsed.getRoles().contains(11)); + assertTrue(parsed.getRoles().contains(13)); + assertEquals(2, parsed.getRoles().size()); + } - public CollectionSet() { - roles = new HashSet<Integer>(); - roles.add(11); - roles.add(13); - } + public static class CollectionSet { - public Collection<Integer> getRoles() { - return roles; - } + private Collection<Integer> roles; + + public CollectionSet() { + roles = new HashSet<Integer>(); + roles.add(11); + roles.add(13); + } + + public Collection<Integer> getRoles() { + return roles; + } - public void setRoles(Collection<Integer> roles) { - this.roles = roles; - } + public void setRoles(Collection<Integer> roles) { + this.roles = roles; } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue73/ArrayListTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue73/ArrayListTest.java index b86c6b43..9fed36f3 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue73/ArrayListTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue73/ArrayListTest.java @@ -1,65 +1,62 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue73; import java.util.ArrayList; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; /** - * Test bean when the implementation is defined: ArrayList instead of just the - * interface List + * Test bean when the implementation is defined: ArrayList instead of just the interface List */ public class ArrayListTest extends TestCase { - public void testListImplementation() { - Bean1 bean = new Bean1(); - bean.setId("ID123"); - ArrayList<String> list = new ArrayList<String>(3); - list.add("zzz"); - list.add("xxx"); - list.add("ccc"); - bean.setList(list); - Yaml yaml = new Yaml(); - String doc = yaml.dump(bean); - // System.out.println(doc); - Bean1 loaded = (Bean1) yaml.load(doc); - assertEquals(3, loaded.getList().size()); - assertEquals(ArrayList.class, loaded.getList().getClass()); - } - - public static class Bean1 { - private ArrayList<String> list; - private String id; - public ArrayList<String> getList() { - return list; - } + public void testListImplementation() { + Bean1 bean = new Bean1(); + bean.setId("ID123"); + ArrayList<String> list = new ArrayList<String>(3); + list.add("zzz"); + list.add("xxx"); + list.add("ccc"); + bean.setList(list); + Yaml yaml = new Yaml(); + String doc = yaml.dump(bean); + // System.out.println(doc); + Bean1 loaded = yaml.load(doc); + assertEquals(3, loaded.getList().size()); + assertEquals(ArrayList.class, loaded.getList().getClass()); + } + + public static class Bean1 { + + private ArrayList<String> list; + private String id; + + public ArrayList<String> getList() { + return list; + } - public void setList(ArrayList<String> list) { - this.list = list; - } + public void setList(ArrayList<String> list) { + this.list = list; + } - public String getId() { - return id; - } + public String getId() { + return id; + } - public void setId(String id) { - this.id = id; - } + public void setId(String id) { + this.id = id; } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue73/Blog.java b/src/test/java/org/yaml/snakeyaml/issues/issue73/Blog.java index 1e0be237..e7b80627 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue73/Blog.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue73/Blog.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue73; @@ -21,59 +19,59 @@ import java.util.TreeSet; public class Blog { - private String name; - private Set<Post> posts = new TreeSet<Post>(); - public Set<Integer> numbers = new LinkedHashSet<Integer>(); - private TreeSet<String> labels = new TreeSet<String>(); + private String name; + private Set<Post> posts = new TreeSet<Post>(); + public Set<Integer> numbers = new LinkedHashSet<Integer>(); + private TreeSet<String> labels = new TreeSet<String>(); - public Blog() { - name = "SuperBlog"; - } + public Blog() { + name = "SuperBlog"; + } - public Blog(String name) { - this.name = name; - } + public Blog(String name) { + this.name = name; + } - public void addPost(Post p) { - posts.add(p); - } + public void addPost(Post p) { + posts.add(p); + } - public Set<Post> getPosts() { - return posts; - } + public Set<Post> getPosts() { + return posts; + } - public String getName() { - return name; - } + public String getName() { + return name; + } - public void setName(String name) { - this.name = name; - } + public void setName(String name) { + this.name = name; + } - public void setPosts(Set<Post> posts) { - this.posts = posts; - } + public void setPosts(Set<Post> posts) { + this.posts = posts; + } - public TreeSet<String> getLabels() { - return labels; - } + public TreeSet<String> getLabels() { + return labels; + } - public void setLabels(TreeSet<String> labels) { - this.labels = labels; - } + public void setLabels(TreeSet<String> labels) { + this.labels = labels; + } - @Override - public boolean equals(Object obj) { - return name.equals(obj.toString()); - } + @Override + public boolean equals(Object obj) { + return name.equals(obj.toString()); + } - @Override - public int hashCode() { - return name.hashCode(); - } + @Override + public int hashCode() { + return name.hashCode(); + } - @Override - public String toString() { - return "Blog '" + name + "'"; - } -}
\ No newline at end of file + @Override + public String toString() { + return "Blog '" + name + "'"; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue73/DumpSetAsSequenceExampleTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue73/DumpSetAsSequenceExampleTest.java index a656a68b..cf465a81 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue73/DumpSetAsSequenceExampleTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue73/DumpSetAsSequenceExampleTest.java @@ -1,25 +1,21 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue73; import java.util.Set; import java.util.TreeSet; - import junit.framework.TestCase; - import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions.FlowStyle; import org.yaml.snakeyaml.Util; @@ -32,67 +28,69 @@ import org.yaml.snakeyaml.representer.Representer; public class DumpSetAsSequenceExampleTest extends TestCase { - public void testDumpFlow() { - DumperOptions options = new DumperOptions(); - options.setAllowReadOnlyProperties(true); - Yaml yaml = new Yaml(new SetRepresenter(), options); - String output = yaml.dump(createBlog()); - // System.out.println(output); - assertEquals(Util.getLocalResource("issues/issue73-dump7.txt"), output); - // - check(output); - } + public void testDumpFlow() { + DumperOptions options = new DumperOptions(); + options.setAllowReadOnlyProperties(true); + Yaml yaml = new Yaml(new SetRepresenter(), options); + String output = yaml.dump(createBlog()); + // System.out.println(output); + assertEquals(Util.getLocalResource("issues/issue73-dump7.txt"), output); + // + check(output); + } - public void testDumpBlock() { - DumperOptions options = new DumperOptions(); - options.setAllowReadOnlyProperties(true); - options.setDefaultFlowStyle(FlowStyle.BLOCK); - Yaml yaml = new Yaml(new SetRepresenter(), options); - String output = yaml.dump(createBlog()); - // System.out.println(output); - assertEquals(Util.getLocalResource("issues/issue73-dump8.txt"), output); - // - check(output); + public void testDumpBlock() { + DumperOptions options = new DumperOptions(); + options.setAllowReadOnlyProperties(true); + options.setDefaultFlowStyle(FlowStyle.BLOCK); + Yaml yaml = new Yaml(new SetRepresenter(), options); + String output = yaml.dump(createBlog()); + // System.out.println(output); + assertEquals(Util.getLocalResource("issues/issue73-dump8.txt"), output); + // + check(output); + } + + private class SetRepresenter extends Representer { + + public SetRepresenter() { + this.multiRepresenters.put(Set.class, new RepresentIterable()); } - private class SetRepresenter extends Representer { - public SetRepresenter() { - this.multiRepresenters.put(Set.class, new RepresentIterable()); - } + private class RepresentIterable implements Represent { - private class RepresentIterable implements Represent { - @SuppressWarnings("unchecked") - public Node representData(Object data) { - return representSequence(getTag(data.getClass(), Tag.SEQ), (Iterable<Object>) data, - null); + @SuppressWarnings("unchecked") + public Node representData(Object data) { + return representSequence(getTag(data.getClass(), Tag.SEQ), (Iterable<Object>) data, + DumperOptions.FlowStyle.AUTO); - } - } + } } + } - private Blog createBlog() { - Blog blog = new Blog("Test Me!"); - blog.addPost(new Post("Title1", "text 1")); - blog.addPost(new Post("Title2", "text text 2")); - blog.numbers.add(19); - blog.numbers.add(17); - TreeSet<String> labels = new TreeSet<String>(); - labels.add("Java"); - labels.add("YAML"); - labels.add("SnakeYAML"); - blog.setLabels(labels); - return blog; - } + private Blog createBlog() { + Blog blog = new Blog("Test Me!"); + blog.addPost(new Post("Title1", "text 1")); + blog.addPost(new Post("Title2", "text text 2")); + blog.numbers.add(19); + blog.numbers.add(17); + TreeSet<String> labels = new TreeSet<String>(); + labels.add("Java"); + labels.add("YAML"); + labels.add("SnakeYAML"); + blog.setLabels(labels); + return blog; + } - private void check(String doc) { - Yaml yamlLoader = new Yaml(); - yamlLoader.setBeanAccess(BeanAccess.FIELD); - Blog blog = (Blog) yamlLoader.load(doc); - assertEquals("Test Me!", blog.getName()); - assertEquals(2, blog.numbers.size()); - assertEquals(2, blog.getPosts().size()); - for (Post post : blog.getPosts()) { - assertEquals(Post.class, post.getClass()); - } + private void check(String doc) { + Yaml yamlLoader = new Yaml(); + yamlLoader.setBeanAccess(BeanAccess.FIELD); + Blog blog = yamlLoader.load(doc); + assertEquals("Test Me!", blog.getName()); + assertEquals(2, blog.numbers.size()); + assertEquals(2, blog.getPosts().size()); + for (Post post : blog.getPosts()) { + assertEquals(Post.class, post.getClass()); } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue73/Post.java b/src/test/java/org/yaml/snakeyaml/issues/issue73/Post.java index 17395495..9204d39e 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue73/Post.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue73/Post.java @@ -1,62 +1,59 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue73; class Post implements Comparable<Post> { - private String title; - private String text; + private String title; + private String text; - protected Post() { - } + protected Post() {} - public Post(String title, String text) { - super(); - this.title = title; - this.text = text; - } + public Post(String title, String text) { + super(); + this.title = title; + this.text = text; + } - public String getTitle() { - return title; - } + public String getTitle() { + return title; + } - public String getText() { - return text; - } + public String getText() { + return text; + } - public int compareTo(Post o) { - return title.compareTo(o.title); - } + public int compareTo(Post o) { + return title.compareTo(o.title); + } - @Override - public boolean equals(Object obj) { - if (obj instanceof Post) { - return toString().equals(obj.toString()); - } else { - return false; - } + @Override + public boolean equals(Object obj) { + if (obj instanceof Post) { + return toString().equals(obj.toString()); + } else { + return false; } + } - @Override - public int hashCode() { - return toString().hashCode(); - } + @Override + public int hashCode() { + return toString().hashCode(); + } - @Override - public String toString() { - return "Post " + title + " " + text; - } -}
\ No newline at end of file + @Override + public String toString() { + return "Post " + title + " " + text; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue73/RecursiveSetTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue73/RecursiveSetTest.java index ef8b01fe..48aedff9 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue73/RecursiveSetTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue73/RecursiveSetTest.java @@ -1,119 +1,120 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue73; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Set; - import junit.framework.TestCase; - +import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; public class RecursiveSetTest extends TestCase { - public void testDumpException() { - Set<Object> set1 = new HashSet<Object>(); - Set<Object> set2 = new HashSet<Object>(); - set1.add(set2); - set2.add(set1); - Yaml yaml = new Yaml(); - try { - yaml.dump(set1); - fail("Recursive sets are not supported."); - } catch (StackOverflowError e) { - assertEquals(null, e.getMessage()); - } + + public void testDumpException() { + Set<Object> set1 = new HashSet<Object>(); + Set<Object> set2 = new HashSet<Object>(); + set1.add(set2); + set2.add(set1); + Yaml yaml = new Yaml(); + try { + yaml.dump(set1); + fail("Recursive sets are not supported."); + } catch (StackOverflowError e) { + assertNull(e.getMessage()); } + } - public void testLoadException() { - String doc = Util.getLocalResource("issues/issue73-recursive4.txt"); - // System.out.println(doc); - Yaml yaml = new Yaml(); - try { - yaml.load(doc); - fail("Recursive sets are not supported."); - } catch (Exception e) { - assertTrue(e.getMessage(), e.getMessage().contains("Set cannot be recursive.")); - } + public void testLoadException() { + String doc = Util.getLocalResource("issues/issue73-recursive4.txt"); + // System.out.println(doc); + Yaml yaml = new Yaml(); + try { + yaml.load(doc); + fail("Recursive sets are not supported."); + } catch (Exception e) { + assertTrue(e.getMessage(), e.getMessage().contains("Set cannot be recursive.")); } + } - /** - * XXX: sets can be recursive - */ - @SuppressWarnings("unchecked") - public void testLoadRecursiveTest() { - String doc = Util.getLocalResource("issues/issue73-recursive5.txt"); - // System.out.println(doc); - Yaml yaml = new Yaml(); - Bean1 obj = (Bean1) yaml.load(doc); - Set<Object> set = obj.getSet(); - // System.out.println(set); - assertEquals(LinkedHashSet.class, set.getClass()); - assertEquals("ID123", obj.getId()); - assertEquals(3, set.size()); - assertTrue(set.remove("zzz")); - assertTrue(set.remove("ccc")); - assertFalse(set.contains("111")); - try { - set.contains(set); - fail("Recursive set fails to provide a hashcode."); - } catch (StackOverflowError e) { - // ignore - } - // - Set<Object> self = (Set<Object>) set.iterator().next(); - assertEquals(LinkedHashSet.class, self.getClass()); - assertEquals(set, self); - assertSame(set, self); - assertEquals(1, set.size()); - assertEquals(1, self.size()); - set.add("111"); - assertEquals(2, set.size()); - assertEquals(2, self.size()); - // - self.clear(); - assertTrue(self.isEmpty()); - assertTrue(set.isEmpty()); - assertFalse("Now it should not be recursive any longer (no StackOverflowError).", - set.contains(set)); - // - set.add("jjj"); - assertEquals(1, set.size()); - assertEquals(1, self.size()); + /** + * XXX: sets can be recursive + */ + @SuppressWarnings("unchecked") + public void testLoadRecursiveTest() { + String doc = Util.getLocalResource("issues/issue73-recursive5.txt"); + // System.out.println(doc); + LoaderOptions options = new LoaderOptions(); + options.setAllowRecursiveKeys(true); + Yaml yaml = new Yaml(options); + Bean1 obj = yaml.load(doc); + Set<Object> set = obj.getSet(); + // System.out.println(set); + assertEquals(LinkedHashSet.class, set.getClass()); + assertEquals("ID123", obj.getId()); + assertEquals(3, set.size()); + assertTrue(set.remove("zzz")); + assertTrue(set.remove("ccc")); + assertFalse(set.contains("111")); + try { + set.contains(set); + fail("Recursive set fails to provide a hashcode."); + } catch (StackOverflowError e) { + // ignore } + // + Set<Object> self = (Set<Object>) set.iterator().next(); + assertEquals(LinkedHashSet.class, self.getClass()); + assertEquals(set, self); + assertSame(set, self); + assertEquals(1, set.size()); + assertEquals(1, self.size()); + set.add("111"); + assertEquals(2, set.size()); + assertEquals(2, self.size()); + // + self.clear(); + assertTrue(self.isEmpty()); + assertTrue(set.isEmpty()); + assertFalse("Now it should not be recursive any longer (no StackOverflowError).", + set.contains(set)); + // + set.add("jjj"); + assertEquals(1, set.size()); + assertEquals(1, self.size()); + } - public static class Bean1 { - private Set<Object> set; - private String id; + public static class Bean1 { - public Set<Object> getSet() { - return set; - } + private Set<Object> set; + private String id; - public void setSet(Set<Object> set) { - this.set = set; - } + public Set<Object> getSet() { + return set; + } - public String getId() { - return id; - } + public void setSet(Set<Object> set) { + this.set = set; + } + + public String getId() { + return id; + } - public void setId(String id) { - this.id = id; - } + public void setId(String id) { + this.id = id; } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue73/RecursiveSortedSetTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue73/RecursiveSortedSetTest.java index 52244298..6229f897 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue73/RecursiveSortedSetTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue73/RecursiveSortedSetTest.java @@ -1,127 +1,128 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue73; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; - import junit.framework.TestCase; - +import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; public class RecursiveSortedSetTest extends TestCase { - public void testDumpException() { - SortedSet<Object> set = new TreeSet<Object>(); - Bean11 bean = new Bean11(); - bean.setId("ID555"); - bean.setSet(set); - set.add("ggg"); - set.add("hhh"); - set.add(bean); - Yaml yaml = new Yaml(); - String doc = yaml.dump(bean); - // System.out.println(doc); - assertEquals(Util.getLocalResource("issues/issue73-recursive9.txt"), doc); - } - public void testLoadException() { - String doc = Util.getLocalResource("issues/issue73-recursive10.txt"); - // System.out.println(doc); - Yaml yaml = new Yaml(); - try { - yaml.load(doc); - fail("Recursive sets are not supported."); - } catch (Exception e) { - assertTrue(e.getMessage(), e.getMessage().contains("Set cannot be recursive.")); - } - } + public void testDumpException() { + SortedSet<Object> set = new TreeSet<Object>(); + Bean11 bean = new Bean11(); + bean.setId("ID555"); + bean.setSet(set); + set.add("ggg"); + set.add("hhh"); + set.add(bean); + Yaml yaml = new Yaml(); + String doc = yaml.dump(bean); + // System.out.println(doc); + assertEquals(Util.getLocalResource("issues/issue73-recursive9.txt"), doc); + } - /** - * set and JavaBean refer to each other - */ - public void testLoadRecursiveTest() { - String doc = Util.getLocalResource("issues/issue73-recursive9.txt"); - // System.out.println(doc); - Yaml yaml = new Yaml(); - Bean11 beanWithSet = (Bean11) yaml.load(doc); - Set<Object> set = beanWithSet.getSet(); - assertEquals(TreeSet.class, set.getClass()); - assertEquals("ID555", beanWithSet.getId()); - assertEquals(3, set.size()); - assertTrue(set.remove("ggg")); - // assertFalse(set.remove("ggg"));??? - assertTrue(set.remove("hhh")); - assertEquals(1, set.size()); - // - Bean11 beanRef = (Bean11) set.iterator().next(); - assertEquals(beanWithSet, beanRef); - assertSame(beanWithSet, beanRef); - // - assertFalse(set.isEmpty()); - assertTrue(set.contains(beanWithSet)); - assertFalse(set.add(beanWithSet)); - assertTrue(set.remove(beanWithSet)); - assertFalse(set.remove(beanWithSet)); - assertTrue(set.isEmpty()); + public void testLoadException() { + String doc = Util.getLocalResource("issues/issue73-recursive10.txt"); + // System.out.println(doc); + Yaml yaml = new Yaml(); + try { + yaml.load(doc); + fail("Recursive sets are not supported."); + } catch (Exception e) { + assertTrue(e.getMessage(), e.getMessage().contains("Set cannot be recursive.")); } + } - public static class Bean11 implements Comparable<Object> { - private SortedSet<Object> set; - private String id; + /** + * set and JavaBean refer to each other + */ + public void testLoadRecursiveTest() { + String doc = Util.getLocalResource("issues/issue73-recursive9.txt"); + // System.out.println(doc); + LoaderOptions options = new LoaderOptions(); + options.setAllowRecursiveKeys(true); + Yaml yaml = new Yaml(options); + Bean11 beanWithSet = yaml.load(doc); + Set<Object> set = beanWithSet.getSet(); + assertEquals(TreeSet.class, set.getClass()); + assertEquals("ID555", beanWithSet.getId()); + assertEquals(3, set.size()); + assertTrue(set.remove("ggg")); + // assertFalse(set.remove("ggg"));??? + assertTrue(set.remove("hhh")); + assertEquals(1, set.size()); + // + Bean11 beanRef = (Bean11) set.iterator().next(); + assertEquals(beanWithSet, beanRef); + assertSame(beanWithSet, beanRef); + // + assertFalse(set.isEmpty()); + assertTrue(set.contains(beanWithSet)); + assertFalse(set.add(beanWithSet)); + assertTrue(set.remove(beanWithSet)); + assertFalse(set.remove(beanWithSet)); + assertTrue(set.isEmpty()); + } - public SortedSet<Object> getSet() { - return set; - } + public static class Bean11 implements Comparable<Object> { - public void setSet(SortedSet<Object> set) { - this.set = set; - } + private SortedSet<Object> set; + private String id; - public String getId() { - return id; - } + public SortedSet<Object> getSet() { + return set; + } - public void setId(String id) { - this.id = id; - } + public void setSet(SortedSet<Object> set) { + this.set = set; + } - public int compareTo(Object o) { - return toString().compareTo(o.toString()); - } + public String getId() { + return id; + } - @Override - public boolean equals(Object obj) { - if (obj instanceof Bean11) { - Bean11 b = (Bean11) obj; - return id.equals(b.id); - } else { - return false; - } - } + public void setId(String id) { + this.id = id; + } + + public int compareTo(Object o) { + return toString().compareTo(o.toString()); + } - @Override - public int hashCode() { - return toString().hashCode(); - } + @Override + public boolean equals(Object obj) { + if (obj instanceof Bean11) { + Bean11 b = (Bean11) obj; + return id.equals(b.id); + } else { + return false; + } + } + + @Override + public int hashCode() { + return toString().hashCode(); + } - @Override - public String toString() { - return "Bean id=" + id + "set=" + System.identityHashCode(set); - } + @Override + public String toString() { + return "Bean id=" + id + "set=" + System.identityHashCode(set); } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue73/SetAsSequenceTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue73/SetAsSequenceTest.java index 7d3b4afe..74f6a92e 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue73/SetAsSequenceTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue73/SetAsSequenceTest.java @@ -1,27 +1,22 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue73; import java.util.HashSet; -import java.util.LinkedHashSet; import java.util.Set; import java.util.TreeSet; - import junit.framework.TestCase; - import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; @@ -29,106 +24,106 @@ import org.yaml.snakeyaml.introspector.BeanAccess; public class SetAsSequenceTest extends TestCase { - public void testDump() { - Blog blog = new Blog("Test Me!"); - blog.addPost(new Post("Title1", "text 1")); - blog.addPost(new Post("Title2", "text text 2")); - blog.numbers.add(19); - blog.numbers.add(17); - TreeSet<String> labels = new TreeSet<String>(); - labels.add("Java"); - labels.add("YAML"); - labels.add("SnakeYAML"); - blog.setLabels(labels); - DumperOptions options = new DumperOptions(); - options.setAllowReadOnlyProperties(true); - Yaml yaml = new Yaml(options); - String output = yaml.dump(blog); - // System.out.println(output); - assertEquals(Util.getLocalResource("issues/issue73-1.txt"), output); - } + public void testDump() { + Blog blog = new Blog("Test Me!"); + blog.addPost(new Post("Title1", "text 1")); + blog.addPost(new Post("Title2", "text text 2")); + blog.numbers.add(19); + blog.numbers.add(17); + TreeSet<String> labels = new TreeSet<String>(); + labels.add("Java"); + labels.add("YAML"); + labels.add("SnakeYAML"); + blog.setLabels(labels); + DumperOptions options = new DumperOptions(); + options.setAllowReadOnlyProperties(true); + Yaml yaml = new Yaml(options); + String output = yaml.dump(blog); + // System.out.println(output); + assertEquals(Util.getLocalResource("issues/issue73-1.txt"), output); + } - public void testLoad() { - Yaml yaml = new Yaml(); - yaml.setBeanAccess(BeanAccess.FIELD); - String doc = Util.getLocalResource("issues/issue73-1.txt"); - Blog blog = (Blog) yaml.load(doc); - // System.out.println(blog); - assertEquals("Test Me!", blog.getName()); - assertEquals(2, blog.numbers.size()); - assertEquals(2, blog.getPosts().size()); - for (Post post : blog.getPosts()) { - assertEquals(Post.class, post.getClass()); - } + public void testLoad() { + Yaml yaml = new Yaml(); + yaml.setBeanAccess(BeanAccess.FIELD); + String doc = Util.getLocalResource("issues/issue73-1.txt"); + Blog blog = yaml.load(doc); + // System.out.println(blog); + assertEquals("Test Me!", blog.getName()); + assertEquals(2, blog.numbers.size()); + assertEquals(2, blog.getPosts().size()); + for (Post post : blog.getPosts()) { + assertEquals(Post.class, post.getClass()); } + } - public void testYaml() { - String serialized = Util.getLocalResource("issues/issue73-2.txt"); - // System.out.println(serialized); - Yaml beanLoader = new Yaml(); - beanLoader.setBeanAccess(BeanAccess.FIELD); - Blog rehydrated = beanLoader.loadAs(serialized, Blog.class); - checkTestBlog(rehydrated); - } + public void testYaml() { + String serialized = Util.getLocalResource("issues/issue73-2.txt"); + // System.out.println(serialized); + Yaml beanLoader = new Yaml(); + beanLoader.setBeanAccess(BeanAccess.FIELD); + Blog rehydrated = beanLoader.loadAs(serialized, Blog.class); + checkTestBlog(rehydrated); + } - protected void checkTestBlog(Blog blog) { - Set<Post> posts = blog.getPosts(); - assertEquals("Blog contains 2 posts", 2, posts.size()); - assertTrue(posts.contains(new Post("Test", "Dummy"))); - assertTrue(posts.contains(new Post("Highly", "Creative"))); - assertEquals("No tags!", blog.getName()); - assertEquals(0, blog.numbers.size()); - } + protected void checkTestBlog(Blog blog) { + Set<Post> posts = blog.getPosts(); + assertEquals("Blog contains 2 posts", 2, posts.size()); + assertTrue(posts.contains(new Post("Test", "Dummy"))); + assertTrue(posts.contains(new Post("Highly", "Creative"))); + assertEquals("No tags!", blog.getName()); + assertEquals(0, blog.numbers.size()); + } - @SuppressWarnings("unchecked") - public void testLoadRootSet() { - Yaml yaml = new Yaml(); - String doc = Util.getLocalResource("issues/issue73-3.txt"); - Set<String> strings = (Set<String>) yaml.load(doc); - // System.out.println(strings); - assertEquals(3, strings.size()); - assertEquals(HashSet.class, strings.getClass()); - assertTrue(strings.contains("aaa")); - assertTrue(strings.contains("bbb")); - assertTrue(strings.contains("ccc")); - } + @SuppressWarnings("unchecked") + public void testLoadRootSet() { + Yaml yaml = new Yaml(); + String doc = Util.getLocalResource("issues/issue73-3.txt"); + Set<String> strings = yaml.load(doc); + // System.out.println(strings); + assertEquals(3, strings.size()); + assertEquals(HashSet.class, strings.getClass()); + assertTrue(strings.contains("aaa")); + assertTrue(strings.contains("bbb")); + assertTrue(strings.contains("ccc")); + } - @SuppressWarnings("unchecked") - public void testLoadRootSet2() { - Yaml yaml = new Yaml(); - String doc = "!!java.util.HashSet {aaa: null, bbb: null, ccc: null}"; - Set<String> strings = (Set<String>) yaml.load(doc); - // System.out.println(strings); - assertEquals(3, strings.size()); - assertEquals(LinkedHashSet.class, strings.getClass()); - assertTrue(strings.contains("aaa")); - assertTrue(strings.contains("bbb")); - assertTrue(strings.contains("ccc")); - } + @SuppressWarnings("unchecked") + public void testLoadRootSet2() { + Yaml yaml = new Yaml(); + String doc = "!!java.util.HashSet {aaa: null, bbb: null, ccc: null}"; + Set<String> strings = yaml.load(doc); + // System.out.println(strings); + assertEquals(3, strings.size()); + assertEquals(HashSet.class, strings.getClass()); + assertTrue(strings.contains("aaa")); + assertTrue(strings.contains("bbb")); + assertTrue(strings.contains("ccc")); + } - @SuppressWarnings("unchecked") - public void testLoadRootSet3() { - Yaml yaml = new Yaml(); - String doc = "!!java.util.TreeSet {aaa: null, bbb: null, ccc: null}"; - Set<String> strings = (Set<String>) yaml.load(doc); - // System.out.println(strings); - assertEquals(3, strings.size()); - assertEquals(TreeSet.class, strings.getClass()); - assertTrue(strings.contains("aaa")); - assertTrue(strings.contains("bbb")); - assertTrue(strings.contains("ccc")); - } + @SuppressWarnings("unchecked") + public void testLoadRootSet3() { + Yaml yaml = new Yaml(); + String doc = "!!java.util.TreeSet {aaa: null, bbb: null, ccc: null}"; + Set<String> strings = yaml.load(doc); + // System.out.println(strings); + assertEquals(3, strings.size()); + assertEquals(TreeSet.class, strings.getClass()); + assertTrue(strings.contains("aaa")); + assertTrue(strings.contains("bbb")); + assertTrue(strings.contains("ccc")); + } - @SuppressWarnings("unchecked") - public void testLoadRootSet6() { - Yaml yaml = new Yaml(); - String doc = Util.getLocalResource("issues/issue73-6.txt"); - Set<String> strings = (Set<String>) yaml.load(doc); - // System.out.println(strings); - assertEquals(3, strings.size()); - assertEquals(TreeSet.class, strings.getClass()); - assertTrue(strings.contains("aaa")); - assertTrue(strings.contains("bbb")); - assertTrue(strings.contains("ccc")); - } + @SuppressWarnings("unchecked") + public void testLoadRootSet6() { + Yaml yaml = new Yaml(); + String doc = Util.getLocalResource("issues/issue73-6.txt"); + Set<String> strings = yaml.load(doc); + // System.out.println(strings); + assertEquals(3, strings.size()); + assertEquals(TreeSet.class, strings.getClass()); + assertTrue(strings.contains("aaa")); + assertTrue(strings.contains("bbb")); + assertTrue(strings.contains("ccc")); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue73/TreeSetTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue73/TreeSetTest.java index 7d4a3e08..924de3ef 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue73/TreeSetTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue73/TreeSetTest.java @@ -1,69 +1,66 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue73; import java.util.TreeSet; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Yaml; /** - * Test bean when the implementation is defined: TreeSet instead of just the - * interface Set + * Test bean when the implementation is defined: TreeSet instead of just the interface Set */ public class TreeSetTest extends TestCase { - public void testSetImplementation() { - Bean1 bean = new Bean1(); - bean.setId("ID123"); - TreeSet<String> list = new TreeSet<String>(); - list.add("zzz"); - list.add("xxx"); - list.add("ccc"); - bean.setSet(list); - Yaml yaml = new Yaml(); - String doc = yaml.dump(bean); - // System.out.println(doc); - // - Bean1 loaded = (Bean1) yaml.load(doc); - assertEquals(3, loaded.getSet().size()); - assertEquals(TreeSet.class, loaded.getSet().getClass()); - assertTrue(loaded.getSet().contains("zzz")); - assertTrue(loaded.getSet().contains("xxx")); - assertTrue(loaded.getSet().contains("ccc")); - } - public static class Bean1 { - private TreeSet<String> set; - private String id; + public void testSetImplementation() { + Bean1 bean = new Bean1(); + bean.setId("ID123"); + TreeSet<String> list = new TreeSet<String>(); + list.add("zzz"); + list.add("xxx"); + list.add("ccc"); + bean.setSet(list); + Yaml yaml = new Yaml(); + String doc = yaml.dump(bean); + // System.out.println(doc); + // + Bean1 loaded = yaml.load(doc); + assertEquals(3, loaded.getSet().size()); + assertEquals(TreeSet.class, loaded.getSet().getClass()); + assertTrue(loaded.getSet().contains("zzz")); + assertTrue(loaded.getSet().contains("xxx")); + assertTrue(loaded.getSet().contains("ccc")); + } + + public static class Bean1 { - public TreeSet<String> getSet() { - return set; - } + private TreeSet<String> set; + private String id; - public void setSet(TreeSet<String> set) { - this.set = set; - } + public TreeSet<String> getSet() { + return set; + } + + public void setSet(TreeSet<String> set) { + this.set = set; + } - public String getId() { - return id; - } + public String getId() { + return id; + } - public void setId(String id) { - this.id = id; - } + public void setId(String id) { + this.id = id; } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue74/ArrayBeanTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue74/ArrayBeanTest.java index 716294e4..3fc91912 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue74/ArrayBeanTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue74/ArrayBeanTest.java @@ -1,150 +1,148 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue74; import java.util.ArrayList; import java.util.List; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; public class ArrayBeanTest extends TestCase { - public void testArrayProperty() { - ArrayMember[] members = new ArrayMember[3]; - members[0] = new ArrayMember("Foo", 21); - members[1] = new ArrayMember("Bar", 23); - members[2] = new ArrayMember("Hue Long Hair", 25); - ArrayBean bean = new ArrayBean(); - bean.setId("ID123"); - bean.setNumber(7); - bean.setMembers(members); - bean.openMembers = new ArrayMember[] { new ArrayMember("OpenFoo", 1000), - new ArrayMember("OpenBar", 2000) }; - List<ArrayMember> list = new ArrayList<ArrayMember>(2); - list.add(new ArrayMember("John", 111)); - list.add(new ArrayMember("Tony", 222)); - bean.setList(list); - Yaml yaml = new Yaml(); - String output = yaml.dumpAsMap(bean); - // System.out.println(output); - assertEquals(Util.getLocalResource("issues/issue74-array1.txt"), output); - Yaml beanLoader = new Yaml(); - ArrayBean parsed = beanLoader.loadAs(output, ArrayBean.class); - // System.out.println(parsed); - assertEquals(3, parsed.getMembers().length); - assertEquals(2, parsed.openMembers.length); - assertEquals(2, parsed.getList().size()); - assertEquals("ID123", parsed.getId()); - assertEquals(7, parsed.getNumber()); - for (ArrayMember member : parsed.getMembers()) { - assertTrue((member.getAge() >= 21) && (member.getAge() <= 25)); - } - } - - public static class ArrayBean { - private String id; - private int number; - private ArrayMember[] members; - public ArrayMember[] openMembers; - private List<ArrayMember> list; - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public int getNumber() { - return number; - } - - public void setNumber(int number) { - this.number = number; - } - - public ArrayMember[] getMembers() { - return members; - } - - public void setMembers(ArrayMember[] members) { - this.members = members; - } - - public List<ArrayMember> getList() { - return list; - } - - public void setList(List<ArrayMember> list) { - this.list = list; - } - } - - public static class ArrayMember { - private String name; - private int age; - - public ArrayMember(String name, int age) { - this.name = name; - this.age = age; - } - - public ArrayMember() { - this.name = "NoName"; - this.age = 0; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - @Override - public boolean equals(Object obj) { - if (obj instanceof ArrayMember) { - ArrayMember m = (ArrayMember) obj; - return age == m.age; - } else { - return false; - } - } - - @Override - public int hashCode() { - return age; - } - - @Override - public String toString() { - return "ArrayMember age=" + age; - } + public void testArrayProperty() { + ArrayMember[] members = new ArrayMember[3]; + members[0] = new ArrayMember("Foo", 21); + members[1] = new ArrayMember("Bar", 23); + members[2] = new ArrayMember("Hue Long Hair", 25); + ArrayBean bean = new ArrayBean(); + bean.setId("ID123"); + bean.setNumber(7); + bean.setMembers(members); + bean.openMembers = + new ArrayMember[] {new ArrayMember("OpenFoo", 1000), new ArrayMember("OpenBar", 2000)}; + List<ArrayMember> list = new ArrayList<ArrayMember>(2); + list.add(new ArrayMember("John", 111)); + list.add(new ArrayMember("Tony", 222)); + bean.setList(list); + Yaml yaml = new Yaml(); + String output = yaml.dumpAsMap(bean); + // System.out.println(output); + assertEquals(Util.getLocalResource("issues/issue74-array1.txt"), output); + Yaml beanLoader = new Yaml(); + ArrayBean parsed = beanLoader.loadAs(output, ArrayBean.class); + // System.out.println(parsed); + assertEquals(3, parsed.getMembers().length); + assertEquals(2, parsed.openMembers.length); + assertEquals(2, parsed.getList().size()); + assertEquals("ID123", parsed.getId()); + assertEquals(7, parsed.getNumber()); + for (ArrayMember member : parsed.getMembers()) { + assertTrue((member.getAge() >= 21) && (member.getAge() <= 25)); + } + } + + public static class ArrayBean { + + private String id; + private int number; + private ArrayMember[] members; + public ArrayMember[] openMembers; + private List<ArrayMember> list; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public int getNumber() { + return number; + } + + public void setNumber(int number) { + this.number = number; + } + + public ArrayMember[] getMembers() { + return members; + } + + public void setMembers(ArrayMember[] members) { + this.members = members; + } + + public List<ArrayMember> getList() { + return list; + } + + public void setList(List<ArrayMember> list) { + this.list = list; + } + } + + public static class ArrayMember { + + private String name; + private int age; + + public ArrayMember(String name, int age) { + this.name = name; + this.age = age; + } + + public ArrayMember() { + this.name = "NoName"; + this.age = 0; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof ArrayMember) { + ArrayMember m = (ArrayMember) obj; + return age == m.age; + } else { + return false; + } + } + + @Override + public int hashCode() { + return age; + } + + @Override + public String toString() { + return "ArrayMember age=" + age; } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue8/Person.java b/src/test/java/org/yaml/snakeyaml/issues/issue8/Person.java index 1714a918..ae58fb35 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue8/Person.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue8/Person.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue8; @@ -21,56 +19,56 @@ import java.io.Serializable; * to test http://code.google.com/p/snakeyaml/issues/detail?id=8 */ public class Person implements Serializable { - private static final long serialVersionUID = 1L; - private String firstName; - private String lastName; - private int hatSize; - public Person() { - } + private static final long serialVersionUID = 1L; + private String firstName; + private String lastName; + private int hatSize; - public Person(String firstName, String lastName, int hatSize) { - this.firstName = firstName; - this.lastName = lastName; - this.hatSize = hatSize; - } + public Person() {} - public String getFirstName() { - return firstName; - } + public Person(String firstName, String lastName, int hatSize) { + this.firstName = firstName; + this.lastName = lastName; + this.hatSize = hatSize; + } - public void setFirstName(String firstName) { - this.firstName = firstName; - } + public String getFirstName() { + return firstName; + } - public String getLastName() { - return lastName; - } + public void setFirstName(String firstName) { + this.firstName = firstName; + } - public void setLastName(String lastName) { - this.lastName = lastName; - } + public String getLastName() { + return lastName; + } - public int getHatSize() { - return hatSize; - } + public void setLastName(String lastName) { + this.lastName = lastName; + } - public void setHatSize(int hatSize) { - this.hatSize = hatSize; - } + public int getHatSize() { + return hatSize; + } - @Override - public boolean equals(Object object) { - if (object instanceof Person) { - Person person = (Person) object; - return firstName.equals(person.firstName) && lastName.equals(person.lastName) - && hatSize == person.hatSize; - } - return false; - } + public void setHatSize(int hatSize) { + this.hatSize = hatSize; + } - @Override - public int hashCode() { - return 1; + @Override + public boolean equals(Object object) { + if (object instanceof Person) { + Person person = (Person) object; + return firstName.equals(person.firstName) && lastName.equals(person.lastName) + && hatSize == person.hatSize; } + return false; + } + + @Override + public int hashCode() { + return 1; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue8/PrattleRepresenterTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue8/PrattleRepresenterTest.java index b51ebb45..01b378c9 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue8/PrattleRepresenterTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue8/PrattleRepresenterTest.java @@ -1,22 +1,19 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue8; import junit.framework.TestCase; - import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @@ -24,43 +21,48 @@ import org.yaml.snakeyaml.Yaml; * to test http://code.google.com/p/snakeyaml/issues/detail?id=8 */ public class PrattleRepresenterTest extends TestCase { - public void test() { - Yaml yaml = new Yaml(); - Person person = new Person("Alan", "Gutierrez", 9); - String etalon = "!!org.yaml.snakeyaml.issues.issue8.Person {firstName: Alan, hatSize: 9, lastName: Gutierrez}\n"; - assertEquals(etalon, yaml.dump(person)); - assertEquals(etalon, yaml.dump(person)); - } - public void test2beans() { - DumperOptions options = new DumperOptions(); - options.setAllowReadOnlyProperties(true); - Yaml yaml = new Yaml(options); - Person person = new Person("Alan", "Gutierrez", 9); - String etalon = "!!org.yaml.snakeyaml.issues.issue8.Person {firstName: Alan, hatSize: 9, lastName: Gutierrez}\n"; - assertEquals(etalon, yaml.dump(person)); - Horse horse = new Horse("Tom", person); - String etalon2 = "!!org.yaml.snakeyaml.issues.issue8.PrattleRepresenterTest$Horse\nname: Tom\nowner: {firstName: Alan, hatSize: 9, lastName: Gutierrez}\n"; - assertEquals(etalon2, yaml.dump(horse)); - } + public void test() { + Yaml yaml = new Yaml(); + Person person = new Person("Alan", "Gutierrez", 9); + String etalon = + "!!org.yaml.snakeyaml.issues.issue8.Person {firstName: Alan, hatSize: 9, lastName: Gutierrez}\n"; + assertEquals(etalon, yaml.dump(person)); + assertEquals(etalon, yaml.dump(person)); + } - public static class Horse { - private String name; - private Person owner; + public void test2beans() { + DumperOptions options = new DumperOptions(); + options.setAllowReadOnlyProperties(true); + Yaml yaml = new Yaml(options); + Person person = new Person("Alan", "Gutierrez", 9); + String etalon = + "!!org.yaml.snakeyaml.issues.issue8.Person {firstName: Alan, hatSize: 9, lastName: Gutierrez}\n"; + assertEquals(etalon, yaml.dump(person)); + Horse horse = new Horse("Tom", person); + String etalon2 = + "!!org.yaml.snakeyaml.issues.issue8.PrattleRepresenterTest$Horse\nname: Tom\nowner: {firstName: Alan, hatSize: 9, lastName: Gutierrez}\n"; + assertEquals(etalon2, yaml.dump(horse)); + } - public Horse(String name, Person owner) { - super(); - this.name = name; - this.owner = owner; - } + public static class Horse { - public String getName() { - return name; - } + private final String name; + private final Person owner; - public Person getOwner() { - return owner; - } + public Horse(String name, Person owner) { + super(); + this.name = name; + this.owner = owner; + } + public String getName() { + return name; } + + public Person getOwner() { + return owner; + } + + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue82/PropOrderInfluenceWhenAliasedInGenericCollectionTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue82/PropOrderInfluenceWhenAliasedInGenericCollectionTest.java index fb07b909..b608776c 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue82/PropOrderInfluenceWhenAliasedInGenericCollectionTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue82/PropOrderInfluenceWhenAliasedInGenericCollectionTest.java @@ -1,29 +1,27 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue82; +import static org.junit.Assert.assertNotEquals; + import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; - import junit.framework.TestCase; - import org.yaml.snakeyaml.TypeDescription; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; @@ -35,286 +33,291 @@ import org.yaml.snakeyaml.representer.Representer; */ public class PropOrderInfluenceWhenAliasedInGenericCollectionTest extends TestCase { - public static interface Account { - } + public interface Account { - public static class GeneralAccount implements Account { - public String name = "General"; - } + } - public static class SuperSaverAccount extends GeneralAccount { + public static class GeneralAccount implements Account { - public SuperSaverAccount() { - name = "SuperSaver"; - } - } + public String name = "General"; + } - public static class CustomerAB { - public Collection<Account> aAll; - public Collection<GeneralAccount> bGeneral; + public static class SuperSaverAccount extends GeneralAccount { - @Override - public String toString() { - return "CustomerAB"; - } + public SuperSaverAccount() { + name = "SuperSaver"; } + } - public static class CustomerBA { - public Collection<GeneralAccount> aGeneral; - public Collection<Account> bAll; - } + public static class CustomerAB { - public static class CustomerAB_MapValue { - public Collection<Account> aAll; - public Map<String, GeneralAccount> bGeneralMap; + public Collection<Account> aAll; + public Collection<GeneralAccount> bGeneral; - @Override - public String toString() { - return "CustomerAB_MapValue"; - } + @Override + public String toString() { + return "CustomerAB"; } + } - public static class CustomerAB_MapKey { - public Collection<Account> aAll; - public Map<GeneralAccount, String> bGeneralMap; + public static class CustomerBA { - @Override - public String toString() { - return "CustomerAB_MapKey"; - } - } + public Collection<GeneralAccount> aGeneral; + public Collection<Account> bAll; + } - public static class CustomerAB_Property { - public Account acc; - public Collection<GeneralAccount> bGeneral; + public static class CustomerAB_MapValue { - @Override - public String toString() { - return "CustomerAB_Property"; - } - } + public Collection<Account> aAll; + public Map<String, GeneralAccount> bGeneralMap; - public void testAB() { - SuperSaverAccount supersaver = new SuperSaverAccount(); - GeneralAccount generalAccount = new GeneralAccount(); - - CustomerAB customerAB = new CustomerAB(); - ArrayList<Account> all = new ArrayList<Account>(); - all.add(supersaver); - all.add(generalAccount); - ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>(); - general.add(generalAccount); - general.add(supersaver); - - customerAB.aAll = all; - customerAB.bGeneral = general; - - Yaml yaml = new Yaml(); - String dump = yaml.dump(customerAB); - // System.out.println(dump); - CustomerAB parsed = (CustomerAB) yaml.load(dump); - assertNotNull(parsed); + @Override + public String toString() { + return "CustomerAB_MapValue"; } + } - public void testAB_Set() { - SuperSaverAccount supersaver = new SuperSaverAccount(); - GeneralAccount generalAccount = new GeneralAccount(); - - CustomerAB customerAB = new CustomerAB(); - ArrayList<Account> all = new ArrayList<Account>(); - all.add(supersaver); - all.add(generalAccount); - Set<GeneralAccount> general = new HashSet<GeneralAccount>(); - general.add(generalAccount); - general.add(supersaver); - - customerAB.aAll = all; - customerAB.bGeneral = general; - - Yaml yaml = new Yaml(); - String dump = yaml.dump(customerAB); - // System.out.println(dump); - CustomerAB parsed = (CustomerAB) yaml.load(dump); - assertNotNull(parsed); - } - - public void testABWithCustomTag() { - SuperSaverAccount supersaver = new SuperSaverAccount(); - GeneralAccount generalAccount = new GeneralAccount(); - - CustomerAB customerAB = new CustomerAB(); - ArrayList<Account> all = new ArrayList<Account>(); - all.add(supersaver); - all.add(generalAccount); - ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>(); - general.add(generalAccount); - general.add(supersaver); - - customerAB.aAll = all; - customerAB.bGeneral = general; - - Constructor constructor = new Constructor(); - Representer representer = new Representer(); - Tag generalAccountTag = new Tag("!GA"); - constructor - .addTypeDescription(new TypeDescription(GeneralAccount.class, generalAccountTag)); - representer.addClassTag(GeneralAccount.class, generalAccountTag); - - Yaml yaml = new Yaml(constructor, representer); - String dump = yaml.dump(customerAB); - // System.out.println(dump); - CustomerAB parsed = (CustomerAB) yaml.load(dump); - assertNotNull(parsed); - } - - public void testABProperty() { - SuperSaverAccount supersaver = new SuperSaverAccount(); - GeneralAccount generalAccount = new GeneralAccount(); - - CustomerAB_Property customerAB_property = new CustomerAB_Property(); - ArrayList<Account> all = new ArrayList<Account>(); - all.add(supersaver); - all.add(generalAccount); - ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>(); - general.add(generalAccount); - general.add(supersaver); - - customerAB_property.acc = generalAccount; - customerAB_property.bGeneral = general; - - Constructor constructor = new Constructor(); - Representer representer = new Representer(); - - Yaml yaml = new Yaml(constructor, representer); - String dump = yaml.dump(customerAB_property); - // System.out.println(dump); - CustomerAB_Property parsed = (CustomerAB_Property) yaml.load(dump); - assertNotNull(parsed); - } + public static class CustomerAB_MapKey { - public void testABPropertyWithCustomTag() { - SuperSaverAccount supersaver = new SuperSaverAccount(); - GeneralAccount generalAccount = new GeneralAccount(); - - CustomerAB_Property customerAB_property = new CustomerAB_Property(); - ArrayList<Account> all = new ArrayList<Account>(); - all.add(supersaver); - all.add(generalAccount); - ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>(); - general.add(generalAccount); - general.add(supersaver); - - customerAB_property.acc = generalAccount; - customerAB_property.bGeneral = general; - - Constructor constructor = new Constructor(); - Representer representer = new Representer(); - - Tag generalAccountTag = new Tag("!GA"); - constructor - .addTypeDescription(new TypeDescription(GeneralAccount.class, generalAccountTag)); - representer.addClassTag(GeneralAccount.class, generalAccountTag); - - Yaml yaml = new Yaml(constructor, representer); - String dump = yaml.dump(customerAB_property); - // System.out.println(dump); - CustomerAB_Property parsed = (CustomerAB_Property) yaml.load(dump); - assertNotNull(parsed); - } + public Collection<Account> aAll; + public Map<GeneralAccount, String> bGeneralMap; - public void testABwithJavaBeanHelpers() { - SuperSaverAccount supersaver = new SuperSaverAccount(); - GeneralAccount generalAccount = new GeneralAccount(); - - CustomerAB customerAB = new CustomerAB(); - ArrayList<Account> all = new ArrayList<Account>(); - all.add(supersaver); - all.add(generalAccount); - ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>(); - general.add(generalAccount); - general.add(supersaver); - - customerAB.aAll = all; - customerAB.bGeneral = general; - - Yaml yaml = new Yaml(); - String dump2 = yaml.dumpAsMap(customerAB); - // System.out.println(dump2); - Yaml loader = new Yaml(); - CustomerAB parsed = loader.loadAs(dump2, CustomerAB.class); - assertNotNull(parsed); + @Override + public String toString() { + return "CustomerAB_MapKey"; } + } - public void testAB_asMapValue() { - SuperSaverAccount supersaver = new SuperSaverAccount(); - GeneralAccount generalAccount = new GeneralAccount(); - - CustomerAB_MapValue customerAB_mapValue = new CustomerAB_MapValue(); - ArrayList<Account> all = new ArrayList<Account>(); - all.add(supersaver); - all.add(generalAccount); - Map<String, GeneralAccount> generalMap = new HashMap<String, GeneralAccount>(); - generalMap.put(generalAccount.name, generalAccount); - generalMap.put(supersaver.name, supersaver); - - customerAB_mapValue.aAll = all; - customerAB_mapValue.bGeneralMap = generalMap; - - Yaml yaml = new Yaml(); - String dump = yaml.dump(customerAB_mapValue); - // System.out.println(dump); - CustomerAB_MapValue parsed = (CustomerAB_MapValue) yaml.load(dump); - assertNotNull(parsed); - } + public static class CustomerAB_Property { - public void testAB_asMapKey() { - SuperSaverAccount supersaver = new SuperSaverAccount(); - GeneralAccount generalAccount = new GeneralAccount(); - - CustomerAB_MapKey customerAB_mapKey = new CustomerAB_MapKey(); - ArrayList<Account> all = new ArrayList<Account>(); - all.add(supersaver); - all.add(generalAccount); - Map<GeneralAccount, String> generalMap = new HashMap<GeneralAccount, String>(); - generalMap.put(generalAccount, generalAccount.name); - generalMap.put(supersaver, supersaver.name); - - customerAB_mapKey.aAll = all; - customerAB_mapKey.bGeneralMap = generalMap; - - Yaml yaml = new Yaml(); - String dump = yaml.dump(customerAB_mapKey); - // System.out.println(dump); - CustomerAB_MapKey parsed = (CustomerAB_MapKey) yaml.load(dump); - assertNotNull(parsed); - } + public Account acc; + public Collection<GeneralAccount> bGeneral; - public void testBA() { - SuperSaverAccount supersaver = new SuperSaverAccount(); - GeneralAccount generalAccount = new GeneralAccount(); - - CustomerBA customerBA = new CustomerBA(); - ArrayList<Account> all = new ArrayList<Account>(); - all.add(supersaver); - all.add(generalAccount); - ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>(); - general.add(generalAccount); - general.add(supersaver); - - customerBA.aGeneral = general; - customerBA.bAll = all; - - Yaml yaml = new Yaml(); - String dump = yaml.dump(customerBA); - // System.out.println(dump); - // - CustomerBA parsed = (CustomerBA) yaml.load(dump); - assertEquals(2, parsed.bAll.size()); - assertEquals(2, parsed.aGeneral.size()); - assertFalse(parsed.bAll.equals(parsed.aGeneral)); - GeneralAccount[] array = parsed.aGeneral.toArray(new GeneralAccount[2]); - assertEquals(GeneralAccount.class, array[0].getClass()); - assertEquals(SuperSaverAccount.class, array[1].getClass()); - assertEquals("SuperSaver", array[1].name); + @Override + public String toString() { + return "CustomerAB_Property"; } + } + + public void testAB() { + SuperSaverAccount supersaver = new SuperSaverAccount(); + GeneralAccount generalAccount = new GeneralAccount(); + + CustomerAB customerAB = new CustomerAB(); + ArrayList<Account> all = new ArrayList<Account>(); + all.add(supersaver); + all.add(generalAccount); + ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>(); + general.add(generalAccount); + general.add(supersaver); + + customerAB.aAll = all; + customerAB.bGeneral = general; + + Yaml yaml = new Yaml(); + String dump = yaml.dump(customerAB); + // System.out.println(dump); + CustomerAB parsed = yaml.load(dump); + assertNotNull(parsed); + } + + public void testAB_Set() { + SuperSaverAccount supersaver = new SuperSaverAccount(); + GeneralAccount generalAccount = new GeneralAccount(); + + CustomerAB customerAB = new CustomerAB(); + ArrayList<Account> all = new ArrayList<Account>(); + all.add(supersaver); + all.add(generalAccount); + Set<GeneralAccount> general = new HashSet<GeneralAccount>(); + general.add(generalAccount); + general.add(supersaver); + + customerAB.aAll = all; + customerAB.bGeneral = general; + + Yaml yaml = new Yaml(); + String dump = yaml.dump(customerAB); + // System.out.println(dump); + CustomerAB parsed = yaml.load(dump); + assertNotNull(parsed); + } + + public void testABWithCustomTag() { + SuperSaverAccount supersaver = new SuperSaverAccount(); + GeneralAccount generalAccount = new GeneralAccount(); + + CustomerAB customerAB = new CustomerAB(); + ArrayList<Account> all = new ArrayList<Account>(); + all.add(supersaver); + all.add(generalAccount); + ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>(); + general.add(generalAccount); + general.add(supersaver); + + customerAB.aAll = all; + customerAB.bGeneral = general; + + Constructor constructor = new Constructor(); + Representer representer = new Representer(); + Tag generalAccountTag = new Tag("!GA"); + constructor.addTypeDescription(new TypeDescription(GeneralAccount.class, generalAccountTag)); + representer.addClassTag(GeneralAccount.class, generalAccountTag); + + Yaml yaml = new Yaml(constructor, representer); + String dump = yaml.dump(customerAB); + // System.out.println(dump); + CustomerAB parsed = yaml.load(dump); + assertNotNull(parsed); + } + + public void testABProperty() { + SuperSaverAccount supersaver = new SuperSaverAccount(); + GeneralAccount generalAccount = new GeneralAccount(); + + CustomerAB_Property customerAB_property = new CustomerAB_Property(); + ArrayList<Account> all = new ArrayList<Account>(); + all.add(supersaver); + all.add(generalAccount); + ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>(); + general.add(generalAccount); + general.add(supersaver); + + customerAB_property.acc = generalAccount; + customerAB_property.bGeneral = general; + + Constructor constructor = new Constructor(); + Representer representer = new Representer(); + + Yaml yaml = new Yaml(constructor, representer); + String dump = yaml.dump(customerAB_property); + // System.out.println(dump); + CustomerAB_Property parsed = yaml.load(dump); + assertNotNull(parsed); + } + + public void testABPropertyWithCustomTag() { + SuperSaverAccount supersaver = new SuperSaverAccount(); + GeneralAccount generalAccount = new GeneralAccount(); + + CustomerAB_Property customerAB_property = new CustomerAB_Property(); + ArrayList<Account> all = new ArrayList<Account>(); + all.add(supersaver); + all.add(generalAccount); + ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>(); + general.add(generalAccount); + general.add(supersaver); + + customerAB_property.acc = generalAccount; + customerAB_property.bGeneral = general; + + Constructor constructor = new Constructor(); + Representer representer = new Representer(); + + Tag generalAccountTag = new Tag("!GA"); + constructor.addTypeDescription(new TypeDescription(GeneralAccount.class, generalAccountTag)); + representer.addClassTag(GeneralAccount.class, generalAccountTag); + + Yaml yaml = new Yaml(constructor, representer); + String dump = yaml.dump(customerAB_property); + // System.out.println(dump); + CustomerAB_Property parsed = yaml.load(dump); + assertNotNull(parsed); + } + + public void testABwithJavaBeanHelpers() { + SuperSaverAccount supersaver = new SuperSaverAccount(); + GeneralAccount generalAccount = new GeneralAccount(); + + CustomerAB customerAB = new CustomerAB(); + ArrayList<Account> all = new ArrayList<Account>(); + all.add(supersaver); + all.add(generalAccount); + ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>(); + general.add(generalAccount); + general.add(supersaver); + + customerAB.aAll = all; + customerAB.bGeneral = general; + + Yaml yaml = new Yaml(); + String dump2 = yaml.dumpAsMap(customerAB); + // System.out.println(dump2); + Yaml loader = new Yaml(); + CustomerAB parsed = loader.loadAs(dump2, CustomerAB.class); + assertNotNull(parsed); + } + + public void testAB_asMapValue() { + SuperSaverAccount supersaver = new SuperSaverAccount(); + GeneralAccount generalAccount = new GeneralAccount(); + + CustomerAB_MapValue customerAB_mapValue = new CustomerAB_MapValue(); + ArrayList<Account> all = new ArrayList<Account>(); + all.add(supersaver); + all.add(generalAccount); + Map<String, GeneralAccount> generalMap = new HashMap<String, GeneralAccount>(); + generalMap.put(generalAccount.name, generalAccount); + generalMap.put(supersaver.name, supersaver); + + customerAB_mapValue.aAll = all; + customerAB_mapValue.bGeneralMap = generalMap; + + Yaml yaml = new Yaml(); + String dump = yaml.dump(customerAB_mapValue); + // System.out.println(dump); + CustomerAB_MapValue parsed = yaml.load(dump); + assertNotNull(parsed); + } + + public void testAB_asMapKey() { + SuperSaverAccount supersaver = new SuperSaverAccount(); + GeneralAccount generalAccount = new GeneralAccount(); + + CustomerAB_MapKey customerAB_mapKey = new CustomerAB_MapKey(); + ArrayList<Account> all = new ArrayList<Account>(); + all.add(supersaver); + all.add(generalAccount); + Map<GeneralAccount, String> generalMap = new HashMap<GeneralAccount, String>(); + generalMap.put(generalAccount, generalAccount.name); + generalMap.put(supersaver, supersaver.name); + + customerAB_mapKey.aAll = all; + customerAB_mapKey.bGeneralMap = generalMap; + + Yaml yaml = new Yaml(); + String dump = yaml.dump(customerAB_mapKey); + // System.out.println(dump); + CustomerAB_MapKey parsed = yaml.load(dump); + assertNotNull(parsed); + } + + public void testBA() { + SuperSaverAccount supersaver = new SuperSaverAccount(); + GeneralAccount generalAccount = new GeneralAccount(); + + CustomerBA customerBA = new CustomerBA(); + ArrayList<Account> all = new ArrayList<Account>(); + all.add(supersaver); + all.add(generalAccount); + ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>(); + general.add(generalAccount); + general.add(supersaver); + + customerBA.aGeneral = general; + customerBA.bAll = all; + + Yaml yaml = new Yaml(); + String dump = yaml.dump(customerBA); + // System.out.println(dump); + // + CustomerBA parsed = yaml.load(dump); + assertEquals(2, parsed.bAll.size()); + assertEquals(2, parsed.aGeneral.size()); + assertNotEquals(parsed.bAll, parsed.aGeneral); + GeneralAccount[] array = parsed.aGeneral.toArray(new GeneralAccount[2]); + assertEquals(GeneralAccount.class, array[0].getClass()); + assertEquals(SuperSaverAccount.class, array[1].getClass()); + assertEquals("SuperSaver", array[1].name); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue94/ChangeRuntimeClassTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue94/ChangeRuntimeClassTest.java index b9823a01..27a04d5a 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue94/ChangeRuntimeClassTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue94/ChangeRuntimeClassTest.java @@ -1,23 +1,22 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue94; import static org.junit.Assert.assertEquals; import org.junit.Test; +import org.yaml.snakeyaml.TypeDescription; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.AbstractConstruct; import org.yaml.snakeyaml.constructor.Construct; @@ -26,71 +25,104 @@ import org.yaml.snakeyaml.nodes.Node; public class ChangeRuntimeClassTest { - @Test - public void testWithGlobalTag() { - String yamlText = "!!org.yaml.snakeyaml.issues.issue94.Entity\n" + "name: Matt\n" - + "nickName: Java\n"; + @Test + public void testWithGlobalTag() { + String yamlText = + "!!org.yaml.snakeyaml.issues.issue94.Entity\n" + "name: Matt\n" + "nickName: Java\n"; - // Now here that I would like to somehow intercept the constructor of - // SnakeYaml and give it - // an fresh instance of EntityLoadingProxy(); based on today's - // temperature, so to speak... - // that is un-preditable statically which proxy I will give it. + // Now here that I would like to somehow intercept the constructor of + // SnakeYaml and give it + // an fresh instance of EntityLoadingProxy(); based on today's + // temperature, so to speak... + // that is un-preditable statically which proxy I will give it. - Yaml yaml = new Yaml(new MyConstructor()); + Yaml yaml = new Yaml(new MyConstructor()); - Entity loadedEntity = null; - loadedEntity = (Entity) yaml.load(yamlText); + Entity loadedEntity = null; + loadedEntity = yaml.load(yamlText); - assertEquals("Matt", loadedEntity.getName()); + assertEquals("Matt", loadedEntity.getName()); - // The expectation below is from having intercepted setNickName() with - // the artifical subclass and - // performed the calculation. - assertEquals("JJ-Java", loadedEntity.getNickName()); - assertEquals(EntityLoadingProxy.class, loadedEntity.getClass()); + // The expectation below is from having intercepted setNickName() with + // the artifical subclass and + // performed the calculation. + assertEquals("JJ-Java", loadedEntity.getNickName()); + assertEquals(EntityLoadingProxy.class, loadedEntity.getClass()); + } + + @Test + public void testWithGlobalTagUsingTypeDescription() { + String yamlText = + "!!org.yaml.snakeyaml.issues.issue94.Entity\n" + "name: Matt\n" + "nickName: Java\n"; + + Yaml yaml = new Yaml(); + yaml.addTypeDescription(new TypeDescription(Entity.class, EntityLoadingProxy.class)); + + Entity loadedEntity = null; + loadedEntity = yaml.load(yamlText); + + assertEquals("Matt", loadedEntity.getName()); + + // The expectation below is from having intercepted setNickName() with + // the artifical subclass and + // performed the calculation. + assertEquals("JJ-Java", loadedEntity.getNickName()); + assertEquals(EntityLoadingProxy.class, loadedEntity.getClass()); + } + + @Test + public void testNoTag() { + String yamlText = "name: Matt\n" + "nickName: Java\n"; + Yaml yaml = new Yaml(new MyConstructor(Entity.class)); + Entity loadedEntity = null; + loadedEntity = yaml.load(yamlText); + assertEquals("Matt", loadedEntity.getName()); + assertEquals("JJ-Java", loadedEntity.getNickName()); + } + + @Test + public void testNoTagWithTypeDescription() { + String yamlText = "name: Matt\n" + "nickName: Java\n"; + Yaml yaml = new Yaml(); + yaml.addTypeDescription(new TypeDescription(Entity.class, EntityLoadingProxy.class)); + Entity loadedEntity = null; + loadedEntity = yaml.loadAs(yamlText, Entity.class); + assertEquals("Matt", loadedEntity.getName()); + assertEquals("JJ-Java", loadedEntity.getNickName()); + } + + /** + * @see Constructor.ConstructYamlObject + */ + private class MyConstructor extends Constructor { + + public MyConstructor() { + super(); + this.yamlConstructors.put(null, new ConstructProxy()); } - @Test - public void testNoTag() { - String yamlText = "name: Matt\n" + "nickName: Java\n"; - Yaml yaml = new Yaml(new MyConstructor(Entity.class)); - Entity loadedEntity = null; - loadedEntity = (Entity) yaml.load(yamlText); - assertEquals("Matt", loadedEntity.getName()); - assertEquals("JJ-Java", loadedEntity.getNickName()); + public MyConstructor(Class<?> clazz) { + super(clazz); + this.yamlConstructors.put(null, new ConstructProxy()); } - /** - * @see Constructor.ConstructYamlObject - */ - private class MyConstructor extends Constructor { - public MyConstructor() { - super(); - this.yamlConstructors.put(null, new ConstructProxy()); - } + private class ConstructProxy extends AbstractConstruct { - public MyConstructor(Class<?> clazz) { - super(clazz); - this.yamlConstructors.put(null, new ConstructProxy()); + private Construct getConstructor(Node node) { + Class<?> cl = getClassForNode(node); + if (cl.equals(Entity.class)) { + // today's temperature is high :) + cl = EntityLoadingProxy.class; } + node.setType(cl); + // call the constructor as if the runtime class is defined + Construct constructor = yamlClassConstructors.get(node.getNodeId()); + return constructor; + } - private class ConstructProxy extends AbstractConstruct { - private Construct getConstructor(Node node) { - Class<?> cl = getClassForNode(node); - if (cl.equals(Entity.class) && true) { - // today's temperature is high :) - cl = EntityLoadingProxy.class; - } - node.setType(cl); - // call the constructor as if the runtime class is defined - Construct constructor = yamlClassConstructors.get(node.getNodeId()); - return constructor; - } - - public Object construct(Node node) { - return getConstructor(node).construct(node); - } - } + public Object construct(Node node) { + return getConstructor(node).construct(node); + } } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue94/Entity.java b/src/test/java/org/yaml/snakeyaml/issues/issue94/Entity.java index eb3f7ebb..63562c08 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue94/Entity.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue94/Entity.java @@ -1,37 +1,36 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue94; public class Entity { - private String name; - private String nickName; - public String getName() { - return name; - } + private String name; + private String nickName; - public void setName(String name) { - this.name = name; - } + public String getName() { + return name; + } - public String getNickName() { - return nickName; - } + public void setName(String name) { + this.name = name; + } - public void setNickName(String nickName) { - this.nickName = nickName; - } + public String getNickName() { + return nickName; + } + + public void setNickName(String nickName) { + this.nickName = nickName; + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue94/EntityLoadingProxy.java b/src/test/java/org/yaml/snakeyaml/issues/issue94/EntityLoadingProxy.java index e8129a1e..e5d2571b 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue94/EntityLoadingProxy.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue94/EntityLoadingProxy.java @@ -1,26 +1,25 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue94; public class EntityLoadingProxy extends Entity { - @Override - public void setNickName(String nickName) { - if (nickName.startsWith("J")) - nickName = "JJ-" + nickName; - super.setNickName(nickName); + @Override + public void setNickName(String nickName) { + if (nickName.startsWith("J")) { + nickName = "JJ-" + nickName; } + super.setNickName(nickName); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue95/ArrayInGenericCollectionTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue95/ArrayInGenericCollectionTest.java index b454953a..6415e708 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue95/ArrayInGenericCollectionTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue95/ArrayInGenericCollectionTest.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue95; @@ -21,9 +19,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; - import junit.framework.TestCase; - import org.junit.Assert; import org.yaml.snakeyaml.DumperOptions.FlowStyle; import org.yaml.snakeyaml.TypeDescription; @@ -34,119 +30,121 @@ import org.yaml.snakeyaml.nodes.Tag; public class ArrayInGenericCollectionTest extends TestCase { - public static class A { - private Map<String, String[]> meta = new HashMap<String, String[]>(); - } - - public static class B { - private List<String[]> meta = new ArrayList<String[]>(); - } - - private A createA() { - A a = new A(); - a.meta.put("met1", new String[] { "whatever" }); - a.meta.put("met2", new String[] { "something", "something else" }); - return a; - } - - private B createB() { - B b = new B(); - b.meta.add(new String[] { "whatever" }); - b.meta.add(new String[] { "something", "something else" }); - return b; - } - - public void testArrayAsMapValue() { - Yaml yaml2dump = new Yaml(); - yaml2dump.setBeanAccess(BeanAccess.FIELD); - A data = createA(); - String dump = yaml2dump.dump(data); - // System.out.println(dump); - - Yaml yaml2load = new Yaml(); - yaml2load.setBeanAccess(BeanAccess.FIELD); - A loaded = (A) yaml2load.load(dump); - - assertEquals(data.meta.size(), loaded.meta.size()); - Set<Entry<String, String[]>> loadedMeta = loaded.meta.entrySet(); - for (Entry<String, String[]> entry : loadedMeta) { - assertTrue(data.meta.containsKey(entry.getKey())); - Assert.assertArrayEquals(data.meta.get(entry.getKey()), entry.getValue()); - } - } - - public void testArrayAsMapValueWithTypeDespriptor() { - Yaml yaml2dump = new Yaml(); - yaml2dump.setBeanAccess(BeanAccess.FIELD); - A data = createA(); - String dump = yaml2dump.dump(data); - // System.out.println(dump); - - TypeDescription aTypeDescr = new TypeDescription(A.class); - aTypeDescr.putMapPropertyType("meta", String.class, String[].class); - - Constructor c = new Constructor(); - c.addTypeDescription(aTypeDescr); - Yaml yaml2load = new Yaml(c); - yaml2load.setBeanAccess(BeanAccess.FIELD); - - A loaded = (A) yaml2load.load(dump); - - assertEquals(data.meta.size(), loaded.meta.size()); - Set<Entry<String, String[]>> loadedMeta = loaded.meta.entrySet(); - for (Entry<String, String[]> entry : loadedMeta) { - assertTrue(data.meta.containsKey(entry.getKey())); - Assert.assertArrayEquals(data.meta.get(entry.getKey()), entry.getValue()); - } + public static class A { + + private final Map<String, String[]> meta = new HashMap<String, String[]>(); + } + + public static class B { + + private final List<String[]> meta = new ArrayList<String[]>(); + } + + private A createA() { + A a = new A(); + a.meta.put("met1", new String[] {"whatever"}); + a.meta.put("met2", new String[] {"something", "something else"}); + return a; + } + + private B createB() { + B b = new B(); + b.meta.add(new String[] {"whatever"}); + b.meta.add(new String[] {"something", "something else"}); + return b; + } + + public void testArrayAsMapValue() { + Yaml yaml2dump = new Yaml(); + yaml2dump.setBeanAccess(BeanAccess.FIELD); + A data = createA(); + String dump = yaml2dump.dump(data); + // System.out.println(dump); + + Yaml yaml2load = new Yaml(); + yaml2load.setBeanAccess(BeanAccess.FIELD); + A loaded = yaml2load.load(dump); + + assertEquals(data.meta.size(), loaded.meta.size()); + Set<Entry<String, String[]>> loadedMeta = loaded.meta.entrySet(); + for (Entry<String, String[]> entry : loadedMeta) { + assertTrue(data.meta.containsKey(entry.getKey())); + Assert.assertArrayEquals(data.meta.get(entry.getKey()), entry.getValue()); } - - public void testArrayAsListValue() { - Yaml yaml2dump = new Yaml(); - yaml2dump.setBeanAccess(BeanAccess.FIELD); - B data = createB(); - String dump = yaml2dump.dump(data); - // System.out.println(dump); - - Yaml yaml2load = new Yaml(); - yaml2load.setBeanAccess(BeanAccess.FIELD); - B loaded = (B) yaml2load.load(dump); - - Assert.assertArrayEquals(data.meta.toArray(), loaded.meta.toArray()); - } - - public void testArrayAsListValueWithTypeDespriptor() { - Yaml yaml2dump = new Yaml(); - yaml2dump.setBeanAccess(BeanAccess.FIELD); - B data = createB(); - String dump = yaml2dump.dump(data); - // System.out.println(dump); - - TypeDescription aTypeDescr = new TypeDescription(B.class); - aTypeDescr.putListPropertyType("meta", String[].class); - - Constructor c = new Constructor(); - c.addTypeDescription(aTypeDescr); - Yaml yaml2load = new Yaml(c); - yaml2load.setBeanAccess(BeanAccess.FIELD); - - B loaded = (B) yaml2load.load(dump); - - Assert.assertArrayEquals(data.meta.toArray(), loaded.meta.toArray()); - } - - public void testNoTags() { - Yaml yaml2dump = new Yaml(); - yaml2dump.setBeanAccess(BeanAccess.FIELD); - B data = createB(); - String dump = yaml2dump.dumpAs(data, Tag.MAP, FlowStyle.AUTO); - // System.out.println(dump); - assertEquals("meta:\n- [whatever]\n- [something, something else]\n", dump); - // - Constructor constr = new Constructor(B.class); - Yaml yaml2load = new Yaml(constr); - yaml2load.setBeanAccess(BeanAccess.FIELD); - B loaded = (B) yaml2load.load(dump); - - Assert.assertArrayEquals(data.meta.toArray(), loaded.meta.toArray()); + } + + public void testArrayAsMapValueWithTypeDespriptor() { + Yaml yaml2dump = new Yaml(); + yaml2dump.setBeanAccess(BeanAccess.FIELD); + A data = createA(); + String dump = yaml2dump.dump(data); + // System.out.println(dump); + + TypeDescription aTypeDescr = new TypeDescription(A.class); + aTypeDescr.putMapPropertyType("meta", String.class, String[].class); + + Constructor c = new Constructor(); + c.addTypeDescription(aTypeDescr); + Yaml yaml2load = new Yaml(c); + yaml2load.setBeanAccess(BeanAccess.FIELD); + + A loaded = yaml2load.load(dump); + + assertEquals(data.meta.size(), loaded.meta.size()); + Set<Entry<String, String[]>> loadedMeta = loaded.meta.entrySet(); + for (Entry<String, String[]> entry : loadedMeta) { + assertTrue(data.meta.containsKey(entry.getKey())); + Assert.assertArrayEquals(data.meta.get(entry.getKey()), entry.getValue()); } + } + + public void testArrayAsListValue() { + Yaml yaml2dump = new Yaml(); + yaml2dump.setBeanAccess(BeanAccess.FIELD); + B data = createB(); + String dump = yaml2dump.dump(data); + // System.out.println(dump); + + Yaml yaml2load = new Yaml(); + yaml2load.setBeanAccess(BeanAccess.FIELD); + B loaded = yaml2load.load(dump); + + Assert.assertArrayEquals(data.meta.toArray(), loaded.meta.toArray()); + } + + public void testArrayAsListValueWithTypeDespriptor() { + Yaml yaml2dump = new Yaml(); + yaml2dump.setBeanAccess(BeanAccess.FIELD); + B data = createB(); + String dump = yaml2dump.dump(data); + // System.out.println(dump); + + TypeDescription aTypeDescr = new TypeDescription(B.class); + aTypeDescr.putListPropertyType("meta", String[].class); + + Constructor c = new Constructor(); + c.addTypeDescription(aTypeDescr); + Yaml yaml2load = new Yaml(c); + yaml2load.setBeanAccess(BeanAccess.FIELD); + + B loaded = yaml2load.load(dump); + + Assert.assertArrayEquals(data.meta.toArray(), loaded.meta.toArray()); + } + + public void testNoTags() { + Yaml yaml2dump = new Yaml(); + yaml2dump.setBeanAccess(BeanAccess.FIELD); + B data = createB(); + String dump = yaml2dump.dumpAs(data, Tag.MAP, FlowStyle.AUTO); + // System.out.println(dump); + assertEquals("meta:\n- [whatever]\n- [something, something else]\n", dump); + // + Constructor constr = new Constructor(B.class); + Yaml yaml2load = new Yaml(constr); + yaml2load.setBeanAccess(BeanAccess.FIELD); + B loaded = yaml2load.load(dump); + + Assert.assertArrayEquals(data.meta.toArray(), loaded.meta.toArray()); + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue97/Blog.java b/src/test/java/org/yaml/snakeyaml/issues/issue97/Blog.java index 8da4dc71..ddb4d7c7 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue97/Blog.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue97/Blog.java @@ -1,30 +1,29 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue97; import java.util.SortedSet; public class Blog { - private SortedSet<Post> posts; - public void addPost(Post p) { - posts.add(p); - } + private SortedSet<Post> posts; - public SortedSet<Post> getPosts() { - return posts; - } -}
\ No newline at end of file + public void addPost(Post p) { + posts.add(p); + } + + public SortedSet<Post> getPosts() { + return posts; + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue97/Post.java b/src/test/java/org/yaml/snakeyaml/issues/issue97/Post.java index 479055de..015f38c8 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue97/Post.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue97/Post.java @@ -1,34 +1,32 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue97; public class Post implements Comparable<Post> { - private String title; - private String text; + private String title; + private String text; - public String getTitle() { - return title; - } + public String getTitle() { + return title; + } - public String getText() { - return text; - } + public String getText() { + return text; + } - public int compareTo(Post o) { - return title.compareTo(o.title); - } -}
\ No newline at end of file + public int compareTo(Post o) { + return title.compareTo(o.title); + } +} diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue97/YamlSortedSetTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue97/YamlSortedSetTest.java index 1b4d3a1d..d6175c56 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue97/YamlSortedSetTest.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue97/YamlSortedSetTest.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue97; @@ -19,9 +17,9 @@ import java.util.Collection; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; - import org.junit.Assert; import org.junit.Test; +import org.yaml.snakeyaml.TypeDescription; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; import org.yaml.snakeyaml.error.YAMLException; @@ -32,48 +30,101 @@ import org.yaml.snakeyaml.nodes.SequenceNode; public class YamlSortedSetTest { - @Test - public void testYaml() { - String serialized = "!!org.yaml.snakeyaml.issues.issue97.Blog\n" + "posts:\n" - + " - text: Dummy\n" + " title: Test\n" + " - text: Creative\n" - + " title: Highly\n"; - // System.out.println(serialized); - Yaml yaml2 = constructYamlParser(); - Blog rehydrated = (Blog) yaml2.load(serialized); - checkTestBlog(rehydrated); - } + @Test + public void testYaml() { + String serialized = + "!!org.yaml.snakeyaml.issues.issue97.Blog\n" + "posts:\n" + " - text: Dummy\n" + + " title: Test\n" + " - text: Creative\n" + " title: Highly\n"; + // System.out.println(serialized); + Yaml yaml2 = constructYamlParser(); + Blog rehydrated = yaml2.load(serialized); + checkTestBlog(rehydrated); + } - protected Yaml constructYamlParser() { - Yaml yaml = new Yaml(new SetContructor()); - yaml.setBeanAccess(BeanAccess.FIELD); - return yaml; - } + public void testYaml2() { + String serialized = + "!!org.yaml.snakeyaml.issues.issue97.Blog\n" + "posts:\n" + " - text: Dummy\n" + + " title: Test\n" + " - text: Creative\n" + " title: Highly\n"; + // System.out.println(serialized); + Yaml yaml2 = constructYamlParser2(); + Blog rehydrated = yaml2.load(serialized); + checkTestBlog(rehydrated); + } + + public void testYaml3() { + String serialized = + "!!org.yaml.snakeyaml.issues.issue97.Blog\n" + "posts:\n" + " - text: Dummy\n" + + " title: Test\n" + " - text: Creative\n" + " title: Highly\n"; + // System.out.println(serialized); + Yaml yaml3 = constructYamlParser3(); + Blog rehydrated = yaml3.loadAs(serialized, Blog.class); + checkTestBlog(rehydrated); + } + + public void testYamlDefault() { + String serialized = + "!!org.yaml.snakeyaml.issues.issue97.Blog\n" + "posts:\n" + " - text: Dummy\n" + + " title: Test\n" + " - text: Creative\n" + " title: Highly\n"; + // System.out.println(serialized); + Yaml yaml = new Yaml(); + yaml.setBeanAccess(BeanAccess.FIELD); + Blog rehydrated = yaml.loadAs(serialized, Blog.class); + checkTestBlog(rehydrated); + } + + protected Yaml constructYamlParser2() { + Yaml yaml = new Yaml(); + yaml.addTypeDescription(new TypeDescription(SortedSet.class) { + @Override + public Object newInstance(Node node) { + return new TreeSet<Object>(); + } + }); + yaml.setBeanAccess(BeanAccess.FIELD); + return yaml; + } - protected void checkTestBlog(Blog blog) { - Set<Post> posts = blog.getPosts(); - Assert.assertEquals("Blog contains 2 posts", 2, posts.size()); + protected Yaml constructYamlParser3() { + Yaml yaml = new Yaml(); + yaml.setBeanAccess(BeanAccess.FIELD); + yaml.addTypeDescription(new TypeDescription(SortedSet.class, TreeSet.class)); + return yaml; + } + + protected Yaml constructYamlParser() { + Yaml yaml = new Yaml(new SetContructor()); + yaml.setBeanAccess(BeanAccess.FIELD); + return yaml; + } + + protected void checkTestBlog(Blog blog) { + Set<Post> posts = blog.getPosts(); + Assert.assertTrue("posts should be SortedSet", (posts instanceof SortedSet)); + Assert.assertEquals("Blog contains 2 posts", 2, posts.size()); + } + + private class SetContructor extends Constructor { + + public SetContructor() { + yamlClassConstructors.put(NodeId.sequence, new ConstructSetFromSequence()); } - private class SetContructor extends Constructor { - public SetContructor() { - yamlClassConstructors.put(NodeId.sequence, new ConstructSetFromSequence()); - } + private class ConstructSetFromSequence extends ConstructSequence { - private class ConstructSetFromSequence extends ConstructSequence { - @Override - public Object construct(Node node) { - if (SortedSet.class.isAssignableFrom(node.getType())) { - if (node.isTwoStepsConstruction()) { - throw new YAMLException("Set cannot be recursive."); - } else { - Collection<Object> result = new TreeSet<Object>(); - SetContructor.this.constructSequenceStep2((SequenceNode) node, result); - return result; - } - } else { - return super.construct(node); - } - } + @Override + public Object construct(Node node) { + if (SortedSet.class.isAssignableFrom(node.getType())) { + if (node.isTwoStepsConstruction()) { + throw new YAMLException("Set cannot be recursive."); + } else { + Collection<Object> result = new TreeSet<Object>(); + SetContructor.this.constructSequenceStep2((SequenceNode) node, result); + return result; + } + } else { + return super.construct(node); } + } } + } } diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue99/YamlBase64Test.java b/src/test/java/org/yaml/snakeyaml/issues/issue99/YamlBase64Test.java index 32e7eefd..c1a742d3 100644 --- a/src/test/java/org/yaml/snakeyaml/issues/issue99/YamlBase64Test.java +++ b/src/test/java/org/yaml/snakeyaml/issues/issue99/YamlBase64Test.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2008, http://www.snakeyaml.org + * Copyright (c) 2008, SnakeYAML * - * Licensed 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 + * Licensed 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 + * 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. + * 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.yaml.snakeyaml.issues.issue99; @@ -19,9 +17,7 @@ import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Map; - import junit.framework.TestCase; - import org.yaml.snakeyaml.Util; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.YamlDocument; @@ -34,117 +30,113 @@ import org.yaml.snakeyaml.nodes.Tag; /** * Example for issue 99 - * - * @see <a href="http://code.google.com/p/snakeyaml/issues/detail?id=99"></a> + * + * @see <a href="http://code.google.com/p/snakeyaml/issues/detail?id=99">issue 99</a> */ public class YamlBase64Test extends TestCase { - /** - * test base64 decoding - */ - public void testBase64() throws IOException { - String text = Util.getLocalResource("issues/issue99-base64_literal.yaml"); - String[] lines = text.split("\n"); - String all = ""; - for (int i = 1; i < lines.length; i++) {// skip first line - all = all + lines[i].trim(); - } - // System.out.println(all); - byte[] decoded = Base64Coder.decode(all.toCharArray()); - assertEquals(3737, decoded.length); - checkBytes(decoded); + /** + * test base64 decoding + */ + public void testBase64() throws IOException { + String text = Util.getLocalResource("issues/issue99-base64_literal.yaml"); + String[] lines = text.split("\n"); + String all = ""; + for (int i = 1; i < lines.length; i++) {// skip first line + all = all + lines[i].trim(); } + // System.out.println(all); + byte[] decoded = Base64Coder.decode(all.toCharArray()); + assertEquals(3737, decoded.length); + checkBytes(decoded); + } - @SuppressWarnings("unchecked") - public void testYamlBase64Loading() throws IOException { - Yaml yaml = new Yaml(); - InputStream inputStream = YamlBase64Test.class - .getResourceAsStream("/issues/issue99-base64_double_quoted.yaml"); - Map<String, Object> bean = (Map<String, Object>) yaml.load(inputStream); - byte[] jpeg = (byte[]) bean.get("jpegPhoto"); - checkBytes(jpeg); - inputStream.close(); - } + @SuppressWarnings("unchecked") + public void testYamlBase64Loading() throws IOException { + Yaml yaml = new Yaml(); + InputStream inputStream = + YamlBase64Test.class.getResourceAsStream("/issues/issue99-base64_double_quoted.yaml"); + Map<String, Object> bean = yaml.load(inputStream); + byte[] jpeg = (byte[]) bean.get("jpegPhoto"); + checkBytes(jpeg); + inputStream.close(); + } - private void checkBytes(byte[] jpeg) throws IOException { - InputStream input; - input = YamlDocument.class.getClassLoader().getResourceAsStream("issues/issue99.jpeg"); - BufferedInputStream is = new BufferedInputStream(input); - int i = 0; - while (i < jpeg.length) { - int etalon = is.read(); - if (jpeg[i] < 0) { - assertEquals(etalon, jpeg[i] + 256); - } else { - assertEquals(etalon, jpeg[i]); - } - i++; - } - is.close(); + private void checkBytes(byte[] jpeg) throws IOException { + InputStream input; + input = YamlDocument.class.getClassLoader().getResourceAsStream("issues/issue99.jpeg"); + BufferedInputStream is = new BufferedInputStream(input); + int i = 0; + while (i < jpeg.length) { + int etalon = is.read(); + if (jpeg[i] < 0) { + assertEquals(etalon, jpeg[i] + 256); + } else { + assertEquals(etalon, jpeg[i]); + } + i++; } + is.close(); + } - /** - * In the literal scalar all the line breaks are significant - * - * @throws IOException - */ - public void testYamlBase64LoadingLiteral() throws IOException { - Yaml yaml = new Yaml(); - InputStream inputStream = YamlBase64Test.class - .getResourceAsStream("/issues/issue99-base64_literal.yaml"); - try { - yaml.load(inputStream); - fail("In the literal scalar all the line breaks are significant"); - } catch (Exception e) { - assertEquals("Length of Base64 encoded input string is not a multiple of 4.", - e.getMessage()); - } finally { - inputStream.close(); - } - } + /** + * In the literal scalar all the line breaks are significant + * + * @throws IOException + */ + public void testYamlBase64LoadingLiteral() throws IOException { + Yaml yaml = new Yaml(); + InputStream inputStream = + YamlBase64Test.class.getResourceAsStream("/issues/issue99-base64_literal.yaml"); + Map<String, Object> bean = yaml.load(inputStream); + byte[] jpeg = (byte[]) bean.get("jpegPhoto"); + checkBytes(jpeg); + inputStream.close(); + } - /** - * Redefine the !!binary global tag in a way that it ignores all the white - * spaces to be able to use literal scalar - */ - @SuppressWarnings("unchecked") - public void testRedefineBinaryTag() throws IOException { - Yaml yaml = new Yaml(new SpecialContructor(Tag.BINARY)); - InputStream inputStream = YamlBase64Test.class - .getResourceAsStream("/issues/issue99-base64_literal.yaml"); - Map<String, Object> bean = (Map<String, Object>) yaml.load(inputStream); - byte[] jpeg = (byte[]) bean.get("jpegPhoto"); - checkBytes(jpeg); - inputStream.close(); - } + /** + * Redefine the !!binary global tag in a way that it ignores all the white spaces to be able to + * use literal scalar + */ + @SuppressWarnings("unchecked") + public void testRedefineBinaryTag() throws IOException { + Yaml yaml = new Yaml(new SpecialContructor(Tag.BINARY)); + InputStream inputStream = + YamlBase64Test.class.getResourceAsStream("/issues/issue99-base64_literal.yaml"); + Map<String, Object> bean = yaml.load(inputStream); + byte[] jpeg = (byte[]) bean.get("jpegPhoto"); + checkBytes(jpeg); + inputStream.close(); + } - private class SpecialContructor extends Constructor { - public SpecialContructor(Tag tag) { - this.yamlConstructors.put(tag, new MyBinaryConstructor()); - } + private class SpecialContructor extends Constructor { - private class MyBinaryConstructor extends AbstractConstruct { - public Object construct(Node node) { - String contentWithNewLines = constructScalar((ScalarNode) node).toString(); - String noNewLines = contentWithNewLines.replaceAll("\\s", ""); - byte[] decoded = Base64Coder.decode(noNewLines.toCharArray()); - return decoded; - } - } + public SpecialContructor(Tag tag) { + this.yamlConstructors.put(tag, new MyBinaryConstructor()); } - /** - * Define a local tag to ignore all the white spaces to be able to use - * literal scalar - */ - @SuppressWarnings("unchecked") - public void testLocalBinaryTag() throws IOException { - Yaml yaml = new Yaml(new SpecialContructor(new Tag("!beautiful"))); - InputStream inputStream = YamlBase64Test.class - .getResourceAsStream("/issues/issue99-base64_literal_custom_tag.yaml"); - Map<String, Object> bean = (Map<String, Object>) yaml.load(inputStream); - byte[] jpeg = (byte[]) bean.get("jpegPhoto"); - checkBytes(jpeg); - inputStream.close(); + private class MyBinaryConstructor extends AbstractConstruct { + + public Object construct(Node node) { + String contentWithNewLines = constructScalar((ScalarNode) node); + String noNewLines = contentWithNewLines.replaceAll("\\s", ""); + byte[] decoded = Base64Coder.decode(noNewLines.toCharArray()); + return decoded; + } } + } + + /** + * Define a local tag to ignore all the white spaces to be able to use literal scalar + */ + @SuppressWarnings("unchecked") + public void testLocalBinaryTag() throws IOException { + Yaml yaml = new Yaml(new SpecialContructor(new Tag("!beautiful"))); + InputStream inputStream = + YamlBase64Test.class.getResourceAsStream("/issues/issue99-base64_literal_custom_tag.yaml"); + Map<String, Object> bean = yaml.load(inputStream); + byte[] jpeg = (byte[]) bean.get("jpegPhoto"); + checkBytes(jpeg); + inputStream.close(); + } } |