<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.captcha.internal;

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

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.xwiki.captcha.CaptchaException;
import org.xwiki.container.Container;
import org.xwiki.container.servlet.ServletRequest;
import org.xwiki.container.servlet.ServletResponse;
import org.xwiki.resource.ResourceReferenceHandlerChain;
import org.xwiki.resource.ResourceReferenceHandlerException;
import org.xwiki.test.junit5.mockito.ComponentTest;
import org.xwiki.test.junit5.mockito.InjectMockComponents;
import org.xwiki.test.junit5.mockito.MockComponent;
import org.xwiki.test.mockito.MockitoComponentManager;

import com.octo.captcha.service.CaptchaService;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.octo.captcha.service.sound.SoundCaptchaService;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Validate the behaviour of {@link JCaptchaResourceReferenceHandler}.
 *
 * @version $Id$
 * @since 11.10
 */
@ComponentTest
public class JCaptchaResourceReferenceHandlerTest
{
    private static final String SESSION_ID = "customSessionId";

    private static final Locale LOCALE = new Locale("customLocale");

    @InjectMockComponents
    private JCaptchaResourceReferenceHandler jCaptchaResourceReferenceHandler;

    @MockComponent
    private CaptchaServiceManager captchaServiceManager;

    @Mock
    private HttpServletResponse response;

    @Mock
    private ServletOutputStream outputStream;

    @Mock
    private HttpSession httpSession;

    @Mock
    private ResourceReferenceHandlerChain resourceReferenceHandlerChain;

    @BeforeEach
    public void setup(MockitoComponentManager mockitoComponentManager) throws Exception
    {
        Container container = mockitoComponentManager.getInstance(Container.class);
        ServletRequest request = mock(ServletRequest.class);
        when(container.getRequest()).thenReturn(request);

        ServletResponse servletResponse = mock(ServletResponse.class);
        when(container.getResponse()).thenReturn(servletResponse);
        when(servletResponse.getHttpServletResponse()).thenReturn(response);
        when(response.getOutputStream()).thenReturn(outputStream);

        HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
        when(request.getHttpServletRequest()).thenReturn(httpServletRequest);
        when(httpServletRequest.getLocale()).thenReturn(LOCALE);

        when(httpServletRequest.getSession()).thenReturn(httpSession);
        when(httpSession.getId()).thenReturn(SESSION_ID);
    }

    @Test
    public void handleCaptchaImage() throws ResourceReferenceHandlerException, CaptchaException, IOException
    {
        ImageCaptchaService imageCaptchaService = mock(ImageCaptchaService.class);
        when(captchaServiceManager.getCaptchaService("customImageEngine")).thenReturn(imageCaptchaService);

        JCaptchaResourceReference jCaptchaResourceReference =
            new JCaptchaResourceReference("image", "customImageEngine");
        jCaptchaResourceReferenceHandler.handle(jCaptchaResourceReference, resourceReferenceHandlerChain);

        verify(captchaServiceManager, times(1)).getCaptchaService("customImageEngine");
        verify(httpSession, times(1)).getId();
        verify(imageCaptchaService, times(1)).getImageChallengeForID(SESSION_ID, LOCALE);
        verify(response, times(1)).setContentType("image/jpeg");
        verify(response, times(1)).getOutputStream();
        verify(outputStream, times(1)).write(any());
        verify(resourceReferenceHandlerChain, times(1)).handleNext(jCaptchaResourceReference);
    }

    @Test
    public void handleCaptchaSound() throws ResourceReferenceHandlerException, CaptchaException, IOException
    {
        SoundCaptchaService soundCaptchaService = mock(SoundCaptchaService.class);
        when(captchaServiceManager.getCaptchaService("customSoundEngine")).thenReturn(soundCaptchaService);

        AudioInputStream audioInputStream = mock(AudioInputStream.class);
        when(soundCaptchaService.getSoundChallengeForID(SESSION_ID, LOCALE)).thenReturn(audioInputStream);
        when(audioInputStream.getFormat()).thenReturn(mock(AudioFormat.class));

        JCaptchaResourceReference jCaptchaResourceReference =
            new JCaptchaResourceReference("sound", "customSoundEngine");
        jCaptchaResourceReferenceHandler.handle(jCaptchaResourceReference, resourceReferenceHandlerChain);

        verify(captchaServiceManager, times(1)).getCaptchaService("customSoundEngine");<commit_msg>[Misc] Make the JCaptcha tests work with Java 11<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.captcha.internal;

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

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.xwiki.captcha.CaptchaException;
import org.xwiki.container.Container;
import org.xwiki.container.servlet.ServletRequest;
import org.xwiki.container.servlet.ServletResponse;
import org.xwiki.resource.ResourceReferenceHandlerChain;
import org.xwiki.resource.ResourceReferenceHandlerException;
import org.xwiki.test.junit5.mockito.ComponentTest;
import org.xwiki.test.junit5.mockito.InjectMockComponents;
import org.xwiki.test.junit5.mockito.MockComponent;
import org.xwiki.test.mockito.MockitoComponentManager;

import com.octo.captcha.service.CaptchaService;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.octo.captcha.service.sound.SoundCaptchaService;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Validate the behaviour of {@link JCaptchaResourceReferenceHandler}.
 *
 * @version $Id$
 * @since 11.10
 */
@ComponentTest
public class JCaptchaResourceReferenceHandlerTest
{
    private static final String SESSION_ID = "customSessionId";

    private static final Locale LOCALE = new Locale("customLocale");

    @InjectMockComponents
    private JCaptchaResourceReferenceHandler jCaptchaResourceReferenceHandler;

    @MockComponent
    private CaptchaServiceManager captchaServiceManager;

    @Mock
    private HttpServletResponse response;

    @Mock
    private ServletOutputStream outputStream;

    @Mock
    private HttpSession httpSession;

    @Mock
    private ResourceReferenceHandlerChain resourceReferenceHandlerChain;

    @BeforeEach
    public void setup(MockitoComponentManager mockitoComponentManager) throws Exception
    {
        Container container = mockitoComponentManager.getInstance(Container.class);
        ServletRequest request = mock(ServletRequest.class);
        when(container.getRequest()).thenReturn(request);

        ServletResponse servletResponse = mock(ServletResponse.class);
        when(container.getResponse()).thenReturn(servletResponse);
        when(servletResponse.getHttpServletResponse()).thenReturn(response);
        when(response.getOutputStream()).thenReturn(outputStream);

        HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
        when(request.getHttpServletRequest()).thenReturn(httpServletRequest);
        when(httpServletRequest.getLocale()).thenReturn(LOCALE);

        when(httpServletRequest.getSession()).thenReturn(httpSession);
        when(httpSession.getId()).thenReturn(SESSION_ID);
    }

    @Test
    public void handleCaptchaImage() throws ResourceReferenceHandlerException, CaptchaException, IOException
    {
        ImageCaptchaService imageCaptchaService = mock(ImageCaptchaService.class);
        when(captchaServiceManager.getCaptchaService("customImageEngine")).thenReturn(imageCaptchaService);

        JCaptchaResourceReference jCaptchaResourceReference =
            new JCaptchaResourceReference("image", "customImageEngine");
        jCaptchaResourceReferenceHandler.handle(jCaptchaResourceReference, resourceReferenceHandlerChain);

        verify(captchaServiceManager, times(1)).getCaptchaService("customImageEngine");
        verify(httpSession, times(1)).getId();
        verify(imageCaptchaService, times(1)).getImageChallengeForID(SESSION_ID, LOCALE);
        verify(response, times(1)).setContentType("image/jpeg");
        verify(response, times(1)).getOutputStream();
        verify(outputStream, times(1)).write(any());
        verify(resourceReferenceHandlerChain, times(1)).handleNext(jCaptchaResourceReference);
    }

    @Test
    public void handleCaptchaSound() throws ResourceReferenceHandlerException, CaptchaException, IOException
    {
        SoundCaptchaService soundCaptchaService = mock(SoundCaptchaService.class);
        when(captchaServiceManager.getCaptchaService("customSoundEngine")).thenReturn(soundCaptchaService);

        AudioInputStream audioInputStream = mock(AudioInputStream.class);
        when(soundCaptchaService.getSoundChallengeForID(SESSION_ID, LOCALE)).thenReturn(audioInputStream);
        AudioFormat audioFormat = mock(AudioFormat.class);
        // Specifying a random encoding format as we don't care what encoding is used.
        when(audioFormat.getEncoding()).thenReturn(AudioFormat.Encoding.PCM_SIGNED);
        when(audioInputStream.getFormat()).thenReturn(audioFormat);

        JCaptchaResourceReference jCaptchaResourceReference =
            new JCaptchaResourceReference("sound", "customSoundEngine");
        jCaptchaResourceReferenceHandler.handle(jCaptchaResourceReference, resourceReferenceHandlerChain);

        verify(captchaServiceManager, times(1)).getCaptchaService("customSoundEngine");
{'fraction_non_alphanumeric': 0.0855550169655841, 'fraction_numerical': 0.004685732751656164, 'mean_word_length': 5.131253095591878, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4236810', 'n_tokens_mistral': 3205, 'n_tokens_neox': 3124, 'n_words': 716}