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
17package org.testng.internal;
18
19import java.lang.reflect.Constructor;
20
21/**
22 * Factory for IPathUtils that returns a concrete instance.
23 */
24public class PathUtilsFactory {
25
26  /**
27   * Tries to make a real PathUtils, if the platform supports it. Otherwise creates
28   * a mock PathUtils that throws UnsupportedOperationException if any method is called on it.
29   */
30  public static IPathUtils newInstance() {
31    try {
32      Class<?> propertyUtilsClass = Class.forName("org.testng.internal.PathUtils");
33      Constructor<?> constructor = propertyUtilsClass.getConstructor();
34      try {
35        return (IPathUtils)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 PathUtilsMock();
44    } catch (NoSuchMethodException e) {
45      // Impossible. PathUtils should have a 0-arg constructor.
46      throw new AssertionError(e);
47    }
48  }
49
50  private PathUtilsFactory() {}
51}
52