#581 Prevent multiple imports of similar simple class name from different packages.

This commit is contained in:
VGT 2015-07-02 22:44:42 +02:00
parent 2d686003c8
commit 64dd46c354
6 changed files with 220 additions and 2 deletions

View File

@ -405,7 +405,8 @@ public class TypeFactory {
}
private boolean isImported(String name, String qualifiedName) {
String importedType = importedQualifiedTypesBySimpleName.get( name );
String trimmedName = TypeFactory.trimSimpleClassName( name );
String importedType = importedQualifiedTypesBySimpleName.get( trimmedName );
boolean imported = false;
if ( importedType != null ) {
@ -414,7 +415,7 @@ public class TypeFactory {
}
}
else {
importedQualifiedTypesBySimpleName.put( name, qualifiedName );
importedQualifiedTypesBySimpleName.put( trimmedName, qualifiedName );
imported = true;
}
return imported;
@ -466,4 +467,15 @@ public class TypeFactory {
return collectionOrMap;
}
static String trimSimpleClassName(String className) {
if ( className == null ) {
return null;
}
String trimmedClassName = className;
while ( trimmedClassName.endsWith( "[]" ) ) {
trimmedClassName = trimmedClassName.substring( 0, trimmedClassName.length() - 2 );
}
return trimmedClassName;
}
}

View File

@ -0,0 +1,63 @@
/**
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* 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.mapstruct.ap.internal.model.common;
import static org.fest.assertions.Assertions.assertThat;
import org.junit.Test;
public class TypeFactoryTest {
@Test
public void shouldReturnNullIfNoClassNameIsProvided() {
String result = TypeFactory.trimSimpleClassName( null );
assertThat( result ).isNull();
}
@Test
public void shouldNotModifyClassNameIfNotAnArray() {
String className = "SimpleClass";
String result = TypeFactory.trimSimpleClassName( className );
assertThat( result ).isEqualTo( className );
}
@Test
public void shouldTrimOneDimensionalArray() {
String result = TypeFactory.trimSimpleClassName( "SimpleClass[]" );
assertThat( result ).isEqualTo( "SimpleClass" );
}
@Test
public void shouldTrimTwoDimensionalArray() {
String result = TypeFactory.trimSimpleClassName( "SimpleClass[][]" );
assertThat( result ).isEqualTo( "SimpleClass" );
}
@Test
public void shouldTrimMultiDimensionalArray() {
String result = TypeFactory.trimSimpleClassName( "SimpleClass[][][][][]" );
assertThat( result ).isEqualTo( "SimpleClass" );
}
}

View File

@ -0,0 +1,44 @@
/**
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* 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.mapstruct.ap.test.bugs._581;
import static org.fest.assertions.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.test.bugs._581.source.Car;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
@IssueKey( "581" )
@WithClasses({ Car.class, org.mapstruct.ap.test.bugs._581._target.Car.class, SourceTargetMapper.class })
@RunWith(AnnotationProcessorTestRunner.class)
public class Issue581Test {
@Test
public void shouldMapSourceAndTargetWithTheSameClassName() {
Car source = new Car();
org.mapstruct.ap.test.bugs._581._target.Car target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
assertThat( target ).isNotNull();
assertThat( target.getFoo() ).isEqualTo( source.getFoo() );
}
}

View File

@ -0,0 +1,35 @@
/**
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* 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.mapstruct.ap.test.bugs._581;
import org.mapstruct.Mapper;
import org.mapstruct.ap.test.bugs._581.source.Car;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface SourceTargetMapper {
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
org.mapstruct.ap.test.bugs._581._target.Car[] sourceToTarget( List<Car> source );
org.mapstruct.ap.test.bugs._581._target.Car sourceToTarget( Car source );
}

View File

@ -0,0 +1,32 @@
/**
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* 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.mapstruct.ap.test.bugs._581._target;
public class Car {
private String foo;
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
}

View File

@ -0,0 +1,32 @@
/**
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* 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.mapstruct.ap.test.bugs._581.source;
public class Car {
private String foo;
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
}