DATAMONGO-813 - Improve handling for non-existing resources in GridFSTemplate.

We now return null for a non-existing resource instead of throwing a NPE.

Original pull request: #106.
This commit is contained in:
Thomas Darimont
2013-12-12 09:58:55 +01:00
committed by Oliver Gierke
parent f3b31fc467
commit c730b8f479
3 changed files with 18 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2011-2012 the original author or authors. * Copyright 2011-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@ import com.mongodb.gridfs.GridFSFile;
* *
* @author Oliver Gierke * @author Oliver Gierke
* @author Philipp Schneider * @author Philipp Schneider
* @author Thomas Darimont
*/ */
public interface GridFsOperations extends ResourcePatternResolver { public interface GridFsOperations extends ResourcePatternResolver {
@@ -126,7 +127,7 @@ public interface GridFsOperations extends ResourcePatternResolver {
* Returns all {@link GridFsResource} with the given file name. * Returns all {@link GridFsResource} with the given file name.
* *
* @param filename * @param filename
* @return * @return the resource if it exists or {@literal null}.
* @see ResourcePatternResolver#getResource(String) * @see ResourcePatternResolver#getResource(String)
*/ */
GridFsResource getResource(String filename); GridFsResource getResource(String filename);

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2011-2012 the original author or authors. * Copyright 2011-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -43,6 +43,7 @@ import com.mongodb.gridfs.GridFSInputFile;
* *
* @author Oliver Gierke * @author Oliver Gierke
* @author Philipp Schneider * @author Philipp Schneider
* @author Thomas Darimont
*/ */
public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver { public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver {
@@ -198,7 +199,9 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
* @see org.springframework.core.io.ResourceLoader#getResource(java.lang.String) * @see org.springframework.core.io.ResourceLoader#getResource(java.lang.String)
*/ */
public GridFsResource getResource(String location) { public GridFsResource getResource(String location) {
return new GridFsResource(findOne(query(whereFilename().is(location))));
GridFSDBFile file = findOne(query(whereFilename().is(location)));
return file != null ? new GridFsResource(file) : null;
} }
/* /*

View File

@@ -45,10 +45,11 @@ import com.mongodb.gridfs.GridFSFile;
* *
* @author Oliver Gierke * @author Oliver Gierke
* @author Philipp Schneider * @author Philipp Schneider
* @author Thomas Darimont
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:gridfs/gridfs.xml") @ContextConfiguration("classpath:gridfs/gridfs.xml")
public class GridFsTemplateIIntegrationTests { public class GridFsTemplateIntegrationTests {
Resource resource = new ClassPathResource("gridfs/gridfs.xml"); Resource resource = new ClassPathResource("gridfs/gridfs.xml");
@@ -162,6 +163,14 @@ public class GridFsTemplateIIntegrationTests {
assertSame(result.get(0), reference); assertSame(result.get(0), reference);
} }
/**
* @see DATAMONGO-813
*/
@Test
public void getResourceShouldReturnNullForNonExistingResource() {
assertThat(operations.getResource("doesnotexist"), is(nullValue()));
}
private static void assertSame(GridFSFile left, GridFSFile right) { private static void assertSame(GridFSFile left, GridFSFile right) {
assertThat(left.getId(), is(right.getId())); assertThat(left.getId(), is(right.getId()));