<commit_before>package com.cedarsoftware.ncube

import com.cedarsoftware.ncube.util.CdnClassLoader
import org.junit.After
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Ignore
import org.junit.Test
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import java.lang.reflect.Field

import static com.cedarsoftware.ncube.NCubeAppContext.getNcubeRuntime
import static org.junit.Assert.*

/**
 * @author <NAME> (<EMAIL>)
 *         <br/>
 *         Copyright (c) Cedar Software LLC
 *         <br/><br/>
 *         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
 *         <br/><br/>
 *         http://www.apache.org/licenses/LICENSE-2.0
 *         <br/><br/>
 *         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.
 */
class TestL3Cache extends NCubeCleanupBaseTest
{
    private NCube cp
    private NCube proto

    private NCube testCube
    private File sourcesDir
    private File classesDir
    private File singleDir
    private String savedDomains

    private static File targetDir
    private static String savedSourcesDir
    private static String savedClassesDir

    private static final Logger LOG = LoggerFactory.getLogger(TestL3Cache.class)

    @BeforeClass
    static void init()
    {
        targetDir = new File ('target')
        assertTrue(targetDir.exists() && targetDir.isDirectory())
        savedSourcesDir = GroovyBase.generatedSourcesDirectory
        savedClassesDir = CdnClassLoader.generatedClassesDirectory
    }

    @Before
    void setUp()
    {
        savedDomains = ncubeRuntime.systemParams[NCUBE_ACCEPTED_DOMAINS]

        sourcesDir = new File("${targetDir.path}/TestL3Cache-sources")
        classesDir = new File("${targetDir.path}/TestL3Cache-classes")
        singleDir = new File ("${targetDir.path}/single-l3cache")

        reloadCubes()

        clearDirectory(sourcesDir)
        clearDirectory(classesDir)
        clearDirectory(singleDir)

        configureDirectories(sourcesDir.path,classesDir.path)
    }

    private void clearDirectory(File dir) {
        if (dir.exists()) {
            assertTrue('directory should be purged', dir.deleteDir())
        }
        assertFalse('directory should not already exist', dir.exists())
    }

    @After
    void tearDown()
    {
        GroovyBase.generatedSourcesDirectory = savedSourcesDir
        CdnClassLoader.generatedClassesDirectory = savedClassesDir
        if (savedDomains)
        {
            ncubeRuntime.systemParams[NCUBE_ACCEPTED_DOMAINS] = savedDomains
        }
        else
        {
            ncubeRuntime.systemParams.remove(NCUBE_ACCEPTED_DOMAINS)
        }
        super.teardown()
    }

    /**
     * Test verifies that the sources and classes directory will be created
     * with the appropriate *.groovy and *.class files
     */
    @Test
    void testCreateCache()
    {
        clearDirectory(sourcesDir)
        clearDirectory(classesDir)

        assertFalse(sourcesDir.exists())
        assertFalse(classesDir.exists())
        assertTrue(getLoadedClasses().isEmpty())

        Map output = [:]
        testCube.getCell([name:'simple'],output)

        Class expClass = output.simple
        assertTrue(expClass.simpleName.startsWith('N_'))
        assertTrue(sourcesDir.exists())
        assertTrue(classesDir.exists())
        verifySourceAndClassFilesExistence(expClass)
        assertEquals(expClass.name,findLoadedClass(expClass).name)
    }

    /**
     * Test verifies that classes generated/loaded for cell expressions are cleared with the cache
     * and then are re-loaded from the L3 cache location
     */
    @Test
    void testClearCache()
    {
        // load the expression initially
        Map output = [:]
        testCube.getCell([name:'simple'],output)
        Class expClass = output.simple

        verifySourceAndClassFilesExistence(expClass)
        assertEquals(expClass.name,findLoadedClass(expClass).name)

        // clear the cache, but make sure l3 cache still exists
        reloadCubes()
        assertTrue(getLoadedClasses().isEmpty())
        verifyClassFileExistence(expClass)

        // clear the source file in order to detect if a recompile occurs or class loaded from l3 cache
        clearDirectory(sourcesDir)

        // re-execute the cube and ensure class was reloaded
        output.clear()
        testCube.getCell([name:'simple'],output)
        assertEquals(expClass.name,findLoadedClass(expClass).name)
        assertFalse(sourcesDir.exists())
    }

