📄 documenttest_java.htm
字号:
count = 0;
for( Enumeration eachChild = testElement.getChildren( useName ); eachChild.hasMoreElements(); count++, eachChild.nextElement() )
;
assertTrue(
"element didnt have expected number of children named '" +useName + "'",
1 == count );
// This check also is important for checking that the behaviour of the
// tree is correct when there are nodes with the same name as the parent in subtrees.
Element testElement3 = doc.createElement( useName, useValue );
testElement2.appendChild( testElement3 );
testElement3 = doc.createElement( useName );
testElement2.appendChild( testElement3 );
count = 0;
for( Enumeration eachChild = testElement2.getChildren( useName ); eachChild.hasMoreElements(); count++, eachChild.nextElement() )
;
assertTrue(
"element didnt have expected number of children named '" +useName + "'",
2 == count );
StructuredDocument likeMe = null;
try {
likeMe = (StructuredTextDocument) instantiator.newInstance( doc.getMimeType(), doc.getStream() );
} catch( java.security.ProviderException thrown ) {
;
} catch( Throwable thrown ) {
thrown.printStackTrace();
fail( "Exception thrown during reconstruction! " + thrown.toString() );
}
if( testElement instanceof Attributable ) {
_testAttributes( (Attributable) testElement );
_testAttributes( (Attributable) testElement3 );
}
try {
likeMe = instantiator.newInstance( doc.getMimeType(), doc.getStream() );
} catch( java.security.ProviderException thrown ) {
;
} catch( Throwable thrown ) {
thrown.printStackTrace();
fail( "Exception thrown during reconstruction! " + thrown.toString() );
}
Writer somewhere = new StringWriter();
(doc).sendToWriter(somewhere);
String docAsString = somewhere.toString().trim();
testElement3 = doc.createElement( useName, docAsString );
testElement2.appendChild( testElement3 );
String docFromElement = (String) testElement3.getValue();
assertTrue(
"Could not faithfully store stream representation of doc in doc. (lengths dont match)",
docAsString.length() == docFromElement.length() );
for( int eachChar = 0; eachChar < docAsString.length(); eachChar++ ) {
assertTrue(
"Could not faithfully store stream representation of doc in doc. (failed at index: " + eachChar + ")",
docAsString.charAt( eachChar ) == docFromElement.charAt(eachChar) );
}
Element testElement4 = doc.createElement( "shortname", "shortvalue" );
Element testElement5 = doc.createElement( "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongname", "shortvalue" );
Element testElement6 = doc.createElement(
"looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongname",
"looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongvalue" );
Element testElement7 = doc.createElement(
"shortname",
"looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongvalue" );
doc.appendChild( testElement4 );
doc.appendChild( testElement5 );
doc.appendChild( testElement6 );
doc.appendChild( testElement7 );
System.out.println( testElement4.toString() );
// System.out.println( testElement5.toString() );
// System.out.println( testElement6.toString() );
// System.out.println( testElement7.toString() );
} catch( Throwable everything ) {
everything.printStackTrace();
fail("caught an unexpected exception - " + everything.toString() );
}
}
public void _testAttributes( Attributable element ) {
try {
final String someName = "attrName";
final String someValue = "attrValue";
Enumeration attribs = element.getAttributes();
assertTrue( "Element already had attributes!", !attribs.hasMoreElements() );
assertTrue( "New attribute returned previous value!",
null == element.addAttribute( someName, someValue ) );
String oldValue = element.addAttribute( new Attribute( someName, someValue) );
assertTrue( "New attribute didnt return previous value!",
(null != oldValue) && (oldValue.equals(someValue)) );
Attribute anAttrib = element.getAttribute( someName );
assertTrue( "Could not get attribute back!", null != anAttrib );
assertTrue( "value of attribute was not correct",
anAttrib.getValue().equals( someValue ) );
anAttrib = element.getAttribute( "bogusName" );
assertTrue( "Should not have been able to get an unknown attribute name", null == anAttrib );
} catch( Throwable everything ) {
everything.printStackTrace();
fail( "Caught an unexpected exception - " + everything.toString() );
}
}
private void _testConstructors( StructuredDocumentFactory.Instantiator instantiator, MimeMediaType type ) {
try {
final String useDocType = "Test";
StructuredTextDocument doc = null;
doc = (StructuredTextDocument) instantiator.newInstance( type, useDocType );
doc = (StructuredTextDocument) instantiator.newInstance( type, useDocType, null );
doc = (StructuredTextDocument) instantiator.newInstance( type, useDocType, "value" );
String stringdoc = doc.toString();
if( type.getSubtype().equalsIgnoreCase( "XML" ) ) {
try {
doc = (StructuredTextDocument) instantiator.newInstance( type, "Really wrong and long" );
fail( "Tag names with spaces should be disallowed" );
} catch ( Exception failed ) {
// that's ok
}
}
try {
doc = (StructuredTextDocument) instantiator.newInstance( type, new ByteArrayInputStream( stringdoc.getBytes() ) );
} catch ( ProviderException notsupported ) {
// thats ok.
}
if( instantiator instanceof StructuredDocumentFactory.TextInstantiator ) {
try {
doc = (StructuredTextDocument) ((StructuredDocumentFactory.TextInstantiator)instantiator).newInstance( type, new StringReader( stringdoc ) );
} catch ( ProviderException notsupported ) {
// thats ok.
}
}
} catch( Throwable everything ) {
everything.printStackTrace();
fail( "Caught an unexpected exception - " + everything.toString() );
}
}
public void _testAttributesSolo( StructuredDocumentFactory.Instantiator instantiator, MimeMediaType type ) {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
StructuredDocument doc = instantiator.newInstance( type, "Message");
assertTrue( "should be no attributes", !((Attributable)doc).getAttributes().hasMoreElements() );
((Attributable)doc).addAttribute("version", "123");
doc.sendToStream(os);
String old = ((Attributable)doc).addAttribute("version", "1xx23");
assertTrue( "updating attribute gave wrong result", "123".equals( old ) );
doc.sendToStream(os);
List attrs = Collections.list( ((Attributable)doc).getAttributes() );
assertTrue( "should be 1 attribute", 1 == attrs.size() );
if( type.getSubtype().equalsIgnoreCase( "XML" ) ) {
try {
((Attributable)doc).addAttribute( new Attribute( "really long and wrong", "whatever" ) );
fail( "Attribute names with spaces should be disallowed" );
} catch ( Exception failed ) {
// that's ok
}
}
} catch( Throwable everything ) {
everything.printStackTrace();
fail( "Caught an unexpected exception - " + everything.toString() );
}
}
public void _testLiteXMLEmptyElement(StructuredDocumentFactory.TextInstantiator instantiator, MimeMediaType type) {
try {
String doc = "<?xml version=\"1.0\"?><whatever/>";
XMLDocument xmldoc = (XMLDocument) instantiator.newInstance( type, new StringReader( doc ) );
Element anElement = xmldoc.createElement( "whynot" );
xmldoc.appendChild( anElement );
Element anotherElement = xmldoc.createElement( "why", "because" );
anElement.appendChild( anotherElement );
System.out.println( xmldoc.toString() );
StringWriter writer = new StringWriter();
xmldoc.sendToWriter( writer );
StringReader reader = new StringReader( writer.toString() );
XMLDocument roundtrip = (XMLDocument) instantiator.newInstance( xmldoc.getMimeType(), reader );
System.out.println( roundtrip.toString() );
StringWriter secondroundwriter = new StringWriter();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -