package com.xpn.xwiki.it;

import java.util.Map;

import junit.framework.TestCase;

import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiConfig;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.cache.api.XWikiCacheService;
import com.xpn.xwiki.cache.impl.OSCacheService;
import com.xpn.xwiki.it.framework.XWikiLDAPTestSetup;
import com.xpn.xwiki.plugin.ldap.XWikiLDAPConnection;
import com.xpn.xwiki.plugin.ldap.XWikiLDAPUtils;
import com.xpn.xwiki.web.XWikiEngineContext;

/**
 * Tests {@link XWikiLDAPUtilsTest}.
 * 
 * @version $Id: $
 */
public class XWikiLDAPUtilsTest extends TestCase
{
    /**
     * The name of the group cache.
     */
    public static final String GROUPCACHE8NAME = "groups";

    /**
     * The LDAP connection tool.
     */
    private XWikiLDAPConnection connection = new XWikiLDAPConnection();

    /**
     * The LDAP tool.
     */
    private XWikiLDAPUtils ldapUtils = new XWikiLDAPUtils(connection);

    /**
     * The XWiki context.
     */
    private XWikiContext context;

    /**
     * {@inheritDoc}
     * 
     * @see junit.framework.TestCase#setUp()
     */
    public void setUp() throws Exception
    {
        this.context = new XWikiContext();

        new XWiki(new XWikiConfig(), this.context)
        {
            private XWikiCacheService cacheService;

            public void initXWiki(XWikiConfig config, XWikiContext context,
                XWikiEngineContext enginecontext, boolean noupdate) throws XWikiException
            {
                context.setWiki(this);
                setConfig(config);
            }

            public XWikiCacheService getCacheService()
            {
                if (this.cacheService == null) {
                    cacheService = new OSCacheService();
                    cacheService.init(this);
                }

                return cacheService;
            }
        };

        this.ldapUtils.setUidAttributeName(XWikiLDAPTestSetup.LDAP_USERUID_FIELD);

        int port = XWikiLDAPTestSetup.getLDAPPort();

        this.connection.open("localhost", port, XWikiLDAPTestSetup.HORATIOHORNBLOWER_DN,
            XWikiLDAPTestSetup.HORATIOHORNBLOWER_PWD, null, false);
    }

    /**
     * Verify if the user uid attribute name has been correctly set.
     */
    public void testGetUidAttributeName()
    {
        assertSame("Wrong uid attribute name", XWikiLDAPTestSetup.LDAP_USERUID_FIELD,
            this.ldapUtils.getUidAttributeName());
    }

    /**
     * Verify that the cache is not created each time it's getted.
     * 
     * @throws XWikiException error when getting the cache.
     */
    public void testGetCache() throws XWikiException
    {
        assertTrue("Cache is recreated",
            this.ldapUtils.getCache(GROUPCACHE8NAME, this.context) == this.ldapUtils.getCache(
                GROUPCACHE8NAME, this.context));
    }

    /**
     * Test {@link XWikiLDAPUtils#getGroupMembers(String, XWikiContext)}.
     * 
     * @throws XWikiException error when getting group members from cache.
     */
    public void testGetGroupMembers() throws XWikiException
    {
        Map members =
            this.ldapUtils.getGroupMembers(XWikiLDAPTestSetup.HMSLYDIA_DN, this.context);

        assertFalse("No member was found", members.isEmpty());

        assertTrue("Wrong members was found", XWikiLDAPTestSetup.HMSLYDIA_MEMBERS.equals(members
            .keySet()));
    }

    /**
     * Test {@link XWikiLDAPUtils#isUserInGroup(String, String, XWikiContext)}.
     * 
     * @throws XWikiException error when getting group members from cache.
     */
    public void testIsUserInGroup() throws XWikiException
    {
        String userDN =
            this.ldapUtils.isUserInGroup(XWikiLDAPTestSetup.HORATIOHORNBLOWER_UID,
                XWikiLDAPTestSetup.HMSLYDIA_DN, this.context);

        assertNotNull("Users not found", userDN);
        assertEquals(XWikiLDAPTestSetup.HORATIOHORNBLOWER_DN, userDN);
    }

    /**
     * {@inheritDoc}
     *
     * @see junit.framework.TestCase#tearDown()
     */
    public void tearDown() throws Exception
    {
        this.connection.close();

        super.tearDown();
    }
}

@@ -207,24 +207,131 @@
 iException;%0A
+import com.xpn.xwiki.cache.api.XWikiCache;%0Aimport com.xpn.xwiki.cache.api.XWikiCacheNeedsRefreshException;%0A
 import com.x
@@ -827,25 +827,25 @@
 g GROUPCACHE
-8
+_
 NAME = %22grou
@@ -2619,22 +2619,21 @@
 %0A     * 
-Verify
+check
  that th
@@ -2676,16 +2676,50 @@
 s getted
+ and correctly handle refresh time
 .%0A     *
@@ -2772,32 +2772,115 @@
 ting the cache.%0A
+     * @throws XWikiCacheNeedsRefreshException%0A     * @throws InterruptedException%0A
      */%0A    publ
@@ -2891,19 +2891,16 @@
 oid test
-Get
 Cache() 
@@ -2924,67 +2924,190 @@
 tion
-%0A    %7B%0A        assertTrue(%22Cache is recreated%22,%0A           
+, XWikiCacheNeedsRefreshException,%0A        InterruptedException%0A    %7B%0A        XWikiCache tmpCache = this.ldapUtils.getCache(GROUPCACHE_NAME, this.context);%0A        XWikiCache cache =
  thi
@@ -3129,33 +3129,33 @@
 Cache(GROUPCACHE
-8
+_
 NAME, this.conte
@@ -3161,83 +3161,499 @@
 ext)
- == this.ldapUtils.getCache(%0A                GROUPCACHE8NAME, this.context)
+;%0A%0A        assertTrue(%22Cache is recreated%22, tmpCache == cache);%0A%0A        cache.putInCache(%22key%22, %22value%22);%0A%0A        String value = (String) cache.getFromCache(%22key%22);%0A%0A        assertEquals(%22Value getted from cache is wrong%22, %22value%22, value);%0A%0A        Thread.sleep(1000);%0A%0A        value = null;%0A        try %7B%0A            value = (String) cache.getFromCache(%22key%22, 1);%0A        %7D catch (XWikiCacheNeedsRefreshException e) %7B%0A%0A        %7D%0A%0A        assertNull(%22Object in cache is not cleaned%22, value
 );%0A 
@@ -4780,16 +4780,17 @@
 %7D%0A     *
+ 
 %0A     * 

add cache refresh time test