1 /* 2 * Copyright (C) 2022 The Libphonenumber Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.i18n.phonenumbers.metadata.init; 18 19 import static java.nio.charset.StandardCharsets.UTF_8; 20 import static org.junit.Assert.assertThrows; 21 22 import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; 23 import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection; 24 import com.google.i18n.phonenumbers.metadata.PhoneMetadataCollectionUtil; 25 import java.io.ByteArrayInputStream; 26 import java.io.IOException; 27 import java.io.InputStream; 28 import java.util.Collection; 29 import junit.framework.TestCase; 30 import org.junit.function.ThrowingRunnable; 31 32 public final class MetadataParserTest extends TestCase { 33 34 private static final MetadataParser metadataParser = MetadataParser.newStrictParser(); 35 test_parse_shouldThrowExceptionForNullInput()36 public void test_parse_shouldThrowExceptionForNullInput() { 37 assertThrows( 38 IllegalArgumentException.class, 39 new ThrowingRunnable() { 40 @Override 41 public void run() { 42 metadataParser.parse(null); 43 } 44 }); 45 } 46 test_parse_shouldThrowExceptionForEmptyInput()47 public void test_parse_shouldThrowExceptionForEmptyInput() { 48 final InputStream emptyInput = new ByteArrayInputStream(new byte[0]); 49 50 assertThrows( 51 IllegalStateException.class, 52 new ThrowingRunnable() { 53 @Override 54 public void run() { 55 metadataParser.parse(emptyInput); 56 } 57 }); 58 } 59 test_parse_shouldThrowExceptionForInvalidInput()60 public void test_parse_shouldThrowExceptionForInvalidInput() { 61 final InputStream invalidInput = new ByteArrayInputStream("Some random input".getBytes(UTF_8)); 62 63 assertThrows( 64 IllegalStateException.class, 65 new ThrowingRunnable() { 66 @Override 67 public void run() { 68 metadataParser.parse(invalidInput); 69 } 70 }); 71 } 72 test_parse_shouldParseValidInput()73 public void test_parse_shouldParseValidInput() throws IOException { 74 InputStream input = PhoneMetadataCollectionUtil.toInputStream( 75 PhoneMetadataCollection.newBuilder() 76 .addMetadata(PhoneMetadata.newBuilder().setId("id").build())); 77 78 Collection<PhoneMetadata> actual = metadataParser.parse(input); 79 80 assertEquals(1, actual.size()); 81 } 82 test_parse_shouldReturnEmptyCollectionForNullInput()83 public void test_parse_shouldReturnEmptyCollectionForNullInput() { 84 Collection<PhoneMetadata> actual = MetadataParser.newLenientParser().parse(null); 85 86 assertTrue(actual.isEmpty()); 87 } 88 }