1 /* 2 * Copyright (C) 2017 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.BufferedReader; 20 import java.io.File; 21 import java.io.FileInputStream; 22 import java.io.InputStreamReader; 23 import junit.framework.TestCase; 24 25 /** 26 * Tests for the output of the JSON metadata producer. 27 */ 28 public final class BuildMetadataJsonFromXmlGoldenTest extends TestCase { 29 30 private static final String INPUT_FILE_NAME = "PhoneNumberMetadataForGoldenTests.xml"; 31 private static final String GOLDEN_FILE_NAME = "expected_metadata.js"; 32 testBuildMetadataJsonFromXmlGolden()33 public void testBuildMetadataJsonFromXmlGolden() throws Exception { 34 File srcDir = new File("target/test-classes/com/google/i18n/phonenumbers/buildtools/testdata"); 35 File inputXml = new File(srcDir, INPUT_FILE_NAME); 36 File outputFile = File.createTempFile("testOutput", ""); 37 outputFile.deleteOnExit(); 38 File golden = new File(srcDir, GOLDEN_FILE_NAME); 39 40 BuildMetadataJsonFromXml.start( 41 inputXml.getAbsolutePath(), outputFile.getAbsolutePath(), false /* not liteBuild */); 42 BufferedReader outputReader = 43 new BufferedReader(new InputStreamReader(new FileInputStream(outputFile), "UTF-8")); 44 BufferedReader goldenReader = 45 new BufferedReader(new InputStreamReader(new FileInputStream(golden), "UTF-8")); 46 while (outputReader.ready() && goldenReader.ready()) { 47 String goldenLine = goldenReader.readLine(); 48 if (goldenLine.contains("PhoneNumberMetadata.xml")) { 49 // The full path of the input file is contained in the output and these lines will be 50 // different, so we just check the output file name is present and continue. 51 assertTrue(outputReader.readLine().contains(INPUT_FILE_NAME)); 52 continue; 53 } 54 assertEquals(outputReader.readLine(), goldenLine); 55 } 56 // Check the files are the same size. 57 assertEquals(outputReader.ready(), goldenReader.ready()); 58 } 59 } 60