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

📄 exportservicetest.java

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

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

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.SearchResult;
import opiam.admin.faare.exception.ServiceException;
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.StandardService;
import opiam.admin.faare.service.services.exports.ExportsService;
import opiam.admin.faare.unittests.TestResult;
import opiam.admin.faare.unittests.TestUtils;

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

import org.apache.log4j.Logger;

import java.io.StringWriter;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;


/**
 * Test of the exports service.
 */
public class ExportServiceTest extends TestCase
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(ExportServiceTest.class);

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

    /** Test user DN. */
    private static final String DN_USER =
        "uid=jreuter, ou=People, dc=mycompany,dc=com";

    /** Test user login. */
    private static final String LOGIN_USER = "jreuter"; //uid

    /** Test user password. */
    private static final String PASSWORD_USER = "destroy";

    /** User context. */
    private static UserContext _USERCONTEXT = new UserContext();

    /** Header format. */
    private static final String HEADER_FORMAT =
        "!!First Name - Last Name - Mail address!!";

    /** Column Header format. */
    private static final String HEADERCOL_FORMAT =
        "!!First Name - Last Name - Mail address;;!!";

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

        try
        {
            PropertiesManager.getInstance();

            if (!_USERCONTEXT.isLoggedIn())
            {
                StandardService.logon(LOGIN_USER, PASSWORD_USER, _USERCONTEXT);
            }
        }
        catch (Exception e)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("ExportServiceTest 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 ExportServiceTest("testBegin"));

        // TESTS SET
        suite.addTest(new ExportServiceTest("testExportMatrixLineContent"));
        suite.addTest(new ExportServiceTest("testExportMatrixHeader"));
        suite.addTest(new ExportServiceTest("testExportMatrix"));
        suite.addTest(new ExportServiceTest("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()
    {
        //  To initialize the tests statement
        TestResult.getInstance().setInitiator("ExportServiceTest");

        _test.displayHeaderClass("ExportServiceTest");
        assertTrue(true);
    }

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

        // To display the tests statement
        _test.displayTestStatement("ExportServiceTest");
    }

    /**
     * Tests the exportMatrix method with the lineContent configuration.
     *
     */
    public static void testExportMatrixLineContent()
    {
        _test.displayHeaderMethod("testExportMatrixLineContent");

        try
        {
            StringWriter sw = new StringWriter();

            // recherche les entr閑s
            Criteria c1 = Criteria.getAndInstance();

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

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

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

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

                _test.displayMsg(TestUtils.NEXT +
                                 "Parameters = exportName : exportCsvLineContent, entries : " +
                                 entries.size()
                                );
                ExportsService.exportMatrix("exportCsvLineContent", entries,
                                            sw, _USERCONTEXT
                                           );
                _test.displayMsg(sw.toString());

                StringTokenizer exportResult =
                    new StringTokenizer(sw.toString(),
                                        System.getProperty("line.separator")
                                       );

                if (exportResult.countTokens() == (entries.size() + 1))
                {
                    _test.displayResultSuccess(null);
                }
                else
                {
                    _test.displayResultFailure(null);
                }
            }
            else
            {
                _test.displayResultFailure("No entry found");
            }
        }
        catch (ServiceException se)
        {
            _test.displayResultFailure(se.toString());
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        try
        {
            StringWriter sw = new StringWriter();

            // recherche les entr閑s
            Criteria c1 = Criteria.getAndInstance();

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

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

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

            if (begEntries != null)
            {
                List entries = begEntries.getLResults();
                _test.displayMsg(TestUtils.NEXT +
                                 "Parameters = exportName : exportCsvLineContentHeader, entries : " +
                                 entries.size()
                                );
                ExportsService.exportMatrix("exportCsvLineContentHeader",
                                            entries, sw, _USERCONTEXT
                                           );
                _test.displayMsg(sw.toString());

                StringTokenizer exportResult =
                    new StringTokenizer(sw.toString(),
                                        System.getProperty("line.separator")
                                       );

                if (exportResult.countTokens() == (entries.size() + 1))
                {
                    _test.displayResultSuccess(null);
                }
                else
                {
                    _test.displayResultFailure(null);
                }
            }
            else
            {
                _test.displayResultFailure("No entry found");
            }
        }
        catch (ServiceException se)
        {
            _test.displayResultFailure(se.toString());
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testExportMatrixLineContent");
    }

    /**
     * Tests the exportMatrix method only for the header.
     *
     */
    public static void testExportMatrixHeader()
    {
        _test.displayHeaderMethod("testExportMatrixHeader");

        try
        {
            StringWriter sw = new StringWriter();

            // recherche les entr閑s
            Criteria c1 = Criteria.getAndInstance();

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

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

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

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

                // exportHeader test
                try
                {
                    _test.displayMsg(TestUtils.NEXT +
                                     "Parameters = exportName : exportHeader, entries : " +
                                     entries.size()
                                    );
                    ExportsService.exportMatrix("exportHeader", entries, sw,
                                                _USERCONTEXT
                                               );

                    StringTokenizer st =
                        new StringTokenizer(sw.toString(),
                                            System.getProperty("line.separator")
                                           );
                    _test.compareValues(HEADERCOL_FORMAT, st.nextToken());
                }
                catch (ServiceException se)
                {
                    _test.displayResultFailure(se.toString());
                }

                // exportHeaderColumns test
                try
                {
                    _test.displayMsg(TestUtils.NEXT +
                                     "Parameters = exportName : exportHeaderColumns, entries : " +
                                     entries.size()
                                    );
                    sw = new StringWriter();
                    ExportsService.exportMatrix("exportHeaderColumns", entries,
                                                sw, _USERCONTEXT
                                               );

                    StringTokenizer st =
                        new StringTokenizer(sw.toString(),
                                            System.getProperty("line.separator")
                                           );
                    _test.compareValues(HEADERCOL_FORMAT, st.nextToken());
                }
                catch (ServiceException se)
                {
                    _test.displayResultFailure(se.toString());
                }

                // exportHeaderAttributeColumns test
                try
                {
                    _test.displayMsg(TestUtils.NEXT +
                                     "Parameters = exportName : exportHeaderAttributeColumns, entries : " +
                                     entries.size()
                                    );
                    sw = new StringWriter();
                    ExportsService.exportMatrix("exportHeaderAttributeColumns",
                                                entries, sw, _USERCONTEXT
                                               );

                    StringTokenizer st =
                        new StringTokenizer(sw.toString(),
                                            System.getProperty("line.separator")
                                           );
                    _test.compareValues(HEADERCOL_FORMAT, st.nextToken());
                }
                catch (ServiceException se)
                {
                    _test.displayResultFailure(se.toString());
                }

                // exportHeaderAttributeCell test
                try
                {
                    _test.displayMsg(TestUtils.NEXT +
                                     "Parameters = exportName : exportHeaderAttributeCell, entries : " +
                                     entries.size()
                                    );
                    sw = new StringWriter();
                    ExportsService.exportMatrix("exportHeaderAttributeCell",
                                                entries, sw, _USERCONTEXT
                                               );

                    StringTokenizer st =
                        new StringTokenizer(sw.toString(),
                                            System.getProperty("line.separator")
                                           );
                    _test.compareValues(HEADER_FORMAT, st.nextToken());
                }
                catch (ServiceException se)
                {
                    _test.displayResultFailure(se.toString());

⌨️ 快捷键说明

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