001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.schema.base64binary;
020:
021: import org.w3.www._2005._05.xmlmime.*;
022: import org.w3.www._2005._05.xmlmime.HexBinary;
023: import org.apache.axiom.attachments.ByteArrayDataSource;
024: import org.apache.axiom.om.OMElement;
025: import org.apache.axiom.om.OMAbstractFactory;
026: import org.apache.axiom.om.util.StAXUtils;
027: import org.apache.axis2.databinding.types.*;
028:
029: import javax.activation.DataHandler;
030: import javax.xml.stream.XMLStreamException;
031: import javax.xml.stream.XMLStreamReader;
032:
033: import junit.framework.TestCase;
034:
035: import java.io.ByteArrayInputStream;
036:
037: /**
038: * Author: amila
039: * Date: May 19, 2007
040: */
041: public class Base64BinaryTest extends TestCase {
042:
043: public void testBase64Binary() {
044: TestBase64Binary testBase64Binary = new TestBase64Binary();
045: Base64Binary base64Binary = new Base64Binary();
046: testBase64Binary.setTestBase64Binary(base64Binary);
047:
048: String testString = "new test string";
049:
050: DataHandler dataHandler = new DataHandler(
051: new ByteArrayDataSource(testString.getBytes()));
052: base64Binary.setBase64Binary(dataHandler);
053: ContentType_type0 contentType_type0 = new ContentType_type0();
054: contentType_type0.setContentType_type0("test content type");
055: base64Binary.setContentType(contentType_type0);
056:
057: try {
058: OMElement omElement = testBase64Binary.getOMElement(
059: TestBase64Binary.MY_QNAME, OMAbstractFactory
060: .getOMFactory());
061: String omElementString = omElement.toStringWithConsume();
062: System.out.println("OM String ==> " + omElementString);
063: XMLStreamReader xmlReader = StAXUtils
064: .createXMLStreamReader(new ByteArrayInputStream(
065: omElementString.getBytes()));
066: TestBase64Binary result = TestBase64Binary.Factory
067: .parse(xmlReader);
068: DataHandler resultDataHandler = result
069: .getTestBase64Binary().getBase64Binary();
070: byte[] bytes = new byte[128];
071: int length = resultDataHandler.getInputStream().read(bytes);
072: String resultString = new String(bytes, 0, length);
073: assertEquals(resultString, testString);
074: assertEquals(result.getTestBase64Binary().getContentType()
075: .getContentType_type0(), "test content type");
076: } catch (XMLStreamException e) {
077: fail();
078: } catch (Exception e) {
079: fail();
080: }
081: }
082:
083: public void testHexBinary() {
084: TestHexBinary testHexBinary = new TestHexBinary();
085: HexBinary hexBinary = new HexBinary();
086: testHexBinary.setTestHexBinary(hexBinary);
087:
088: String testString = "ab";
089:
090: org.apache.axis2.databinding.types.HexBinary adbHexBinary = new org.apache.axis2.databinding.types.HexBinary(
091: testString);
092:
093: hexBinary.setHexBinary(adbHexBinary);
094: ContentType_type0 contentType_type0 = new ContentType_type0();
095: contentType_type0.setContentType_type0("test content type");
096: hexBinary.setContentType(contentType_type0);
097:
098: try {
099: OMElement omElement = testHexBinary.getOMElement(
100: TestBase64Binary.MY_QNAME, OMAbstractFactory
101: .getOMFactory());
102: String omElementString = omElement.toStringWithConsume();
103: System.out.println("OM String ==> " + omElementString);
104: XMLStreamReader xmlReader = StAXUtils
105: .createXMLStreamReader(new ByteArrayInputStream(
106: omElementString.getBytes()));
107: TestHexBinary result = TestHexBinary.Factory
108: .parse(xmlReader);
109: assertEquals(result.getTestHexBinary().getHexBinary()
110: .toString(), testString);
111: assertEquals(result.getTestHexBinary().getContentType()
112: .getContentType_type0(), "test content type");
113: } catch (XMLStreamException e) {
114: fail();
115: } catch (Exception e) {
116: fail();
117: }
118: }
119:
120: }
|