/*
 * Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
 *
 * 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 com.querydsl.core.types;

import static org.junit.Assert.*;

import java.util.Arrays;

import org.junit.Test;

import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.StringPath;

public class ConstructorExpressionTest {

    StringPath str1 = Expressions.stringPath("str1");
    StringPath str2 = Expressions.stringPath("str2");
    StringPath str3 = Expressions.stringPath("str3");
    Concatenation concat = new Concatenation(str1, str2);

    @Test
    public void constructor() {
        Expression<Long> longVal = ConstantImpl.create(1L);
        Expression<String> stringVal = ConstantImpl.create("");
        ProjectionExample instance = new ConstructorExpression<ProjectionExample>(ProjectionExample.class,
                new Class<?>[]{long.class, String.class}, longVal, stringVal).newInstance(0L, "");
        assertNotNull(instance);
        assertEquals((Long) 0L, instance.id);
        assertTrue(instance.text.isEmpty());
    }

    @Test
    public void create() {
        Expression<Long> longVal = ConstantImpl.create(1L);
        Expression<String> stringVal = ConstantImpl.create("");
        assertNotNull(Projections.constructor(ProjectionExample.class, longVal, stringVal).newInstance(0L, ""));
    }

    @Test
    public void create2() {
        Expression<Long> longVal = ConstantImpl.create(1L);
        assertNotNull(Projections.constructor(ProjectionExample.class, longVal).newInstance(0L));
    }

    @Test
    public void create3() {
        assertNotNull(Projections.constructor(ProjectionExample.class).newInstance());
    }

    @Test
    public void create4() {
        Expression<String> stringVal = ConstantImpl.create("");
        assertNotNull(Projections.constructor(ProjectionExample.class, stringVal).newInstance(""));
    }

    @Test
    public void createNullPrimitive() {
        Expression<Boolean> booleanVal = ConstantImpl.create(false);
        Expression<Byte> byteVal = ConstantImpl.create((byte) 0);
        Expression<Character> charVal = ConstantImpl.create('\0');
        Expression<Short> shortVal = ConstantImpl.create((short) 0);
        Expression<Integer> intVal = ConstantImpl.create(0);
        Expression<Long> longVal = ConstantImpl.create(0L);
        Expression<Float> floatVal = ConstantImpl.create(0.0F);
        Expression<Double> doubleVal = ConstantImpl.create(0.0);
        ProjectionExample instance = Projections.constructor(ProjectionExample.class,
                booleanVal, byteVal,
                charVal, shortVal,
                intVal, longVal,
                floatVal, doubleVal)
                .newInstance(null, null,
                        null, null,
                        null, null,
                        null, null);
        assertNotNull(instance);
    }

    @Test
    public void factoryExpression_has_right_args() {
        FactoryExpression<ProjectionExample> constructor = Projections.constructor(ProjectionExample.class, concat);
        constructor = FactoryExpressionUtils.wrap(constructor);
        assertEquals(Arrays.asList(str1, str2), constructor.getArgs());
    }

    @Test
    public void factoryExpression_newInstance() {
        FactoryExpression<ProjectionExample> constructor = Projections.constructor(ProjectionExample.class, concat);
        constructor = FactoryExpressionUtils.wrap(constructor);
        ProjectionExample projection = constructor.newInstance("12", "34");
        assertEquals("1234", projection.text);
    }

}

@@ -735,16 +735,64 @@
 .Test;%0A%0A
+import com.querydsl.core.testutil.ThreadSafety;%0A
 import c
@@ -4153,10 +4153,355 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void threadSafety() %7B%0A        final ConstructorExpression%3CString%3E expr = Projections.constructor(String.class);%0A        Runnable invoker = new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                expr.newInstance();%0A            %7D%0A        %7D;%0A        ThreadSafety.check(invoker, invoker);%0A    %7D%0A%0A
 %7D%0A

Add thread safety test for ConstructorExpression