ramanathan1504 commented on code in PR #4473:
URL: https://github.com/apache/logging-log4j2/pull/4473#discussion_r4573246515


##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderCreateOnDemandTest.java:
##########
@@ -0,0 +1,221 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.log4j.core.appender;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.NullConfiguration;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.layout.PatternLayout;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.apache.logging.log4j.test.ListStatusListener;
+import org.apache.logging.log4j.test.junit.UsingStatusListener;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+/**
+ * Tests the {@code createOnDemand} attribute of {@link 
MemoryMappedFileAppender}.
+ *
+ * @see <a 
href="https://github.com/apache/logging-log4j2/issues/3083";>#3083</a>
+ */

Review Comment:
   ```suggestion
   ```



##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileAppenderCreateOnDemandTest.java:
##########
@@ -0,0 +1,229 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.log4j.core.appender.rolling;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.PosixFilePermissions;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender;
+import org.apache.logging.log4j.core.config.NullConfiguration;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.layout.PatternLayout;
+import org.apache.logging.log4j.core.util.FileUtils;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.apache.logging.log4j.test.ListStatusListener;
+import org.apache.logging.log4j.test.junit.UsingStatusListener;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+/**
+ * Tests the {@code createOnDemand} attribute of {@link 
RollingRandomAccessFileAppender}.
+ *
+ * @see <a 
href="https://github.com/apache/logging-log4j2/issues/3083";>#3083</a>
+ */

Review Comment:
   ```suggestion
   ```



##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/RandomAccessFileAppenderCreateOnDemandTest.java:
##########
@@ -0,0 +1,198 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.log4j.core.appender;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.NullConfiguration;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.layout.PatternLayout;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.apache.logging.log4j.test.ListStatusListener;
+import org.apache.logging.log4j.test.junit.UsingStatusListener;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+/**
+ * Tests the {@code createOnDemand} attribute of {@link 
RandomAccessFileAppender}.
+ *
+ * @see <a 
href="https://github.com/apache/logging-log4j2/issues/3083";>#3083</a>
+ */

Review Comment:
   ```suggestion
   ```



##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/RandomAccessFileAppenderCreateOnDemandTest.java:
##########
@@ -0,0 +1,198 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.log4j.core.appender;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.config.NullConfiguration;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.layout.PatternLayout;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.apache.logging.log4j.test.ListStatusListener;
+import org.apache.logging.log4j.test.junit.UsingStatusListener;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+/**
+ * Tests the {@code createOnDemand} attribute of {@link 
RandomAccessFileAppender}.
+ *
+ * @see <a 
href="https://github.com/apache/logging-log4j2/issues/3083";>#3083</a>
+ */
+@UsingStatusListener
+class RandomAccessFileAppenderCreateOnDemandTest {
+
+    private static final String MESSAGE = "Hello Log4j";
+    private static final String OLD_CONTENT = "Old content, which is longer 
than the new message.";
+
+    private static RandomAccessFileAppender.Builder<?> newBuilder(
+            final Path logFile, final boolean append, final boolean 
createOnDemand) {
+        return RandomAccessFileAppender.newBuilder()
+                .setName("RandomAccessFileAppenderCreateOnDemandTest")
+                .setFileName(logFile.toString())
+                .setAppend(append)
+                .setCreateOnDemand(createOnDemand)
+                
.setLayout(PatternLayout.newBuilder().withPattern("%m").build())
+                .setConfiguration(new NullConfiguration());
+    }
+
+    private static RandomAccessFileAppender createAppender(
+            final Path logFile, final boolean append, final boolean 
createOnDemand) {
+        final RandomAccessFileAppender appender =
+                newBuilder(logFile, append, createOnDemand).build();
+        assertThat(appender).isNotNull();
+        appender.start();
+        return appender;
+    }
+
+    private static LogEvent createEvent(final String message) {
+        return Log4jLogEvent.newBuilder()
+                .setLoggerName("test")
+                .setLevel(Level.INFO)
+                .setMessage(new SimpleMessage(message))
+                // the appender flushes the manager at the end of a batch
+                .setEndOfBatch(true)
+                .build();
+    }
+
+    private static String contentOf(final Path file) throws Exception {
+        return new String(Files.readAllBytes(file), UTF_8);
+    }
+
+    private static void assertNoStatusWarnings(final ListStatusListener 
listener) {
+        assertThat(listener.findStatusData(Level.WARN))
+                .extracting(data -> data.getMessage().getFormattedMessage())
+                .isEmpty();
+    }
+
+    @Test
+    void fileIsNotCreatedBeforeFirstEvent(@TempDir final Path tempDir, final 
ListStatusListener listener)
+            throws Exception {
+        final Path logDir = tempDir.resolve("onDemand");
+        final Path logFile = logDir.resolve("test.log");
+
+        final RandomAccessFileAppender appender = createAppender(logFile, 
true, true);
+        try {
+            assertThat(logDir).doesNotExist();
+            assertThat(logFile).doesNotExist();
+
+            appender.append(createEvent(MESSAGE));
+            assertThat(logFile).exists();
+        } finally {
+            appender.stop();
+        }
+
+        assertThat(contentOf(logFile)).isEqualTo(MESSAGE);
+        assertNoStatusWarnings(listener);
+    }
+
+    @Test
+    void fileIsNotCreatedWithoutEvents(@TempDir final Path tempDir, final 
ListStatusListener listener) {
+        final Path logDir = tempDir.resolve("noEvents");
+        final Path logFile = logDir.resolve("test.log");
+
+        final RandomAccessFileAppender appender = createAppender(logFile, 
true, true);
+        appender.stop();
+
+        assertThat(logDir).doesNotExist();
+        assertThat(logFile).doesNotExist();
+        assertNoStatusWarnings(listener);
+    }
+
+    @Test
+    void fileIsNotCreatedByEmptyEvent(@TempDir final Path tempDir, final 
ListStatusListener listener) {
+        final Path logDir = tempDir.resolve("emptyEvent");
+        final Path logFile = logDir.resolve("test.log");
+
+        final RandomAccessFileAppender appender = createAppender(logFile, 
true, true);
+        try {
+            // Nothing is written, but the manager is flushed.

Review Comment:
   ```suggestion
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to