Skip to content

Commit

Permalink
Added AbstractAdapterTest as super class for new GcsAdapterTest and D…
Browse files Browse the repository at this point in the history
…atastoreAdapterTest, corrected spelling in persistence package.
  • Loading branch information
tfrdidi committed Aug 24, 2015
1 parent 14e24e3 commit a575607
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/wahlzeit/model/PhotoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.google.appengine.api.images.Image;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Work;
import org.wahlzeit.model.persistance.ImageStorage;
import org.wahlzeit.model.persistence.ImageStorage;
import org.wahlzeit.services.LogBuilder;
import org.wahlzeit.services.ObjectManager;
import org.wahlzeit.services.Persistent;
Expand Down Expand Up @@ -250,7 +250,7 @@ public List<Tag> addTagsThatMatchCondition(List<Tag> tags, String condition) {
/**
* @methodtype command
* <p/>
* Persists all available sizes of the Photo. If one size exceeds the limit of the persistance layer, e.g. > 1MB for
* Persists all available sizes of the Photo. If one size exceeds the limit of the persistence layer, e.g. > 1MB for
* the Datastore, it is simply not persisted.
*/
protected void saveScaledImages(Photo photo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* <http://www.gnu.org/licenses/>.
*/

package org.wahlzeit.model.persistance;
package org.wahlzeit.model.persistence;

import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesServiceFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
* <http://www.gnu.org/licenses/>.
*/

package org.wahlzeit.model.persistance;
package org.wahlzeit.model.persistence;

import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.tools.cloudstorage.GcsFileMetadata;
import com.google.appengine.tools.cloudstorage.GcsFileOptions;
import com.google.appengine.tools.cloudstorage.GcsFilename;
import com.google.appengine.tools.cloudstorage.GcsInputChannel;
Expand All @@ -41,7 +42,7 @@
import java.util.logging.Logger;

/**
* Adapter for the Google Cloud Storage. Use {@link org.wahlzeit.model.persistance.GcsAdapter.Builder} to create an
* Adapter for the Google Cloud Storage. Use {@link org.wahlzeit.model.persistence.GcsAdapter.Builder} to create an
* object.
* <p/>
* Created by Lukas Hahmann on 28.04.15.
Expand All @@ -57,7 +58,7 @@ public class GcsAdapter extends ImageStorage {
private GcsService gcsService;

/**
* Do not use directly, instead use {@link org.wahlzeit.model.persistance.GcsAdapter.Builder} to create an object.
* Do not use directly, instead use {@link org.wahlzeit.model.persistence.GcsAdapter.Builder} to create an object.
*/
private GcsAdapter(String bucketName, String photoFolderName, String defaultImageMimeTypeName, int bufferLength,
GcsService gcsService) {
Expand Down Expand Up @@ -107,8 +108,13 @@ protected Image doReadImage(String filename, int size) throws IOException {

GcsInputChannel readChannel = gcsService.openReadChannel(gcsFilename, 0);
ByteBuffer bb = ByteBuffer.allocate(bufferLength);
readChannel.read(bb);
Image result = ImagesServiceFactory.makeImage(bb.array());
Image result = null;
try {
readChannel.read(bb);
result = ImagesServiceFactory.makeImage(bb.array());
} catch (IOException e) {
// when image does not exist, IOException is thrown
}
if (result == null) {
log.warning(LogBuilder.createSystemMessage().addMessage("does not exist!").toString());
} else {
Expand All @@ -123,7 +129,8 @@ protected boolean doDoesImageExist(String photoIdAsString, int size) {
boolean result;
try {
// will be null if file does not exist
result = gcsService.getMetadata(gcsFilename) != null;
GcsFileMetadata gcsFileMetadata = gcsService.getMetadata(gcsFilename);
result = gcsFileMetadata != null;
} catch (IOException e) {
result = false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.wahlzeit.model.persistance;
package org.wahlzeit.model.persistence;

import org.wahlzeit.model.PhotoSize;
import org.wahlzeit.services.LogBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package org.wahlzeit.model;
package org.wahlzeit.model.persistence;

import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesServiceFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.wahlzeit.model.persistance.DatastoreAdapter;
import org.wahlzeit.model.persistance.ImageStorage;
import org.wahlzeit.testEnvironmentProvider.LocalDatastoreServiceTestConfigProvider;
import org.wahlzeit.testEnvironmentProvider.RegisteredOfyEnvironmentProvider;

import java.io.IOException;
import java.io.Serializable;
Expand All @@ -19,35 +13,42 @@
import static org.junit.Assert.fail;

/**
* Test class for {@link DatastoreAdapter}
* <p/>
* Created by Lukas Hahmann on 20.08.15.
* Abstract super class for all Adapter classes that implement the {@link ImageStorage}.
*
* Created by Lukas Hahmann on 24.08.15.
*/
public class DatastoreAdapterTest {

@ClassRule
public static TestRule chain = RuleChain.
outerRule(new LocalDatastoreServiceTestConfigProvider()).
around(new RegisteredOfyEnvironmentProvider());

private ImageStorage imageStorage;
private Image smallTestImage;
private Image maxSizeTestImage;
private Image tooLargeTestImage;
public abstract class AbstractAdapterTest {

protected ImageStorage imageStorage;
protected Image smallTestImage;
protected Image maxSizeTestImage;

@Before
public void setUp() {
imageStorage = new DatastoreAdapter();

public void SetUp() {
ByteBuffer bb = ByteBuffer.allocate(1024);
smallTestImage = ImagesServiceFactory.makeImage(bb.array());

bb = ByteBuffer.allocate(1024*1023);
bb = ByteBuffer.allocate(1024 * 1023);
maxSizeTestImage = ImagesServiceFactory.makeImage(bb.array());

bb = ByteBuffer.allocate(1024*1025);
tooLargeTestImage = ImagesServiceFactory.makeImage(bb.array());
storageDependentSetUp();
}

@After
public void tearDown() {
storageDependentTearDown();
}

/**
* @methodproperty hook
*/
protected void storageDependentSetUp() {
}

/**
* @methodproperty hook
*/
protected void storageDependentTearDown() {
}


Expand All @@ -67,16 +68,6 @@ public void testWriteImage() {
}


@Test(expected = ArrayIndexOutOfBoundsException.class)
public void testUpperSizeLimit() {
try {
imageStorage.writeImage(tooLargeTestImage, "blub", 1);
} catch (IOException e) {
fail("IOException should not be thrown!");
}
}


@Test
public void testReadImage() {
try {
Expand Down Expand Up @@ -115,9 +106,9 @@ public void testReadImage() {

@Test
public void testImageExistence() {
boolean exists = false;
boolean exists;

exists = imageStorage.doesImageExist("exists", 1);
exists = imageStorage.doesImageExist("doesNotExist", 1);
assert !exists;

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.wahlzeit.model.persistence;

import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesServiceFactory;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.wahlzeit.testEnvironmentProvider.LocalDatastoreServiceTestConfigProvider;
import org.wahlzeit.testEnvironmentProvider.RegisteredOfyEnvironmentProvider;

import java.io.IOException;
import java.nio.ByteBuffer;

import static org.junit.Assert.fail;

/**
* Test class for {@link DatastoreAdapter}
* <p/>
* Created by Lukas Hahmann on 20.08.15.
*/
public class DatastoreAdapterTest extends AbstractAdapterTest {

@ClassRule
public static TestRule chain = RuleChain.
outerRule(new LocalDatastoreServiceTestConfigProvider()).
around(new RegisteredOfyEnvironmentProvider());

private Image tooLargeTestImage;


@Override
protected void storageDependentSetUp() {
imageStorage = new DatastoreAdapter();

ByteBuffer bb = ByteBuffer.allocate(1024 * 1025);
tooLargeTestImage = ImagesServiceFactory.makeImage(bb.array());
}


@Test(expected = ArrayIndexOutOfBoundsException.class)
public void testUpperSizeLimit() {
try {
imageStorage.writeImage(tooLargeTestImage, "blub", 1);
} catch (IOException e) {
fail("IOException should not be thrown!");
}
}
}
23 changes: 23 additions & 0 deletions src/test/java/org/wahlzeit/model/persistence/GcsAdapterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.wahlzeit.model.persistence;

import com.google.appengine.tools.development.testing.LocalBlobstoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

/**
* Created by Lukas Hahmann on 24.08.15.
*/
public class GcsAdapterTest extends AbstractAdapterTest {

private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalBlobstoreServiceTestConfig());

@Override
protected void storageDependentSetUp() {
helper.setUp();
imageStorage = new GcsAdapter.Builder().build();
}

@Override
protected void storageDependentTearDown() {
helper.tearDown();
}
}

0 comments on commit a575607

Please sign in to comment.