de.kaiserpfalzEdv.commons.jee.spring.ServiceLoggingTest.java Source code

Java tutorial

Introduction

Here is the source code for de.kaiserpfalzEdv.commons.jee.spring.ServiceLoggingTest.java

Source

/*
 * Copyright 2015 Kaiserpfalz EDV-Service Roland Lichti
 *
 * 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 de.kaiserpfalzEdv.commons.jee.spring;

import de.kaiserpfalzEdv.commons.jee.spring.service.test.ServiceLoggingImpl;
import org.slf4j.MDC;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * This class tests the POJO functionality of {@link ServiceLogging}.
 *
 * @author klenkes
 * @since 2012Q1
 */
@Test
@ContextConfiguration("/beans-test.xml")
public class ServiceLoggingTest extends AbstractTestNGSpringContextTests {
    private static final UUID UUID_OBJECT_ARRAY0 = UUID.fromString("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa");
    private static final UUID UUID_OBJECT_ARRAY1 = UUID.fromString("11111111-1111-1111-1111-111111111111");
    private static final UUID UUID_OBJECT_ARRAY2 = UUID.fromString("22222222-2222-2222-2222-222222222222");
    private static final UUID UUID_OBJECT_ARRAY3 = UUID.fromString("33333333-3333-3333-3333-333333333333");
    private static final UUID UUID_OBJECT_ARRAY4 = UUID.fromString("44444444-4444-4444-4444-444444444444");
    private static final UUID UUID_OBJECT_ARRAY5 = UUID.fromString("55555555-5555-5555-5555-555555555555");
    private static final UUID UUID_OBJECT_ARRAY6 = UUID.fromString("66666666-6666-6666-6666-666666666666");

    @Resource
    private ServiceLoggingImpl service;

    @BeforeMethod
    protected void setUpService() {
        MDC.remove("id");
        MDC.remove("test");
    }

    @AfterMethod
    protected void tearDownService() {
        MDC.remove("id");
        MDC.remove("test");
    }

    @DataProvider(name = "arguments")
    protected Iterator<Object[]> arguments() {
        List<Object[]> result = new CopyOnWriteArrayList<>();

        result.add(new Object[] { "random-all-null", null, null, null, null });

        result.add(new Object[] { "uuid1", UUID_OBJECT_ARRAY1.toString(), UUID_OBJECT_ARRAY0.toString(),
                UUID_OBJECT_ARRAY1, "uuid1" });
        result.add(new Object[] { "uuid2", UUID_OBJECT_ARRAY2.toString(), null, UUID_OBJECT_ARRAY2, "uuid2" });
        result.add(new Object[] { "uuid3", UUID_OBJECT_ARRAY3.toString(), "UUIDObjectArray", UUID_OBJECT_ARRAY3,
                "uuid3" });
        result.add(
                new Object[] { "uuid4", UUID_OBJECT_ARRAY4.toString(), UUID_OBJECT_ARRAY4.toString(), null, null });
        result.add(
                new Object[] { "uuid5", UUID_OBJECT_ARRAY5.toString(), UUID_OBJECT_ARRAY5.toString(), null, null });
        result.add(new Object[] { "uuid6", UUID_OBJECT_ARRAY6.toString(), null, UUID_OBJECT_ARRAY6,
                UUID_OBJECT_ARRAY0 });

        result.add(new Object[] { "string-null-args", "nullObjectArray", "nullObjectArray", null, null });
        result.add(new Object[] { "string-empty-args", "emptyObjectArray", "emptyObjectArray", null, null });

        return result.iterator();
    }

    @Test(dataProvider = "arguments")
    public void updateMDC(final String test, final String wantedId, final String id, final UUID arg1,
            final Object arg2) throws Throwable {
        MDC.put("test", test);

        if (id != null)
            MDC.put("id", id);

        ServiceLogging.oplog.debug("Testing for wantedId='{}', id='{}', args='{}'", wantedId, id,
                new Object[] { arg1, arg2 });

        Assert.assertTrue(service.check(arg1, wantedId, arg2));
    }
}