Browse Source

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.
pull/125/merge
Oliver Gierke 12 years ago
parent
commit
8392f4275f
  1. 5
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsOperations.java
  2. 7
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java
  3. 16
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsTemplateIntegrationTests.java

5
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsOperations.java

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

7
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -43,6 +43,7 @@ import com.mongodb.gridfs.GridFSInputFile; @@ -43,6 +43,7 @@ import com.mongodb.gridfs.GridFSInputFile;
*
* @author Oliver Gierke
* @author Philipp Schneider
* @author Thomas Darimont
*/
public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver {
@ -190,7 +191,9 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver @@ -190,7 +191,9 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
* @see org.springframework.core.io.ResourceLoader#getResource(java.lang.String)
*/
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;
}
/*

16
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsTemplateIIntegrationTests.java → spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsTemplateIntegrationTests.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -42,15 +42,15 @@ import com.mongodb.gridfs.GridFSFile; @@ -42,15 +42,15 @@ import com.mongodb.gridfs.GridFSFile;
*
* @author Oliver Gierke
* @author Philipp Schneider
* @author Thomas Darimont
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:gridfs/gridfs.xml")
public class GridFsTemplateIIntegrationTests {
public class GridFsTemplateIntegrationTests {
Resource resource = new ClassPathResource("gridfs/gridfs.xml");
@Autowired
GridFsOperations operations;
@Autowired GridFsOperations operations;
@Before
public void setUp() {
@ -127,6 +127,14 @@ public class GridFsTemplateIIntegrationTests { @@ -127,6 +127,14 @@ public class GridFsTemplateIIntegrationTests {
assertThat(resources[0].getContentType(), is(reference.getContentType()));
}
/**
* @see DATAMONGO-813
*/
@Test
public void getResourceShouldReturnNullForNonExistingResource() {
assertThat(operations.getResource("doesnotexist"), is(nullValue()));
}
private static void assertSame(GridFSFile left, GridFSFile right) {
assertThat(left.getId(), is(right.getId()));
Loading…
Cancel
Save