|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.comet.iceberg |
| 21 | + |
| 22 | +import java.util.Collections |
| 23 | + |
| 24 | +import org.scalatest.funsuite.AnyFunSuite |
| 25 | + |
| 26 | +import org.apache.iceberg.BaseMetastoreTableOperations |
| 27 | +import org.apache.iceberg.BaseTable |
| 28 | +import org.apache.iceberg.Schema |
| 29 | +import org.apache.iceberg.TableMetadata |
| 30 | +import org.apache.iceberg.io.FileIO |
| 31 | +import org.apache.iceberg.types.Types |
| 32 | + |
| 33 | +class IcebergReflectionSuite extends AnyFunSuite { |
| 34 | + |
| 35 | + /** Mimics HiveTableOperations/GlueTableOperations which inherit current(). */ |
| 36 | + class StubTableOperations extends BaseMetastoreTableOperations { |
| 37 | + override protected def tableName(): String = "test" |
| 38 | + override def refresh(): TableMetadata = null |
| 39 | + override def io(): FileIO = null |
| 40 | + } |
| 41 | + |
| 42 | + test("getTableMetadata succeeds when operations class inherits current()") { |
| 43 | + val ops = new StubTableOperations() |
| 44 | + val schema = new Schema(Types.NestedField.required(1, "id", Types.IntegerType.get())) |
| 45 | + val expectedMetadata = TableMetadata.newTableMetadata( |
| 46 | + schema, |
| 47 | + org.apache.iceberg.PartitionSpec.unpartitioned(), |
| 48 | + "file:///tmp/test-table", |
| 49 | + Collections.emptyMap[String, String]()) |
| 50 | + val metadataField = classOf[BaseMetastoreTableOperations] |
| 51 | + .getDeclaredField("currentMetadata") |
| 52 | + metadataField.setAccessible(true) |
| 53 | + metadataField.set(ops, expectedMetadata) |
| 54 | + // current() checks shouldRefresh (default true) and calls refresh() instead of |
| 55 | + // returning currentMetadata. Set to false so current() returns our stubbed metadata. |
| 56 | + val refreshField = classOf[BaseMetastoreTableOperations] |
| 57 | + .getDeclaredField("shouldRefresh") |
| 58 | + refreshField.setAccessible(true) |
| 59 | + refreshField.set(ops, false) |
| 60 | + |
| 61 | + val table = new BaseTable(ops, "test-table") |
| 62 | + val metadata = IcebergReflection.getTableMetadata(table) |
| 63 | + assert(metadata.isDefined) |
| 64 | + assert(metadata.get.isInstanceOf[TableMetadata]) |
| 65 | + } |
| 66 | +} |
0 commit comments