• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 com.google.i18n.phonenumbers.metadata.DefaultMetadataDependenciesProvider;
20 import com.google.i18n.phonenumbers.metadata.source.MetadataSourceImpl;
21 import com.google.i18n.phonenumbers.metadata.source.MultiFileModeFileNameProvider;
22 import junit.framework.TestCase;
23 
24 /**
25  * Root class for PhoneNumberUtil tests that depend on the test metadata file.
26  * <p>
27  * Note since tests that extend this class do not use the normal metadata file, they should not be
28  * used for regression test purposes.
29  * <p>
30  * Ideally the {@code phoneUtil} field (which uses test metadata) would be the only way that tests
31  * need to interact with a PhoneNumberUtil instance. However as some static methods in the library
32  * invoke "getInstance()" internally, we must also inject the test instance as the PhoneNumberUtil
33  * singleton. This means it is unsafe to run tests derived from this class in parallel with each
34  * other or at the same time as other tests which might require the singleton instance.
35  *
36  * @author Shaopeng Jia
37  */
38 public class TestMetadataTestCase extends TestCase {
39 
40   private static final String TEST_METADATA_FILE_PREFIX =
41       "/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting";
42 
43   /**
44    * An instance of PhoneNumberUtil that uses test metadata.
45    */
46   protected final PhoneNumberUtil phoneUtil;
47 
TestMetadataTestCase()48   public TestMetadataTestCase() {
49     phoneUtil = new PhoneNumberUtil(
50         new MetadataSourceImpl(new MultiFileModeFileNameProvider(TEST_METADATA_FILE_PREFIX),
51             DefaultMetadataDependenciesProvider.getInstance().getMetadataLoader(),
52             DefaultMetadataDependenciesProvider.getInstance().getMetadataParser()),
53         CountryCodeToRegionCodeMapForTesting.getCountryCodeToRegionCodeMap());
54   }
55 
56   @Override
setUp()57   protected void setUp() throws Exception {
58     super.setUp();
59     PhoneNumberUtil.setInstance(phoneUtil);
60   }
61 
62   @Override
tearDown()63   protected void tearDown() throws Exception {
64     PhoneNumberUtil.setInstance(null);
65     super.tearDown();
66   }
67 }
68