|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.facebook.presto.orc.metadata; |
| 15 | + |
| 16 | +import com.facebook.presto.orc.DwrfEncryptionProvider; |
| 17 | +import com.facebook.presto.orc.DwrfKeyProvider; |
| 18 | +import com.facebook.presto.orc.OrcCorruptionException; |
| 19 | +import com.facebook.presto.orc.OrcDataSource; |
| 20 | +import com.facebook.presto.orc.OrcDataSourceId; |
| 21 | +import com.facebook.presto.orc.OrcDecompressor; |
| 22 | +import com.facebook.presto.orc.metadata.statistics.HiveBloomFilter; |
| 23 | +import com.facebook.presto.orc.protobuf.InvalidProtocolBufferException; |
| 24 | +import com.google.common.collect.ImmutableList; |
| 25 | +import org.testng.Assert.ThrowingRunnable; |
| 26 | +import org.testng.annotations.Test; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStream; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Optional; |
| 32 | + |
| 33 | +import static com.facebook.presto.orc.metadata.PostScript.HiveWriterVersion.ORIGINAL; |
| 34 | +import static com.google.common.base.Preconditions.checkState; |
| 35 | +import static org.testng.Assert.expectThrows; |
| 36 | + |
| 37 | +public class TestExceptionWrappingMetadataReader |
| 38 | +{ |
| 39 | + private static final OrcDataSourceId ORC_DATA_SOURCE_ID = new OrcDataSourceId("test"); |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testReadPostScriptExceptionHandling() |
| 43 | + { |
| 44 | + byte[] data = new byte[10]; |
| 45 | + TestMetadataReader delegate = new TestMetadataReader(); |
| 46 | + ExceptionWrappingMetadataReader metadataReader = new ExceptionWrappingMetadataReader(ORC_DATA_SOURCE_ID, delegate); |
| 47 | + assertExceptions(() -> metadataReader.readPostScript(data, 0, 1)); |
| 48 | + delegate.assertAllExceptionsThrown(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testReadMetadataExceptionHandling() |
| 53 | + { |
| 54 | + TestMetadataReader delegate = new TestMetadataReader(); |
| 55 | + ExceptionWrappingMetadataReader metadataReader = new ExceptionWrappingMetadataReader(ORC_DATA_SOURCE_ID, delegate); |
| 56 | + ThrowingRunnable lambda = () -> metadataReader.readMetadata(ORIGINAL, null); |
| 57 | + assertExceptions(lambda); |
| 58 | + delegate.assertAllExceptionsThrown(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testReadFooterExceptionHandling() |
| 63 | + { |
| 64 | + TestMetadataReader delegate = new TestMetadataReader(); |
| 65 | + ExceptionWrappingMetadataReader metadataReader = new ExceptionWrappingMetadataReader(ORC_DATA_SOURCE_ID, delegate); |
| 66 | + ThrowingRunnable lambda = () -> metadataReader.readFooter( |
| 67 | + ORIGINAL, |
| 68 | + null, |
| 69 | + null, |
| 70 | + null, |
| 71 | + null, |
| 72 | + null); |
| 73 | + assertExceptions(lambda); |
| 74 | + delegate.assertAllExceptionsThrown(); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testReadStripeFooterExceptionHandling() |
| 79 | + { |
| 80 | + TestMetadataReader delegate = new TestMetadataReader(); |
| 81 | + ExceptionWrappingMetadataReader metadataReader = new ExceptionWrappingMetadataReader(ORC_DATA_SOURCE_ID, delegate); |
| 82 | + ThrowingRunnable lambda = () -> metadataReader.readStripeFooter(ORC_DATA_SOURCE_ID, null, null); |
| 83 | + assertExceptions(lambda); |
| 84 | + delegate.assertAllExceptionsThrown(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testReadRowIndexesExceptionHandling() |
| 89 | + { |
| 90 | + TestMetadataReader delegate = new TestMetadataReader(); |
| 91 | + ExceptionWrappingMetadataReader metadataReader = new ExceptionWrappingMetadataReader(ORC_DATA_SOURCE_ID, delegate); |
| 92 | + ThrowingRunnable lambda = () -> metadataReader.readRowIndexes(ORIGINAL, null, null); |
| 93 | + assertExceptions(lambda); |
| 94 | + delegate.assertAllExceptionsThrown(); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testReadBloomFilterIndexesExceptionHandling() |
| 99 | + { |
| 100 | + TestMetadataReader delegate = new TestMetadataReader(); |
| 101 | + ExceptionWrappingMetadataReader metadataReader = new ExceptionWrappingMetadataReader(ORC_DATA_SOURCE_ID, delegate); |
| 102 | + ThrowingRunnable lambda = () -> metadataReader.readBloomFilterIndexes(null); |
| 103 | + assertExceptions(lambda); |
| 104 | + delegate.assertAllExceptionsThrown(); |
| 105 | + } |
| 106 | + |
| 107 | + private static void assertExceptions(ThrowingRunnable lambda) |
| 108 | + { |
| 109 | + expectThrows(RuntimeException.class, lambda); |
| 110 | + expectThrows(IOException.class, lambda); |
| 111 | + expectThrows(OrcCorruptionException.class, lambda); |
| 112 | + } |
| 113 | + |
| 114 | + private static class TestMetadataReader |
| 115 | + implements MetadataReader |
| 116 | + { |
| 117 | + private static final List<Exception> EXCEPTIONS = ImmutableList.of( |
| 118 | + new RuntimeException("test runtime exception"), |
| 119 | + new IOException("test io exception"), |
| 120 | + new InvalidProtocolBufferException("test protobuf exception")); |
| 121 | + private int currentExceptionIndex; |
| 122 | + |
| 123 | + private void throwNextException() |
| 124 | + throws IOException |
| 125 | + { |
| 126 | + checkState(currentExceptionIndex < EXCEPTIONS.size()); |
| 127 | + Exception ex = EXCEPTIONS.get(currentExceptionIndex++); |
| 128 | + if (ex instanceof IOException) { |
| 129 | + throw (IOException) ex; |
| 130 | + } |
| 131 | + throw (RuntimeException) ex; |
| 132 | + } |
| 133 | + |
| 134 | + public void assertAllExceptionsThrown() |
| 135 | + { |
| 136 | + checkState(currentExceptionIndex == EXCEPTIONS.size()); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public PostScript readPostScript(byte[] data, int offset, int length) |
| 141 | + throws IOException |
| 142 | + { |
| 143 | + throwNextException(); |
| 144 | + return null; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public Metadata readMetadata(PostScript.HiveWriterVersion hiveWriterVersion, InputStream inputStream) |
| 149 | + throws IOException |
| 150 | + { |
| 151 | + throwNextException(); |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public Footer readFooter( |
| 157 | + PostScript.HiveWriterVersion hiveWriterVersion, |
| 158 | + InputStream inputStream, |
| 159 | + DwrfEncryptionProvider dwrfEncryptionProvider, |
| 160 | + DwrfKeyProvider dwrfKeyProvider, |
| 161 | + OrcDataSource orcDataSource, |
| 162 | + Optional<OrcDecompressor> decompressor) |
| 163 | + throws IOException |
| 164 | + { |
| 165 | + throwNextException(); |
| 166 | + return null; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public StripeFooter readStripeFooter(OrcDataSourceId orcDataSourceId, List<OrcType> types, InputStream inputStream) |
| 171 | + throws IOException |
| 172 | + { |
| 173 | + throwNextException(); |
| 174 | + return null; |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public List<RowGroupIndex> readRowIndexes(PostScript.HiveWriterVersion hiveWriterVersion, InputStream inputStream, List<HiveBloomFilter> bloomFilters) |
| 179 | + throws IOException |
| 180 | + { |
| 181 | + throwNextException(); |
| 182 | + return null; |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public List<HiveBloomFilter> readBloomFilterIndexes(InputStream inputStream) |
| 187 | + throws IOException |
| 188 | + { |
| 189 | + throwNextException(); |
| 190 | + return null; |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments