@@ -84,43 +84,47 @@ public void testRocksDb() throws RocksDBException, IOException {
8484 RocksDB db = RocksDB .open (options , file .toString ())) {
8585 db .put ("1" .getBytes (StandardCharsets .UTF_8 ), "1" .getBytes (StandardCharsets .UTF_8 ));
8686 db .put ("2" .getBytes (StandardCharsets .UTF_8 ), "2" .getBytes (StandardCharsets .UTF_8 ));
87- RockStoreIterator iterator = new RockStoreIterator (db .newIterator (), new ReadOptions ());
88- iterator .seekToFirst ();
89- Assert .assertArrayEquals ("1" .getBytes (StandardCharsets .UTF_8 ), iterator .getKey ());
90- Assert .assertArrayEquals ("1" .getBytes (StandardCharsets .UTF_8 ), iterator .next ().getValue ());
91- Assert .assertTrue (iterator .hasNext ());
92-
93- Assert .assertArrayEquals ("2" .getBytes (StandardCharsets .UTF_8 ), iterator .getValue ());
94- Assert .assertArrayEquals ("2" .getBytes (StandardCharsets .UTF_8 ), iterator .next ().getKey ());
95- Assert .assertFalse (iterator .hasNext ());
96-
97- try {
98- iterator .seekToLast ();
99- } catch (Exception e ) {
100- Assert .assertTrue (e instanceof IllegalStateException );
87+ try (RockStoreIterator iterator =
88+ new RockStoreIterator (db .newIterator (), new ReadOptions ())) {
89+ iterator .seekToFirst ();
90+ Assert .assertArrayEquals ("1" .getBytes (StandardCharsets .UTF_8 ), iterator .getKey ());
91+ Assert .assertArrayEquals ("1" .getBytes (StandardCharsets .UTF_8 ), iterator .next ().getValue ());
92+ Assert .assertTrue (iterator .hasNext ());
93+
94+ Assert .assertArrayEquals ("2" .getBytes (StandardCharsets .UTF_8 ), iterator .getValue ());
95+ Assert .assertArrayEquals ("2" .getBytes (StandardCharsets .UTF_8 ), iterator .next ().getKey ());
96+ Assert .assertFalse (iterator .hasNext ());
97+
98+ try {
99+ iterator .seekToLast ();
100+ } catch (Exception e ) {
101+ Assert .assertTrue (e instanceof IllegalStateException );
102+ }
101103 }
102-
103- iterator = new RockStoreIterator (db .newIterator (), new ReadOptions ());
104- iterator .seekToLast ();
105- Assert .assertArrayEquals ("2" .getBytes (StandardCharsets .UTF_8 ), iterator .getKey ());
106- Assert .assertArrayEquals ("2" .getBytes (StandardCharsets .UTF_8 ), iterator .getValue ());
107- iterator .seekToFirst ();
108- while (iterator .hasNext ()) {
104+ try (
105+ RockStoreIterator iterator =
106+ new RockStoreIterator (db .newIterator (), new ReadOptions ())) {
107+ iterator .seekToLast ();
108+ Assert .assertArrayEquals ("2" .getBytes (StandardCharsets .UTF_8 ), iterator .getKey ());
109+ Assert .assertArrayEquals ("2" .getBytes (StandardCharsets .UTF_8 ), iterator .getValue ());
110+ iterator .seekToFirst ();
111+ while (iterator .hasNext ()) {
112+ iterator .next ();
113+ }
114+ Assert .assertFalse (iterator .hasNext ());
115+ try {
116+ iterator .getKey ();
117+ } catch (Exception e ) {
118+ Assert .assertTrue (e instanceof IllegalStateException );
119+ }
120+ try {
121+ iterator .getValue ();
122+ } catch (Exception e ) {
123+ Assert .assertTrue (e instanceof IllegalStateException );
124+ }
125+ thrown .expect (NoSuchElementException .class );
109126 iterator .next ();
110127 }
111- Assert .assertFalse (iterator .hasNext ());
112- try {
113- iterator .getKey ();
114- } catch (Exception e ) {
115- Assert .assertTrue (e instanceof IllegalStateException );
116- }
117- try {
118- iterator .getValue ();
119- } catch (Exception e ) {
120- Assert .assertTrue (e instanceof IllegalStateException );
121- }
122- thrown .expect (NoSuchElementException .class );
123- iterator .next ();
124128 }
125129 }
126130
0 commit comments