Modified: webservices/commons/trunk/modules/axiom/src/org/apache/axiom/attachments/PartOnFile.java URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/attachments/PartOnFile.java?rev=395360&r1=395359&r2=395360&view=diff ============================================================================== --- webservices/commons/trunk/modules/axiom/src/org/apache/axiom/attachments/PartOnFile.java (original) +++ webservices/commons/trunk/modules/axiom/src/org/apache/axiom/attachments/PartOnFile.java Wed Apr 19 12:21:36 2006 @@ -1,184 +1,193 @@ -/* - * Copyright 2004,2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.axiom.attachments; - -import org.apache.axiom.om.OMException; - -import javax.activation.DataHandler; -import javax.activation.FileDataSource; -import javax.mail.MessagingException; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.Enumeration; -import java.util.HashMap; - -public class PartOnFile implements Part { - - File cacheFile; - - Part bodyPart; - - String contentType; - - String contentID; - - HashMap headers; - - public PartOnFile(PushbackFilePartInputStream inStream, String repoDir) { - super(); - - headers = new HashMap(); - - if (repoDir == null) { - repoDir = "."; - } - try { - cacheFile = File.createTempFile("Axis2", ".att", - (repoDir == null) ? null : new File(repoDir)); - - FileOutputStream fileOutStream = new FileOutputStream(cacheFile); - int value; - value = parseTheHeaders(inStream); - fileOutStream.write(value); - while (!inStream.getBoundaryStatus()) { - value = inStream.read(); - if (!inStream.getBoundaryStatus()) { - fileOutStream.write(value); - } - - } - - fileOutStream.flush(); - fileOutStream.close(); - } catch (IOException e) { - throw new OMException("Error creating temporary File.", e); - } - } - - private int parseTheHeaders(InputStream inStream) throws IOException { - int value; - boolean readingHeaders = true; - StringBuffer header = new StringBuffer(); - while (readingHeaders & (value = inStream.read()) != -1) { - if (value == 13) { - if ((value = inStream.read()) == 10) { - if ((value = inStream.read()) == 13) { - if ((value = inStream.read()) == 10) { - putToMap(header); - readingHeaders = false; - } - } else { - putToMap(header); - header = new StringBuffer(); - header.append((char) value); - } - } else { - header.append(13); - header.append(value); - } - } else { - header.append((char) value); - } - } - return value; - } - - private void putToMap(StringBuffer header) { - String headerString = header.toString(); - int delimiter = headerString.indexOf(":"); - headers.put(headerString.substring(0, delimiter).trim(), headerString - .substring(delimiter + 1, headerString.length()).trim()); - } - - public String getContentID() { - String cID = (String) headers.get("Content-ID"); - if (cID == null) { - cID = (String) headers.get("Content-Id"); - if (cID == null) { - cID = (String) headers.get("Content-id"); - if (cID == null) { - cID = (String) headers.get("content-id"); - } - } - - } - return cID; - } - - public int getSize() throws MessagingException { - return (int) cacheFile.length(); - } - - public int getLineCount() throws MessagingException { - throw new UnsupportedOperationException(); - } - - public String getDescription() throws MessagingException { - throw new UnsupportedOperationException(); - } - - public void setDescription(String arg0) throws MessagingException { - throw new UnsupportedOperationException(); - } - - public String getFileName() throws MessagingException { - return cacheFile.getAbsolutePath(); - } - - public InputStream getInputStream() throws IOException, MessagingException { - return new FileInputStream(cacheFile); - } - - public DataHandler getDataHandler() throws MessagingException { - return new DataHandler(new FileDataSource(cacheFile)); - } - - public Object getContent() throws IOException, MessagingException { - return getDataHandler().getContent(); - } - - public void writeTo(OutputStream outStream) throws IOException, - MessagingException { - getDataHandler().writeTo(outStream); - } - - public String getHeader(String arg0) throws MessagingException { - String header; - header = (String) headers.get(arg0); - return header; - } - - public Enumeration getAllHeaders() throws MessagingException { - return null; - } - - public String getContentType() throws MessagingException { - String cType = (String) headers.get("Content-Type"); - if (cType == null) { - cType = (String) headers.get("Content-type"); - if (cType == null) { - cType = (String) headers.get("content-type"); - } - } - return cType; - } - +/* + * Copyright 2004,2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.axiom.attachments; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Enumeration; +import java.util.Hashtable; + +import javax.activation.DataHandler; +import javax.activation.FileDataSource; +import javax.mail.Header; +import javax.mail.MessagingException; + +import org.apache.axiom.om.OMException; + +public class PartOnFile implements Part { + + File cacheFile; + + Part bodyPart; + + String contentType; + + String contentID; + + Hashtable headers; + + public PartOnFile(PushbackFilePartInputStream inStream, String repoDir) { + super(); + + headers = new Hashtable(); + + if (repoDir == null) { + repoDir = "."; + } + try { + cacheFile = File.createTempFile("Axis2", ".att", + (repoDir == null) ? null : new File(repoDir)); + + FileOutputStream fileOutStream = new FileOutputStream(cacheFile); + int value; + value = parseTheHeaders(inStream); + fileOutStream.write(value); + while (!inStream.getBoundaryStatus()) { + value = inStream.read(); + if (!inStream.getBoundaryStatus()) { + fileOutStream.write(value); + } + + } + + fileOutStream.flush(); + fileOutStream.close(); + } catch (IOException e) { + throw new OMException("Error creating temporary File.", e); + } + } + + private int parseTheHeaders(InputStream inStream) throws IOException { + int value; + boolean readingHeaders = true; + StringBuffer header = new StringBuffer(); + while (readingHeaders & (value = inStream.read()) != -1) { + if (value == 13) { + if ((value = inStream.read()) == 10) { + if ((value = inStream.read()) == 13) { + if ((value = inStream.read()) == 10) { + putToMap(header); + readingHeaders = false; + } + } else { + putToMap(header); + header = new StringBuffer(); + header.append((char) value); + } + } else { + header.append(13); + header.append(value); + } + } else { + header.append((char) value); + } + } + return value; + } + + private void putToMap(StringBuffer header) { + String headerString = header.toString(); + int delimiter = headerString.indexOf(":"); + + String name = headerString.substring(0, delimiter).trim(); + String value= headerString.substring(delimiter + 1, headerString.length()).trim(); + + Header headerObj = new Header(name, value); + headers.put(name, headerObj); + } + + public String getContentID() { + Header cID = (Header) headers.get("Content-ID"); + if (cID == null) { + cID = (Header) headers.get("Content-Id"); + if (cID == null) { + cID = (Header) headers.get("Content-id"); + if (cID == null) { + cID = (Header) headers.get("content-id"); + } + } + + } + return (String) cID.getValue(); + } + + public int getSize() throws MessagingException { + return (int) cacheFile.length(); + } + + public int getLineCount() throws MessagingException { + throw new UnsupportedOperationException(); + } + + public String getDescription() throws MessagingException { + throw new UnsupportedOperationException(); + } + + public void setDescription(String arg0) throws MessagingException { + throw new UnsupportedOperationException(); + } + + public String getFileName() throws MessagingException { + return cacheFile.getAbsolutePath(); + } + + public InputStream getInputStream() throws IOException, MessagingException { + return new FileInputStream(cacheFile); + } + + public DataHandler getDataHandler() throws MessagingException { + return new DataHandler(new FileDataSource(cacheFile)); + } + + public Object getContent() throws IOException, MessagingException { + return getDataHandler().getContent(); + } + + public void writeTo(OutputStream outStream) throws IOException, + MessagingException { + getDataHandler().writeTo(outStream); + } + + public String getHeader(String arg0) throws MessagingException { + return (String) ((Header) headers.get(arg0)).getValue(); + } + + public void addHeader(String arg0, String arg1) throws MessagingException { + Header headerObj = new Header(arg0, arg1); + headers.put(arg0, headerObj); + } + + public Enumeration getAllHeaders() throws MessagingException { + return headers.elements(); + } + + public String getContentType() throws MessagingException { + Header cType = (Header) headers.get("Content-Type"); + if (cType == null) { + cType = (Header) headers.get("Content-type"); + if (cType == null) { + cType = (Header) headers.get("content-type"); + } + } + return (String) cType.getValue(); + } + }
Modified: webservices/commons/trunk/modules/axiom/src/org/apache/axiom/attachments/PartOnMemory.java URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/attachments/PartOnMemory.java?rev=395360&r1=395359&r2=395360&view=diff ============================================================================== --- webservices/commons/trunk/modules/axiom/src/org/apache/axiom/attachments/PartOnMemory.java (original) +++ webservices/commons/trunk/modules/axiom/src/org/apache/axiom/attachments/PartOnMemory.java Wed Apr 19 12:21:36 2006 @@ -1,66 +1,70 @@ -/* - * Copyright 2004,2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.axiom.attachments; - -import javax.activation.DataHandler; -import javax.mail.MessagingException; -import javax.mail.internet.MimeBodyPart; -import java.io.IOException; -import java.io.InputStream; -import java.util.Enumeration; - -public class PartOnMemory implements Part{ - - MimeBodyPart part; - public PartOnMemory(InputStream partInStream) throws MessagingException - { - part = new MimeBodyPart(partInStream); - } - - public int getSize() throws MessagingException { - return part.getSize(); - } - - public String getContentType() throws MessagingException { - return part.getContentType(); - } - - public String getFileName() throws MessagingException { - return part.getFileName(); - } - - public InputStream getInputStream() throws IOException, MessagingException { - return part.getInputStream(); - } - - public DataHandler getDataHandler() throws MessagingException { - return part.getDataHandler(); - } - - public String getHeader(String arg0) throws MessagingException { - return part.getHeader(arg0)[0]; - } - - public Enumeration getAllHeaders() throws MessagingException { - return part.getAllHeaders(); - } - - - public String getContentID() throws MessagingException { - return part.getContentID(); - } -} +/* + * Copyright 2004,2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.axiom.attachments; + +import javax.activation.DataHandler; +import javax.mail.MessagingException; +import javax.mail.internet.MimeBodyPart; +import java.io.IOException; +import java.io.InputStream; +import java.util.Enumeration; + +public class PartOnMemory implements Part{ + + MimeBodyPart part; + public PartOnMemory(InputStream partInStream) throws MessagingException + { + part = new MimeBodyPart(partInStream); + } + + public int getSize() throws MessagingException { + return part.getSize(); + } + + public String getContentType() throws MessagingException { + return part.getContentType(); + } + + public String getFileName() throws MessagingException { + return part.getFileName(); + } + + public InputStream getInputStream() throws IOException, MessagingException { + return part.getInputStream(); + } + + public DataHandler getDataHandler() throws MessagingException { + return part.getDataHandler(); + } + + public void addHeader(String arg0, String arg1) throws MessagingException { + part.addHeader(arg0, arg1); + } + + public String getHeader(String arg0) throws MessagingException { + return part.getHeader(arg0)[0]; + } + + public Enumeration getAllHeaders() throws MessagingException { + return part.getAllHeaders(); + } + + + public String getContentID() throws MessagingException { + return part.getContentID(); + } +} Added: webservices/commons/trunk/modules/axiom/test-resources/mtom/MTOMAttachmentStream.bin URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/test-resources/mtom/MTOMAttachmentStream.bin?rev=395360&view=auto ============================================================================== Binary files /tmp/tmpULG-_p and /tmp/tmplc_cLZ differ Added: webservices/commons/trunk/modules/axiom/test-resources/mtom/img/test2.jpg URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/test-resources/mtom/img/test2.jpg?rev=395360&view=auto ============================================================================== Binary file - no diff available. Propchange: webservices/commons/trunk/modules/axiom/test-resources/mtom/img/test2.jpg ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Added: webservices/commons/trunk/modules/axiom/test/org/apache/axiom/attachments/IncomingAttachmentInputTest.java URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/test/org/apache/axiom/attachments/IncomingAttachmentInputTest.java?rev=395360&view=auto ============================================================================== --- webservices/commons/trunk/modules/axiom/test/org/apache/axiom/attachments/IncomingAttachmentInputTest.java (added) +++ webservices/commons/trunk/modules/axiom/test/org/apache/axiom/attachments/IncomingAttachmentInputTest.java Wed Apr 19 12:21:36 2006 @@ -0,0 +1,69 @@ +/* + * Copyright 2004,2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.axiom.attachments; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; + +import org.apache.axiom.om.AbstractTestCase; + + +/** + * Test the PartOnFile class + */ + +public class IncomingAttachmentInputTest extends AbstractTestCase { + + public IncomingAttachmentInputTest(String testName) { + super(testName); + } + + String inMimeFileName = "mtom/MTOMAttachmentStream.bin"; + String contentTypeString = "multipart/related; boundary=MIMEBoundaryurn:uuid:A3ADBAEE51A1A87B2A11443668160701; type=\"application/xop+xml\"; start=\"<0.urn:uuid:[EMAIL PROTECTED]>\"; start-info=\"application/soap+xml\"; charset=UTF-8;action=\"mtomSample\""; + File temp; + + public void testIncomingAttachmentInputStreamFunctions() throws Exception { + InputStream inStream = new FileInputStream(getTestResourceFile(inMimeFileName)); + MIMEHelper mimeHelper = new MIMEHelper(inStream, contentTypeString); + + // Get the inputstream container + IncomingAttachmentStreams ias = mimeHelper.getIncomingAttachmentStreams(); + + IncomingAttachmentInputStream dataIs; + + // Img1 stream + dataIs = ias.getNextStream(); + + // Make sure it was the first attachment + assertEquals("<1.urn:uuid:[EMAIL PROTECTED]>", dataIs.getContentId()); + + // Consume the stream + while (dataIs.read() != -1); + + // Img2 stream + dataIs = ias.getNextStream(); + assertEquals("<2.urn:uuid:[EMAIL PROTECTED]>", dataIs.getContentId()); + + // Test if getContentType() works.. + assertEquals("image/jpeg", dataIs.getContentType()); + + // Test if a adding/getting a header works + dataIs.addHeader("new-header", "test-value"); + assertEquals("test-value", dataIs.getHeader("new-header")); + } +} Modified: webservices/commons/trunk/modules/axiom/test/org/apache/axiom/attachments/MIMEHelperTest.java URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/test/org/apache/axiom/attachments/MIMEHelperTest.java?rev=395360&r1=395359&r2=395360&view=diff ============================================================================== --- webservices/commons/trunk/modules/axiom/test/org/apache/axiom/attachments/MIMEHelperTest.java (original) +++ webservices/commons/trunk/modules/axiom/test/org/apache/axiom/attachments/MIMEHelperTest.java Wed Apr 19 12:21:36 2006 @@ -1,36 +1,183 @@ -/* - * Copyright 2004,2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.axiom.attachments; - -import junit.framework.TestCase; - -public class MIMEHelperTest extends TestCase { - - - public void testMIMEHelper() { - } - - public void testGetAttachmentSpecType() { - } - - public void testGetSOAPPartInputStream() { - } - - public void testGetDataHandler() { - } - -} \ No newline at end of file +/* + * Copyright 2004,2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.axiom.attachments; + +import java.awt.Image; +import java.io.FileInputStream; +import java.io.InputStream; + +import javax.activation.DataHandler; +import javax.imageio.ImageIO; + +import org.apache.axiom.attachments.utils.ImageDataSource; +import org.apache.axiom.om.AbstractTestCase; + +public class MIMEHelperTest extends AbstractTestCase { + + public MIMEHelperTest(String testName) { + super(testName); + } + + String inMimeFileName = "mtom/MTOMAttachmentStream.bin"; + String img1FileName = "mtom/img/test.jpg"; + String img2FileName = "mtom/img/test2.jpg"; + + String contentTypeString = "multipart/related; boundary=MIMEBoundaryurn:uuid:A3ADBAEE51A1A87B2A11443668160701; type=\"application/xop+xml\"; start=\"<0.urn:uuid:[EMAIL PROTECTED]>\"; start-info=\"application/soap+xml\"; charset=UTF-8;action=\"mtomSample\""; + + public void testMIMEHelper() { + } + + public void testGetAttachmentSpecType() { + } + + public void testSimultaneousStreamAccess() throws Exception { + InputStream inStream; + MIMEHelper mimeHelper; + + inStream = new FileInputStream(getTestResourceFile(inMimeFileName)); + mimeHelper = new MIMEHelper(inStream, contentTypeString); + + mimeHelper.getDataHandler("2.urn:uuid:[EMAIL PROTECTED]"); + + // This should throw an error + try { + mimeHelper.getIncomingAttachmentStreams(); + fail("No exception caught when attempting to access datahandler and stream at the same time"); + } catch (IllegalStateException ise) { + // Nothing + } + + inStream.close(); + + // Try the other way around. + inStream = new FileInputStream(getTestResourceFile(inMimeFileName)); + mimeHelper = new MIMEHelper(inStream, contentTypeString); + + mimeHelper.getIncomingAttachmentStreams(); + + // These should NOT throw error even though they are using part based access + try { + assertEquals("application/xop+xml; charset=UTF-8; type=\"application/soap+xml\";", mimeHelper.getSOAPPartContentType()); + } catch (IllegalStateException ise) { + fail("No exception expected when requesting SOAP part data"); + ise.printStackTrace(); + } + + try { + mimeHelper.getSOAPPartInputStream(); + } catch (IllegalStateException ise) { + fail("No exception expected when requesting SOAP part data"); + } + + // These should throw an error + try { + mimeHelper.getDataHandler("2.urn:uuid:[EMAIL PROTECTED]"); + fail("No exception caught when attempting to access stream and datahandler at the same time"); + } catch (IllegalStateException ise) { + // Nothing + } + + // Additionally, we also need to ensure mutual exclusion if someone + // tries to access part data directly + + try { + mimeHelper.getAllContentIDs(); + fail("No exception caught when attempting to access stream and contentids list at the same time"); + } catch (IllegalStateException ise) { + // Nothing + } + + try { + mimeHelper.getPart("2.urn:uuid:[EMAIL PROTECTED]"); + fail("No exception caught when attempting to access stream and part at the same time"); + } catch (IllegalStateException ise) { + // Nothing + } + } + + public void testGetInputAttachhmentStreams() throws Exception { + + Image expectedImage; + IncomingAttachmentInputStream dataIs; + ImageDataSource dataSource; + InputStream expectedDataIs; + + InputStream inStream = new FileInputStream(getTestResourceFile(inMimeFileName)); + MIMEHelper mimeHelper = new MIMEHelper(inStream, contentTypeString); + + // Since SOAP part operated independently of other streams, access it + // directly, and then get to the streams. If this sequence throws an + // error, something is wrong with the stream handling code. + InputStream is = mimeHelper.getSOAPPartInputStream(); + while (is.read() != -1); + + // Get the inputstream container + IncomingAttachmentStreams ias = mimeHelper.getIncomingAttachmentStreams(); + + dataIs = ias.getNextStream(); + expectedImage = ImageIO.read(new FileInputStream(getTestResourceFile(img1FileName))); + dataSource = new ImageDataSource("test1.jpg", expectedImage); + expectedDataIs = dataSource.getInputStream(); + compareStreams(dataIs, expectedDataIs); + + dataIs = ias.getNextStream(); + expectedImage = ImageIO.read(new FileInputStream(getTestResourceFile(img2FileName))); + dataSource = new ImageDataSource("test2.jpg", expectedImage); + expectedDataIs = dataSource.getInputStream(); + compareStreams(dataIs, expectedDataIs); + + // Confirm that no more streams are left + assertEquals(null, ias.getNextStream()); + + // After all is done, we should *still* be able to access and + // re-consume the SOAP part stream, as it should be cached.. can we? + is = mimeHelper.getSOAPPartInputStream(); + while (is.read() != -1); + } + + private void compareStreams(InputStream data, InputStream expected) throws Exception { + // Compare data across streams + int i = 0, expectedData = 0; + + while ((i = data.read()) != -1 && (expectedData = expected.read()) != -1) { + if (i != expectedData) { + fail("Data streams do not match: " + i + " != " + expectedData); + } + } + + // Ensure that *both* streams have ended + if ((i == -1 && expected.read() != -1) || (expectedData == -1 && i != -1)) { + fail("Data streams do not match: " + i + " != " + expectedData); + } + } + + public void testGetDataHandler() throws Exception { + + InputStream inStream = new FileInputStream(getTestResourceFile(inMimeFileName)); + MIMEHelper mimeHelper = new MIMEHelper(inStream, contentTypeString); + + DataHandler dh = mimeHelper.getDataHandler("2.urn:uuid:[EMAIL PROTECTED]"); + InputStream dataIs = dh.getDataSource().getInputStream(); + + Image expectedImage = ImageIO.read(new FileInputStream(getTestResourceFile(img2FileName))); + ImageDataSource dataSource = new ImageDataSource("test.jpg", expectedImage); + InputStream expectedDataIs = dataSource.getInputStream(); + + // Compare data across streams + compareStreams(dataIs, expectedDataIs); + } +} Added: webservices/commons/trunk/modules/axiom/test/org/apache/axiom/attachments/PartOnFileTest.java URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/test/org/apache/axiom/attachments/PartOnFileTest.java?rev=395360&view=auto ============================================================================== --- webservices/commons/trunk/modules/axiom/test/org/apache/axiom/attachments/PartOnFileTest.java (added) +++ webservices/commons/trunk/modules/axiom/test/org/apache/axiom/attachments/PartOnFileTest.java Wed Apr 19 12:21:36 2006 @@ -0,0 +1,142 @@ +/* + * Copyright 2004,2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.axiom.attachments; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.Enumeration; + +import javax.mail.Header; + +import org.apache.axiom.om.AbstractTestCase; + + +/** + * Test the PartOnFile class + */ + +public class PartOnFileTest extends AbstractTestCase { + + public PartOnFileTest(String testName) { + super(testName); + } + + String inMimeFileName = "mtom/MTOMAttachmentStream.bin"; + String contentTypeString = "multipart/related; boundary=MIMEBoundaryurn:uuid:A3ADBAEE51A1A87B2A11443668160701; type=\"application/xop+xml\"; start=\"<0.urn:uuid:[EMAIL PROTECTED]>\"; start-info=\"application/soap+xml\"; charset=UTF-8;action=\"mtomSample\""; + + File temp; + + public void setUp() throws Exception { + createTemporaryDirectory(); + } + + public void tearDown() throws Exception { + deleteTemporaryDirectory(); + } + + public void testHeaderGetSet() throws Exception { + + InputStream inStream = new FileInputStream(getTestResourceFile(inMimeFileName)); + MIMEHelper mimeHelper = new MIMEHelper(inStream, contentTypeString, true, temp.getPath(), "1"); + + Part p = mimeHelper.getPart("1.urn:uuid:[EMAIL PROTECTED]"); + + if (!(p instanceof PartOnFile)) { + fail("Expected PartOnFile"); + } + + assertEquals("<1.urn:uuid:[EMAIL PROTECTED]>", p.getContentID()); + assertEquals("image/jpeg", p.getContentType()); + + p.addHeader("Some-New-Header", "TestNH"); + assertEquals(p.getHeader("Some-New-Header"), "TestNH"); + } + + public void testGetAllheaders() throws Exception { + + InputStream inStream = new FileInputStream(getTestResourceFile(inMimeFileName)); + MIMEHelper mimeHelper = new MIMEHelper(inStream, contentTypeString, true, temp.getPath(), "1"); + + Part p = mimeHelper.getPart("1.urn:uuid:[EMAIL PROTECTED]"); + + if (!(p instanceof PartOnFile)) { + fail("Expected PartOnFile"); + } + + assertEquals("<1.urn:uuid:[EMAIL PROTECTED]>", p.getContentID()); + + // Check if the enumeration works + p.addHeader("Some-New-Header", "TestNH"); + + Enumeration e = p.getAllHeaders(); + boolean cTypeFound = false; + boolean cTransferEncFound = false; + boolean cIdFound = false; + boolean snhFound = false; + + while (e.hasMoreElements()) { + Header h = (Header) e.nextElement(); + if (h.getName().toLowerCase().equals("content-type")) { + cTypeFound = true; + } + + if (h.getName().toLowerCase().equals("content-transfer-encoding")) { + cTransferEncFound = true; + } + + if (h.getName().toLowerCase().equals("content-id")) { + cIdFound = true; + } + + if (h.getName().toLowerCase().equals("some-new-header")) { + snhFound = true; + } + } + + if (!cTypeFound || !cTransferEncFound || !cIdFound || !snhFound) { + fail("Header enumeration failed"); + } + + } + + private void createTemporaryDirectory() throws Exception { + temp = File.createTempFile("partOnFileTest", ".tmp"); + + if (!temp.delete()) { + fail("Cannot delete from temporary directory. File: " + temp.toURL()); + } + + if (!temp.mkdir()) { + fail("Cannot create a temporary location for part files"); + } + } + + private void deleteTemporaryDirectory() throws Exception { + + String[] fileList = temp.list(); + for (int i=0; i < fileList.length; i++) { + if (!(new File(temp, fileList[i])).delete()) { + System.err.println("WARNING: temporary directory removal failed."); + } + } + + if (!temp.delete()) { + System.err.println("WARNING: temporary directory removal failed."); + } + } +}