    /**
     * Tests that NCube compile() generates sources and classes for all expressions
     * that are defined as cells or meta properties
     */
    @Test
    void testCompile()
    {
        testCube.compile()

        // exercise ncube in a variety of ways to invoke cells and meta properties
        Map output = [:]
        testCube.getCell([name:'simple'],output)
        testCube.getCell([useRule:true],output)
        testCube.extractMetaPropertyValue(testCube.getMetaProperty('metaTest'),[:],output)
        Axis nameAxis = testCube.getAxis('name')
        testCube.extractMetaPropertyValue(nameAxis.getMetaProperty('metaTest'),[:],output)
        testCube.extractMetaPropertyValue(nameAxis.findColumn('simple').getMetaProperty('metaTest'),[:],output)

        // validate sources/classes have been created for the expressions
        verifySourceAndClassFilesExistence(output.metaCube as Class)
        verifySourceAndClassFilesExistence(output.metaAxis as Class)
        verifySourceAndClassFilesExistence(output.metaColumn as Class)
        verifySourceAndClassFilesExistence(output.metaRule as Class)
        verifySourceAndClassFilesExistence(output.simple as Class)
    }

    /**
     * Test verifies that inner classes are supported
     */
    @Test<commit_msg>Updated class loader and accepted domains for testCompile<commit_after>package com.cedarsoftware.ncube

import com.cedarsoftware.ncube.util.CdnClassLoader
import org.junit.After
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Ignore
import org.junit.Test
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import java.lang.reflect.Field

import static com.cedarsoftware.ncube.NCubeAppContext.getNcubeRuntime
import static org.junit.Assert.*

/**
 * @author <NAME> (<EMAIL>)
 *         <br/>
 *         Copyright (c) Cedar Software LLC
 *         <br/><br/>
 *         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
 *         <br/><br/>
 *         http://www.apache.org/licenses/LICENSE-2.0
 *         <br/><br/>
 *         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.
 */
class TestL3Cache extends NCubeCleanupBaseTest
{
    private NCube cp
    private NCube proto

    private NCube testCube
    private File sourcesDir
    private File classesDir
    private File singleDir
    private String savedDomains

    private static File targetDir
    private static String savedSourcesDir
    private static String savedClassesDir

    private static final Logger LOG = LoggerFactory.getLogger(TestL3Cache.class)

    @BeforeClass
    static void init()
    {
        targetDir = new File ('target')
        assertTrue(targetDir.exists() && targetDir.isDirectory())
        savedSourcesDir = GroovyBase.generatedSourcesDirectory
        savedClassesDir = CdnClassLoader.generatedClassesDirectory
    }

    @Before
    void setUp()
    {
        savedDomains = ncubeRuntime.systemParams[NCUBE_ACCEPTED_DOMAINS]

        sourcesDir = new File("${targetDir.path}/TestL3Cache-sources")
        classesDir = new File("${targetDir.path}/TestL3Cache-classes")
        singleDir = new File ("${targetDir.path}/single-l3cache")

        reloadCubes()

        clearDirectory(sourcesDir)
        clearDirectory(classesDir)
        clearDirectory(singleDir)

        configureDirectories(sourcesDir.path,classesDir.path)
    }

    private void clearDirectory(File dir) {
        if (dir.exists()) {
            assertTrue('directory should be purged', dir.deleteDir())
        }
        assertFalse('directory should not already exist', dir.exists())
    }

