|
| 1 | +/* |
| 2 | + * Password Management Servlets (PWM) |
| 3 | + * http://www.pwm-project.org |
| 4 | + * |
| 5 | + * Copyright (c) 2006-2009 Novell, Inc. |
| 6 | + * Copyright (c) 2009-2023 The PWM Project |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +package password.pwm.util.db; |
| 22 | + |
| 23 | +import org.junit.Assert; |
| 24 | +import org.junit.Rule; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.rules.TemporaryFolder; |
| 27 | +import password.pwm.PwmApplication; |
| 28 | +import password.pwm.config.Configuration; |
| 29 | +import password.pwm.config.PwmSetting; |
| 30 | +import password.pwm.config.stored.StoredConfigurationFactory; |
| 31 | +import password.pwm.config.stored.StoredConfigurationModifier; |
| 32 | +import password.pwm.config.value.PasswordValue; |
| 33 | +import password.pwm.config.value.StringValue; |
| 34 | +import password.pwm.util.PasswordData; |
| 35 | +import password.pwm.util.java.ClosableIterator; |
| 36 | +import password.pwm.util.localdb.TestHelper; |
| 37 | + |
| 38 | +import java.io.File; |
| 39 | +import java.util.Map; |
| 40 | + |
| 41 | +public class DatabaseServiceTest |
| 42 | +{ |
| 43 | + @Rule |
| 44 | + public TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 45 | + |
| 46 | + private static final DatabaseTable TEST_TABLE = DatabaseTable.PWM_META; |
| 47 | + private static final int TEST_COUNTS = 10_000; |
| 48 | + |
| 49 | + static PwmApplication makeDbApp( final TemporaryFolder temporaryFolder ) |
| 50 | + throws Exception |
| 51 | + { |
| 52 | + final File dbFilePath = temporaryFolder.newFolder(); |
| 53 | + final String jdbcUrl = "jdbc:h2:mem:pwmtest;DB_CLOSE_DELAY=-1;MODE=MySQL;NON_KEYWORDS=id,value"; |
| 54 | + |
| 55 | + final StoredConfigurationModifier config = StoredConfigurationFactory.newModifiableConfig(); |
| 56 | + config.writeSetting( PwmSetting.DATABASE_CLASS, null, |
| 57 | + new StringValue( "org.h2.Driver" ), null ); |
| 58 | + config.writeSetting( PwmSetting.DATABASE_URL, null, |
| 59 | + new StringValue( jdbcUrl ), null ); |
| 60 | + config.writeSetting( PwmSetting.DATABASE_USERNAME, null, |
| 61 | + new StringValue( "username" ), null ); |
| 62 | + config.writeSetting( PwmSetting.DATABASE_PASSWORD, null, |
| 63 | + new PasswordValue( PasswordData.forStringValue( "username" ) ), null ); |
| 64 | + |
| 65 | + return TestHelper.makeTestPwmApplication( |
| 66 | + dbFilePath, |
| 67 | + new Configuration( config.newStoredConfiguration() ) ); |
| 68 | + } |
| 69 | + |
| 70 | + static DatabaseAccessor makeAccessor( final TemporaryFolder temporaryFolder ) |
| 71 | + throws Exception |
| 72 | + { |
| 73 | + final PwmApplication pwmApplication = makeDbApp( temporaryFolder ); |
| 74 | + final DatabaseService databaseService = pwmApplication.getDatabaseService(); |
| 75 | + |
| 76 | + ( ( DatabaseAccessorImpl ) databaseService.getAccessor() ).clearTable( TEST_TABLE ); |
| 77 | + |
| 78 | + return databaseService.getAccessor(); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testGetPut() throws Exception |
| 84 | + { |
| 85 | + final DatabaseAccessor accessor = makeAccessor( temporaryFolder ); |
| 86 | + accessor.put( TEST_TABLE, "key1", "value1" ); |
| 87 | + final String returnValue = accessor.get( TEST_TABLE, "key1" ); |
| 88 | + Assert.assertEquals( "value1", returnValue ); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testSize() throws Exception |
| 93 | + { |
| 94 | + final DatabaseAccessor accessor = makeAccessor( temporaryFolder ); |
| 95 | + for ( int i = 0; i < TEST_COUNTS; i++ ) |
| 96 | + { |
| 97 | + accessor.put( TEST_TABLE, i + "key", i + "value" ); |
| 98 | + Assert.assertEquals( i + 1, accessor.size( TEST_TABLE ) ); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testIterator() throws Exception |
| 104 | + { |
| 105 | + final DatabaseAccessor accessor = makeAccessor( temporaryFolder ); |
| 106 | + for ( int i = 0; i < TEST_COUNTS; i++ ) |
| 107 | + { |
| 108 | + final String key = i + "key"; |
| 109 | + final String value = i + "value"; |
| 110 | + accessor.put( TEST_TABLE, key, value ); |
| 111 | + } |
| 112 | + |
| 113 | + try ( ClosableIterator<Map.Entry<String, String>> iterator = accessor.iterator( TEST_TABLE ) ) |
| 114 | + { |
| 115 | + int loopCounter = 0; |
| 116 | + while ( iterator.hasNext() ) |
| 117 | + { |
| 118 | + final String key = loopCounter + "key"; |
| 119 | + final String value = loopCounter + "value"; |
| 120 | + final Map.Entry<String, String> entry = iterator.next(); |
| 121 | + Assert.assertEquals( entry.getKey(), key ); |
| 122 | + Assert.assertEquals( entry.getValue(), value ); |
| 123 | + Assert.assertThrows( UnsupportedOperationException.class, iterator::remove ); |
| 124 | + loopCounter++; |
| 125 | + } |
| 126 | + Assert.assertEquals( TEST_COUNTS, loopCounter ); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testGets() throws Exception |
| 132 | + { |
| 133 | + final DatabaseAccessor accessor = makeAccessor( temporaryFolder ); |
| 134 | + for ( int i = 0; i < TEST_COUNTS; i++ ) |
| 135 | + { |
| 136 | + final String key = i + "key"; |
| 137 | + final String value = i + "value"; |
| 138 | + accessor.put( TEST_TABLE, key, value ); |
| 139 | + } |
| 140 | + |
| 141 | + for ( int i = 0; i < TEST_COUNTS; i++ ) |
| 142 | + { |
| 143 | + final String key = i + "key"; |
| 144 | + final String value = i + "value"; |
| 145 | + Assert.assertEquals( value, accessor.get( TEST_TABLE, key ) ); |
| 146 | + } |
| 147 | + |
| 148 | + for ( int i = 0; i < TEST_COUNTS; i++ ) |
| 149 | + { |
| 150 | + final String key = i + "key"; |
| 151 | + Assert.assertNotEquals( "badvalue", accessor.get( TEST_TABLE, key ) ); |
| 152 | + } |
| 153 | + |
| 154 | + for ( int i = 0; i < TEST_COUNTS; i++ ) |
| 155 | + { |
| 156 | + final String key = i + "key"; |
| 157 | + Assert.assertNull( accessor.get( TEST_TABLE, key + "badkey" ) ); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void testContains() throws Exception |
| 163 | + { |
| 164 | + final DatabaseAccessor accessor = makeAccessor( temporaryFolder ); |
| 165 | + for ( int i = 0; i < TEST_COUNTS; i++ ) |
| 166 | + { |
| 167 | + final String key = i + "key"; |
| 168 | + final String value = i + "value"; |
| 169 | + accessor.put( TEST_TABLE, key, value ); |
| 170 | + } |
| 171 | + |
| 172 | + for ( int i = 0; i < TEST_COUNTS; i++ ) |
| 173 | + { |
| 174 | + final String key = i + "key"; |
| 175 | + Assert.assertTrue( accessor.contains( TEST_TABLE, key ) ); |
| 176 | + } |
| 177 | + |
| 178 | + for ( int i = 0; i < TEST_COUNTS; i++ ) |
| 179 | + { |
| 180 | + final String key = i + "key"; |
| 181 | + Assert.assertFalse( accessor.contains( TEST_TABLE, key + "false" ) ); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void testRemoves() throws Exception |
| 187 | + { |
| 188 | + final DatabaseAccessor accessor = makeAccessor( temporaryFolder ); |
| 189 | + for ( int i = 0; i < TEST_COUNTS; i++ ) |
| 190 | + { |
| 191 | + final String key = i + "key"; |
| 192 | + final String value = i + "value"; |
| 193 | + accessor.put( TEST_TABLE, key, value ); |
| 194 | + } |
| 195 | + |
| 196 | + for ( int i = 0; i < TEST_COUNTS; i++ ) |
| 197 | + { |
| 198 | + final String key = i + "key"; |
| 199 | + Assert.assertTrue( accessor.contains( TEST_TABLE, key ) ); |
| 200 | + Assert.assertEquals( TEST_COUNTS - i, accessor.size( TEST_TABLE ) ); |
| 201 | + accessor.remove( TEST_TABLE, key ); |
| 202 | + Assert.assertFalse( accessor.contains( TEST_TABLE, key ) ); |
| 203 | + Assert.assertEquals( ( TEST_COUNTS - i ) - 1, accessor.size( TEST_TABLE ) ); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + public void testPutIfAbsent() throws Exception |
| 209 | + { |
| 210 | + final DatabaseAccessor accessor = makeAccessor( temporaryFolder ); |
| 211 | + |
| 212 | + Assert.assertEquals( 0, accessor.size( TEST_TABLE ) ); |
| 213 | + Assert.assertTrue( accessor.putIfAbsent( TEST_TABLE, "key1", "value1" ) ); |
| 214 | + Assert.assertFalse( accessor.putIfAbsent( TEST_TABLE, "key1", "value2" ) ); |
| 215 | + Assert.assertEquals( "value1", accessor.get( TEST_TABLE, "key1" ) ); |
| 216 | + Assert.assertEquals( 1, accessor.size( TEST_TABLE ) ); |
| 217 | + } |
| 218 | +} |
0 commit comments