Package org.dozer.functional_tests

Source Code of org.dozer.functional_tests.CustomConverterParamMappingTest

/*
* Copyright 2005-2010 the original author or authors.
*
* 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.dozer.functional_tests;

import org.dozer.vo.AnotherTestObject;
import org.dozer.vo.Fruit;
import org.dozer.vo.Individual;
import org.dozer.vo.SimpleObj;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

import java.util.StringTokenizer;

/**
* @author johnsen.knut-erik
*/
public class CustomConverterParamMappingTest extends AbstractFunctionalTest {

  @Override
  @Before
  public void setUp() throws Exception {
    mapper = getMapper("fieldCustomConverterParam.xml");
  }

  @Test
  public void testSimpleCustomConverter() throws Exception {
    SimpleObj src = newInstance(SimpleObj.class);
    src.setField1(String.valueOf(System.currentTimeMillis()));

    AnotherTestObject dest = mapper.map(src, AnotherTestObject.class);

    // Custom converter specified for the field1 mapping, so verify custom converter was actually used
    assertNotNull("dest field1 should not be null", dest.getField3());
    StringTokenizer st = new StringTokenizer(dest.getField3(), "-");
    assertEquals("dest field1 value should contain a hyphon", 2, st.countTokens());
    String token1 = st.nextToken();
    assertEquals("1st portion of dest field1 value should equal src field value", src.getField1(), token1);
    String token2 = st.nextToken();
    assertEquals("custom converter param should have been appended to by the cust converter", "CustomConverterParamTest", token2);

  }

  @Test
  public void testGlobalCustomConverter() {
    Individual individual = newInstance(Individual.class);
    individual.setUsername("ABC");
    Fruit result = mapper.map(individual, Fruit.class, "1");
    assertNotNull("", result.getName());
    assertTrue(result.getName().startsWith("ABC-null"));
  }

  @Test
  public void testGlobalCustomConverter_ParamProvided() {
    Individual individual = newInstance(Individual.class);
    individual.setUsername("ABC");
    Fruit result = mapper.map(individual, Fruit.class, "2");
    assertNotNull("", result.getName());
    assertTrue(result.getName().startsWith("ABC-PARAM"));
  }

}
TOP

Related Classes of org.dozer.functional_tests.CustomConverterParamMappingTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.