]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/test/java/org/rocksdb/InfoLogLevelTest.java
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / java / src / test / java / org / rocksdb / InfoLogLevelTest.java
1 package org.rocksdb;
2
3 import org.junit.ClassRule;
4 import org.junit.Rule;
5 import org.junit.Test;
6 import org.junit.rules.TemporaryFolder;
7 import org.rocksdb.util.Environment;
8
9 import java.io.IOException;
10
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;
14
15 public class InfoLogLevelTest {
16
17 @ClassRule
18 public static final RocksMemoryResource rocksMemoryResource =
19 new RocksMemoryResource();
20
21 @Rule
22 public TemporaryFolder dbFolder = new TemporaryFolder();
23
24 @Test
25 public void testInfoLogLevel() throws RocksDBException,
26 IOException {
27 try (final RocksDB db =
28 RocksDB.open(dbFolder.getRoot().getAbsolutePath())) {
29 db.put("key".getBytes(), "value".getBytes());
30 assertThat(getLogContentsWithoutHeader()).isNotEmpty();
31 }
32 }
33
34 @Test
35 public void testFatalLogLevel() throws RocksDBException,
36 IOException {
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();
48 }
49 }
50
51 @Test
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);
59 final RocksDB db =
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();
67 }
68 }
69
70 @Test(expected = IllegalArgumentException.class)
71 public void failIfIllegalByteValueProvided() {
72 InfoLogLevel.getInfoLogLevel((byte) -1);
73 }
74
75 @Test
76 public void valueOf() {
77 assertThat(InfoLogLevel.valueOf("DEBUG_LEVEL")).
78 isEqualTo(InfoLogLevel.DEBUG_LEVEL);
79 }
80
81 /**
82 * Read LOG file contents into String.
83 *
84 * @return LOG file contents as String.
85 * @throws IOException if file is not found.
86 */
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);
92
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;
98 break;
99 }
100 }
101 StringBuilder builder = new StringBuilder();
102 for (int i = first_non_header; i < lines.length; ++i) {
103 builder.append(lines[i]).append(separator);
104 }
105 return builder.toString();
106 }
107 }