⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sortservicetest.java

📁 OPIAM stands for Open Identity and Access Management. This Suite will provide modules for user & rig
💻 JAVA
字号:
/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests.service.services;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.SearchResult;
import opiam.admin.faare.exception.ServiceException;
import opiam.admin.faare.persistence.javabeans.JBTop;
import opiam.admin.faare.service.UserContext;
import opiam.admin.faare.service.javabeans.Criteria;
import opiam.admin.faare.service.javabeans.SearchArgument;
import opiam.admin.faare.service.services.SortService;
import opiam.admin.faare.service.services.StandardService;
import opiam.admin.faare.unittests.TestResult;
import opiam.admin.faare.unittests.TestUtils;
import opiam.admin.faare.unittests.javabeans.PersonTest;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * Test of the SortService methods.
 */
public class SortServiceTest extends TestCase
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(SortServiceTest.class);

    /** Test administrator DN. */
    private static final String DN_ADMIN = "uid=hmiller,ou=People,dc=mycompany,dc=com";

    /** Test administrator login. */
    private static final String LOGIN_ADMIN = "hmiller";

    /** Test administrator password. */
    private static final String PASSWORD_ADMIN = "hillock";

    /** Admin context. */
    private static UserContext _ADMINCONTEXT = new UserContext();

    /* on utilise les noms des attributs d閒inis dans faare_mapping.xml */

    /** Sort attribute. */
    private static String _SORTATTRIBUTE = "nom"; //"sn";

    /** Sort attributes list. */
    private static String[] _SORTATTRIBUTES = {"nom", "prenom"}; //{"sn","givenName"};

    /** Instance of the trace class. */
    private static TestUtils _test = new TestUtils(_logger);

    /**
     * Creates a new SortServiceTest object.
     *
     * @param param  Parameter.
     */
    public SortServiceTest(String param)
    {
        super(param);

        try
        {
            PropertiesManager.getInstance();

            if (!_ADMINCONTEXT.isLoggedIn())
            {
                StandardService.logon(LOGIN_ADMIN, PASSWORD_ADMIN, _ADMINCONTEXT);
            }
        }
        catch (Exception e)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("SortServiceTest constructor error");
            msg.append(TestUtils.NEXT);
            msg.append(e.toString());
            _test.displayResultError(msg.toString());
        }
    }

    /**
     * Method of the tests suite.
     *
     * @return Tests result.
     */
    public static Test suite()
    {
        TestSuite suite = new TestSuite();

        suite.addTest(new SortServiceTest("testBegin"));

        // Set of tests
        suite.addTest(new SortServiceTest("testSortAtt"));
        suite.addTest(new SortServiceTest("testSortAttsArray"));

        suite.addTest(new SortServiceTest("testEnd"));

        return (suite);
    }

    /**
     * Main method of the test class.
     *
     * @param args  Sets of the parameters
     */
    public static void main(String[] args)
    {
        junit.textui.TestRunner.run(suite());
    }

    /**
     * Method to indicate the beginning of the class test.
     *
     */
    public static void testBegin()
    {
        TestResult.getInstance().setInitiator("SortServiceTest");
        _test.displayHeaderClass("SortServiceTest");
        assertTrue(true);
    }

    /**
     * Method to indicate the end of the class test.
     *
     */
    public static void testEnd()
    {
        _test.displayFooterClass(null);
        assertTrue(true);
        _test.displayTestStatement("SortServiceTest");
    }

    /**
     * Tests the sort on one attribute.
     */
    public static void testSortAtt()
    {
        _test.displayHeaderMethod("testSortAtt");

        try
        {
            try
            {
                Criteria c1 = Criteria.getAndInstance();

                // id  -> uid  comme defini dans faare-mapping.xml
                c1.addBeginsWith("id", "a");

                SearchArgument arg = new SearchArgument(c1,
                        _ADMINCONTEXT.findJBRessourceByName("person"),
                        _ADMINCONTEXT);
                List args = new ArrayList();
                args.add(arg);

                StringBuffer filter = new StringBuffer();
                filter.append("SearchArgument : ");
                filter.append(TestUtils.NEXT);
                filter.append(arg.getLdapFilter());
                filter.append(TestUtils.NEXT);
                _test.displayMsg(filter.toString());

                SearchResult begEntries = StandardService.search(args,
                        _ADMINCONTEXT);

                if (begEntries != null)
                {
                    List entries = begEntries.getLResults();

                    if (entries.size() > 0)
                    {
                        Iterator it = entries.iterator();
                        JBTop entry = null;

                        _test.displayMsg("Search result in LDAP order");

                        while (it.hasNext())
                        {
                            entry = (JBTop) it.next();
                            _test.displayMsg("entry.getDn() = " +
                                entry.getDn());
                        }

                        _test.displayMsg(TestUtils.EMPTY);
                        _test.displayMsg("Search result ordered by " +
                            _SORTATTRIBUTE);
                        SortService.sort(entries, PersonTest.class,
                            _SORTATTRIBUTE, _ADMINCONTEXT);

                        it = entries.iterator();

                        while (it.hasNext())
                        {
                            entry = (JBTop) it.next();
                            _test.displayMsg("entry.getDn() = " +
                                entry.getDn());
                        }

                        // checks if the test is ok
                        int i = 0;

                        while ((i < (entries.size() - 1)) &&
                                (((JBTop) entries.get(i++)).getDn().compareTo(((JBTop) entries.get(
                                        i)).getDn()) < 0))
                        {
                        }

                        if (i == (entries.size() - 1))
                        {
                            _test.displayResultSuccess(null);
                        }
                        else
                        {
                            _test.displayResultFailure(null);
                        }
                    }
                    else
                    {
                        _test.displayResultFailure("No entries");
                    }
                }
                else
                {
                    _test.displayResultFailure("SearchResult = null");
                }
            }
            catch (ServiceException e)
            {
                StringBuffer msg = new StringBuffer();
                msg.append("ServiceException : ");
                msg.append(TestUtils.NEXT);
                msg.append(e.toString());
                _test.displayResultError(msg.toString());
            }
        }
        catch (Exception ex)
        {
            StringBuffer msgEx = new StringBuffer();
            msgEx.append("Exception : ");
            msgEx.append(TestUtils.NEXT);
            msgEx.append(ex.toString());
            msgEx.append(TestUtils.NEXT);
            _test.displayResultError(msgEx.toString());
        }

        _test.displayFooterMethod("testSortAtt");
    }

    /**
     * Tests the sort on several attributes.
     *
     */
    public static void testSortAttsArray()
    {
        _test.displayHeaderMethod("testSortAttsArray");

        try
        {
            Criteria c1 = Criteria.getAndInstance();
            c1.addBeginsWith("id", "a");

            SearchArgument arg = new SearchArgument(c1,
                    _ADMINCONTEXT.findJBRessourceByName("person"), _ADMINCONTEXT);
            List args = new ArrayList();
            args.add(arg);

            StringBuffer filter = new StringBuffer();
            filter.append("SearchArgument : ");
            filter.append(TestUtils.NEXT);
            filter.append(arg.getLdapFilter());
            filter.append(TestUtils.NEXT);
            _test.displayMsg(filter.toString());

            SearchResult begEntries = StandardService.search(args, _ADMINCONTEXT);

            if (begEntries != null)
            {
                List entries = begEntries.getLResults();

                if (entries.size() > 0)
                {
                    Iterator it = entries.iterator();
                    JBTop entry = null;

                    _test.displayMsg("Search result in LDAP order");

                    while (it.hasNext())
                    {
                        entry = (JBTop) it.next();
                        _test.displayMsg("entry.getDn() = " + entry.getDn());
                    }

                    _test.displayMsg(TestUtils.EMPTY);

                    StringBuffer msgSort = new StringBuffer();
                    msgSort.append("Search result ordered by ");

                    for (int i = 0; i < _SORTATTRIBUTES.length; i++)
                    {
                        if (i > 0)
                        {
                            msgSort.append(" and ");
                        }

                        msgSort.append(_SORTATTRIBUTES[i]);
                    }

                    _test.displayMsg(msgSort.toString());
                    SortService.sort(entries, PersonTest.class,
                        _SORTATTRIBUTES, _ADMINCONTEXT);

                    it = entries.iterator();

                    while (it.hasNext())
                    {
                        entry = (JBTop) it.next();
                        _test.displayMsg("entry.getDn() = " + entry.getDn());
                    }

                    // checks if the test is ok
                    int i = 0;

                    while ((i < (entries.size() - 1)) &&
                            (((JBTop) entries.get(i++)).getDn().compareTo(((JBTop) entries.get(
                                    i)).getDn()) < 0))
                    {
                    }

                    if (i == (entries.size() - 1))
                    {
                        _test.displayResultSuccess(null);
                    }
                    else
                    {
                        _test.displayResultFailure(null);
                    }
                }
                else
                {
                    _test.displayResultFailure("No entries");
                }
            }
            else
            {
                _test.displayResultFailure("SearchResult = null");
            }
        }
        catch (Exception ex)
        {
            StringBuffer msgEx = new StringBuffer();
            msgEx.append("Exception : ");
            msgEx.append(TestUtils.NEXT);
            msgEx.append(ex.toString());
            _test.displayResultError(msgEx.toString());
        }

        _test.displayFooterMethod("testSortAttsArray");
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -