// Copyright 2006, 2007, 2008, 2009, 2011 The Apache Software Foundation
//
// 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 org.apache.tapestry5.ioc.internal;

import org.apache.tapestry5.ioc.ObjectLocator;
import org.apache.tapestry5.ioc.OrderedConfiguration;
import org.apache.tapestry5.ioc.internal.util.Orderer;
import org.slf4j.Logger;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings(
{ "rawtypes", "unchecked" })
public class ValidatingOrderedConfigurationWrapperTest extends IOCInternalTestCase
{
    @Test
    public void valid_type_long_form()
    {
        Runnable contribution = mockRunnable();
        Runnable coerced = mockRunnable();
        Runnable pre = mockRunnable();
        Runnable post = mockRunnable();
        Logger logger = mockLogger();
        Orderer<Runnable> orderer = new Orderer<Runnable>(logger);
        TypeCoercerProxy tc = mockTypeCoercerProxy();

        expect(tc.coerce(contribution, Runnable.class)).andReturn(coerced);

        orderer.add("pre", pre);
        orderer.add("post", post);

        replay();

        OrderedConfiguration<Runnable> wrapper = new ValidatingOrderedConfigurationWrapper<Runnable>(Runnable.class,
                null, tc, orderer, null, null);

        wrapper.add("id", contribution, "after:pre", "before:post");

        verify();

        assertListsEquals(orderer.getOrdered(), pre, coerced, post);
    }

    @Test
    public void contribute_valid_class()
    {
        Logger logger = mockLogger();
        Orderer<Map> orderer = new Orderer<Map>(logger);
        Map pre = new HashMap();
        Map post = new HashMap();
        HashMap contribution = new HashMap();
        ObjectLocator locator = mockObjectLocator();
        TypeCoercerProxy tc = mockTypeCoercerProxy();

        train_autobuild(locator, HashMap.class, contribution);

        expect(tc.coerce(contribution, Map.class)).andReturn(contribution);

        orderer.add("pre", pre);
        orderer.add("post", post);

        replay();

        OrderedConfiguration<Map> wrapper = new ValidatingOrderedConfigurationWrapper<Map>(Map.class, locator, tc,
                orderer, null, null);

        wrapper.addInstance("id", HashMap.class, "after:pre", "before:post");

        verify();

        assertListsEquals(orderer.getOrdered(), pre, contribution, post);
    }

    @Test
    public void null_object_passed_through()
    {
        Logger logger = mockLogger();
        Orderer<Runnable> orderer = new Orderer<Runnable>(logger);

        replay();

        OrderedConfiguration<Runnable> wrapper = new ValidatingOrderedConfigurationWrapper<Runnable>(Runnable.class,
                null, null, orderer, null, null);

        wrapper.add("id", null);

        verify();

        assertTrue(orderer.getOrdered().isEmpty());
    }

}
