/*
 * Copyright 2016-2017 Talsma ICT
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.talsmasoftware.reflection;

import nl.talsmasoftware.reflection.errorhandling.MissingClassException;
import nl.talsmasoftware.test.TestUtil;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

/**
 * @author Sjoerd Talsma
 */
public class ClassesTest {
    private static final String NONEXISTENT_TYPE = "nl.talsmasoftware.reflection.NonExistentType";

    @Test
    public void testUnsupportedConstructor() {
        TestUtil.assertUnsupportedConstructor(Classes.class);
    }

    @Test
    public void testGetClass() {
        assertThat(Classes.getClass(String.class.getName()), is(equalTo((Class) String.class)));
    }

    @Test(expected = MissingClassException.class)
    public void testGetClass_NameNull() {
        Classes.getClass(null);
    }

    @Test(expected = MissingClassException.class)
    public void testGetClass_NotFound() {
        Classes.getClass(NONEXISTENT_TYPE);
    }

    @Test
    public void testGetClasses() {
        assertThat(Classes.getClasses((String[]) null), is(equalTo(new Class[0])));
        assertThat(Classes.getClasses(), is(equalTo(new Class[0])));
        assertThat(Classes.getClasses(String.class.getName()), is(equalTo(new Class[]{String.class})));
        assertThat(Classes.getClasses(Integer.class.getName(), String.class.getName()),
                is(equalTo(new Class[]{Integer.class, String.class})));
    }

    @Test(expected = MissingClassException.class)
    public void testGetClasses_NotFound() {
        Classes.getClasses(String.class.getName(), NONEXISTENT_TYPE, Integer.class.getName());
    }

    @Test
    public void testFindClass() {
        assertThat(Classes.findClass(Boolean.class.getName()), is(equalTo((Class) Boolean.class)));
    }

    @Test
    public void testFindClass_NameNull() {
        assertThat(Classes.findClass(null), is(nullValue()));
    }

    @Test
    public void testFindClass_NotFound() {
        assertThat(Classes.findClass(NONEXISTENT_TYPE), is(nullValue()));
    }

    @Test
    public void testFindClasses() {
        assertThat(Classes.findClasses((String[]) null), is(equalTo(new Class[0])));
        assertThat(Classes.findClasses(), is(equalTo(new Class[0])));
        assertThat(Classes.findClasses(String.class.getName(), NONEXISTENT_TYPE, Integer.class.getName()),
                is(equalTo(new Class[]{String.class, null, Integer.class})));
    }
}

@@ -634,16 +634,90 @@
 ction;%0A%0A
+import nl.talsmasoftware.reflection.errorhandling.InstantiatingException;%0A
 import n
@@ -845,16 +845,68 @@
 .Test;%0A%0A
+import static nl.talsmasoftware.test.TestUtil.fail;%0A
 import s
@@ -3160,11 +3160,936 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testCreateNew() %7B%0A        assertThat(Classes.createNew(%22java.lang.Object%22), is(notNullValue()));%0A        assertThat(Classes.createNew(Object.class), is(notNullValue()));%0A    %7D%0A%0A    @Test%0A    public void testTryCreateNew() %7B%0A        assertThat(Classes.tryCreateNew(%22java.lang.Object%22), is(notNullValue()));%0A        assertThat(Classes.tryCreateNew(Object.class), is(notNullValue()));%0A    %7D%0A%0A    @Test(expected = InstantiatingException.class)%0A    public void testCreateNew_exceptionInConstructor() %7B%0A        Classes.createNew(Thrower.class);%0A        fail(%22InstantiatingException expected.%22);%0A    %7D%0A%0A    @Test%0A    public void testTryCreateNew_exceptionInConstructor() %7B%0A        assertThat(Classes.tryCreateNew(Thrower.class), is(nullValue()));%0A    %7D%0A%0A    public static class Thrower %7B%0A        public Thrower() %7B%0A            throw new IllegalStateException(%22Exception in constructor.%22);%0A        %7D%0A    %7D
 %0A%7D%0A

Add a few Classes.create... unit tests.