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 assertThat(getLogContentsWithoutHeader()).isNotEmpty();
35 public void testFatalLogLevel() throws RocksDBException
,
37 try (final Options options
= new Options().
38 setCreateIfMissing(true).
39 setInfoLogLevel(InfoLogLevel
.FATAL_LEVEL
);
40 final RocksDB db
= RocksDB
.open(options
,
41 dbFolder
.getRoot().getAbsolutePath())) {
42 assertThat(options
.infoLogLevel()).
43 isEqualTo(InfoLogLevel
.FATAL_LEVEL
);
44 db
.put("key".getBytes(), "value".getBytes());
45 // As InfoLogLevel is set to FATAL_LEVEL, here we expect the log
46 // content to be empty.
47 assertThat(getLogContentsWithoutHeader()).isEmpty();
52 public void testFatalLogLevelWithDBOptions()
53 throws RocksDBException
, IOException
{
54 try (final DBOptions dbOptions
= new DBOptions().
55 setInfoLogLevel(InfoLogLevel
.FATAL_LEVEL
);
56 final Options options
= new Options(dbOptions
,
57 new ColumnFamilyOptions()).
58 setCreateIfMissing(true);
60 RocksDB
.open(options
, dbFolder
.getRoot().getAbsolutePath())) {
61 assertThat(dbOptions
.infoLogLevel()).
62 isEqualTo(InfoLogLevel
.FATAL_LEVEL
);
63 assertThat(options
.infoLogLevel()).
64 isEqualTo(InfoLogLevel
.FATAL_LEVEL
);
65 db
.put("key".getBytes(), "value".getBytes());
66 assertThat(getLogContentsWithoutHeader()).isEmpty();
70 @Test(expected
= IllegalArgumentException
.class)
71 public void failIfIllegalByteValueProvided() {
72 InfoLogLevel
.getInfoLogLevel((byte) -1);
76 public void valueOf() {
77 assertThat(InfoLogLevel
.valueOf("DEBUG_LEVEL")).
78 isEqualTo(InfoLogLevel
.DEBUG_LEVEL
);
82 * Read LOG file contents into String.
84 * @return LOG file contents as String.
85 * @throws IOException if file is not found.
87 private String
getLogContentsWithoutHeader() throws IOException
{
88 final String separator
= Environment
.isWindows() ?
89 "\n" : System
.getProperty("line.separator");
90 final String
[] lines
= new String(readAllBytes(get(
91 dbFolder
.getRoot().getAbsolutePath() + "/LOG"))).split(separator
);
93 int first_non_header
= lines
.length
;
94 // Identify the last line of the header
95 for (int i
= lines
.length
- 1; i
>= 0; --i
) {
96 if (lines
[i
].indexOf("Options.") >= 0 && lines
[i
].indexOf(':') >= 0) {
97 first_non_header
= i
+ 1;
101 StringBuilder builder
= new StringBuilder();
102 for (int i
= first_non_header
; i
< lines
.length
; ++i
) {
103 builder
.append(lines
[i
]).append(separator
);
105 return builder
.toString();