|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.eval.app.db; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.AfterEach; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +public class JDBCUtilTest { |
| 27 | + |
| 28 | + private String originalCacheSize; |
| 29 | + |
| 30 | + @BeforeEach |
| 31 | + public void stashCacheSizeProperty() { |
| 32 | + originalCacheSize = System.getProperty(JDBCUtil.H2_CACHE_SIZE_KB_PROPERTY); |
| 33 | + System.clearProperty(JDBCUtil.H2_CACHE_SIZE_KB_PROPERTY); |
| 34 | + } |
| 35 | + |
| 36 | + @AfterEach |
| 37 | + public void restoreCacheSizeProperty() { |
| 38 | + if (originalCacheSize == null) { |
| 39 | + System.clearProperty(JDBCUtil.H2_CACHE_SIZE_KB_PROPERTY); |
| 40 | + } else { |
| 41 | + System.setProperty(JDBCUtil.H2_CACHE_SIZE_KB_PROPERTY, originalCacheSize); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testJdbcStringPassesThrough() { |
| 47 | + String jdbc = "jdbc:postgresql://localhost/tika_eval"; |
| 48 | + assertEquals(jdbc, JDBCUtil.getJdbcConnectionString(jdbc)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testH2Defaults() { |
| 53 | + long cacheSizeKb = JDBCUtil.getH2CacheSizeKb(); |
| 54 | + assertTrue(cacheSizeKb >= 65_536L && cacheSizeKb <= 1_048_576L, "clamped to [64MB, 1GB]: " + cacheSizeKb); |
| 55 | + String connectionString = JDBCUtil.getJdbcConnectionString("mydb"); |
| 56 | + assertTrue(connectionString.startsWith("jdbc:h2:file:"), connectionString); |
| 57 | + assertTrue(connectionString.endsWith(";RETENTION_TIME=0;CACHE_SIZE=" + cacheSizeKb), connectionString); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testH2CacheSizeOverrideIsUnclamped() { |
| 62 | + System.setProperty(JDBCUtil.H2_CACHE_SIZE_KB_PROPERTY, "8388608"); |
| 63 | + assertEquals(8_388_608L, JDBCUtil.getH2CacheSizeKb()); |
| 64 | + } |
| 65 | +} |
0 commit comments