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

📄 authhelpertest.java

📁 anewssystem新闻发布系统集成使用了spring hibernate freemarker EXTJS等开源框架 可以作为学习参考
💻 JAVA
字号:
package anni.core.security;

import java.util.*;

import junit.framework.TestCase;


public class AuthHelperTest extends TestCase {
    @Override
    protected void setUp() {
    }

    @Override
    protected void tearDown() {
    }

    public void testTrue() {
        assertTrue(true);
    }

    public void testSaveAuth() {
        Collection authSet = new HashSet();
        Object authObject = new Object();

        // 以前没有这个权限,就添加
        AuthHelper.saveAuth(authSet, authObject, false);
        assertTrue(authSet.contains(authObject));

        // 以前有了这个权限,就删除
        AuthHelper.saveAuth(authSet, authObject, true);
        assertFalse(authSet.contains(authObject));

        // 想添加,但以前就有这个权限了
        authSet.add(authObject);
        AuthHelper.saveAuth(authSet, authObject, false);
        assertTrue(authSet.contains(authObject));

        // 想删除,但以前没有这个权限
        authSet.remove(authObject);
        AuthHelper.saveAuth(authSet, authObject, true);
        assertFalse(authSet.contains(authObject));
    }

    public void testJudgeAuthorized() {
        TestResource res1 = new TestResource();
        TestResource res2 = new TestResource();

        Collection auths = new HashSet();
        Collection autheds = new HashSet();
        auths.add(res1);
        auths.add(res2);
        autheds.add(res2);

        AuthHelper.judgeAuthorized(auths, autheds);

        assertTrue(res2.authorized);
        assertFalse(res1.authorized);
    }

    public void testCannotJudgeAuthorized() {
        Collection auths = new HashSet();
        Collection autheds = new HashSet();
        auths.add(new TestResource2());

        try {
            AuthHelper.judgeAuthorized(auths, autheds);
            fail();
        } catch (IllegalArgumentException ex) {
            assertTrue(true);
        }
    }

    public class TestResource {
        boolean authorized = false;

        public void setAuthorized(boolean authorizedIn) {
            authorized = authorizedIn;
        }
    }

    // 只有在方法,或类,不是public的情况下才抛
    class TestResource2 {
        boolean authorized = false;

        public void setAuthorized(boolean authorizedIn) {
            authorized = authorizedIn;
        }
    }
}

⌨️ 快捷键说明

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