<commit_before>/*
 * Copyright 2012-2018 the original author or authors.
 *
 * Licensed 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.springframework.boot.autoconfigure.web.embedded;

import java.io.File;
import java.io.IOException;
import java.util.Locale;
import java.util.TimeZone;

import org.eclipse.jetty.server.NCSARequestLog;
import org.eclipse.jetty.server.RequestLog;
import org.junit.Before;
import org.junit.Test;

import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/**
 * Tests for {@link JettyWebServerFactoryCustomizer}.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class JettyWebServerFactoryCustomizerTests {

	private MockEnvironment environment;

	private ServerProperties serverProperties;

	private JettyWebServerFactoryCustomizer customizer;

	@Before
	public void setup() {
		this.environment = new MockEnvironment();
		this.serverProperties = new ServerProperties();
		ConfigurationPropertySources.attach(this.environment);
		this.customizer = new JettyWebServerFactoryCustomizer(this.environment,
				this.serverProperties);
	}

	@Test
	public void deduceUseForwardHeaders() {
		this.environment.setProperty("DYNO", "-");
		ConfigurableJettyWebServerFactory factory = mock(
				ConfigurableJettyWebServerFactory.class);
		this.customizer.customize(factory);
		verify(factory).setUseForwardHeaders(true);
	}

	@Test
	public void defaultUseForwardHeaders() {
		ConfigurableJettyWebServerFactory factory = mock(
				ConfigurableJettyWebServerFactory.class);
		this.customizer.customize(factory);
		verify(factory).setUseForwardHeaders(false);
	}

	@Test
	public void accessLogCanBeCustomized() throws IOException {
		File logFile = File.createTempFile("jetty_log", ".log");
		String timezone = TimeZone.getDefault().getID();
		bind("server.jetty.accesslog.enabled=true",
				"server.jetty.accesslog.filename=" + logFile.getAbsolutePath(),
				"server.jetty.accesslog.file-date-format=yyyy-MM-dd",
				"server.jetty.accesslog.retention-period=42",
				"server.jetty.accesslog.append=true",
				"server.jetty.accesslog.extended-format=true",
				"server.jetty.accesslog.date-format=HH:mm:ss",
				"server.jetty.accesslog.locale=en_BE",
				"server.jetty.accesslog.time-zone=" + timezone,
				"server.jetty.accesslog.log-cookies=true",
				"server.jetty.accesslog.log-server=true",
				"server.jetty.accesslog.log-latency=true");
		JettyWebServer server = customizeAndGetServer();
		NCSARequestLog requestLog = getNCSARequestLog(server);
		assertThat(requestLog.getFilename()).isEqualTo(logFile.getAbsolutePath());
		assertThat(requestLog.getFilenameDateFormat()).isEqualTo("yyyy-MM-dd");
		assertThat(requestLog.getRetainDays()).isEqualTo(42);
		assertThat(requestLog.isAppend()).isTrue();
		assertThat(requestLog.isExtended()).isTrue();
		assertThat(requestLog.getLogDateFormat()).isEqualTo("HH:mm:ss");
		assertThat(requestLog.getLogLocale()).isEqualTo(new Locale("en", "BE"));<commit_msg>Escape backslashes so they are preserved during Properties parsing<commit_after>/*
 * Copyright 2012-2018 the original author or authors.
 *
 * Licensed 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.springframework.boot.autoconfigure.web.embedded;

import java.io.File;
import java.io.IOException;
import java.util.Locale;
import java.util.TimeZone;

import org.eclipse.jetty.server.NCSARequestLog;
import org.eclipse.jetty.server.RequestLog;
import org.junit.Before;
import org.junit.Test;

import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/**
 * Tests for {@link JettyWebServerFactoryCustomizer}.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class JettyWebServerFactoryCustomizerTests {

	private MockEnvironment environment;

	private ServerProperties serverProperties;

	private JettyWebServerFactoryCustomizer customizer;

	@Before
	public void setup() {
		this.environment = new MockEnvironment();
		this.serverProperties = new ServerProperties();
		ConfigurationPropertySources.attach(this.environment);
		this.customizer = new JettyWebServerFactoryCustomizer(this.environment,
				this.serverProperties);
	}

	@Test
	public void deduceUseForwardHeaders() {
		this.environment.setProperty("DYNO", "-");
		ConfigurableJettyWebServerFactory factory = mock(
				ConfigurableJettyWebServerFactory.class);
		this.customizer.customize(factory);
		verify(factory).setUseForwardHeaders(true);
	}

	@Test
	public void defaultUseForwardHeaders() {
		ConfigurableJettyWebServerFactory factory = mock(
				ConfigurableJettyWebServerFactory.class);
		this.customizer.customize(factory);
		verify(factory).setUseForwardHeaders(false);
	}

	@Test
	public void accessLogCanBeCustomized() throws IOException {
		File logFile = File.createTempFile("jetty_log", ".log");
		String timezone = TimeZone.getDefault().getID();
		bind("server.jetty.accesslog.enabled=true",
				"server.jetty.accesslog.filename="
						+ logFile.getAbsolutePath().replace("\\", "\\\\"),
				"server.jetty.accesslog.file-date-format=yyyy-MM-dd",
				"server.jetty.accesslog.retention-period=42",
				"server.jetty.accesslog.append=true",
				"server.jetty.accesslog.extended-format=true",
				"server.jetty.accesslog.date-format=HH:mm:ss",
				"server.jetty.accesslog.locale=en_BE",
				"server.jetty.accesslog.time-zone=" + timezone,
				"server.jetty.accesslog.log-cookies=true",
				"server.jetty.accesslog.log-server=true",
				"server.jetty.accesslog.log-latency=true");
		JettyWebServer server = customizeAndGetServer();
		NCSARequestLog requestLog = getNCSARequestLog(server);
		assertThat(requestLog.getFilename()).isEqualTo(logFile.getAbsolutePath());
		assertThat(requestLog.getFilenameDateFormat()).isEqualTo("yyyy-MM-dd");
		assertThat(requestLog.getRetainDays()).isEqualTo(42);
		assertThat(requestLog.isAppend()).isTrue();
		assertThat(requestLog.isExtended()).isTrue();
		assertThat(requestLog.getLogDateFormat()).isEqualTo("HH:mm:ss");
		assertThat(requestLog.getLogLocale()).isEqualTo(new Locale("en", "BE"));
{'fraction_non_alphanumeric': 0.11405488891529049, 'fraction_numerical': 0.0038018296305096827, 'mean_word_length': 5.739791833466773, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6278882', 'n_tokens_mistral': 2636, 'n_tokens_neox': 2436, 'n_words': 465}