001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.test;
021
022import org.apache.commons.io.FileUtils;
023import org.apache.commons.lang.StringUtils;
024
025import java.io.File;
026import java.lang.reflect.Constructor;
027import java.lang.reflect.Modifier;
028import java.net.URL;
029
030/**
031 * Utilities for unit tests
032 *
033 * @since 2.2
034 */
035public final class TestUtils {
036
037  private TestUtils() {
038  }
039
040  /**
041   * Search for a test resource in the classpath. For example getResource("org/sonar/MyClass/foo.txt");
042   *
043   * @param path the starting slash is optional
044   * @return the resource. Null if resource not found
045   */
046  public static File getResource(String path) {
047    String resourcePath = path;
048    if (!resourcePath.startsWith("/")) {
049      resourcePath = "/" + resourcePath;
050    }
051    URL url = TestUtils.class.getResource(resourcePath);
052    if (url != null) {
053      return FileUtils.toFile(url);
054    }
055    return null;
056  }
057
058  /**
059   * Search for a resource in the classpath. For example calling the method getResource(getClass(), "myTestName/foo.txt") from
060   * the class org.sonar.Foo loads the file $basedir/src/test/resources/org/sonar/Foo/myTestName/foo.txt
061   *
062   * @return the resource. Null if resource not found
063   */
064  public static File getResource(Class baseClass, String path) {
065    String resourcePath = StringUtils.replaceChars(baseClass.getCanonicalName(), '.', '/');
066    if (!path.startsWith("/")) {
067      resourcePath += "/";
068    }
069    resourcePath += path;
070    return getResource(resourcePath);
071  }
072
073  /**
074   * Asserts that all constructors are private, usually for helper classes with
075   * only static methods. If a constructor does not have any parameters, then
076   * it's instantiated.
077   */
078  public static boolean hasOnlyPrivateConstructors(Class clazz) {
079    boolean ok = true;
080    for (Constructor constructor : clazz.getDeclaredConstructors()) {
081      ok &= Modifier.isPrivate(constructor.getModifiers());
082      if (constructor.getParameterTypes().length == 0) {
083        constructor.setAccessible(true);
084        try {
085          constructor.newInstance();
086        } catch (Exception e) {
087          throw new IllegalStateException(String.format("Fail to instantiate %s", clazz), e);
088        }
089      }
090    }
091    return ok;
092  }
093
094  public static File newTempDir(String prefix) {
095    try {
096      // Technique to create a temp directory from a temp file
097      File f = File.createTempFile(prefix, "");
098      f.delete();
099      f.mkdir();
100      return f;
101    } catch (Exception e) {
102      throw new IllegalStateException("Fail to create temp dir", e);
103    }
104  }
105}