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

📄 servertest.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Christopher Kohlhaas                                  |
 |                                                                          |
 | This program is free software; you can redistribute it and/or modify     |
 | it under the terms of the GNU General Public License as published by the |
 | Free Software Foundation. A copy of the license has been included with   |
 | these distribution in the COPYING file, if not go to www.fsf.org         |
 |                                                                          |
 | As a special exception, you are granted the permissions to link this     |
 | program with every library, which license fulfills the Open Source       |
 | Definition as published by the Open Source Initiative (OSI).             |
 *--------------------------------------------------------------------------*/
package org.rapla;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.Locale;

import org.rapla.components.util.DateTools;
import org.rapla.entities.Category;
import org.rapla.entities.DependencyException;
import org.rapla.entities.Entity;
import org.rapla.entities.User;
import org.rapla.entities.configuration.CalendarModelConfiguration;
import org.rapla.entities.configuration.Preferences;
import org.rapla.entities.configuration.RaplaMap;
import org.rapla.entities.domain.Allocatable;
import org.rapla.entities.domain.Appointment;
import org.rapla.entities.domain.Permission;
import org.rapla.entities.domain.Repeating;
import org.rapla.entities.domain.Reservation;
import org.rapla.entities.dynamictype.Attribute;
import org.rapla.entities.dynamictype.AttributeType;
import org.rapla.entities.dynamictype.ClassificationFilter;
import org.rapla.entities.dynamictype.ConstraintIds;
import org.rapla.entities.dynamictype.DynamicType;
import org.rapla.entities.dynamictype.DynamicTypeAnnotations;
import org.rapla.facade.ClientFacade;
import org.rapla.framework.RaplaException;
import org.rapla.plugin.weekview.WeekViewFactory;
import org.rapla.server.ServerService;

public class ServerTest extends ServletTestBase {
    ServerService raplaServer;

    protected ClientFacade facade1;
    Locale locale;

    public ServerTest(String name) {
        super(name);
    }

