📄 insertandretrievaltest.java
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Library License version 1 published by ozone-db.org.//// The original code and portions created by SMB are// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.//// $Id: InsertAndRetrievalTest.java,v 1.3 2003/06/23 14:41:48 per_nyfelt Exp $package test.ozoneDB.xml.dom4j;import junit.framework.TestCase;import org.dom4j.Document;import org.dom4j.Element;import org.ozoneDB.ExternalDatabase;import org.ozoneDB.xml.dom4j.O3DocumentHelper;/** * $Id: InsertAndRetrievalTest.java,v 1.3 2003/06/23 14:41:48 per_nyfelt Exp $ */public class InsertAndRetrievalTest extends TestCase { ExternalDatabase db; public InsertAndRetrievalTest(String methodName) { super(methodName); } private void init() throws Exception { db = ExternalDatabase.openDatabase("ozonedb:remote://localhost:3333"); O3DocumentHelper.configure(db); } public void testInsertAndRetrieval() { try { init(); Document doc = O3DocumentHelper.fetchDocument("testDoc"); if (doc != null) { System.out.println("deleting existing testDoc"); O3DocumentHelper.deleteDocument(doc); } doc = O3DocumentHelper.createDocument("testDoc"); assertNotNull("Creating document", doc); //db.nameObject((OzoneRemote) doc, "testDoc"); Element root = O3DocumentHelper.createElement("addresses"); assertNotNull("Creating adresses element", root); assertEquals("addresses", root.getName()); doc.setRootElement(root); assertEquals("adding root element to doc ", root, doc.getRootElement()); Element el1 = O3DocumentHelper.createElement("address"); assertEquals("Creating element address 1","address", el1.getName() ); //Attribute att = O3DocumentHelper.createAttribute(null, "name", "Andreas"); //el1.add(att); el1.addAttribute("name", "Andreas"); assertEquals("Adding name attribute to el1", "Andreas", el1.attributeValue("name")); Element town1 = O3DocumentHelper.createElement("town"); town1.setText("New York"); assertEquals("setting text new york", "New York", town1.getText() ); el1.add(town1); root.add(el1); //System.out.println("[DocumentHelperTest] - Creating element address 2"); Element el2 = O3DocumentHelper.createElement("address"); //System.out.println("[DocumentHelperTest] - Adding name attribute to el2"); O3DocumentHelper.createAttribute(el2, "name", "Lars"); //el2.addAttribute("name","Lars"); //System.out.println("[DocumentHelperTest] - creating element town 2"); Element town2 = O3DocumentHelper.createElement("town"); //System.out.println("[DocumentHelperTest] - setting text to Los Angeles"); town2.setText("Los Angeles"); //System.out.println("[DocumentHelperTest] - Adding town2 to el2"); el2.add(town2); //System.out.println("[DocumentHelperTest] - Adding el2 to root"); root.add(el2); assertEquals("Second element should have a town element callled Los Angeles", ((Element)root.elements().get(1)).element("town").getText(), "Los Angeles"); String docXML = doc.asXML(); //System.out.println("created document " + docXML); db.close(); init(); Document doc2 = O3DocumentHelper.fetchDocument("testDoc"); String doc2XML = doc2.asXML(); //System.out.println("found document " + doc2XML); db.close(); init(); assertEquals("matching asXML output", docXML, doc2XML); O3DocumentHelper.deleteDocument(doc2); db.close(); } catch (Exception e) { e.printStackTrace(); fail(e.toString()); } } public void testChainedBuilding() { try { init(); Document document = null; try { document = O3DocumentHelper.createDocument(); } catch (Exception e) { fail(e.toString()); } Element root = document.addElement("root"); root.addElement("author") .addAttribute("name", "Toby") .addAttribute("location", "Germany") .addText("Tobias Rademacher"); assertEquals("First attribute should be name, ", "name", root.element("author").attribute(0).getQName().getName()); root.addElement("author") .addAttribute("name", "James") .addAttribute("location", "UK") .addText("James Strachan"); assertEquals("Author text should be James Strachan, ", "James Strachan", ((Element)root.elements().get(1)).getText()); String xmlDoc = document.asXML(); assertNotNull(xmlDoc); //System.out.println("document created: " + xmlDoc); O3DocumentHelper.deleteDocument(document); db.close(); } catch (Exception e) { e.printStackTrace(); fail(e.toString()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -