<commit_before>/*
 * $Id$
 *
 * 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.tiles.portlet.wildcard;

import junit.framework.TestCase;

import org.apache.tiles.portlet.wildcard.WildcardPortletTilesApplicationContext;
import org.easymock.EasyMock;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Set;
import java.util.Vector;
import java.util.HashSet;

import javax.portlet.PortletContext;


/**
 * Tests {@link WildcardPortletTilesApplicationContext}.
 *
 * @version $Rev$ $Date$
 */
public class WildcardPortletTilesApplicationContextTest extends TestCase {

    /**
     * Number of properties container inside the test.properties file.
     */
    private static final int TEST_PROPERTIES_SIZE = 3;

    /**
     * Number of test classes.
     */
    private static final int TEST_TEST_SIZE = 1;

    /**
     * The root Tiles application context.
     */
    private PortletContext portletContext;

    /**
     * The enhanced Tiles application context.
     */
    private WildcardPortletTilesApplicationContext context;

    /**
     * The original class loader.
     */
    private ClassLoader original;

    /** {@inheritDoc} */
    @Override
    public void setUp() {
        portletContext = EasyMock.createMock(PortletContext.class);
        original = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(new MockClassLoader());
        } catch (MalformedURLException e) {
            throw new RuntimeException("Error when using the mock classloader");
        }
        context = new WildcardPortletTilesApplicationContext(portletContext);
    }

    /** {@inheritDoc} */
    @Override
    protected void tearDown() throws Exception {
        Thread.currentThread().setContextClassLoader(original);
    }

    /**
     * Tests resource getting.
     *
     * @throws IOException If something goes wrong.
     */
    public void testGetResources() throws IOException {
        String url = "test.properties";
        HashSet<URL> set = new HashSet<URL>();
        URL u = new URL("file://tiles/test.properties");
        set.add(u);
        EasyMock.expect(portletContext.getResource("/" + url)).andReturn(u)
                .anyTimes();
        File dir = new File(".");
        EasyMock.expect(portletContext.getResource("/WEB-INF/")).andReturn(
                dir.toURI().toURL());
        URL pomUrl = new URL("file://tiles/pom.xml");
        EasyMock.expect(portletContext.getResource("/WEB-INF/pom.xml"))
                .andReturn(pomUrl);
        Set<String> elementSet = new HashSet<String>();
        elementSet.add("/WEB-INF/pom.xml");
        EasyMock.expect(portletContext.getResourcePaths("/WEB-INF/")).andReturn(elementSet);
        EasyMock.replay(portletContext);

        assertEquals(u, context.getResource("/" + url));
        assertEquals(pomUrl, context.getResource("/WEB-INF/*.xml"));
        assertEquals(TEST_PROPERTIES_SIZE, context.getResources(
                "classpath*:/test.properties").size());

        assertEquals(TEST_TEST_SIZE, context.getResources(
                "classpath*:/org/apache/tiles/portlet/context/wildcard/*Test.class").size());
        EasyMock.verify(portletContext);<commit_msg>TILES-458 Refactored tiles-portlet-wildcard test.<commit_after>/*
 * $Id$
 *
 * 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.tiles.portlet.wildcard;

import junit.framework.TestCase;

import org.easymock.EasyMock;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Set;
import java.util.Vector;
import java.util.HashSet;

import javax.portlet.PortletContext;


/**
 * Tests {@link WildcardPortletTilesApplicationContext}.
 *
 * @version $Rev$ $Date$
 */
public class WildcardPortletTilesApplicationContextTest extends TestCase {

    /**
     * Number of properties container inside the test.properties file.
     */
    private static final int TEST_PROPERTIES_SIZE = 3;

    /**
     * Number of test classes.
     */
    private static final int TEST_TEST_SIZE = 1;

    /**
     * The root Tiles application context.
     */
    private PortletContext portletContext;

    /**
     * The enhanced Tiles application context.
     */
    private WildcardPortletTilesApplicationContext context;

    /**
     * The original class loader.
     */
    private ClassLoader original;

    /** {@inheritDoc} */
    @Override
    public void setUp() {
        portletContext = EasyMock.createMock(PortletContext.class);
        original = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(new MockClassLoader());
        } catch (MalformedURLException e) {
            throw new RuntimeException("Error when using the mock classloader");
        }
        context = new WildcardPortletTilesApplicationContext(portletContext);
    }

    /** {@inheritDoc} */
    @Override
    protected void tearDown() throws Exception {
        Thread.currentThread().setContextClassLoader(original);
    }

    /**
     * Tests resource getting.
     *
     * @throws IOException If something goes wrong.
     */
    public void testGetResources() throws IOException {
        String url = "test.properties";
        HashSet<URL> set = new HashSet<URL>();
        URL u = new URL("file://tiles/test.properties");
        set.add(u);
        EasyMock.expect(portletContext.getResource("/" + url)).andReturn(u)
                .anyTimes();
        File dir = new File(".");
        EasyMock.expect(portletContext.getResource("/WEB-INF/")).andReturn(
                dir.toURI().toURL());
        URL pomUrl = new URL("file://tiles/pom.xml");
        EasyMock.expect(portletContext.getResource("/WEB-INF/pom.xml"))
                .andReturn(pomUrl);
        Set<String> elementSet = new HashSet<String>();
        elementSet.add("/WEB-INF/pom.xml");
        EasyMock.expect(portletContext.getResourcePaths("/WEB-INF/")).andReturn(elementSet);
        EasyMock.replay(portletContext);

        assertEquals(u, context.getResource("/" + url));
        assertEquals(pomUrl, context.getResource("/WEB-INF/*.xml"));
        assertEquals(TEST_PROPERTIES_SIZE, context.getResources(
                "classpath*:/test.properties").size());

        assertEquals(TEST_TEST_SIZE, context.getResources(
                "classpath*:/org/apache/tiles/portlet/wildcard/*Test.class").size());
        EasyMock.verify(portletContext);
{'fraction_non_alphanumeric': 0.11015596217610217, 'fraction_numerical': 0.001842072946088665, 'mean_word_length': 3.047713717693837, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, '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': '3027227', 'n_tokens_mistral': 2244, 'n_tokens_neox': 2185, 'n_words': 584}