22
33import static org .hamcrest .CoreMatchers .is ;
44import static org .hamcrest .Matchers .nullValue ;
5+ import static org .junit .Assert .assertEquals ;
56import static org .junit .Assert .assertNotNull ;
67import static org .junit .Assert .assertThat ;
78
3233@ ContextConfiguration (classes = MongoConfig .class )
3334@ RunWith (SpringJUnit4ClassRunner .class )
3435public class GridFSTest {
35-
36+
3637 private final Logger logger = LoggerFactory .getLogger (getClass ());
37-
38+
3839 @ Autowired
3940 private GridFsTemplate gridFsTemplate ;
40-
41+
4142 @ After
4243 public void tearDown () {
4344 List <GridFSDBFile > fileList = gridFsTemplate .find (null );
44- for (GridFSDBFile file : fileList ) {
45+ for (GridFSDBFile file : fileList ) {
4546 gridFsTemplate .delete (new Query (Criteria .where ("filename" ).is (file .getFilename ())));
4647 }
4748 }
@@ -52,7 +53,6 @@ public void whenStoringFileWithMetadata_thenFileAndMetadataAreStored() {
5253 metaData .put ("key" , "value" );
5354 InputStream inputStream = null ;
5455 String id = "" ;
55-
5656 try {
5757 inputStream = new FileInputStream ("src/main/resources/test.png" );
5858 id = gridFsTemplate .store (inputStream , "test.png" , "image/png" , metaData ).getId ().toString ();
@@ -77,7 +77,6 @@ public void givenFileWithMetadataExist_whenFindingFileById_thenFileWithMetadataI
7777 metaData .put ("key" , "value" );
7878 InputStream inputStream = null ;
7979 String id = "" ;
80-
8180 try {
8281 inputStream = new FileInputStream ("src/main/resources/test.png" );
8382 id = gridFsTemplate .store (inputStream , "test.png" , "image/png" , metaData ).getId ().toString ();
@@ -92,9 +91,9 @@ public void givenFileWithMetadataExist_whenFindingFileById_thenFileWithMetadataI
9291 }
9392 }
9493 }
95-
94+
9695 GridFSDBFile gridFSDBFile = gridFsTemplate .findOne (new Query (Criteria .where ("_id" ).is (id )));
97-
96+
9897 assertNotNull (gridFSDBFile );
9998 assertThat (gridFSDBFile .getFilename (), is ("test.png" ));
10099 assertThat (gridFSDBFile .getMetaData ().get ("key" ), is ("value" ));
@@ -121,21 +120,19 @@ public void givenMetadataAndFilesExist_whenFindingAllFiles_thenFilesWithMetadata
121120 }
122121 }
123122 }
124-
123+
125124 List <GridFSDBFile > gridFSDBFiles = gridFsTemplate .find (null );
126-
125+
127126 assertNotNull (gridFSDBFiles );
128127 assertThat (gridFSDBFiles .size (), is (2 ));
129128 }
130-
131-
129+
132130 @ Test
133131 public void givenFileWithMetadataExist_whenDeletingFileById_thenFileWithMetadataIsDeleted () {
134132 DBObject metaData = new BasicDBObject ();
135133 metaData .put ("key" , "value" );
136134 InputStream inputStream = null ;
137135 String id = "" ;
138-
139136 try {
140137 inputStream = new FileInputStream ("src/main/resources/test.png" );
141138 id = gridFsTemplate .store (inputStream , "test.png" , "image/png" , metaData ).getId ().toString ();
@@ -150,19 +147,18 @@ public void givenFileWithMetadataExist_whenDeletingFileById_thenFileWithMetadata
150147 }
151148 }
152149 }
153-
150+
154151 gridFsTemplate .delete (new Query (Criteria .where ("_id" ).is (id )));
155-
152+
156153 assertThat (gridFsTemplate .findOne (new Query (Criteria .where ("_id" ).is (id ))), is (nullValue ()));
157154 }
158-
155+
159156 @ Test
160157 public void givenFileWithMetadataExist_whenGettingFileByResource_thenFileWithMetadataIsGotten () {
161158 DBObject metaData = new BasicDBObject ();
162159 metaData .put ("key" , "value" );
163160 InputStream inputStream = null ;
164161 String id = "" ;
165-
166162 try {
167163 inputStream = new FileInputStream ("src/main/resources/test.png" );
168164 id = gridFsTemplate .store (inputStream , "test.png" , "image/png" , metaData ).getId ().toString ();
@@ -177,10 +173,11 @@ public void givenFileWithMetadataExist_whenGettingFileByResource_thenFileWithMet
177173 }
178174 }
179175 }
180-
176+
181177 GridFsResource [] gridFsResource = gridFsTemplate .getResources ("test*" );
182-
178+
183179 assertNotNull (gridFsResource );
180+ assertEquals (gridFsResource .length , 1 );
184181 assertThat (gridFsResource [0 ].getFilename (), is ("test.png" ));
185182 }
186183}
0 commit comments