1 package org.codehaus.plexus.redback.rbac.jdo;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import java.io.IOException;
23 import java.io.StringReader;
24 import java.io.StringWriter;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import java.util.List;
28 import java.util.Locale;
30 import javax.xml.stream.XMLStreamException;
32 import junit.framework.TestCase;
34 import org.codehaus.plexus.redback.rbac.Operation;
35 import org.codehaus.plexus.redback.rbac.Resource;
36 import org.codehaus.plexus.redback.rbac.jdo.io.stax.RbacJdoModelStaxReader;
37 import org.codehaus.plexus.redback.rbac.jdo.io.stax.RbacJdoModelStaxWriter;
40 * Test the StAX reader and writer generated.
42 public class RbacJdoModelStaxTest
45 @SuppressWarnings("unchecked")
46 public void testStax()
47 throws IOException, XMLStreamException
49 RbacDatabase database = new RbacDatabase();
51 JdoRole role = new JdoRole();
52 role.setAssignable( true );
53 role.setDescription( "descriptor" );
54 role.setName( "name" );
55 role.setPermanent( true );
56 role.addChildRoleName( "childRole1" );
57 role.addChildRoleName( "childRole2" );
59 JdoPermission permission = new JdoPermission();
60 permission.setDescription( "permDesc" );
61 permission.setName( "permName" );
63 JdoOperation operation = new JdoOperation();
64 operation.setDescription( "opDesc" );
65 operation.setName( "opName" );
66 operation.setPermanent( true );
67 operation.setResourceRequired( true );
68 permission.setOperation( operation );
69 database.addOperation( operation );
71 JdoResource resource = new JdoResource();
72 resource.setIdentifier( "resId" );
73 resource.setPattern( true );
74 resource.setPermanent( true );
75 permission.setResource( resource );
76 database.addResource( resource );
77 permission.setPermanent( true );
78 role.addPermission( permission );
79 database.addPermission( permission );
81 database.addRole( role );
83 JdoUserAssignment assignment = new JdoUserAssignment();
84 assignment.setPermanent( false );
85 assignment.setPrincipal( "principal" );
86 assignment.setTimestamp( new Date() );
87 assignment.addRoleName( "name" );
89 database.addUserAssignment( assignment );
91 StringWriter w = new StringWriter();
92 new RbacJdoModelStaxWriter().write( w, database );
94 RbacDatabase newDatabase = new RbacJdoModelStaxReader().read( new StringReader( w.toString() ) );
96 List<JdoRole> expectedRoles = database.getRoles();
97 List<JdoRole> roles = newDatabase.getRoles();
98 assertEquals( expectedRoles.size(), roles.size() );
99 for ( JdoRole r : roles )
101 boolean found = false;
102 for ( JdoRole expectedRole : expectedRoles )
104 if ( expectedRole.getName().equals( r.getName() ) )
108 assertRole( expectedRole, r );
113 fail( "Couldn't find role: " + r.getName() );
117 List<JdoUserAssignment> expectedUserAssignments = database.getUserAssignments();
118 List<JdoUserAssignment> userAssignments = newDatabase.getUserAssignments();
119 assertEquals( expectedUserAssignments.size(), userAssignments.size() );
120 for ( JdoUserAssignment a : userAssignments )
122 boolean found = false;
123 for ( JdoUserAssignment expectedAssignment : expectedUserAssignments )
125 if ( expectedAssignment.getPrincipal().equals( a.getPrincipal() ) )
129 assertUserAssignment( expectedAssignment, a );
134 fail( "Couldn't find assignment: " + a.getPrincipal() );
139 @SuppressWarnings("unchecked")
140 private void assertRole( JdoRole expectedRole, JdoRole role )
142 assertEquals( expectedRole.getDescription(), role.getDescription() );
143 assertPermissions( expectedRole.getPermissions(), role.getPermissions() );
144 assertEquals( expectedRole.getChildRoleNames(), role.getChildRoleNames() );
147 private void assertUserAssignment( JdoUserAssignment expectedAssignment, JdoUserAssignment assignment )
149 SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", Locale.US );
150 assertNotNull( expectedAssignment.getTimestamp() );
151 assertNotNull( assignment.getTimestamp() );
153 assertEquals( sdf.format( expectedAssignment.getTimestamp() ), sdf.format( assignment.getTimestamp() ) );
154 assertEquals( expectedAssignment.getRoleNames(), assignment.getRoleNames() );
157 private void assertPermissions( List<JdoPermission> expectedPermissions, List<JdoPermission> permissions )
159 assertEquals( expectedPermissions.size(), permissions.size() );
160 for ( JdoPermission permission : permissions )
162 boolean found = false;
163 for ( JdoPermission expectedPermission : expectedPermissions )
165 if ( expectedPermission.getName().equals( permission.getName() ) )
169 assertPermission( expectedPermission, permission );
174 fail( "Couldn't find permission: " + permission.getName() );
179 private void assertPermission( JdoPermission expectedPermission, JdoPermission permission )
181 assertEquals( expectedPermission.getDescription(), permission.getDescription() );
182 assertOperation( expectedPermission.getOperation(), permission.getOperation() );
183 assertResource( expectedPermission.getResource(), permission.getResource() );
186 private void assertResource( Resource expectedResource, Resource resource )
188 assertEquals( expectedResource.getIdentifier(), resource.getIdentifier() );
191 private void assertOperation( Operation expectedOperation, Operation operation )
193 assertEquals( expectedOperation.getName(), operation.getName() );
194 assertEquals( expectedOperation.getDescription(), operation.getDescription() );