Author: fanningpj
Date: Thu Apr 20 11:00:11 2023
New Revision: 1909275
URL: http://svn.apache.org/viewvc?rev=1909275&view=rev
Log:
[XMLBEANS-636] add tests
Added:
xmlbeans/trunk/src/test/java/scomp/abstractTypes/
xmlbeans/trunk/src/test/java/scomp/abstractTypes/detailed/
xmlbeans/trunk/src/test/java/scomp/abstractTypes/detailed/AbstractTypesTest.java
- copied, changed from r1909271,
xmlbeans/trunk/src/test/java/scomp/xmlbeans583/detailed/Xmlbeans583Test.java
xmlbeans/trunk/src/test/resources/xbean/scomp/abstractType/
xmlbeans/trunk/src/test/resources/xbean/scomp/abstractType/abstractBase.xsd
- copied unchanged from r1909271,
xmlbeans/trunk/samples/AbstractTypes/schemas/abstractBase.xsd
xmlbeans/trunk/src/test/resources/xbean/scomp/abstractType/myfigures.xsd
- copied unchanged from r1909271,
xmlbeans/trunk/samples/AbstractTypes/schemas/myfigures.xsd
Copied:
xmlbeans/trunk/src/test/java/scomp/abstractTypes/detailed/AbstractTypesTest.java
(from r1909271,
xmlbeans/trunk/src/test/java/scomp/xmlbeans583/detailed/Xmlbeans583Test.java)
URL:
http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/scomp/abstractTypes/detailed/AbstractTypesTest.java?p2=xmlbeans/trunk/src/test/java/scomp/abstractTypes/detailed/AbstractTypesTest.java&p1=xmlbeans/trunk/src/test/java/scomp/xmlbeans583/detailed/Xmlbeans583Test.java&r1=1909271&r2=1909275&rev=1909275&view=diff
==============================================================================
---
xmlbeans/trunk/src/test/java/scomp/xmlbeans583/detailed/Xmlbeans583Test.java
(original)
+++
xmlbeans/trunk/src/test/java/scomp/abstractTypes/detailed/AbstractTypesTest.java
Thu Apr 20 11:00:11 2023
@@ -12,28 +12,112 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package scomp.xmlbeans583.detailed;
+package scomp.abstractTypes.detailed;
+import abstractFigures.RootDocument;
+import abstractFigures.Shape;
+import figures.Circle;
+import figures.Square;
+import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.XmlOptions;
import org.junit.jupiter.api.Test;
-import xsd.xmlToStringTest1Xsd.Ns1T1;
-import xsd.xmlToStringTest2Xsd.Ns2E1Document;
-import xsd.xmlToStringTest3Xsd.Ns3E1Document;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
-public class Xmlbeans583Test {
+public class AbstractTypesTest {
@Test
- void testXmlToString() throws Throwable {
- final Ns2E1Document ns2e1Doc = Ns2E1Document.Factory.newInstance();
- ns2e1Doc.addNewNs2E1().addNewNs2T1E1().setNs1T1E1("Ns1T1E1");
-
- final Ns3E1Document ns3e1Doc = Ns3E1Document.Factory.newInstance();
-
ns3e1Doc.addNewNs3E1().setNs3T1E1(Ns1T1.Factory.parse(ns2e1Doc.getNs2E1().getNs2T1E1().toString()));
-
- final Ns3E1Document pNs3e1Doc =
Ns3E1Document.Factory.parse(ns3e1Doc.toString());
- final Ns2E1Document pNs2e1Doc = Ns2E1Document.Factory.newInstance();
- pNs2e1Doc.addNewNs2E1().setNs2T1E1(pNs3e1Doc.getNs3E1().getNs3T1E1());
+ void testBuildDocument() {
+ XmlObject doc = buildDocument(false);
+ assertTrue(doc.validate());
+ }
+
+ @Test
+ void testBuildDocument2() {
+ XmlObject doc = buildDocument2(false);
+ assertTrue(doc.validate());
+ }
+
+ @Test
+ void testParseDocument() throws Throwable {
+ final String document =
+ "<abs:root xmlns:abs=\"AbstractFigures\">\r\n"
+ + " <figure xsi:type=\"fig:circle\"
xmlns:fig=\"Figures\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n"
+ + " <radius>10.0</radius>\r\n"
+ + " </figure>\r\n"
+ + " <figure xsi:type=\"fig:square\"
xmlns:fig=\"Figures\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n"
+ + " <side>20.0</side>\r\n"
+ + " </figure>\r\n"
+ + "</abs:root>";
+ RootDocument doc = RootDocument.Factory.parse(document);
+
+ Shape[] shapeArray = doc.getRoot().getFigureArray();
+ assertTrue(shapeArray[0] instanceof Circle, "Shape #1 is a Circle?");
+ assertTrue(shapeArray[1] instanceof Square, "Shape #2 is a Square?");
+ }
+
+ public static XmlObject buildDocument(boolean enableOutput)
+ {
+ XmlOptions opt = (new XmlOptions()).setSavePrettyPrint();
+
+ // Build a new document
+ RootDocument doc = RootDocument.Factory.newInstance();
+ RootDocument.Root figures = doc.addNewRoot();
+ if (enableOutput)
+ System.out.println("Empty document:\n" + doc.xmlText(opt) + "\n");
+
+ // Add abstract figures
+ Shape s1 = figures.addNewFigure();
+ s1.setId("001");
+ Shape s2 = figures.addNewFigure();
+ s2.setId("002");
+ // Document contains two shapes now
+ // Because the shape is abstract, the document will not yet be valid
+ if (enableOutput)
+ {
+ System.out.println("Document containing the abstract types:\n" +
doc.xmlText(opt));
+ System.out.println("Valid = " + doc.validate() + "\n");
+ }
+
+ // Change the abstract figures to concrete ones
+ Circle circle = (Circle) s1.changeType(Circle.type);
+ circle.setRadius(10.0);
+ Square square = (Square) s2.changeType(Square.type);
+ square.setSide(20.0);
+ // Document contains two concrete shapes and is valid
+ if (enableOutput)
+ {
+ System.out.println("Final document:\n" + doc.xmlText(opt));
+ System.out.println("Vald = " + doc.validate());
+ }
+
+ return doc;
+ }
+
+ public static XmlObject buildDocument2(boolean enableOutput)
+ {
+ if (enableOutput)
+ System.out.println("buildDocument2:\n");
+ XmlOptions opt = (new XmlOptions()).setSavePrettyPrint();
+
+ // Build a new document
+ RootDocument doc = RootDocument.Factory.newInstance();
+ RootDocument.Root figures = doc.addNewRoot();
+ if (enableOutput)
+ System.out.println("Empty document:\n" + doc.xmlText(opt) + "\n");
+
+ Circle circle = Circle.Factory.newInstance();
+ circle.setRadius(10.0);
+ Square square = Square.Factory.newInstance();
+ square.setSide(20.0);
+ figures.setFigureArray(new Shape[] {circle, square});
+
+ // Document contains two concrete shapes and is valid
+ if (enableOutput)
+ {
+ System.out.println("Final document:\n" + doc.xmlText(opt));
+ System.out.println("Valid = " + doc.validate());
+ }
- assertNotNull(Ns2E1Document.Factory.parse(pNs2e1Doc.toString()));
+ return doc;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]