    static public void main(String[] args) {
        String method =
            "testLoad"
            ;
        ServerTest test = new ServerTest( method );
        try {
            test.run();
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }

    protected void setUp() throws Exception {
        super.setUp();
        initTestData();
        // start the server
        raplaServer = (ServerService)
            getContext().lookup(ServerService.ROLE + "/" + getStorageName());
        // start the client service
        facade1 = (ClientFacade)
            getContext().lookup(ClientFacade.ROLE + "/remote-facade");
        facade1.login("homer","duffs".toCharArray());
        locale = Locale.getDefault();
    }

    protected void initTestData() throws Exception{

    }

    protected String getStorageName() {
        return "storage-file";
    }

    protected void tearDown() throws Exception {
        facade1.logout();
        super.tearDown();
    }

    public void testLoad() throws Exception {
        facade1.getAllocatables();
    }

    public void testChangeReservation() throws Exception {
        ClientFacade facade2 = (ClientFacade)
        getContext().lookup(ClientFacade.ROLE + "/remote-facade-2");
        facade2.login("homer","duffs".toCharArray());
        Reservation r1 = facade1.newReservation();
        String typeKey = r1.getClassification().getType().getElementKey();
        r1.getClassification().setValue("name","test-reservation");
        r1.addAppointment( facade1.newAppointment(facade1.today(), new Date()));
        facade1.store( r1 );
        // Wait for the update
        facade2.refresh();

        Reservation r2 = findReservation(facade2, typeKey, "test-reservation");
        assertEquals(1, r2.getAppointments().length);
        assertEquals(0, r2.getAllocatables().length);

        // Modify Reservation in first facade
        Reservation r1clone = (Reservation) facade1.edit( r2);
        r1clone.addAllocatable( facade1.getAllocatables()[0]);
        facade1.store( r1clone );
        // Wait for the update
        facade2.refresh();
        // test for modify in second facade
        assertEquals(1, r2.getAllocatables().length);
        facade2.logout();
    }

    public void testChangeDynamicType() throws Exception {
    	Allocatable allocatable = (Allocatable) facade1.getAllocatables()[0];
        assertEquals(3, allocatable.getClassification().getAttributes().length);
        DynamicType type = (DynamicType) facade1.getDynamicType("room");
        Attribute newAttribute; 
        {
            newAttribute = (Attribute) facade1.newAttribute(AttributeType.CATEGORY);
            DynamicType typeEdit1 = (DynamicType) facade1.edit(type);
            newAttribute.setConstraint(ConstraintIds.KEY_ROOT_CATEGORY, facade1.getUserGroupsCategory());
            newAttribute.setKey("test");
            newAttribute.getName().setName("en","test");
            typeEdit1.addAttribute(newAttribute);
            facade1.store(typeEdit1);
        }

        
        {
            Allocatable newResource =  facade1.newResource();
            newResource.setClassification( type.newClassification());
            newResource.getClassification().setValue("name", "test-resource");
            newResource.getClassification().setValue("test", facade1.getUserGroupsCategory().getCategories()[0]);
            facade1.store(newResource);
        }

        {
            ClientFacade facade2 = (ClientFacade)
            getContext().lookup(ClientFacade.ROLE + "/remote-facade-2");
                facade2.login("homer","duffs".toCharArray());
            //Dyn
            DynamicType typeInSecondFacade = (DynamicType) facade2.getDynamicType("room");
            Attribute att = (Attribute)typeInSecondFacade.getAttribute("test");
            assertEquals( "test",att.getKey());
            assertEquals( AttributeType.CATEGORY,att.getType());
            assertEquals( facade2.getUserGroupsCategory(),att.getConstraint(ConstraintIds.KEY_ROOT_CATEGORY));
            
            ClassificationFilter filter = typeInSecondFacade.newClassificationFilter();
            filter.addEqualsRule("name", "test-resource");
            Allocatable newResource =facade2.getAllocatables( filter.toArray())[0];
            Category userGroup = (Category)newResource.getClassification().getValue("test"); 
            assertEquals( "Category attribute value is not stored",facade2.getUserGroupsCategory().getCategories()[0].getKey(), userGroup.getKey());
            facade2.logout();
        }

        
        assertEquals(4, allocatable.getClassification().getAttributes().length);
        DynamicType typeEdit2 = (DynamicType) facade1.edit(type);
        Attribute attributeLater =  typeEdit2.getAttribute("test");
        assertTrue("Attributes identy changed after storing ", attributeLater.equals(newAttribute));
        typeEdit2.removeAttribute(attributeLater);
        facade1.store(typeEdit2);
        assertEquals( facade1.getAllocatables().length, 5);
        assertEquals(3, allocatable.getClassification().getAttributes().length);

        User user = facade1.newUser();
        user.setUsername("test-user");
        facade1.store( user );

        removeAnAttribute();
        // Wait for the update
        {
            ClientFacade facade2 = (ClientFacade)
            getContext().lookup(ClientFacade.ROLE + "/remote-facade-2");
                facade2.login("homer","duffs".toCharArray());
                facade2.getUser("test-user");
            facade2.logout();
        }
    }

    public void removeAnAttribute() throws Exception {
        DynamicType typeEdit3 = (DynamicType) facade1.edit(facade1.getDynamicType("room"));
        typeEdit3.removeAttribute( typeEdit3.getAttribute("belongsto"));
        Allocatable allocatable = (Allocatable) facade1.getAllocatables()[0];
        assertEquals("erwin", allocatable.getName( locale ) );

        Entity allocatableClone = facade1.edit(allocatable);
        assertEquals(3, allocatable.getClassification().getAttributes().length);
        facade1.storeObjects(new Entity[] {allocatableClone, typeEdit3});
        assertEquals( 5, facade1.getAllocatables().length);
        assertEquals(2, allocatable.getClassification().getAttributes().length);
        
        
        ClientFacade facade2 = (ClientFacade)
        getContext().lookup(ClientFacade.ROLE + "/remote-facade-2");
        facade2.login("homer","duffs".toCharArray());
        // we check if the store affectes the second client.

⌨️ 快捷键说明

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