You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2022/01/13 12:42:39 UTC
[jspwiki] 11/15: same for JDBC user database
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 2bc8d773cecd17a07058dfa2149e748f2af37cf0
Author: Juan Pablo Santos RodrÃguez <ju...@gmail.com>
AuthorDate: Thu Jan 13 13:35:34 2022 +0100
same for JDBC user database
---
.../apache/wiki/auth/user/JDBCUserDatabase.java | 47 ++---
.../wiki/auth/user/JDBCUserDatabaseTest.java | 220 ++++++++-------------
2 files changed, 99 insertions(+), 168 deletions(-)
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/user/JDBCUserDatabase.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/user/JDBCUserDatabase.java
index a9ecf6c..c939b47 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/auth/user/JDBCUserDatabase.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/user/JDBCUserDatabase.java
@@ -310,7 +310,7 @@ public class JDBCUserDatabase extends AbstractUserDatabase {
* @param loginName the login name of the user profile that shall be deleted
*/
@Override
- public void deleteByLoginName( final String loginName ) throws NoSuchPrincipalException, WikiSecurityException {
+ public void deleteByLoginName( final String loginName ) throws WikiSecurityException {
// Get the existing user; if not found, throws NoSuchPrincipalException
findByLoginName( loginName );
@@ -392,12 +392,11 @@ public class JDBCUserDatabase extends AbstractUserDatabase {
final Set<Principal> principals = new HashSet<>();
try( final Connection conn = m_ds.getConnection();
final PreparedStatement ps = conn.prepareStatement( m_findAll );
- final ResultSet rs = ps.executeQuery() )
- {
- while ( rs.next() ) {
+ final ResultSet rs = ps.executeQuery() ) {
+ while( rs.next() ) {
final String wikiName = rs.getString( m_wikiName );
- if( wikiName == null ) {
- log.warn( "Detected null wiki name in XMLUserDataBase. Check your user database." );
+ if( StringUtils.isEmpty( wikiName ) ) {
+ log.warn( "Detected null or empty wiki name for {} in JDBCUserDataBase. Check your user database.", rs.getString( m_loginName ) );
} else {
final Principal principal = new WikiPrincipal( wikiName, WikiPrincipal.WIKI_NAME );
principals.add( principal );
@@ -494,7 +493,7 @@ public class JDBCUserDatabase extends AbstractUserDatabase {
log.error( "DB connectivity error: " + e.getMessage() );
throw new WikiSecurityException("DB connectivity error: " + e.getMessage(), e );
}
- log.info( "JDBCUserDatabase initialized from JNDI DataSource: " + jndiName );
+ log.info( "JDBCUserDatabase initialized from JNDI DataSource: {}", jndiName );
// Determine if the datasource supports commits
try( final Connection conn = m_ds.getConnection() ) {
@@ -505,7 +504,7 @@ public class JDBCUserDatabase extends AbstractUserDatabase {
log.info( "JDBCUserDatabase supports transactions. Good; we will use them." );
}
} catch( final SQLException e ) {
- log.warn( "JDBCUserDatabase warning: user database doesn't seem to support transactions. Reason: " + e.getMessage() );
+ log.warn( "JDBCUserDatabase warning: user database doesn't seem to support transactions. Reason: {}", e.getMessage() );
}
}
@@ -513,7 +512,7 @@ public class JDBCUserDatabase extends AbstractUserDatabase {
* @see org.apache.wiki.auth.user.UserDatabase#rename(String, String)
*/
@Override
- public void rename( final String loginName, final String newName ) throws NoSuchPrincipalException, DuplicateUserException, WikiSecurityException {
+ public void rename( final String loginName, final String newName ) throws DuplicateUserException, WikiSecurityException {
// Get the existing user; if not found, throws NoSuchPrincipalException
final UserProfile profile = findByLoginName( loginName );
@@ -529,8 +528,7 @@ public class JDBCUserDatabase extends AbstractUserDatabase {
try( final Connection conn = m_ds.getConnection();
final PreparedStatement ps1 = conn.prepareStatement( m_renameProfile );
- final PreparedStatement ps2 = conn.prepareStatement( m_renameRoles ) )
- {
+ final PreparedStatement ps2 = conn.prepareStatement( m_renameRoles ) ) {
if( m_supportsCommits ) {
conn.setAutoCommit( false );
}
@@ -599,8 +597,7 @@ public class JDBCUserDatabase extends AbstractUserDatabase {
final PreparedStatement ps1 = conn.prepareStatement( m_insertProfile );
final PreparedStatement ps2 = conn.prepareStatement( m_findRoles );
final PreparedStatement ps3 = conn.prepareStatement( m_insertRole );
- final PreparedStatement ps4 = conn.prepareStatement( m_updateProfile ) )
- {
+ final PreparedStatement ps4 = conn.prepareStatement( m_updateProfile ) ) {
if( m_supportsCommits ) {
conn.setAutoCommit( false );
}
@@ -608,8 +605,7 @@ public class JDBCUserDatabase extends AbstractUserDatabase {
final Timestamp ts = new Timestamp( System.currentTimeMillis() );
final Date modDate = new Date( ts.getTime() );
final java.sql.Date lockExpiry = profile.getLockExpiry() == null ? null : new java.sql.Date( profile.getLockExpiry().getTime() );
- if( existingProfile == null )
- {
+ if( existingProfile == null ) {
// User is new: insert new user record
ps1.setString( 1, profile.getUid() );
ps1.setString( 2, profile.getEmail() );
@@ -652,12 +648,9 @@ public class JDBCUserDatabase extends AbstractUserDatabase {
ps4.setString( 5, profile.getWikiName() );
ps4.setTimestamp( 6, ts );
ps4.setString( 7, profile.getLoginName() );
- try
- {
+ try {
ps4.setString( 8, Serializer.serializeToBase64( profile.getAttributes() ) );
- }
- catch ( final IOException e )
- {
+ } catch ( final IOException e ) {
throw new WikiSecurityException( "Could not save user profile attribute. Reason: " + e.getMessage(), e );
}
ps4.setDate( 9, lockExpiry );
@@ -671,9 +664,7 @@ public class JDBCUserDatabase extends AbstractUserDatabase {
if( m_supportsCommits ) {
conn.commit();
}
- }
- catch( final SQLException e )
- {
+ } catch( final SQLException e ) {
throw new WikiSecurityException( e.getMessage(), e );
}
}
@@ -685,7 +676,7 @@ public class JDBCUserDatabase extends AbstractUserDatabase {
* to set (either a String or a Long)
* @param index the value to match
* @return the resolved UserProfile
- * @throws SQLException
+ * @throws NoSuchPrincipalException problems accessing the database
*/
private UserProfile findByPreparedStatement( final String sql, final Object index ) throws NoSuchPrincipalException
{
@@ -698,16 +689,16 @@ public class JDBCUserDatabase extends AbstractUserDatabase {
}
// Set the parameter to search by
- if ( index instanceof String ) {
- ps.setString( 1, (String)index );
+ if( index instanceof String ) {
+ ps.setString( 1, ( String )index );
} else if ( index instanceof Long ) {
- ps.setLong( 1, ( (Long)index).longValue() );
+ ps.setLong( 1, ( Long )index );
} else {
throw new IllegalArgumentException( "Index type not recognized!" );
}
// Go and get the record!
- try ( final ResultSet rs = ps.executeQuery() ) {
+ try( final ResultSet rs = ps.executeQuery() ) {
while ( rs.next() ) {
if( profile != null ) {
unique = false;
diff --git a/jspwiki-main/src/test/java/org/apache/wiki/auth/user/JDBCUserDatabaseTest.java b/jspwiki-main/src/test/java/org/apache/wiki/auth/user/JDBCUserDatabaseTest.java
index 68f47a4..b2cd826 100644
--- a/jspwiki-main/src/test/java/org/apache/wiki/auth/user/JDBCUserDatabaseTest.java
+++ b/jspwiki-main/src/test/java/org/apache/wiki/auth/user/JDBCUserDatabaseTest.java
@@ -17,6 +17,7 @@
under the License.
*/
package org.apache.wiki.auth.user;
+
import org.apache.wiki.HsqlDbUtils;
import org.apache.wiki.TestJDBCDataSource;
import org.apache.wiki.TestJNDIContext;
@@ -43,65 +44,60 @@ import java.util.Map;
import java.util.Properties;
/**
+ *
*/
-public class JDBCUserDatabaseTest
-{
- private final HsqlDbUtils m_hu = new HsqlDbUtils();
+public class JDBCUserDatabaseTest {
+ private final HsqlDbUtils m_hu = new HsqlDbUtils();
- private JDBCUserDatabase m_db = null;
+ private JDBCUserDatabase m_db = null;
private static final String TEST_ATTRIBUTES = "rO0ABXNyABFqYXZhLnV0aWwuSGFzaE1hcAUH2sHDFmDRAwACRgAKbG9hZEZhY3RvckkACXRocmVzaG9sZHhwP0AAAAAAAAx3CAAAABAAAAACdAAKYXR0cmlidXRlMXQAEXNvbWUgcmFuZG9tIHZhbHVldAAKYXR0cmlidXRlMnQADWFub3RoZXIgdmFsdWV4";
private static final String INSERT_JANNE = "INSERT INTO users (" +
- JDBCUserDatabase.DEFAULT_DB_UID + "," +
- JDBCUserDatabase.DEFAULT_DB_EMAIL + "," +
- JDBCUserDatabase.DEFAULT_DB_FULL_NAME + "," +
- JDBCUserDatabase.DEFAULT_DB_LOGIN_NAME + "," +
- JDBCUserDatabase.DEFAULT_DB_PASSWORD + "," +
- JDBCUserDatabase.DEFAULT_DB_WIKI_NAME + "," +
- JDBCUserDatabase.DEFAULT_DB_CREATED + "," +
- JDBCUserDatabase.DEFAULT_DB_ATTRIBUTES + ") VALUES (" +
- "'-7739839977499061014'," + "'janne@ecyrd.com'," + "'Janne Jalkanen'," + "'janne'," +
- "'{SHA}457b08e825da547c3b77fbc1ff906a1d00a7daee'," +
- "'JanneJalkanen'," +
- "'" + new Timestamp( new Timestamp( System.currentTimeMillis() ).getTime() ).toString() + "'," +
- "'" + TEST_ATTRIBUTES +"'" + ");";
+ JDBCUserDatabase.DEFAULT_DB_UID + "," +
+ JDBCUserDatabase.DEFAULT_DB_EMAIL + "," +
+ JDBCUserDatabase.DEFAULT_DB_FULL_NAME + "," +
+ JDBCUserDatabase.DEFAULT_DB_LOGIN_NAME + "," +
+ JDBCUserDatabase.DEFAULT_DB_PASSWORD + "," +
+ JDBCUserDatabase.DEFAULT_DB_WIKI_NAME + "," +
+ JDBCUserDatabase.DEFAULT_DB_CREATED + "," +
+ JDBCUserDatabase.DEFAULT_DB_ATTRIBUTES + ") VALUES (" +
+ "'-7739839977499061014'," + "'janne@ecyrd.com'," + "'Janne Jalkanen'," + "'janne'," +
+ "'{SHA}457b08e825da547c3b77fbc1ff906a1d00a7daee'," +
+ "'JanneJalkanen'," +
+ "'" + new Timestamp( new Timestamp( System.currentTimeMillis() ).getTime() ) + "'," +
+ "'" + TEST_ATTRIBUTES + "'" + ");";
private static final String INSERT_USER = "INSERT INTO users (" +
- JDBCUserDatabase.DEFAULT_DB_UID + "," +
- JDBCUserDatabase.DEFAULT_DB_EMAIL + "," +
- JDBCUserDatabase.DEFAULT_DB_LOGIN_NAME + "," +
- JDBCUserDatabase.DEFAULT_DB_PASSWORD + "," +
- JDBCUserDatabase.DEFAULT_DB_CREATED + ") VALUES (" +
- "'-8629747547991531672'," + "'jspwiki.tests@mailinator.com'," + "'user'," +
- "'{SHA}5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8'," +
- "'" + new Timestamp( new Timestamp( System.currentTimeMillis() ).getTime() ).toString() + "'" + ");";
+ JDBCUserDatabase.DEFAULT_DB_UID + "," +
+ JDBCUserDatabase.DEFAULT_DB_EMAIL + "," +
+ JDBCUserDatabase.DEFAULT_DB_LOGIN_NAME + "," +
+ JDBCUserDatabase.DEFAULT_DB_PASSWORD + "," +
+ JDBCUserDatabase.DEFAULT_DB_CREATED + ") VALUES (" +
+ "'-8629747547991531672'," + "'jspwiki.tests@mailinator.com'," + "'user'," +
+ "'{SHA}5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8'," +
+ "'" + new Timestamp( new Timestamp( System.currentTimeMillis() ).getTime() ) + "'" + ");";
/**
*
*/
@BeforeEach
- public void setUp() throws Exception
- {
+ public void setUp() throws Exception {
m_hu.setUp();
// Set up the mock JNDI initial context
TestJNDIContext.initialize();
final Context initCtx = new InitialContext();
- try
- {
+ try {
initCtx.bind( "java:comp/env", new TestJNDIContext() );
- }
- catch( final NameAlreadyBoundException e )
- {
+ } catch( final NameAlreadyBoundException e ) {
// ignore
}
- final Context ctx = (Context) initCtx.lookup( "java:comp/env" );
+ final Context ctx = ( Context ) initCtx.lookup( "java:comp/env" );
final DataSource ds = new TestJDBCDataSource( new File( "target/test-classes/jspwiki-custom.properties" ), m_hu.getDriverUrl() );
ctx.bind( JDBCUserDatabase.DEFAULT_DB_JNDI_NAME, ds );
// Get the JDBC connection and init tables
- try
- {
+ try {
final Connection conn = ds.getConnection();
final Statement stmt = conn.createStatement();
final String sql;
@@ -121,39 +117,35 @@ public class JDBCUserDatabaseTest
// Initialize the user database
m_db = new JDBCUserDatabase();
m_db.initialize( null, new Properties() );
- }
- catch( final SQLException e )
- {
- Assertions.fail("Looks like your database could not be connected to - "+
- "please make sure that you have started your database, exception: " + e.getMessage());
+ } catch( final SQLException e ) {
+ Assertions.fail( "Looks like your database could not be connected to - " +
+ "please make sure that you have started your database, exception: " + e.getMessage() );
}
}
@AfterEach
- public void tearDown() throws Exception
- {
+ public void tearDown() throws Exception {
m_hu.tearDown();
}
@Test
- public void testDeleteByLoginName() throws WikiSecurityException
- {
+ public void testDeleteByLoginName() throws WikiSecurityException {
// First, count the number of users in the db now.
final int oldUserCount = m_db.getWikiNames().length;
// Create a new user with random name
final String loginName = "TestUser" + System.currentTimeMillis();
UserProfile profile = m_db.newProfile();
- profile.setEmail("jspwiki.tests@mailinator.com");
+ profile.setEmail( "jspwiki.tests@mailinator.com" );
profile.setLoginName( loginName );
- profile.setFullname( "FullName"+loginName );
- profile.setPassword("password");
- m_db.save(profile);
+ profile.setFullname( "FullName" + loginName );
+ profile.setPassword( "password" );
+ m_db.save( profile );
// Make sure the profile saved successfully
profile = m_db.findByLoginName( loginName );
Assertions.assertEquals( loginName, profile.getLoginName() );
- Assertions.assertEquals( oldUserCount+1, m_db.getWikiNames().length );
+ Assertions.assertEquals( oldUserCount + 1, m_db.getWikiNames().length );
// Now delete the profile; should be back to old count
m_db.deleteByLoginName( loginName );
@@ -161,11 +153,10 @@ public class JDBCUserDatabaseTest
}
@Test
- public void testAttributes() throws Exception
- {
+ public void testAttributes() throws Exception {
UserProfile profile = m_db.findByEmail( "janne@ecyrd.com" );
- Map<String,Serializable> attributes = profile.getAttributes();
+ Map< String, Serializable > attributes = profile.getAttributes();
Assertions.assertEquals( 2, attributes.size() );
Assertions.assertTrue( attributes.containsKey( "attribute1" ) );
Assertions.assertTrue( attributes.containsKey( "attribute2" ) );
@@ -195,10 +186,8 @@ public class JDBCUserDatabaseTest
}
@Test
- public void testFindByEmail()
- {
- try
- {
+ public void testFindByEmail() {
+ try {
final UserProfile profile = m_db.findByEmail( "janne@ecyrd.com" );
Assertions.assertEquals( "-7739839977499061014", profile.getUid() );
Assertions.assertEquals( "janne", profile.getLoginName() );
@@ -208,28 +197,21 @@ public class JDBCUserDatabaseTest
Assertions.assertEquals( "janne@ecyrd.com", profile.getEmail() );
Assertions.assertNotNull( profile.getCreated() );
Assertions.assertNull( profile.getLastModified() );
- }
- catch( final NoSuchPrincipalException e )
- {
+ } catch( final NoSuchPrincipalException e ) {
Assertions.fail();
}
- try
- {
+ try {
m_db.findByEmail( "foo@bar.org" );
// We should never get here
Assertions.fail();
- }
- catch( final NoSuchPrincipalException e )
- {
+ } catch( final NoSuchPrincipalException e ) {
Assertions.assertTrue( true );
}
}
@Test
- public void testFindByFullName()
- {
- try
- {
+ public void testFindByFullName() {
+ try {
final UserProfile profile = m_db.findByFullName( "Janne Jalkanen" );
Assertions.assertEquals( "-7739839977499061014", profile.getUid() );
Assertions.assertEquals( "janne", profile.getLoginName() );
@@ -239,28 +221,21 @@ public class JDBCUserDatabaseTest
Assertions.assertEquals( "janne@ecyrd.com", profile.getEmail() );
Assertions.assertNotNull( profile.getCreated() );
Assertions.assertNull( profile.getLastModified() );
- }
- catch( final NoSuchPrincipalException e )
- {
+ } catch( final NoSuchPrincipalException e ) {
Assertions.fail();
}
- try
- {
+ try {
m_db.findByEmail( "foo@bar.org" );
// We should never get here
Assertions.fail();
- }
- catch( final NoSuchPrincipalException e )
- {
+ } catch( final NoSuchPrincipalException e ) {
Assertions.assertTrue( true );
}
}
@Test
- public void testFindByUid()
- {
- try
- {
+ public void testFindByUid() {
+ try {
final UserProfile profile = m_db.findByUid( "-7739839977499061014" );
Assertions.assertEquals( "-7739839977499061014", profile.getUid() );
Assertions.assertEquals( "janne", profile.getLoginName() );
@@ -270,28 +245,21 @@ public class JDBCUserDatabaseTest
Assertions.assertEquals( "janne@ecyrd.com", profile.getEmail() );
Assertions.assertNotNull( profile.getCreated() );
Assertions.assertNull( profile.getLastModified() );
- }
- catch( final NoSuchPrincipalException e )
- {
+ } catch( final NoSuchPrincipalException e ) {
Assertions.fail();
}
- try
- {
+ try {
m_db.findByEmail( "foo@bar.org" );
// We should never get here
Assertions.fail();
- }
- catch( final NoSuchPrincipalException e )
- {
+ } catch( final NoSuchPrincipalException e ) {
Assertions.assertTrue( true );
}
}
@Test
- public void testFindByWikiName()
- {
- try
- {
+ public void testFindByWikiName() {
+ try {
final UserProfile profile = m_db.findByWikiName( "JanneJalkanen" );
Assertions.assertEquals( "-7739839977499061014", profile.getUid() );
Assertions.assertEquals( "janne", profile.getLoginName() );
@@ -301,28 +269,21 @@ public class JDBCUserDatabaseTest
Assertions.assertEquals( "janne@ecyrd.com", profile.getEmail() );
Assertions.assertNotNull( profile.getCreated() );
Assertions.assertNull( profile.getLastModified() );
- }
- catch( final NoSuchPrincipalException e )
- {
+ } catch( final NoSuchPrincipalException e ) {
Assertions.fail();
}
- try
- {
+ try {
m_db.findByEmail( "foo" );
// We should never get here
Assertions.fail();
- }
- catch( final NoSuchPrincipalException e )
- {
+ } catch( final NoSuchPrincipalException e ) {
Assertions.assertTrue( true );
}
}
@Test
- public void testFindByLoginName()
- {
- try
- {
+ public void testFindByLoginName() {
+ try {
final UserProfile profile = m_db.findByLoginName( "janne" );
Assertions.assertEquals( "-7739839977499061014", profile.getUid() );
Assertions.assertEquals( "janne", profile.getLoginName() );
@@ -332,41 +293,31 @@ public class JDBCUserDatabaseTest
Assertions.assertEquals( "janne@ecyrd.com", profile.getEmail() );
Assertions.assertNotNull( profile.getCreated() );
Assertions.assertNull( profile.getLastModified() );
- }
- catch( final NoSuchPrincipalException e )
- {
+ } catch( final NoSuchPrincipalException e ) {
Assertions.fail();
}
- try
- {
+ try {
m_db.findByEmail( "FooBar" );
// We should never get here
Assertions.fail();
- }
- catch( final NoSuchPrincipalException e )
- {
+ } catch( final NoSuchPrincipalException e ) {
Assertions.assertTrue( true );
}
}
@Test
- public void testGetWikiName() throws WikiSecurityException
- {
+ public void testGetWikiName() throws WikiSecurityException {
final Principal[] principals = m_db.getWikiNames();
Assertions.assertEquals( 1, principals.length );
}
@Test
- public void testRename() throws Exception
- {
+ public void testRename() throws Exception {
// Try renaming a non-existent profile; it should Assertions.fail
- try
- {
+ try {
m_db.rename( "nonexistentname", "renameduser" );
Assertions.fail( "Should not have allowed rename..." );
- }
- catch ( final NoSuchPrincipalException e )
- {
+ } catch( final NoSuchPrincipalException e ) {
// Cool; that's what we expect
}
@@ -381,13 +332,10 @@ public class JDBCUserDatabaseTest
Assertions.assertNotNull( profile );
// Try renaming to a login name that's already taken; it should Assertions.fail
- try
- {
+ try {
m_db.rename( "olduser", "janne" );
Assertions.fail( "Should not have allowed rename..." );
- }
- catch ( final DuplicateUserException e )
- {
+ } catch( final DuplicateUserException e ) {
// Cool; that's what we expect
}
@@ -395,13 +343,10 @@ public class JDBCUserDatabaseTest
m_db.rename( "olduser", "renameduser" );
// The old user shouldn't be found
- try
- {
+ try {
m_db.findByLoginName( "olduser" );
Assertions.fail( "Old user was found, but it shouldn't have been." );
- }
- catch ( final NoSuchPrincipalException e )
- {
+ } catch( final NoSuchPrincipalException e ) {
// Cool, it's gone
}
@@ -417,10 +362,8 @@ public class JDBCUserDatabaseTest
}
@Test
- public void testSave() throws Exception
- {
- try
- {
+ public void testSave() throws Exception {
+ try {
// Overwrite existing user
UserProfile profile = m_db.newProfile();
profile.setEmail( "jspwiki.tests@mailinator.com" );
@@ -459,16 +402,13 @@ public class JDBCUserDatabaseTest
final String uid = profile.getUid();
Assertions.assertNotNull( m_db.findByUid( uid ) );
- }
- catch( final WikiSecurityException e )
- {
+ } catch( final WikiSecurityException e ) {
Assertions.fail();
}
}
@Test
- public void testValidatePassword()
- {
+ public void testValidatePassword() {
Assertions.assertFalse( m_db.validatePassword( "janne", "test" ) );
Assertions.assertTrue( m_db.validatePassword( "janne", "myP@5sw0rd" ) );
Assertions.assertTrue( m_db.validatePassword( "user", "password" ) );