3 import org
.junit
.ClassRule
;
6 import org
.junit
.rules
.TemporaryFolder
;
7 import org
.rocksdb
.util
.Environment
;
9 import java
.io
.IOException
;
11 import static java
.nio
.file
.Files
.readAllBytes
;
12 import static java
.nio
.file
.Paths
.get
;
13 import static org
.assertj
.core
.api
.Assertions
.assertThat
;
15 public class InfoLogLevelTest
{
18 public static final RocksMemoryResource rocksMemoryResource
=
19 new RocksMemoryResource();
22 public TemporaryFolder dbFolder
= new TemporaryFolder();
25 public void testInfoLogLevel() throws RocksDBException
,
27 try (final RocksDB db
=
28 RocksDB
.open(dbFolder
.getRoot().getAbsolutePath())) {
29 db
.put("key".getBytes(), "value".getBytes());
30 db
.flush(new FlushOptions().setWaitForFlush(true));
31 assertThat(getLogContentsWithoutHeader()).isNotEmpty();
36 public void testFatalLogLevel() throws RocksDBException
,
38 try (final Options options
= new Options().
39 setCreateIfMissing(true).
40 setInfoLogLevel(InfoLogLevel
.FATAL_LEVEL
);
41 final RocksDB db
= RocksDB
.open(options
,
42 dbFolder
.getRoot().getAbsolutePath())) {
43 assertThat(options
.infoLogLevel()).
44 isEqualTo(InfoLogLevel
.FATAL_LEVEL
);
45 db
.put("key".getBytes(), "value".getBytes());
46 // As InfoLogLevel is set to FATAL_LEVEL, here we expect the log
47 // content to be empty.
48 assertThat(getLogContentsWithoutHeader()).isEmpty();
53 public void testFatalLogLevelWithDBOptions()
54 throws RocksDBException
, IOException
{
55 try (final DBOptions dbOptions
= new DBOptions().
56 setInfoLogLevel(InfoLogLevel
.FATAL_LEVEL
);
57 final Options options
= new Options(dbOptions
,
58 new ColumnFamilyOptions()).
59 setCreateIfMissing(true);
61 RocksDB
.open(options
, dbFolder
.getRoot().getAbsolutePath())) {
62 assertThat(dbOptions
.infoLogLevel()).
63 isEqualTo(InfoLogLevel
.FATAL_LEVEL
);
64 assertThat(options
.infoLogLevel()).
65 isEqualTo(InfoLogLevel
.FATAL_LEVEL
);
66 db
.put("key".getBytes(), "value".getBytes());
67 assertThat(getLogContentsWithoutHeader()).isEmpty();
71 @Test(expected
= IllegalArgumentException
.class)
72 public void failIfIllegalByteValueProvided() {
73 InfoLogLevel
.getInfoLogLevel((byte) -1);
77 public void valueOf() {
78 assertThat(InfoLogLevel
.valueOf("DEBUG_LEVEL")).
79 isEqualTo(InfoLogLevel
.DEBUG_LEVEL
);
83 * Read LOG file contents into String.
85 * @return LOG file contents as String.
86 * @throws IOException if file is not found.
88 private String
getLogContentsWithoutHeader() throws IOException
{
89 final String separator
= Environment
.isWindows() ?
90 "\n" : System
.getProperty("line.separator");
91 final String
[] lines
= new String(readAllBytes(get(
92 dbFolder
.getRoot().getAbsolutePath() + "/LOG"))).split(separator
);
94 int first_non_header
= lines
.length
;
95 // Identify the last line of the header
96 for (int i
= lines
.length
- 1; i
>= 0; --i
) {
97 if (lines
[i
].indexOf("DB pointer") >= 0) {
98 first_non_header
= i
+ 1;
102 StringBuilder builder
= new StringBuilder();
103 for (int i
= first_non_header
; i
< lines
.length
; ++i
) {
104 builder
.append(lines
[i
]).append(separator
);
106 return builder
.toString();