byte[] swaRefBytes = "This is a bunch of random bytes that are to be used as an SWA ref attachment.".getBytes();
byte[] explicitBase64Bytes = "This is some more random bytes that are to be used as a base 64 encoded attachment.".getBytes();
byte[] attachment1Bytes = "This is some more random bytes that are to be used as the first MTOM attachment.".getBytes();
byte[] attachment2Bytes = "This is some more random bytes that are to be used as the second MTOM attachment.".getBytes();
byte[] attachment3Bytes = "This is some more random bytes that are to be used as the third MTOM attachment.".getBytes();
CanvasAttachment attachment1 = new CanvasAttachment();
attachment1.setValue(attachment1Bytes);
CanvasAttachment attachment2 = new CanvasAttachment();
attachment2.setValue(attachment2Bytes);
CanvasAttachment attachment3 = new CanvasAttachment();
attachment3.setValue(attachment3Bytes);
ByteArrayDataSource dataSource = new ByteArrayDataSource(swaRefBytes, "application/octet-stream");
dataSource.setName("somename");
//todo: uncomment when JAXB bug is fixed
// canvas.setBackgroundImage(new DataHandler(dataSource));
canvas.setExplicitBase64Attachment(explicitBase64Bytes);
canvas.setOtherAttachments(Arrays.asList(attachment1, attachment2, attachment3));
JAXBContext context = JAXBContext.newInstance(Canvas.class);
JAXBContext clientContext = JAXBContext.newInstance(shapes.draw.Canvas.class);
Marshaller marshaller = context.createMarshaller();
AttachmentMarshaller attachmentHandler = new AttachmentMarshaller() {
public String addMtomAttachment(DataHandler data, String elementNamespace, String elementLocalName) {
return null;
}
public String addMtomAttachment(byte[] data, int offset, int length, String mimeType, String elementNamespace, String elementLocalName) {
return null;
}
public String addSwaRefAttachment(DataHandler data) {
return null;
}
};
marshaller.setAttachmentMarshaller(attachmentHandler);
Marshaller clientMarshaller = clientContext.createMarshaller();
clientMarshaller.setAttachmentMarshaller(attachmentHandler);
Unmarshaller clientUnmarshaller = clientContext.createUnmarshaller();
ByteArrayOutputStream out = new ByteArrayOutputStream();
marshaller.marshal(canvas, out);
//set up the attachments that were written
shapes.draw.Canvas clientCanvas = (shapes.draw.Canvas) clientUnmarshaller.unmarshal(new ByteArrayInputStream(out.toByteArray()));
Collection clientShapes = clientCanvas.getShapes();
assertEquals(3, clientShapes.size());
for (Object clientShape : clientShapes) {
if (clientShape instanceof shapes.Circle) {
assertEquals("circleId", ((shapes.Circle) clientShape).getId());
assertEquals(10, ((shapes.Circle) clientShape).getRadius());
}
else if (clientShape instanceof shapes.Rectangle) {
assertEquals("rectId", ((shapes.Rectangle) clientShape).getId());
assertEquals(50, ((shapes.Rectangle) clientShape).getHeight());
}
else if (clientShape instanceof shapes.Triangle) {
assertEquals("triId", ((shapes.Triangle) clientShape).getId());
assertEquals(80, ((shapes.Triangle) clientShape).getBase());
}
else {
fail("Unknown shape: " + clientShape);
}
}
Collection clientFigures = clientCanvas.getFigures();
assertEquals(3, clientFigures.size());
for (Object clientFigure : clientFigures) {
if (clientFigure instanceof shapes.vehicles.Bus) {
shapes.vehicles.Bus clientBus = (shapes.vehicles.Bus) clientFigure;
assertEquals("busId", clientBus.getId());
shapes.Rectangle clientBusFrame = clientBus.getFrame();
assertNotNull(clientBusFrame);
assertEquals(100, busFrame.getWidth());
}
else if (clientFigure instanceof shapes.animals.Cat) {
shapes.animals.Cat clientCat = (shapes.animals.Cat) clientFigure;
assertEquals("catId", clientCat.getId());
shapes.Circle clientCatFace = clientCat.getFace();
assertNotNull(clientCatFace);
assertEquals(30, clientCatFace.getRadius());
}
else if (clientFigure instanceof shapes.structures.House) {
shapes.structures.House clientHouse = (shapes.structures.House) clientFigure;
assertEquals("houseId", clientHouse.getId());
shapes.Rectangle clientHouseBase = clientHouse.getBase();
assertNotNull(clientHouseBase);
assertEquals(76, clientHouseBase.getWidth());
}
else {
fail("Unknown figure: " + clientFigure);
}
}
// DataHandler backgroundImage = clientCanvas.getBackgroundImage();
// InputStream attachmentStream = backgroundImage.getInputStream();
// ByteArrayOutputStream bgImageIn = new ByteArrayOutputStream();
// int byteIn = attachmentStream.read();
// while (byteIn > 0) {
// bgImageIn.write(byteIn);
// byteIn = attachmentStream.read();
// }
// assertTrue(Arrays.equals(swaRefBytes, bgImageIn.toByteArray()));
byte[] base64Attachment = clientCanvas.getExplicitBase64Attachment();
assertNotNull(base64Attachment);
assertTrue(Arrays.equals(explicitBase64Bytes, base64Attachment));
Collection otherAttachments = clientCanvas.getOtherAttachments();
assertEquals(3, otherAttachments.size());
Iterator attachmentsIt = otherAttachments.iterator();
int attachmentCount = 0;
while (attachmentsIt.hasNext()) {
shapes.draw.CanvasAttachment otherAttachment = (shapes.draw.CanvasAttachment) attachmentsIt.next();
byte[] otherAttachmentBytes = otherAttachment.getValue();
if (Arrays.equals(attachment1Bytes, otherAttachmentBytes)) {
attachmentCount++;
}
else if (Arrays.equals(attachment2Bytes, otherAttachmentBytes)) {
attachmentCount++;
}
else if (Arrays.equals(attachment3Bytes, otherAttachmentBytes)) {
attachmentCount++;
}
else {
fail("Unknown attachment.");
}
}
assertEquals(3, attachmentCount);
out = new ByteArrayOutputStream();
clientMarshaller.marshal(clientCanvas, out);
Unmarshaller unmarshaller = context.createUnmarshaller();
canvas = (Canvas) unmarshaller.unmarshal(new ByteArrayInputStream(out.toByteArray()));
Collection shapes = canvas.getShapes();
assertEquals(3, shapes.size());
for (Object Shape : shapes) {
if (Shape instanceof Circle) {
assertEquals("circleId", ((Circle) Shape).getId());
assertEquals(10, ((Circle) Shape).getRadius());
}
else if (Shape instanceof Rectangle) {
assertEquals("rectId", ((Rectangle) Shape).getId());
assertEquals(50, ((Rectangle) Shape).getHeight());
}
else if (Shape instanceof Triangle) {
assertEquals("triId", ((Triangle) Shape).getId());
assertEquals(80, ((Triangle) Shape).getBase());
}
else {
fail("Unknown shape: " + Shape);
}
}
Collection figures = canvas.getFigures();
assertEquals(3, figures.size());
for (Object Figure : figures) {
if (Figure instanceof Bus) {
bus = (Bus) Figure;
assertEquals("busId", bus.getId());
Rectangle BusFrame = bus.getFrame();
assertNotNull(BusFrame);
assertEquals(100, busFrame.getWidth());
}
else if (Figure instanceof Cat) {
cat = (Cat) Figure;
assertEquals("catId", cat.getId());
Circle CatFace = cat.getFace();
assertNotNull(CatFace);
assertEquals(30, CatFace.getRadius());
}
else if (Figure instanceof House) {
house = (House) Figure;
assertEquals("houseId", house.getId());
Rectangle HouseBase = house.getBase();
assertNotNull(HouseBase);
assertEquals(76, HouseBase.getWidth());
}
else {
fail("Unknown figure: " + Figure);
}
}
// backgroundImage = canvas.getBackgroundImage();
// attachmentStream = backgroundImage.getInputStream();
// bgImageIn = new ByteArrayOutputStream();
// byteIn = attachmentStream.read();
// while (byteIn > 0) {
// bgImageIn.write(byteIn);
// byteIn = attachmentStream.read();
// }
//
// assertTrue(Arrays.equals(swaRefBytes, bgImageIn.toByteArray()));
base64Attachment = canvas.getExplicitBase64Attachment();
assertNotNull(base64Attachment);
assertTrue(Arrays.equals(explicitBase64Bytes, base64Attachment));
otherAttachments = canvas.getOtherAttachments();
assertEquals(3, otherAttachments.size());
attachmentsIt = otherAttachments.iterator();
attachmentCount = 0;
while (attachmentsIt.hasNext()) {
CanvasAttachment otherAttachment = (CanvasAttachment) attachmentsIt.next();
byte[] otherAttachmentBytes = otherAttachment.getValue();
if (Arrays.equals(attachment1Bytes, otherAttachmentBytes)) {
attachmentCount++;
}
else if (Arrays.equals(attachment2Bytes, otherAttachmentBytes)) {
attachmentCount++;