View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.flatfile.conversion;
18  
19  import net.sf.morph.reflect.Reflector;
20  import net.sf.morph.reflect.reflectors.SimpleDelegatingReflector;
21  import net.sf.morph.transform.DecoratedConverter;
22  import net.sf.morph.transform.Transformer;
23  import net.sf.morph.transform.copiers.PropertyNameMatchingCopier;
24  import net.sf.morph.transform.transformers.SimpleDelegatingTransformer;
25  
26  //import com.pgac.fixedlength.morph.EntityInstantiatingReflector;
27  import org.apache.commons.flatfile.Entity;
28  import org.apache.commons.flatfile.EntityParserTestBase;
29  import org.apache.commons.flatfile.dsl.DefaultEntityNameStrategy;
30  import org.apache.commons.flatfile.dsl.EntityNameStrategy;
31  import org.apache.commons.flatfile.morph.ContainerToIndexedEntityCollectionCopier;
32  import org.apache.commons.flatfile.morph.EntityCollectionReflector;
33  import org.apache.commons.flatfile.morph.EntityInstantiatingReflector;
34  import org.junit.Before;
35  import org.junit.Test;
36  
37  /**
38   *
39   */
40  public abstract class ConversionTestBase extends EntityParserTestBase {
41      private static final EntityNameStrategy DEFAULT_NAME_STRATEGY = new DefaultEntityNameStrategy();
42  
43      protected static class TestPair {
44          private String entityValue;
45  
46          private Object modelObject;
47  
48          public TestPair(String entityValue, Object modelObject) {
49              this.entityValue = entityValue;
50              this.modelObject = modelObject;
51          }
52  
53          public synchronized String getEntityValue() {
54              return entityValue;
55          }
56  
57          public synchronized Object getModelObject() {
58              return modelObject;
59          }
60      }
61  
62      protected DecoratedConverter converter;
63  
64      @Override
65      @Before
66      public void setUp() throws Exception {
67          super.setUp();
68          entityFactory.setEntityNameStrategy(getEntityNameStrategy());
69          Reflector reflector = new SimpleDelegatingReflector(new Reflector[] {
70                  new EntityInstantiatingReflector(entityFactory),
71                  new EntityCollectionReflector() }, true);
72          PropertyNameMatchingCopier entityCopier = new PropertyNameMatchingCopier();
73          entityCopier.setDestinationClasses(new Class[] { Entity.class });
74          entityCopier.setReflector(reflector);
75          SimpleDelegatingTransformer sdt = new SimpleDelegatingTransformer(
76                  new Transformer[] {
77                          new ContainerToIndexedEntityCollectionCopier(),
78                          entityCopier }, true);
79          converter = sdt;
80      }
81  
82      @Test
83      public void testMe() throws Exception {
84          for (TestPair p : getTestPairs()) {
85              assertConversion(p);
86          }
87      }
88  
89      protected void assertConversion(TestPair pair) {
90          Entity e = (Entity) converter.convert(Entity.class, pair
91                  .getModelObject());
92          assertValue(pair.getEntityValue(), e);
93      }
94  
95      /**
96       * Override this method for multiple test pairs
97       * @return
98       */
99      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 }