1 /* 2 * Copyright (C) 2016 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; 18 19 import java.io.BufferedInputStream; 20 import java.io.BufferedReader; 21 import java.io.IOException; 22 import java.io.InputStreamReader; 23 import java.nio.charset.Charset; 24 import junit.framework.TestCase; 25 26 /** 27 * Tests to ensure that the {@link MetadataFilter} logic over excludable fields cover all applicable 28 * fields. 29 */ 30 public final class MetadataFilterCoverageTest extends TestCase { 31 private static final String CODE; 32 33 static { 34 try { 35 BufferedReader source = new BufferedReader(new InputStreamReader(new BufferedInputStream( 36 MetadataFilterTest.class.getResourceAsStream( 37 "/com/google/i18n/phonenumbers/MetadataFilter.java")), 38 Charset.forName("UTF-8"))); 39 StringBuilder codeBuilder = new StringBuilder(); 40 for (String line = source.readLine(); line != null; line = source.readLine()) { 41 codeBuilder.append(line).append("\n"); 42 } 43 CODE = codeBuilder.toString(); 44 } catch (IOException e) { 45 throw new RuntimeException("MetadataFilter.java resource not set up properly", e); 46 } 47 } 48 testCoverageOfExcludableParentFields()49 public void testCoverageOfExcludableParentFields() { 50 for (String field : MetadataFilter.excludableParentFields) { 51 String capitalized = Character.toUpperCase(field.charAt(0)) + field.substring(1); 52 String conditional = String.format("(?s).*if \\(metadata.has%s\\(\\)\\) \\{\\s+" 53 + "metadata.set%s\\(getFiltered\\(\"%s\",\\s+metadata.get%s\\(\\)\\)\\);\\s+\\}.*", 54 capitalized, capitalized, field, capitalized); 55 assertTrue("Code is missing correct conditional for " + field, CODE.matches(conditional)); 56 } 57 58 assertEquals(countOccurrencesOf("metadata.has", CODE), 59 MetadataFilter.excludableParentFields.size()); 60 } 61 testCoverageOfExcludableChildFields()62 public void testCoverageOfExcludableChildFields() { 63 for (String field : MetadataFilter.excludableChildFields) { 64 String capitalized = Character.toUpperCase(field.charAt(0)) + field.substring(1); 65 String conditional = String.format("(?s).*if \\(shouldDrop\\(type, \"%s\"\\)\\) \\{\\s+" 66 + "builder.clear%s\\(\\);\\s+\\}.*", field, capitalized); 67 assertTrue("Code is missing correct conditional for " + field, CODE.matches(conditional)); 68 } 69 70 assertEquals(countOccurrencesOf("shouldDrop(type, \"", CODE), 71 MetadataFilter.excludableChildFields.size()); 72 } 73 testCoverageOfExcludableChildlessFields()74 public void testCoverageOfExcludableChildlessFields() { 75 for (String field : MetadataFilter.excludableChildlessFields) { 76 String capitalized = Character.toUpperCase(field.charAt(0)) + field.substring(1); 77 String conditional = String.format("(?s).*if \\(shouldDrop\\(\"%s\"\\)\\) \\{\\s+" 78 + "metadata.clear%s\\(\\);\\s+\\}.*", field, capitalized); 79 assertTrue("Code is missing correct conditional for " + field, CODE.matches(conditional)); 80 } 81 82 assertEquals(countOccurrencesOf("shouldDrop(\"", CODE), 83 MetadataFilter.excludableChildlessFields.size()); 84 } 85 countOccurrencesOf(String substring, String string)86 private static int countOccurrencesOf(String substring, String string) { 87 int count = 0; 88 for (int i = string.indexOf(substring); i != -1; i = string.indexOf(substring, i + 1)) { 89 count++; 90 } 91 return count; 92 } 93 } 94