1 /* 2 * Copyright 2016 The Android Open Source Project 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 org.testng.internal; 18 19 import java.lang.reflect.Constructor; 20 21 /** 22 * Factory for IPropertyUtils that returns a concrete instance. 23 */ 24 public class PropertyUtilsFactory { 25 26 /** 27 * Tries to make a real PropertyUtils, if the platform supports it. Otherwise creates 28 * a mock PropertyUtils that throws UnsupportedOperationException if any method is called on it. 29 */ newInstance()30 public static IPropertyUtils newInstance() { 31 try { 32 Class<?> propertyUtilsClass = Class.forName("org.testng.internal.PropertyUtils"); 33 Constructor<?> constructor = propertyUtilsClass.getConstructor(); 34 try { 35 return (IPropertyUtils)constructor.newInstance(); 36 } 37 catch (Exception e) { 38 // Impossible: Constructor should not be failing. 39 throw new AssertionError(e); 40 } 41 } catch (ClassNotFoundException e) { 42 // OK: On a platform where java beans are not supported 43 return new PropertyUtilsMock(); 44 } catch (NoSuchMethodException e) { 45 // Impossible. PropertyUtils should have a 0-arg constructor. 46 throw new AssertionError(e); 47 } 48 } 49 PropertyUtilsFactory()50 private PropertyUtilsFactory() {} 51 } 52