issue 87: add example for CON without package names
diff --git a/src/test/java/org/yaml/snakeyaml/extensions/compactnotation/Box.java b/src/test/java/org/yaml/snakeyaml/extensions/compactnotation/Box.java
new file mode 100644
index 0000000..804a620
--- /dev/null
+++ b/src/test/java/org/yaml/snakeyaml/extensions/compactnotation/Box.java
@@ -0,0 +1,54 @@
+/**
+ * Copyright (c) 2008-2010, http://code.google.com/p/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.extensions.compactnotation;
+
+public class Box {
+ private String id;
+ private String name;
+ private Item top;
+ private Item bottom;
+
+ public Box(String id, String name) {
+ super();
+ this.id = id;
+ this.name = name;
+ }
+
+ public Item getTop() {
+ return top;
+ }
+
+ public void setTop(Item top) {
+ this.top = top;
+ }
+
+ public Item getBottom() {
+ return bottom;
+ }
+
+ public void setBottom(Item bottom) {
+ this.bottom = bottom;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public String getName() {
+ return name;
+ }
+}
diff --git a/src/test/java/org/yaml/snakeyaml/extensions/compactnotation/CompactConstructorExampleTest.java b/src/test/java/org/yaml/snakeyaml/extensions/compactnotation/CompactConstructorExampleTest.java
index 844bc9a..62c5a05 100644
--- a/src/test/java/org/yaml/snakeyaml/extensions/compactnotation/CompactConstructorExampleTest.java
+++ b/src/test/java/org/yaml/snakeyaml/extensions/compactnotation/CompactConstructorExampleTest.java
@@ -23,6 +23,7 @@
import org.yaml.snakeyaml.Util;
import org.yaml.snakeyaml.Yaml;
+import org.yaml.snakeyaml.constructor.Constructor;
public class CompactConstructorExampleTest extends TestCase {
@@ -127,6 +128,7 @@
}
@SuppressWarnings("unchecked")
+ // TODO it is unclear how the result should look like for CON
public void test9() {
CompactConstructor compact = new CompactConstructor();
Yaml yaml = new Yaml(compact);
@@ -153,5 +155,29 @@
List<Container> containers = (List<Container>) map.get("something");
// System.out.println(obj);
assertEquals(3, containers.size());
+ for (Container c : containers) {
+ assertTrue(c.toString(), c.getId().matches("id\\d+"));
+ assertTrue(c.toString(), c.getName().matches("child\\d+"));
+ // System.out.println(c);
+ }
+ }
+
+ public void test11withoutPackageNames() {
+ Constructor compact = new CustomCompactConstructor(
+ "org.yaml.snakeyaml.extensions.compactnotation");
+ Yaml yaml = new Yaml(compact);
+ String doc = Util.getLocalResource("compactnotation/container11.yaml");
+ Box box = (Box) yaml.load(doc);
+ assertNotNull(box);
+ assertEquals("id11", box.getId());
+ assertEquals("Main box", box.getName());
+ Item top = box.getTop();
+ assertEquals("id003", top.getId());
+ assertEquals("25.0", top.getPrice());
+ assertEquals("parrot", top.getName());
+ Item bottom = box.getBottom();
+ assertEquals("id004", bottom.getId());
+ assertEquals("3.5", bottom.getPrice());
+ assertEquals("sweet", bottom.getName());
}
}
diff --git a/src/test/java/org/yaml/snakeyaml/extensions/compactnotation/CustomCompactConstructor.java b/src/test/java/org/yaml/snakeyaml/extensions/compactnotation/CustomCompactConstructor.java
new file mode 100644
index 0000000..7b53492
--- /dev/null
+++ b/src/test/java/org/yaml/snakeyaml/extensions/compactnotation/CustomCompactConstructor.java
@@ -0,0 +1,38 @@
+/**
+ * Copyright (c) 2008-2010, http://code.google.com/p/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.extensions.compactnotation;
+
+public class CustomCompactConstructor extends CompactConstructor {
+ private String packageName;
+
+ public CustomCompactConstructor(String packageName) {
+ this.packageName = packageName;
+ }
+
+ @Override
+ protected Class<?> getClassForName(String name) throws ClassNotFoundException {
+ if (name.indexOf('.') < 0) {
+ try {
+ Class<?> clazz = Class.forName(packageName + "." + name);
+ return clazz;
+ } catch (ClassNotFoundException e) {
+ // use super implementation
+ }
+ }
+ return super.getClassForName(name);
+ }
+}
diff --git a/src/test/java/org/yaml/snakeyaml/extensions/compactnotation/Item.java b/src/test/java/org/yaml/snakeyaml/extensions/compactnotation/Item.java
new file mode 100644
index 0000000..170f9c8
--- /dev/null
+++ b/src/test/java/org/yaml/snakeyaml/extensions/compactnotation/Item.java
@@ -0,0 +1,47 @@
+/**
+ * Copyright (c) 2008-2010, http://code.google.com/p/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.extensions.compactnotation;
+
+public class Item {
+ private String id;
+ private String price;
+ private String name;
+
+ public Item(String id) {
+ this.id = id;
+ }
+
+ public String getPrice() {
+ return price;
+ }
+
+ public void setPrice(String price) {
+ this.price = price;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getId() {
+ return id;
+ }
+}
diff --git a/src/test/resources/compactnotation/container11.yaml b/src/test/resources/compactnotation/container11.yaml
new file mode 100644
index 0000000..3e3bb19
--- /dev/null
+++ b/src/test/resources/compactnotation/container11.yaml
@@ -0,0 +1,4 @@
+Box(id11, Main box):
+ top: Item(id003, price=25.0, name=parrot)
+ bottom: Item(id004, price=3.5, name=sweet)
+