use of org.apache.axiom.testutils.activation.RandomDataSource in project webservices-axiom by apache.
the class TestCloneNodeBinary method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
DataSource ds = new RandomDataSource(666L, 1000);
Text text = (Text) factory.createOMText(new DataHandler(ds), false);
String base64 = text.getData();
assertTrue(base64.length() > 0);
assertEquals(base64, ((Text) text.cloneNode(true)).getData());
}
use of org.apache.axiom.testutils.activation.RandomDataSource in project webservices-axiom by apache.
the class TestGetNodeValueBinary method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
DataSource ds = new RandomDataSource(666L, 1000);
Text text = (Text) factory.createOMText(new DataHandler(ds), false);
String nodeValue = text.getNodeValue();
assertThat(nodeValue).isNotNull();
IOTestUtils.compareStreams(ds.getInputStream(), new ByteArrayInputStream(Base64.decodeBase64(nodeValue)));
}
use of org.apache.axiom.testutils.activation.RandomDataSource in project webservices-axiom by apache.
the class DataSourceRegistryTest method test.
public void test() throws Exception {
RandomDataSource ds = new RandomDataSource(1000);
DataSourceRegistration registration = DataSourceRegistry.registerDataSource(ds);
try {
// We must be able to connect to the URL after converting it to a String
URL url = new URL(registration.getURL().toString());
URLConnection connection = url.openConnection();
IOTestUtils.compareStreams(connection.getInputStream(), "actual", ds.getInputStream(), "expected");
} finally {
registration.unregister();
}
}
use of org.apache.axiom.testutils.activation.RandomDataSource in project webservices-axiom by apache.
the class AttachmentsTest method testDataHandlerStreaming.
/**
* Tests that a call to {@link DataHandlerExt#readOnce()} on a {@link DataHandler} returned by
* the {@link Attachments} object streams the content of the MIME part.
*
* @throws Exception
*/
public void testDataHandlerStreaming() throws Exception {
// Note: We are only interested in the MimeMultipart, but we need to create a
// MimeMessage to be able to calculate the correct content type
MimeMessage message = new MimeMessage((Session) null);
final MimeMultipart mp = new MimeMultipart("related");
// Prepare the "SOAP" part
MimeBodyPart bp1 = new MimeBodyPart();
// Obviously this is not SOAP, but this is irrelevant for this test
bp1.setText("<root/>", "utf-8", "xml");
bp1.addHeader("Content-Transfer-Encoding", "binary");
bp1.addHeader("Content-ID", "part1@apache.org");
mp.addBodyPart(bp1);
// Create an attachment that is larger than the maximum heap
DataSource dataSource = new RandomDataSource((int) Math.min(Runtime.getRuntime().maxMemory(), Integer.MAX_VALUE));
MimeBodyPart bp2 = new MimeBodyPart();
bp2.setDataHandler(new DataHandler(dataSource));
bp2.addHeader("Content-Transfer-Encoding", "binary");
bp2.addHeader("Content-ID", "part2@apache.org");
mp.addBodyPart(bp2);
message.setContent(mp);
// Compute the correct content type
message.saveChanges();
// We use a pipe (with a producer running in a separate thread) because obviously we can't
// store the multipart in memory.
final PipedOutputStream pipeOut = new PipedOutputStream();
PipedInputStream pipeIn = new PipedInputStream(pipeOut);
Thread producerThread = new Thread(new Runnable() {
public void run() {
try {
try {
mp.writeTo(pipeOut);
} finally {
pipeOut.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
producerThread.start();
try {
// We configure Attachments to buffer MIME parts in memory. If the part content is not
// streamed, then this will result in an OOM error.
Attachments attachments = new Attachments(pipeIn, message.getContentType());
DataHandlerExt dh = (DataHandlerExt) attachments.getDataHandler("part2@apache.org");
IOTestUtils.compareStreams(dataSource.getInputStream(), dh.readOnce());
} finally {
pipeIn.close();
}
}
Aggregations