This is an automated email from the ASF dual-hosted git repository. juanpablo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/jspwiki.git
commit acd1af63c6c2493eb355e1b8b2e41e0c0eb4a29c Author: Juan Pablo Santos RodrÃguez <[email protected]> AuthorDate: Thu Jan 13 11:35:25 2022 +0100 more code formatting --- .../wiki/auth/authorize/XMLGroupDatabase.java | 31 +- .../wiki/auth/authorize/XMLGroupDatabaseTest.java | 316 ++++++++++----------- 2 files changed, 167 insertions(+), 180 deletions(-) diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/authorize/XMLGroupDatabase.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/authorize/XMLGroupDatabase.java index e8f8ea4..3ce77ce 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/auth/authorize/XMLGroupDatabase.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/authorize/XMLGroupDatabase.java @@ -18,6 +18,7 @@ */ package org.apache.wiki.auth.authorize; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringEscapeUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -122,8 +123,7 @@ public class XMLGroupDatabase implements GroupDatabase { * the commit did not succeed */ @Override - public void delete( final Group group ) throws WikiSecurityException - { + public void delete( final Group group ) throws WikiSecurityException { final String index = group.getName(); final boolean exists = m_groups.containsKey( index ); @@ -148,8 +148,7 @@ public class XMLGroupDatabase implements GroupDatabase { * @throws WikiSecurityException if the groups cannot be returned by the back-end */ @Override - public Group[] groups() throws WikiSecurityException - { + public Group[] groups() throws WikiSecurityException { buildDOM(); final Collection<Group> groups = m_groups.values(); return groups.toArray( new Group[0] ); @@ -161,8 +160,7 @@ public class XMLGroupDatabase implements GroupDatabase { * whose key is {@link #PROP_DATABASE}. * @param engine the wiki engine * @param props the properties used to initialize the group database - * @throws NoRequiredPropertyException if the user database cannot be - * located, parsed, or opened + * @throws NoRequiredPropertyException if the user database cannot be located, parsed, or opened * @throws WikiSecurityException if the database could not be initialized successfully */ @Override @@ -215,8 +213,7 @@ public class XMLGroupDatabase implements GroupDatabase { final String index = group.getName(); final boolean isNew = !( m_groups.containsKey( index ) ); final Date modDate = new Date( System.currentTimeMillis() ); - if ( isNew ) - { + if( isNew ) { // If new, set created info group.setCreated( modDate ); group.setCreator( modifier.getName() ); @@ -237,19 +234,21 @@ public class XMLGroupDatabase implements GroupDatabase { factory.setExpandEntityReferences( false ); factory.setIgnoringComments( true ); factory.setNamespaceAware( false ); + //factory.setAttribute( XMLConstants.ACCESS_EXTERNAL_DTD, "" ); + //factory.setAttribute( XMLConstants.ACCESS_EXTERNAL_SCHEMA, "" ); try { m_dom = factory.newDocumentBuilder().parse( m_file ); log.debug( "Database successfully initialized" ); m_lastModified = m_file.lastModified(); m_lastCheck = System.currentTimeMillis(); } catch( final ParserConfigurationException e ) { - log.error( "Configuration error: " + e.getMessage() ); + log.error( "Configuration error: {}", e.getMessage() ); } catch( final SAXException e ) { - log.error( "SAX error: " + e.getMessage() ); + log.error( "SAX error: {}", e.getMessage() ); } catch( final FileNotFoundException e ) { log.info( "Group database not found; creating from scratch..." ); } catch( final IOException e ) { - log.error( "IO error: " + e.getMessage() ); + log.error( "IO error: {}", e.getMessage() ); } if ( m_dom == null ) { try { @@ -264,16 +263,14 @@ public class XMLGroupDatabase implements GroupDatabase { } // Ok, now go and read this sucker in - if ( m_dom != null ) { + if( m_dom != null ) { final NodeList groupNodes = m_dom.getElementsByTagName( GROUP_TAG ); for( int i = 0; i < groupNodes.getLength(); i++ ) { final Element groupNode = (Element) groupNodes.item( i ); final String groupName = groupNode.getAttribute( GROUP_NAME ); - if ( groupName == null ) { - log.warn( "Detected null group name in XMLGroupDataBase. Check your group database." ); - } - else - { + if( StringUtils.isEmpty( groupName ) ) { + log.warn( "Detected null or empty group name in XMLGroupDataBase. Check your group database." ); + } else { final Group group = buildGroup( groupNode, groupName ); m_groups.put( groupName, group ); } diff --git a/jspwiki-main/src/test/java/org/apache/wiki/auth/authorize/XMLGroupDatabaseTest.java b/jspwiki-main/src/test/java/org/apache/wiki/auth/authorize/XMLGroupDatabaseTest.java index e0ee706..6323458 100644 --- a/jspwiki-main/src/test/java/org/apache/wiki/auth/authorize/XMLGroupDatabaseTest.java +++ b/jspwiki-main/src/test/java/org/apache/wiki/auth/authorize/XMLGroupDatabaseTest.java @@ -18,9 +18,6 @@ */ package org.apache.wiki.auth.authorize; -import java.security.Principal; -import java.util.Properties; - import org.apache.wiki.TestEngine; import org.apache.wiki.WikiEngine; import org.apache.wiki.api.exceptions.WikiException; @@ -31,189 +28,182 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.security.Principal; +import java.util.Properties; + /** + * */ -public class XMLGroupDatabaseTest -{ +public class XMLGroupDatabaseTest { - private XMLGroupDatabase m_db; + private XMLGroupDatabase m_db; - private String m_wiki; + private String m_wiki; - /** - * - */ @BeforeEach public void setUp() throws Exception { - final Properties props = TestEngine.getTestProperties(); - final WikiEngine engine = new TestEngine( props ); - m_db = new XMLGroupDatabase(); - m_db.initialize( engine, props ); - m_wiki = engine.getApplicationName(); + final Properties props = TestEngine.getTestProperties(); + final WikiEngine engine = new TestEngine( props ); + m_db = new XMLGroupDatabase(); + m_db.initialize( engine, props ); + m_wiki = engine.getApplicationName(); } @Test public void testDelete() throws WikiException { - // First, count the number of groups in the db now. - final int oldUserCount = m_db.groups().length; - - // Create a new group with random name - final String name = "TestGroup" + String.valueOf( System.currentTimeMillis() ); - Group group = new Group( name, m_wiki ); - final Principal al = new WikiPrincipal( "Al" ); - final Principal bob = new WikiPrincipal( "Bob" ); - group.add( al ); - group.add( bob ); - m_db.save(group, new WikiPrincipal( "Tester") ); - - // Make sure the profile saved successfully - group = backendGroup( name ); - Assertions.assertEquals( name, group.getName() ); - Assertions.assertEquals( oldUserCount+1, m_db.groups().length ); - - // Now delete the profile; should be back to old count - m_db.delete( group ); - Assertions.assertEquals( oldUserCount, m_db.groups().length ); + // First, count the number of groups in the db now. + final int oldUserCount = m_db.groups().length; + + // Create a new group with random name + final String name = "TestGroup" + System.currentTimeMillis(); + Group group = new Group( name, m_wiki ); + final Principal al = new WikiPrincipal( "Al" ); + final Principal bob = new WikiPrincipal( "Bob" ); + group.add( al ); + group.add( bob ); + m_db.save( group, new WikiPrincipal( "Tester" ) ); + + // Make sure the profile saved successfully + group = backendGroup( name ); + Assertions.assertEquals( name, group.getName() ); + Assertions.assertEquals( oldUserCount + 1, m_db.groups().length ); + + // Now delete the profile; should be back to old count + m_db.delete( group ); + Assertions.assertEquals( oldUserCount, m_db.groups().length ); } @Test public void testGroups() throws WikiSecurityException { - // Test file has 4 groups in it: TV, Literature, Art, and Admin - final Group[] groups = m_db.groups(); - Assertions.assertEquals( 4, groups.length ); - - Group group; - - // Group TV has 3 members - group = backendGroup( "TV" ); - Assertions.assertEquals("TV", group.getName() ); - Assertions.assertEquals( 3, group.members().length ); - - // Group Literature has 2 members - group = backendGroup( "Literature" ); - Assertions.assertEquals("Literature", group.getName() ); - Assertions.assertEquals( 2, group.members().length ); - - // Group Art has no members - group = backendGroup( "Art" ); - Assertions.assertEquals("Art", group.getName() ); - Assertions.assertEquals( 0, group.members().length ); - - // Group Admin has 1 member (Administrator) - group = backendGroup( "Admin" ); - Assertions.assertEquals("Admin", group.getName() ); - Assertions.assertEquals( 1, group.members().length ); - Assertions.assertEquals( "Administrator", group.members()[0].getName() ); - - // Group Archaeology doesn't exist - try - { - group = backendGroup( "Archaeology" ); - // We should never get here - Assertions.fail(); - } - catch (final NoSuchPrincipalException e) - { - Assertions.assertTrue(true); - } + // Test file has 4 groups in it: TV, Literature, Art, and Admin + final Group[] groups = m_db.groups(); + Assertions.assertEquals( 4, groups.length ); + + Group group; + + // Group TV has 3 members + group = backendGroup( "TV" ); + Assertions.assertEquals( "TV", group.getName() ); + Assertions.assertEquals( 3, group.members().length ); + + // Group Literature has 2 members + group = backendGroup( "Literature" ); + Assertions.assertEquals( "Literature", group.getName() ); + Assertions.assertEquals( 2, group.members().length ); + + // Group Art has no members + group = backendGroup( "Art" ); + Assertions.assertEquals( "Art", group.getName() ); + Assertions.assertEquals( 0, group.members().length ); + + // Group Admin has 1 member (Administrator) + group = backendGroup( "Admin" ); + Assertions.assertEquals( "Admin", group.getName() ); + Assertions.assertEquals( 1, group.members().length ); + Assertions.assertEquals( "Administrator", group.members()[ 0 ].getName() ); + + // Group Archaeology doesn't exist + try { + backendGroup( "Archaeology" ); + // We should never get here + Assertions.fail(); + } catch( final NoSuchPrincipalException e ) { + Assertions.assertTrue( true ); + } } @Test public void testSave() throws Exception { - // Create a new group with random name - final String name = "TestGroup" + String.valueOf( System.currentTimeMillis() ); - Group group = new Group( name, m_wiki ); - final Principal al = new WikiPrincipal( "Al" ); - final Principal bob = new WikiPrincipal( "Bob" ); - final Principal cookie = new WikiPrincipal( "Cookie" ); - group.add( al ); - group.add( bob ); - group.add( cookie ); - m_db.save(group, new WikiPrincipal( "Tester" ) ); - - // Make sure the profile saved successfully - group = backendGroup( name ); - Assertions.assertEquals( name, group.getName() ); - Assertions.assertEquals( 3, group.members().length ); - Assertions.assertTrue( group.isMember( new WikiPrincipal( "Al" ) ) ); - Assertions.assertTrue( group.isMember( new WikiPrincipal( "Bob" ) ) ); - Assertions.assertTrue( group.isMember( new WikiPrincipal( "Cookie" ) ) ); - - // The back-end should have timestamped the create/modify fields - Assertions.assertNotNull( group.getCreator() ); - Assertions.assertEquals( "Tester", group.getCreator() ); - Assertions.assertNotNull( group.getCreated() ); - Assertions.assertNotNull( group.getModifier() ); - Assertions.assertEquals( "Tester", group.getModifier() ); - Assertions.assertNotNull( group.getLastModified() ); - Assertions.assertNotSame( group.getCreated(), group.getLastModified() ); - - // Remove the group - m_db.delete( group ); - } + // Create a new group with random name + final String name = "TestGroup" + System.currentTimeMillis(); + Group group = new Group( name, m_wiki ); + final Principal al = new WikiPrincipal( "Al" ); + final Principal bob = new WikiPrincipal( "Bob" ); + final Principal cookie = new WikiPrincipal( "Cookie" ); + group.add( al ); + group.add( bob ); + group.add( cookie ); + m_db.save( group, new WikiPrincipal( "Tester" ) ); + + // Make sure the profile saved successfully + group = backendGroup( name ); + Assertions.assertEquals( name, group.getName() ); + Assertions.assertEquals( 3, group.members().length ); + Assertions.assertTrue( group.isMember( new WikiPrincipal( "Al" ) ) ); + Assertions.assertTrue( group.isMember( new WikiPrincipal( "Bob" ) ) ); + Assertions.assertTrue( group.isMember( new WikiPrincipal( "Cookie" ) ) ); + + // The back-end should have timestamped the create/modify fields + Assertions.assertNotNull( group.getCreator() ); + Assertions.assertEquals( "Tester", group.getCreator() ); + Assertions.assertNotNull( group.getCreated() ); + Assertions.assertNotNull( group.getModifier() ); + Assertions.assertEquals( "Tester", group.getModifier() ); + Assertions.assertNotNull( group.getLastModified() ); + Assertions.assertNotSame( group.getCreated(), group.getLastModified() ); + + // Remove the group + m_db.delete( group ); + } @Test public void testResave() throws Exception { - // Create a new group with random name & 3 members - final String name = "TestGroup" + String.valueOf( System.currentTimeMillis() ); - Group group = new Group( name, m_wiki ); - final Principal al = new WikiPrincipal( "Al" ); - final Principal bob = new WikiPrincipal( "Bob" ); - final Principal cookie = new WikiPrincipal( "Cookie" ); - group.add( al ); - group.add( bob ); - group.add( cookie ); - m_db.save(group, new WikiPrincipal( "Tester" ) ); - - // Make sure the profile saved successfully - group = backendGroup( name ); - Assertions.assertEquals( name, group.getName() ); - - // Modify the members by adding the group; re-add Al while we're at it - final Principal dave = new WikiPrincipal( "Dave" ); - group.add( al ); - group.add( dave ); - m_db.save(group, new WikiPrincipal( "SecondTester" ) ); - - // We should see 4 members and new timestamp info - Principal[] members = group.members(); - Assertions.assertEquals( 4, members.length ); - Assertions.assertNotNull( group.getCreator() ); - Assertions.assertEquals( "Tester", group.getCreator() ); - Assertions.assertNotNull( group.getCreated() ); - Assertions.assertNotNull( group.getModifier() ); - Assertions.assertEquals( "SecondTester", group.getModifier() ); - Assertions.assertNotNull( group.getLastModified() ); - - // Check the back-end; We should see the same thing - group = backendGroup( name ); - members = group.members(); - Assertions.assertEquals( 4, members.length ); - Assertions.assertNotNull( group.getCreator() ); - Assertions.assertEquals( "Tester", group.getCreator() ); - Assertions.assertNotNull( group.getCreated() ); - Assertions.assertNotNull( group.getModifier() ); - Assertions.assertEquals( "SecondTester", group.getModifier() ); - Assertions.assertNotNull( group.getLastModified() ); - - // Remove the group - m_db.delete( group ); - } - - private Group backendGroup(final String name ) throws WikiSecurityException - { - final Group[] groups = m_db.groups(); - for ( int i = 0; i < groups.length; i++ ) - { - final Group group = groups[i]; - if ( group.getName().equals( name ) ) - { - return group; - } - } - throw new NoSuchPrincipalException( "No group named " + name ); - } + // Create a new group with random name & 3 members + final String name = "TestGroup" + System.currentTimeMillis(); + Group group = new Group( name, m_wiki ); + final Principal al = new WikiPrincipal( "Al" ); + final Principal bob = new WikiPrincipal( "Bob" ); + final Principal cookie = new WikiPrincipal( "Cookie" ); + group.add( al ); + group.add( bob ); + group.add( cookie ); + m_db.save( group, new WikiPrincipal( "Tester" ) ); + + // Make sure the profile saved successfully + group = backendGroup( name ); + Assertions.assertEquals( name, group.getName() ); + + // Modify the members by adding the group; re-add Al while we're at it + final Principal dave = new WikiPrincipal( "Dave" ); + group.add( al ); + group.add( dave ); + m_db.save( group, new WikiPrincipal( "SecondTester" ) ); + + // We should see 4 members and new timestamp info + Principal[] members = group.members(); + Assertions.assertEquals( 4, members.length ); + Assertions.assertNotNull( group.getCreator() ); + Assertions.assertEquals( "Tester", group.getCreator() ); + Assertions.assertNotNull( group.getCreated() ); + Assertions.assertNotNull( group.getModifier() ); + Assertions.assertEquals( "SecondTester", group.getModifier() ); + Assertions.assertNotNull( group.getLastModified() ); + + // Check the back-end; We should see the same thing + group = backendGroup( name ); + members = group.members(); + Assertions.assertEquals( 4, members.length ); + Assertions.assertNotNull( group.getCreator() ); + Assertions.assertEquals( "Tester", group.getCreator() ); + Assertions.assertNotNull( group.getCreated() ); + Assertions.assertNotNull( group.getModifier() ); + Assertions.assertEquals( "SecondTester", group.getModifier() ); + Assertions.assertNotNull( group.getLastModified() ); + + // Remove the group + m_db.delete( group ); + } + + private Group backendGroup( final String name ) throws WikiSecurityException { + final Group[] groups = m_db.groups(); + for( final Group group : groups ) { + if( group.getName().equals( name ) ) { + return group; + } + } + throw new NoSuchPrincipalException( "No group named " + name ); + } }
