Copied: incubator/wave/trunk/src/org/waveprotocol/wave/media/model/Attachment.java (from r1401742, incubator/wave/trunk/src/org/waveprotocol/wave/media/model/ClientAttachment.java) URL: http://svn.apache.org/viewvc/incubator/wave/trunk/src/org/waveprotocol/wave/media/model/Attachment.java?p2=incubator/wave/trunk/src/org/waveprotocol/wave/media/model/Attachment.java&p1=incubator/wave/trunk/src/org/waveprotocol/wave/media/model/ClientAttachment.java&r1=1401742&r2=1404734&rev=1404734&view=diff ============================================================================== --- incubator/wave/trunk/src/org/waveprotocol/wave/media/model/ClientAttachment.java (original) +++ incubator/wave/trunk/src/org/waveprotocol/wave/media/model/Attachment.java Thu Nov 1 19:50:22 2012 @@ -1,6 +1,4 @@ /** - * Copyright 2009 Google Inc. - * * 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 @@ -18,34 +16,39 @@ package org.waveprotocol.wave.media.mode /** - * Represents a set of client-side attachment metadata for a single attachment - * instance on a wavelet. - * - * Implementations of this class should be thread-safe. - * - * TODO(user): This interface will eventually replace the existing - * org.waveprotocol.wave.model.wave.Attachment interface. Once this - * is done, remove the old code. - * + * An attachment. This interface is used both by the client (web-app) and on the server-side. */ -// TODO(user): Replace all uses of this interface by either AttachmentV3, or a server-side -// extension of that interface. -public interface ClientAttachment { - /** - * Gets the relative URL by which this attachment may be retrieved. - * - * @return a relative URL or null if the attachment is not yet ready to - * download - */ - String getAttachmentUrl(); +public interface Attachment { + public enum Status { + NOT_UPLOADING, + SUCCEEDED, + IN_PROGRESS, + FAILED_AND_RETRYABLE, + FAILED_AND_NOT_RETRYABLE; + } - /** - * Gets the total size of the attachment in bytes if known, or null - * otherwise. - * - * @return the total size of the attachment, or null. - */ - Long getSize(); + /** Metadata for an image, whether the original attachment or a thumbnail. */ + public interface ImageMetadata { + /** Returns the image width. */ + public int getWidth(); + /** Returns the image height. */ + public int getHeight(); + } + + /** Enumeration of possible error conditions. */ + public enum ErrorCondition { + /** The uploaded file is larger than the per file limit. */ + FILE_TOO_LARGE, + + /** The user has exceeded their storage quota. */ + QUOTA_EXCEEDED, + + /** The upload has been retried the maximum number of times. */ + TOO_MANY_RETRIES; + } + + /** Returns the attachmentId, which should never be null. */ + String getAttachmentId(); /** * Gets the user-provided filename of the attachment. This may have been @@ -56,26 +59,27 @@ public interface ClientAttachment { String getFilename(); /** - * Gets the MIME type of the attachment if known. + * Gets the user id of the user who created the attachment. * - * @return the MIME type as a {@code String} or {@code null} if unknown + * @return the user id or null if the creator is unknown (may occur if an + * attach request comes in before the creation request) */ - String getMimeType(); + String getCreator(); /** - * Gets the image annotation data for the attachment. + * Gets the MIME type of the attachment if known. * - * @return the ImageAnnotation or null if the attachment is not an image - * or the metadata is not yet known + * @return the MIME type as a {@code String} or {@code null} if unknown */ - Image getImage(); + String getMimeType(); /** - * Gets the thumbnail annotation data for the attachment. + * Gets the relative URL by which this attachment may be retrieved. * - * @return the ThumbnailAnnotation or null if there is no thumbnail present + * @return a relative URL or null if the attachment is not yet ready to + * download */ - Thumbnail getThumbnail(); + String getAttachmentUrl(); /** * Gets the relative URL by which this thumbnail may be retrieved. @@ -85,6 +89,23 @@ public interface ClientAttachment { String getThumbnailUrl(); /** + * Gets the total size of the attachment in bytes if known, or null + * otherwise. + * + * @return the total size of the attachment, or null. + */ + Long getSize(); + + /** + * Returns the original attachment's metadata, if the attachment is an image, or null if + * unavailable. + */ + ImageMetadata getContentImageMetadata(); + + /** Returns the thumbnail image's metadata, or null if unavailable. */ + ImageMetadata getThumbnailImageMetadata(); + + /** * Gets the upload progress of the attachment as bytes. When this value is * non-zero and equal to the value returned by {@link #getSize}, * the attachment upload is complete. @@ -109,57 +130,9 @@ public interface ClientAttachment { boolean isMalware(); /** - * The upload status of the attachment. + * The status of the attachment. * * @return the status of the upload. */ - String getStatus(); - - /** - * Gets the user id of the user who created the attachment. - * - * @return the user id or null if the creator is unknown (may occur if an - * attach request comes in before the creation request) - */ - String getCreator(); - - /** - * The thumbnail part of the Attachment metadata. - */ - interface Thumbnail { - /** - * The width of the thumbnail in pixels. - * - * @return the width in pixels - */ - int getWidth(); - - /** - * The height of the thumbnail in pixels. - * - * @return the height in pixels - */ - int getHeight(); - } - - /** - * The image part of the Attachment metadata. Only used if the attachment - * is an image. - */ - interface Image { - /** - * Gets the height of the image attachment. - * - * @return the height of the image in pixels - */ - int getHeight(); - - - /** - * Gets the width of the image attachment. - * - * @return the width of the image in pixels - */ - int getWidth(); - } + Status getStatus(); }
Modified: incubator/wave/trunk/src/org/waveprotocol/wave/media/model/AttachmentDocumentWrapper.java URL: http://svn.apache.org/viewvc/incubator/wave/trunk/src/org/waveprotocol/wave/media/model/AttachmentDocumentWrapper.java?rev=1404734&r1=1404733&r2=1404734&view=diff ============================================================================== --- incubator/wave/trunk/src/org/waveprotocol/wave/media/model/AttachmentDocumentWrapper.java (original) +++ incubator/wave/trunk/src/org/waveprotocol/wave/media/model/AttachmentDocumentWrapper.java Thu Nov 1 19:50:22 2012 @@ -39,45 +39,6 @@ import org.waveprotocol.wave.model.util. */ public class AttachmentDocumentWrapper<N, E extends N, T extends N> implements MutableClientAttachment { - private static class ImageImpl implements Image { - private final int width; - private final int height; - - public ImageImpl(int width, int height) { - this.width = width; - this.height = height; - } - - @Override - public int getHeight() { - return height; - } - - @Override - public int getWidth() { - return width; - } - } - - private static class ThumbnailImpl implements Thumbnail { - private final int width; - private final int height; - - public ThumbnailImpl(int width, int height) { - this.width = width; - this.height = height; - } - - @Override - public int getHeight() { - return height; - } - - @Override - public int getWidth() { - return width; - } - } /** * Represents a name of an entity in the document. @@ -96,6 +57,7 @@ public class AttachmentDocumentWrapper<N */ // VisibleForTesting enum KeyName { + ATTACHMENT_ID, ATTACHMENT_SIZE, ATTACHMENT_URL, CREATOR, @@ -118,12 +80,36 @@ public class AttachmentDocumentWrapper<N } } + class ImageMetadataImpl implements ImageMetadata { + + private final int width; + private final int height; + + public ImageMetadataImpl(int width, int height) { + this.width = width; + this.height = height; + } + + + @Override + public int getWidth() { + return width; + } + + @Override + public int getHeight() { + return height; + } + + } + private final DocumentBasedBasicMap<E, KeyName, String> dataMap; private final ObservableMutableDocument<N, E, T> internalDocument; - private volatile ImageImpl image = null; - private volatile ThumbnailImpl thumbnail = null; + private ImageMetadata image; + + private ImageMetadata thumbnail; /** * Factory method for creating AttachmentDocumentWrappers. @@ -150,6 +136,11 @@ public class AttachmentDocumentWrapper<N } @Override + public String getAttachmentId() { + return dataMap.get(KeyName.ATTACHMENT_ID); + } + + @Override public String getAttachmentUrl() { return dataMap.get(KeyName.ATTACHMENT_URL); } @@ -170,12 +161,12 @@ public class AttachmentDocumentWrapper<N } @Override - public ImageImpl getImage() { + public ImageMetadata getContentImageMetadata() { if (image == null) { Integer width = getAsInt(KeyName.IMAGE_WIDTH); Integer height = getAsInt(KeyName.IMAGE_HEIGHT); if (width != null && height != null) { - image = new ImageImpl(width, height); + image = new ImageMetadataImpl(width, height); } } return image; @@ -192,12 +183,12 @@ public class AttachmentDocumentWrapper<N } @Override - public ThumbnailImpl getThumbnail() { + public ImageMetadata getThumbnailImageMetadata() { if (thumbnail == null) { Integer width = getAsInt(KeyName.THUMBNAIL_WIDTH); Integer height = getAsInt(KeyName.THUMBNAIL_HEIGHT); if (width != null && height != null) { - thumbnail = new ThumbnailImpl(width, height); + thumbnail = new ImageMetadataImpl(width, height); } } return thumbnail; @@ -226,8 +217,8 @@ public class AttachmentDocumentWrapper<N } @Override - public String getStatus() { - return dataMap.get(KeyName.STATUS); + public Status getStatus() { + return Status.valueOf(dataMap.get(KeyName.STATUS)); } @Override @@ -251,10 +242,10 @@ public class AttachmentDocumentWrapper<N } @Override - public Image setImage(int width, int height) { + public ImageMetadata setImage(int width, int height) { dataMap.put(KeyName.IMAGE_WIDTH, Integer.toString(width)); dataMap.put(KeyName.IMAGE_HEIGHT, Integer.toString(height)); - image = new ImageImpl(width, height); + image = new ImageMetadataImpl(width, height); return image; } @@ -278,15 +269,15 @@ public class AttachmentDocumentWrapper<N * throughout the code base. */ @Override - public void setStatus(String status) { - dataMap.put(KeyName.STATUS, status); + public void setStatus(Status status) { + dataMap.put(KeyName.STATUS, status.name()); } @Override - public Thumbnail setThumbnail(int width, int height) { + public ImageMetadata setThumbnail(int width, int height) { dataMap.put(KeyName.THUMBNAIL_WIDTH, Integer.toString(width)); dataMap.put(KeyName.THUMBNAIL_HEIGHT, Integer.toString(height)); - thumbnail = new ThumbnailImpl(width, height); + thumbnail = new ImageMetadataImpl(width, height); return thumbnail; } Modified: incubator/wave/trunk/src/org/waveprotocol/wave/media/model/AttachmentV3.java URL: http://svn.apache.org/viewvc/incubator/wave/trunk/src/org/waveprotocol/wave/media/model/AttachmentV3.java?rev=1404734&r1=1404733&r2=1404734&view=diff ============================================================================== --- incubator/wave/trunk/src/org/waveprotocol/wave/media/model/AttachmentV3.java (original) +++ incubator/wave/trunk/src/org/waveprotocol/wave/media/model/AttachmentV3.java Thu Nov 1 19:50:22 2012 @@ -1,67 +0,0 @@ -/** - * Copyright 2009 Google Inc. - * - * 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.waveprotocol.wave.media.model; - - -/** - * An attachment. This interface is used both by the client (web-app) and on the server-side. - * - */ -// TODO(user): Rename this to just Attachment, after deleting: -// + ClientAttachment.java -public interface AttachmentV3 { - /** Metadata for an image, whether the original attachment or a thumbnail. */ - public interface ImageMetadata { - /** Returns the image width. */ - public int getWidth(); - /** Returns the image height. */ - public int getHeight(); - } - - /** Enumeration of possible error conditions. */ - public enum ErrorCondition { - /** The uploaded file is larger than the per file limit. */ - FILE_TOO_LARGE, - - /** The user has exceeded their storage quota. */ - QUOTA_EXCEEDED, - - /** The upload has been retried the maximum number of times. */ - TOO_MANY_RETRIES; - } - - /** Returns the attachmentId, which should never be null. */ - public String getAttachmentId(); - - /** Returns the creator's userId (e.g. [email protected]), or null if unavailable. */ - public String getCreator(); - - /** Returns the attachment's MIME type, or null if unavailable. */ - public String getMimeType(); - - /** Returns whether the attachment contains malware. */ - public boolean isMalware(); - - /** - * Returns the original attachment's metadata, if the attachment is an image, or null if - * unavailable. - */ - public ImageMetadata getContentImageMetadata(); - - /** Returns the thumbnail image's metadata, or null if unavailable. */ - public ImageMetadata getThumbnailImageMetadata(); -} Modified: incubator/wave/trunk/src/org/waveprotocol/wave/media/model/ClientAttachment.java URL: http://svn.apache.org/viewvc/incubator/wave/trunk/src/org/waveprotocol/wave/media/model/ClientAttachment.java?rev=1404734&r1=1404733&r2=1404734&view=diff ============================================================================== --- incubator/wave/trunk/src/org/waveprotocol/wave/media/model/ClientAttachment.java (original) +++ incubator/wave/trunk/src/org/waveprotocol/wave/media/model/ClientAttachment.java Thu Nov 1 19:50:22 2012 @@ -1,165 +0,0 @@ -/** - * Copyright 2009 Google Inc. - * - * 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.waveprotocol.wave.media.model; - - -/** - * Represents a set of client-side attachment metadata for a single attachment - * instance on a wavelet. - * - * Implementations of this class should be thread-safe. - * - * TODO(user): This interface will eventually replace the existing - * org.waveprotocol.wave.model.wave.Attachment interface. Once this - * is done, remove the old code. - * - */ -// TODO(user): Replace all uses of this interface by either AttachmentV3, or a server-side -// extension of that interface. -public interface ClientAttachment { - /** - * Gets the relative URL by which this attachment may be retrieved. - * - * @return a relative URL or null if the attachment is not yet ready to - * download - */ - String getAttachmentUrl(); - - /** - * Gets the total size of the attachment in bytes if known, or null - * otherwise. - * - * @return the total size of the attachment, or null. - */ - Long getSize(); - - /** - * Gets the user-provided filename of the attachment. This may have been - * sanitized for safer consumption. - * - * @return the attachment filename or null if the filename is not known - */ - String getFilename(); - - /** - * Gets the MIME type of the attachment if known. - * - * @return the MIME type as a {@code String} or {@code null} if unknown - */ - String getMimeType(); - - /** - * Gets the image annotation data for the attachment. - * - * @return the ImageAnnotation or null if the attachment is not an image - * or the metadata is not yet known - */ - Image getImage(); - - /** - * Gets the thumbnail annotation data for the attachment. - * - * @return the ThumbnailAnnotation or null if there is no thumbnail present - */ - Thumbnail getThumbnail(); - - /** - * Gets the relative URL by which this thumbnail may be retrieved. - * - * @return a relative URL - */ - String getThumbnailUrl(); - - /** - * Gets the upload progress of the attachment as bytes. When this value is - * non-zero and equal to the value returned by {@link #getSize}, - * the attachment upload is complete. - * - * @return the upload progress of the attachment in bytes - */ - long getUploadedByteCount(); - - /** - * The number of times the upload for this attachment has been retried. - * - * @return the number of times the upload has been retried. - */ - long getUploadRetryCount(); - - /** - * Gets the malware status of the attachment. - * - * @return true if the attachment is known or suspected to be malware, false - * if it is either known not to be malware or if the malware status is unknown - */ - boolean isMalware(); - - /** - * The upload status of the attachment. - * - * @return the status of the upload. - */ - String getStatus(); - - /** - * Gets the user id of the user who created the attachment. - * - * @return the user id or null if the creator is unknown (may occur if an - * attach request comes in before the creation request) - */ - String getCreator(); - - /** - * The thumbnail part of the Attachment metadata. - */ - interface Thumbnail { - /** - * The width of the thumbnail in pixels. - * - * @return the width in pixels - */ - int getWidth(); - - /** - * The height of the thumbnail in pixels. - * - * @return the height in pixels - */ - int getHeight(); - } - - /** - * The image part of the Attachment metadata. Only used if the attachment - * is an image. - */ - interface Image { - /** - * Gets the height of the image attachment. - * - * @return the height of the image in pixels - */ - int getHeight(); - - - /** - * Gets the width of the image attachment. - * - * @return the width of the image in pixels - */ - int getWidth(); - } -} Modified: incubator/wave/trunk/src/org/waveprotocol/wave/media/model/MutableClientAttachment.java URL: http://svn.apache.org/viewvc/incubator/wave/trunk/src/org/waveprotocol/wave/media/model/MutableClientAttachment.java?rev=1404734&r1=1404733&r2=1404734&view=diff ============================================================================== --- incubator/wave/trunk/src/org/waveprotocol/wave/media/model/MutableClientAttachment.java (original) +++ incubator/wave/trunk/src/org/waveprotocol/wave/media/model/MutableClientAttachment.java Thu Nov 1 19:50:22 2012 @@ -24,7 +24,7 @@ package org.waveprotocol.wave.media.mode * Implementations of this class should be thread-safe. * */ -public interface MutableClientAttachment extends ClientAttachment { +public interface MutableClientAttachment extends Attachment { /** * Sets the relative URL to download this attachment. * @@ -70,7 +70,7 @@ public interface MutableClientAttachment * @param height the height of the image * @return the image metadata representation */ - Image setImage(int width, int height); + ImageMetadata setImage(int width, int height); /** * Sets the malware status of the attachment. @@ -94,14 +94,14 @@ public interface MutableClientAttachment * @param height the height of the thumbnail * @return the thumbnail metadata representation */ - Thumbnail setThumbnail(int width, int height); + ImageMetadata setThumbnail(int width, int height); /** * Sets the upload status of the attachment. * * @param status the upload status. */ - void setStatus(String status); + void setStatus(Status status); /** * Sets the relative URL to download the thumbnail for this attachment. Modified: incubator/wave/trunk/test/org/waveprotocol/box/server/persistence/AttachmentStoreTestBase.java URL: http://svn.apache.org/viewvc/incubator/wave/trunk/test/org/waveprotocol/box/server/persistence/AttachmentStoreTestBase.java?rev=1404734&r1=1404733&r2=1404734&view=diff ============================================================================== --- incubator/wave/trunk/test/org/waveprotocol/box/server/persistence/AttachmentStoreTestBase.java (original) +++ incubator/wave/trunk/test/org/waveprotocol/box/server/persistence/AttachmentStoreTestBase.java Thu Nov 1 19:50:22 2012 @@ -1,179 +1,169 @@ -/** - * Copyright 2010 Google Inc. - * - * 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.waveprotocol.box.server.persistence; - -import junit.framework.TestCase; - -import org.waveprotocol.box.server.persistence.AttachmentStore.AttachmentData; -import org.waveprotocol.wave.model.id.WaveId; -import org.waveprotocol.wave.model.id.WaveletId; -import org.waveprotocol.wave.model.id.WaveletName; - -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; - -/** - * Test cases for the Attachment Stores. - * - * @author [email protected] (Joseph Gentle) - */ -public abstract class AttachmentStoreTestBase extends TestCase { - - private final static WaveletName WAVELET_NAME = WaveletName.of(WaveId.of("example.com", "w+abc"), - WaveletId.of("example.com", "conv+root")); - - public void testStoreReturnsNullForNonexistantId() { - AttachmentStore store = newAttachmentStore(); - assertNull(store.getAttachment(WAVELET_NAME, "some_madeup_id")); - } - - public void testStoreCanStoreData() throws Exception { - String testData = "some file data"; - String id = "id_1"; - AttachmentStore store = makeStoreWithData(WAVELET_NAME, id, testData); - - AttachmentData data = store.getAttachment(WAVELET_NAME, id); - assertEquals(testData, dataToString(data)); - } - - public void testStoreReturnsNullForWrongWaveletName() throws Exception { - String testData = "some file data"; - String id = "id_1"; - AttachmentStore store = makeStoreWithData(WAVELET_NAME, id, testData); - - WaveletName otherWaveletName = WaveletName.of(WaveId.of("example.com", "w+abd"), - WaveletId.of("example.com", "conv+root")); - AttachmentData data = store.getAttachment(otherWaveletName, id); - assertNull(data); - } - - public void testContentLengthMatchesDataSize() throws Exception { - String testData = "blah blah blah"; - String id = "id_2"; - AttachmentStore store = makeStoreWithData(WAVELET_NAME, id, testData); - - AttachmentData data = store.getAttachment(WAVELET_NAME, id); - assertEquals(testData.length(), data.getContentSize()); - } - - public void testStoreCanDeleteData() throws Exception { - String testData = "some day, I'm going to run out of test strings"; - String id = "id_3"; - AttachmentStore store = makeStoreWithData(WAVELET_NAME, id, testData); - - store.deleteAttachment(WAVELET_NAME, id); - AttachmentData data = store.getAttachment(WAVELET_NAME, id); - assertNull(data); - } - - public void testAttachmentCanWriteToOutputStream() throws Exception { - String testData = "maybe there's some easy way to generate test strings"; - String id = "id_4"; - AttachmentStore store = makeStoreWithData(WAVELET_NAME, id, testData); - AttachmentData data = store.getAttachment(WAVELET_NAME, id); - - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - data.writeDataTo(stream); - assertEquals(testData, stream.toString("UTF-8")); - } - - public void testAttachmentHasWorkingInputStream() throws Exception { - String testData = "I suppose these strings don't actually need to be different"; - String id = "id_5"; - AttachmentStore store = makeStoreWithData(WAVELET_NAME, id, testData); - AttachmentData data = store.getAttachment(WAVELET_NAME, id); - - BufferedReader reader = new BufferedReader(new InputStreamReader(data.getInputStream())); - - StringBuilder builder = new StringBuilder(); - String line; - while ((line = reader.readLine()) != null) { - // This little snippet will discard any "\n" characters, but it shouldn't - // matter. - builder.append(line); - } - - assertEquals(testData, builder.toString()); - reader.close(); - } - - public void testGetStreamReturnsNewStream() throws Exception { - String testData = "There's something quite peaceful about writing tests."; - String id = "id_6"; - AttachmentStore store = makeStoreWithData(WAVELET_NAME, id, testData); - AttachmentData data = store.getAttachment(WAVELET_NAME, id); - - InputStream is1 = data.getInputStream(); - InputStream is2 = data.getInputStream(); - assertNotSame(is1, is2); - - int firstByte = is1.read(); - assertSame(firstByte, is2.read()); - - // Check that a new input stream created now still has the same first byte. - InputStream is3 = data.getInputStream(); - assertSame(firstByte, is3.read()); - - is1.close(); - is2.close(); - is3.close(); - } - - public void testOverwriteAttachmentReturnsFalse() throws Exception { - String testData = "First."; - String id = "id_7"; - AttachmentStore store = makeStoreWithData(WAVELET_NAME, id, testData); - - // A second element added with the same ID should not write. - boolean written = writeStringDataToAttachmentStore(store, WAVELET_NAME, id, "Second"); - assertFalse(written); - - // Check that the database still contains the original entry - assertEquals(testData, dataToString(store.getAttachment(WAVELET_NAME, id))); - } - - // Helpers. - /** - * Create and return a new attachment store instance of the type being tested. - * @return a new attachment store - */ - protected abstract AttachmentStore newAttachmentStore(); - - protected boolean writeStringDataToAttachmentStore( - AttachmentStore store, WaveletName waveletName, String id, String data) throws IOException { - return store.storeAttachment(waveletName, id, new ByteArrayInputStream(data.getBytes("UTF-8"))); - } - - protected AttachmentStore makeStoreWithData(WaveletName waveletName, String id, String data) - throws Exception { - AttachmentStore store = newAttachmentStore(); - boolean written = writeStringDataToAttachmentStore(store, waveletName, id, data); - if (!written) { - throw new RuntimeException("Could not write attachment to store"); - } - return store; - } - - protected String dataToString(AttachmentData data) throws IOException { - return AttachmentUtil.writeAttachmentDataToString(data, "UTF-8"); - } -} +/** + * Copyright 2010 Google Inc. + * + * 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.waveprotocol.box.server.persistence; + +import junit.framework.TestCase; + +import org.waveprotocol.box.server.persistence.AttachmentStore.AttachmentData; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import org.waveprotocol.wave.media.model.AttachmentId; + +/** + * Test cases for the Attachment Stores. + * + * @author [email protected] (Joseph Gentle) + * @author [email protected] (A. Kaplanov) + */ +public abstract class AttachmentStoreTestBase extends TestCase { + + public void testStoreReturnsNullForNonexistantId() throws IOException { + AttachmentStore store = newAttachmentStore(); + AttachmentId id = new AttachmentId("", "some_madeup_id"); + assertNull(store.getAttachment(id)); + } + + public void testStoreCanStoreData() throws Exception { + String testData = "some file data"; + AttachmentId id = new AttachmentId("", "id_1"); + AttachmentStore store = makeStoreWithData(id, testData); + + AttachmentData data = store.getAttachment(id); + assertEquals(testData, dataToString(data)); + } + + public void testContentLengthMatchesDataSize() throws Exception { + String testData = "blah blah blah"; + AttachmentId id = new AttachmentId("", "id_2"); + AttachmentStore store = makeStoreWithData(id, testData); + + AttachmentData data = store.getAttachment(id); + assertEquals(testData.length(), data.getSize()); + } + + public void testStoreCanDeleteData() throws Exception { + String testData = "some day, I'm going to run out of test strings"; + AttachmentId id = new AttachmentId("", "id_3"); + AttachmentStore store = makeStoreWithData(id, testData); + + store.deleteAttachment(id); + AttachmentData data = store.getAttachment(id); + assertNull(data); + } + + public void testAttachmentCanWriteToOutputStream() throws Exception { + String testData = "maybe there's some easy way to generate test strings"; + AttachmentId id = new AttachmentId("", "id_4"); + AttachmentStore store = makeStoreWithData(id, testData); + AttachmentData data = store.getAttachment(id); + + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + AttachmentUtil.writeTo(data.getInputStream(), stream); + assertEquals(testData, stream.toString("UTF-8")); + } + + public void testAttachmentHasWorkingInputStream() throws Exception { + String testData = "I suppose these strings don't actually need to be different"; + AttachmentId id = new AttachmentId("", "id_5"); + AttachmentStore store = makeStoreWithData(id, testData); + AttachmentData data = store.getAttachment(id); + + BufferedReader reader = new BufferedReader(new InputStreamReader(data.getInputStream())); + + StringBuilder builder = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + // This little snippet will discard any "\n" characters, but it shouldn't + // matter. + builder.append(line); + } + + assertEquals(testData, builder.toString()); + reader.close(); + } + + public void testGetStreamReturnsNewStream() throws Exception { + String testData = "There's something quite peaceful about writing tests."; + AttachmentId id = new AttachmentId("", "id_6"); + AttachmentStore store = makeStoreWithData(id, testData); + AttachmentData data = store.getAttachment(id); + + InputStream is1 = data.getInputStream(); + InputStream is2 = data.getInputStream(); + assertNotSame(is1, is2); + + int firstByte = is1.read(); + assertSame(firstByte, is2.read()); + + // Check that a new input stream created now still has the same first byte. + InputStream is3 = data.getInputStream(); + assertSame(firstByte, is3.read()); + + is1.close(); + is2.close(); + is3.close(); + } + + public void testOverwriteAttachmentThrowsException() throws Exception { + String testData = "First."; + AttachmentId id = new AttachmentId("", "id_7"); + AttachmentStore store = makeStoreWithData(id, testData); + + boolean exceptionThrown=false; + try { + // A second element added with the same ID should not write. + writeStringDataToAttachmentStore(store, id, "Second"); + } catch (IOException ex) { + exceptionThrown=true; + } + assertTrue(exceptionThrown); + + // Check that the database still contains the original entry + assertEquals(testData, dataToString(store.getAttachment(id))); + } + + // Helpers. + /** + * Create and return a new attachment store instance of the type being tested. + * @return a new attachment store + */ + protected abstract AttachmentStore newAttachmentStore(); + + protected void writeStringDataToAttachmentStore( + AttachmentStore store, AttachmentId id, String data) throws IOException { + store.storeAttachment(id, new ByteArrayInputStream(data.getBytes("UTF-8"))); + } + + protected AttachmentStore makeStoreWithData(AttachmentId id, String data) + throws Exception { + AttachmentStore store = newAttachmentStore(); + writeStringDataToAttachmentStore(store, id, data); + return store; + } + + protected String dataToString(AttachmentData data) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + AttachmentUtil.writeTo(data.getInputStream(), out); + return out.toString("UTF-8"); + } +} Modified: incubator/wave/trunk/test/org/waveprotocol/wave/media/model/AttachmentDocumentWrapperTest.java URL: http://svn.apache.org/viewvc/incubator/wave/trunk/test/org/waveprotocol/wave/media/model/AttachmentDocumentWrapperTest.java?rev=1404734&r1=1404733&r2=1404734&view=diff ============================================================================== --- incubator/wave/trunk/test/org/waveprotocol/wave/media/model/AttachmentDocumentWrapperTest.java (original) +++ incubator/wave/trunk/test/org/waveprotocol/wave/media/model/AttachmentDocumentWrapperTest.java Thu Nov 1 19:50:22 2012 @@ -20,6 +20,8 @@ package org.waveprotocol.wave.media.mode import junit.framework.TestCase; +import org.waveprotocol.wave.media.model.Attachment.ImageMetadata; +import org.waveprotocol.wave.media.model.Attachment.Status; import org.waveprotocol.wave.model.document.Doc; import org.waveprotocol.wave.model.document.ObservableDocument; import org.waveprotocol.wave.model.testing.BasicFactories; @@ -36,7 +38,7 @@ public class AttachmentDocumentWrapperTe private static final Long SIZE = 123456789123L; private static final String MIME_TYPE = "image/jpeg"; private static final long UPLOAD_PROGRESS = 12345L; - private static final String UPLOAD_STATUS = "IN_PROGRESS"; + private static final Attachment.Status UPLOAD_STATUS = Status.IN_PROGRESS; private static final int IMAGE_HEIGHT = 123; private static final int IMAGE_WIDTH = 234; @@ -48,10 +50,10 @@ public class AttachmentDocumentWrapperTe * read. */ public void testBasicMetadata() { - ClientAttachment wrapper = createBasicWrapper(); + MutableClientAttachment wrapper = createBasicWrapper(); checkBasicFields(wrapper); - assertNull(wrapper.getImage()); - assertNull(wrapper.getThumbnail()); + assertNull(wrapper.getContentImageMetadata()); + assertNull(wrapper.getThumbnailImageMetadata()); } /** @@ -95,7 +97,7 @@ public class AttachmentDocumentWrapperTe * * @param wrapper the wrapper to check */ - private static void checkBasicFields(ClientAttachment wrapper) { + private static void checkBasicFields(MutableClientAttachment wrapper) { assertEquals(FILENAME, wrapper.getFilename()); assertEquals(CREATOR, wrapper.getCreator()); assertEquals(MIME_TYPE, wrapper.getMimeType()); @@ -112,11 +114,11 @@ public class AttachmentDocumentWrapperTe * @param wrapper the wrapper */ private static void checkElementFields(MutableClientAttachment wrapper) { - ClientAttachment.Image attachmentImage = wrapper.getImage(); + ImageMetadata attachmentImage = wrapper.getContentImageMetadata(); assertEquals(IMAGE_HEIGHT, attachmentImage.getHeight()); assertEquals(IMAGE_WIDTH, attachmentImage.getWidth()); - ClientAttachment.Thumbnail attachmentThumbnail = wrapper.getThumbnail(); + ImageMetadata attachmentThumbnail = wrapper.getThumbnailImageMetadata(); assertEquals(THUMB_HEIGHT, attachmentThumbnail.getHeight()); assertEquals(THUMB_WIDTH, attachmentThumbnail.getWidth()); } Added: incubator/wave/trunk/thumbnail_patterns/application_pdf URL: http://svn.apache.org/viewvc/incubator/wave/trunk/thumbnail_patterns/application_pdf?rev=1404734&view=auto ============================================================================== Files incubator/wave/trunk/thumbnail_patterns/application_pdf (added) and incubator/wave/trunk/thumbnail_patterns/application_pdf Thu Nov 1 19:50:22 2012 differ Added: incubator/wave/trunk/thumbnail_patterns/application_zip URL: http://svn.apache.org/viewvc/incubator/wave/trunk/thumbnail_patterns/application_zip?rev=1404734&view=auto ============================================================================== Files incubator/wave/trunk/thumbnail_patterns/application_zip (added) and incubator/wave/trunk/thumbnail_patterns/application_zip Thu Nov 1 19:50:22 2012 differ Added: incubator/wave/trunk/thumbnail_patterns/default URL: http://svn.apache.org/viewvc/incubator/wave/trunk/thumbnail_patterns/default?rev=1404734&view=auto ============================================================================== Files incubator/wave/trunk/thumbnail_patterns/default (added) and incubator/wave/trunk/thumbnail_patterns/default Thu Nov 1 19:50:22 2012 differ Added: incubator/wave/trunk/thumbnail_patterns/text_plain URL: http://svn.apache.org/viewvc/incubator/wave/trunk/thumbnail_patterns/text_plain?rev=1404734&view=auto ============================================================================== Files incubator/wave/trunk/thumbnail_patterns/text_plain (added) and incubator/wave/trunk/thumbnail_patterns/text_plain Thu Nov 1 19:50:22 2012 differ
