001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.flatfile.conversion;
018
019import net.sf.morph.reflect.Reflector;
020import net.sf.morph.reflect.reflectors.SimpleDelegatingReflector;
021import net.sf.morph.transform.DecoratedConverter;
022import net.sf.morph.transform.Transformer;
023import net.sf.morph.transform.copiers.PropertyNameMatchingCopier;
024import net.sf.morph.transform.transformers.SimpleDelegatingTransformer;
025
026//import com.pgac.fixedlength.morph.EntityInstantiatingReflector;
027import org.apache.commons.flatfile.Entity;
028import org.apache.commons.flatfile.EntityParserTestBase;
029import org.apache.commons.flatfile.dsl.DefaultEntityNameStrategy;
030import org.apache.commons.flatfile.dsl.EntityNameStrategy;
031import org.apache.commons.flatfile.morph.ContainerToIndexedEntityCollectionCopier;
032import org.apache.commons.flatfile.morph.EntityCollectionReflector;
033import org.apache.commons.flatfile.morph.EntityInstantiatingReflector;
034import org.junit.Before;
035import org.junit.Test;
036
037/**
038 *
039 */
040public abstract class ConversionTestBase extends EntityParserTestBase {
041    private static final EntityNameStrategy DEFAULT_NAME_STRATEGY = new DefaultEntityNameStrategy();
042
043    protected static class TestPair {
044        private String entityValue;
045
046        private Object modelObject;
047
048        public TestPair(String entityValue, Object modelObject) {
049            this.entityValue = entityValue;
050            this.modelObject = modelObject;
051        }
052
053        public synchronized String getEntityValue() {
054            return entityValue;
055        }
056
057        public synchronized Object getModelObject() {
058            return modelObject;
059        }
060    }
061
062    protected DecoratedConverter converter;
063
064    @Override
065    @Before
066    public void setUp() throws Exception {
067        super.setUp();
068        entityFactory.setEntityNameStrategy(getEntityNameStrategy());
069        Reflector reflector = new SimpleDelegatingReflector(new Reflector[] {
070                new EntityInstantiatingReflector(entityFactory),
071                new EntityCollectionReflector() }, true);
072        PropertyNameMatchingCopier entityCopier = new PropertyNameMatchingCopier();
073        entityCopier.setDestinationClasses(new Class[] { Entity.class });
074        entityCopier.setReflector(reflector);
075        SimpleDelegatingTransformer sdt = new SimpleDelegatingTransformer(
076                new Transformer[] {
077                        new ContainerToIndexedEntityCollectionCopier(),
078                        entityCopier }, true);
079        converter = sdt;
080    }
081
082    @Test
083    public void testMe() throws Exception {
084        for (TestPair p : getTestPairs()) {
085            assertConversion(p);
086        }
087    }
088
089    protected void assertConversion(TestPair pair) {
090        Entity e = (Entity) converter.convert(Entity.class, pair
091                .getModelObject());
092        assertValue(pair.getEntityValue(), e);
093    }
094
095    /**
096     * Override this method for multiple test pairs
097     * @return
098     */
099    protected TestPair[] getTestPairs() {
100        return new TestPair[] { getTestPair() };
101    }
102
103    /**
104     * Override this method for a single test pair
105     * @return
106     */
107    protected TestPair getTestPair() {
108        throw new UnsupportedOperationException("getTestPair");
109    }
110
111    /**
112     * Override to provide a different naming strategy
113     * @return
114     */
115    protected EntityNameStrategy getEntityNameStrategy() {
116        return DEFAULT_NAME_STRATEGY;
117    }
118}