KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > security > TestCmsPrincipal


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/security/TestCmsPrincipal.java,v $
3  * Date : $Date: 2006/03/27 14:53:03 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31  
32 package org.opencms.security;
33
34 import org.opencms.db.CmsDbEntryNotFoundException;
35 import org.opencms.file.CmsObject;
36 import org.opencms.main.CmsException;
37 import org.opencms.main.OpenCms;
38 import org.opencms.test.OpenCmsTestCase;
39 import org.opencms.test.OpenCmsTestProperties;
40
41 import junit.extensions.TestSetup;
42 import junit.framework.Test;
43 import junit.framework.TestSuite;
44
45 /**
46  * Tests for <code>{@link org.opencms.security.CmsPrincipal}</code> (and it's subclasses).<p>
47  */

48 public class TestCmsPrincipal extends OpenCmsTestCase {
49
50     /**
51      * Default JUnit constructor.<p>
52      *
53      * @param arg0 JUnit parameters
54      */

55     public TestCmsPrincipal(String JavaDoc arg0) {
56         
57         super(arg0);
58     }
59     
60     /**
61      * Test suite for this test class.<p>
62      *
63      * @return the test suite
64      */

65     public static Test suite() {
66         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
67         
68         TestSuite suite = new TestSuite();
69         suite.setName(TestCmsPrincipal.class.getName());
70
71         suite.addTest(new TestCmsPrincipal("testBasicReadOperation"));
72         
73         TestSetup wrapper = new TestSetup(suite) {
74             
75             protected void setUp() {
76                 setupOpenCms("simpletest", "/sites/default/");
77             }
78             
79             protected void tearDown() {
80                 removeOpenCms();
81             }
82         };
83         
84         return wrapper;
85     }
86     
87     /**
88      * Tests basic principal read operation.<p>
89      *
90      * @throws Exception if the test fails
91      */

92     public void testBasicReadOperation() throws Exception JavaDoc {
93
94         echo("Testing basic principal read operation");
95         CmsObject cms = getCmsObject();
96
97         I_CmsPrincipal principal;
98         String JavaDoc prefixedName;
99
100         prefixedName = CmsPrincipal.getPrefixedUser(OpenCms.getDefaultUsers().getUserAdmin());
101         principal = CmsPrincipal.readPrefixedPrincipal(cms, prefixedName);
102         assertTrue(principal.isUser());
103         assertFalse(principal.isGroup());
104         assertEquals(prefixedName, principal.getPrefixedName());
105
106         prefixedName = CmsPrincipal.getPrefixedGroup(OpenCms.getDefaultUsers().getGroupAdministrators());
107         principal = CmsPrincipal.readPrefixedPrincipal(cms, prefixedName);
108         assertFalse(principal.isUser());
109         assertTrue(principal.isGroup());
110         assertEquals(prefixedName, principal.getPrefixedName());
111         
112         // negative test
113
prefixedName = "kaputt";
114         CmsException caught = null;
115         try {
116             principal = CmsPrincipal.readPrefixedPrincipal(cms, prefixedName);
117         } catch (CmsException e) {
118             caught = e;
119         }
120         assertNotNull(caught);
121         assertTrue(caught instanceof CmsSecurityException);
122         assertSame(Messages.ERR_INVALID_PRINCIPAL_1, caught.getMessageContainer().getKey());
123         
124         // negative test 2
125
prefixedName = CmsPrincipal.getPrefixedUser("kaputt");
126         caught = null;
127         try {
128             principal = CmsPrincipal.readPrefixedPrincipal(cms, prefixedName);
129         } catch (CmsException e) {
130             caught = e;
131         }
132         assertNotNull(caught);
133         assertTrue(caught instanceof CmsDbEntryNotFoundException);
134         assertSame(org.opencms.db.Messages.ERR_READ_USER_FOR_NAME_1, caught.getMessageContainer().getKey());
135         
136         // negative test 3
137
prefixedName = CmsPrincipal.getPrefixedGroup("kaputt");
138         caught = null;
139         try {
140             principal = CmsPrincipal.readPrefixedPrincipal(cms, prefixedName);
141         } catch (CmsException e) {
142             caught = e;
143         }
144         assertNotNull(caught);
145         assertTrue(caught instanceof CmsDbEntryNotFoundException);
146         assertSame(org.opencms.db.Messages.ERR_READ_GROUP_FOR_NAME_1, caught.getMessageContainer().getKey());
147
148     }
149 }
Popular Tags