1 /*
2  * Copyright (C) 2018 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 BuildShortNumberMetadataJsonFromXmlGoldenTest extends TestCase {
29 
30   private static final String INPUT_FILE_NAME = "ShortNumberMetadataForGoldenTests.xml";
31   private static final String GOLDEN_FILE_NAME = "expected_shortnumbermetadata.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         "i18n.phonenumbers.shortnumbergoldenmetadata" /* namespace */);
43     BufferedReader outputReader =
44         new BufferedReader(new InputStreamReader(new FileInputStream(outputFile), "UTF-8"));
45     BufferedReader goldenReader =
46         new BufferedReader(new InputStreamReader(new FileInputStream(golden), "UTF-8"));
47     while (outputReader.ready() && goldenReader.ready()) {
48       String goldenLine = goldenReader.readLine();
49       if (goldenLine.contains("ShortNumberMetadata.xml")) {
50         // The full path of the input file is contained in the output and these lines will be
51         // different, so we just check the output file name is present and continue.
52         assertTrue(outputReader.readLine().contains(INPUT_FILE_NAME));
53         continue;
54       }
55       assertEquals(outputReader.readLine(), goldenLine);
56     }
57     // Check the files are the same size.
58     assertEquals(outputReader.ready(), goldenReader.ready());
59   }
60 }
61