    @After
    void tearDown()
    {
        GroovyBase.generatedSourcesDirectory = savedSourcesDir
        CdnClassLoader.generatedClassesDirectory = savedClassesDir
        if (savedDomains)
        {
            ncubeRuntime.systemParams[NCUBE_ACCEPTED_DOMAINS] = savedDomains
        }
        else
        {
            ncubeRuntime.systemParams.remove(NCUBE_ACCEPTED_DOMAINS)
        }
        super.teardown()
    }

    /**
     * Test verifies that the sources and classes directory will be created
     * with the appropriate *.groovy and *.class files
     */
    @Test
    void testCreateCache()
    {
        clearDirectory(sourcesDir)
        clearDirectory(classesDir)

        assertFalse(sourcesDir.exists())
        assertFalse(classesDir.exists())
        assertTrue(getLoadedClasses().isEmpty())

        Map output = [:]
        testCube.getCell([name:'simple'],output)

        Class expClass = output.simple
        assertTrue(expClass.simpleName.startsWith('N_'))
        assertTrue(sourcesDir.exists())
        assertTrue(classesDir.exists())
        verifySourceAndClassFilesExistence(expClass)
        assertEquals(expClass.name,findLoadedClass(expClass).name)
    }

    /**
     * Test verifies that classes generated/loaded for cell expressions are cleared with the cache
     * and then are re-loaded from the L3 cache location
     */
    @Test
    void testClearCache()
    {
        // load the expression initially
        Map output = [:]
        testCube.getCell([name:'simple'],output)
        Class expClass = output.simple

        verifySourceAndClassFilesExistence(expClass)
        assertEquals(expClass.name,findLoadedClass(expClass).name)

        // clear the cache, but make sure l3 cache still exists
        reloadCubes()
        assertTrue(getLoadedClasses().isEmpty())
        verifyClassFileExistence(expClass)

        // clear the source file in order to detect if a recompile occurs or class loaded from l3 cache
        clearDirectory(sourcesDir)

        // re-execute the cube and ensure class was reloaded
        output.clear()
        testCube.getCell([name:'simple'],output)
        assertEquals(expClass.name,findLoadedClass(expClass).name)
        assertFalse(sourcesDir.exists())
    }

    /**
     * Tests that NCube compile() generates sources and classes for all expressions
     * that are defined as cells or meta properties
     */
    @Test
    void testCompile()
    {
        ncubeRuntime.systemParams[NCUBE_ACCEPTED_DOMAINS] = 'org.apache.'
        reloadCubes('sys.classpath.L3Cache.json')

        testCube.compile()

        // exercise ncube in a variety of ways to invoke cells and meta properties
        Map output = [:]
        testCube.getCell([name:'simple'],output)
        testCube.getCell([useRule:true],output)
        testCube.extractMetaPropertyValue(testCube.getMetaProperty('metaTest'),[:],output)
        Axis nameAxis = testCube.getAxis('name')
        testCube.extractMetaPropertyValue(nameAxis.getMetaProperty('metaTest'),[:],output)
        testCube.extractMetaPropertyValue(nameAxis.findColumn('simple').getMetaProperty('metaTest'),[:],output)

        // validate sources/classes have been created for the expressions
        verifySourceAndClassFilesExistence(output.metaCube as Class)
        verifySourceAndClassFilesExistence(output.metaAxis as Class)
        verifySourceAndClassFilesExistence(output.metaColumn as Class)
        verifySourceAndClassFilesExistence(output.metaRule as Class)
        verifySourceAndClassFilesExistence(output.simple as Class)
    }

    /**
     * Test verifies that inner classes are supported
     */
    @Test
{'fraction_non_alphanumeric': 0.08437375745526839, 'fraction_numerical': 0.002306163021868787, 'mean_word_length': 2.854121973643886, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '3225077', 'n_tokens_mistral': 3376, 'n_tokens_neox': 3298, 'n_words': 847}