Skip to content

Commit

Permalink
move tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agliznetsov authored and elaatifi committed Oct 3, 2021
1 parent 166a26d commit 232848f
Show file tree
Hide file tree
Showing 340 changed files with 43,026 additions and 0 deletions.
55 changes: 55 additions & 0 deletions core/src/test/java/DefaultPackageTestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import org.junit.Assert;
import org.junit.Test;

import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.OrikaSystemProperties;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.impl.generator.JavassistCompilerStrategy;

public class DefaultPackageTestCase {
public static class Label {
private String text;

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}
}

public static class XmlLabel {
private String text;

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}
}

@Test
public void test() {

System.setProperty(OrikaSystemProperties.COMPILER_STRATEGY,
JavassistCompilerStrategy.class.getName());

System.setProperty(OrikaSystemProperties.WRITE_SOURCE_FILES, "true");
System.setProperty(OrikaSystemProperties.WRITE_CLASS_FILES, "true");

MapperFactory factory = new DefaultMapperFactory.Builder().build();
MapperFacade mapper = factory.getMapperFacade();

XmlLabel xmlLabel1 = new XmlLabel();
xmlLabel1.setText("label");

Label label = mapper.map(xmlLabel1, Label.class);

Assert.assertEquals(xmlLabel1.getText(), label.getText());

}
}
172 changes: 172 additions & 0 deletions core/src/test/java/ma/glasnost/orika/examples/Example1TestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* Orika - simpler, better and faster Java bean mapping
*
* Copyright (C) 2011-2013 Orika 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 ma.glasnost.orika.examples;

import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.metadata.Property;
import ma.glasnost.orika.metadata.TypeFactory;
import ma.glasnost.orika.property.IntrospectorPropertyResolver;
import ma.glasnost.orika.property.PropertyResolver;
import ma.glasnost.orika.test.MappingUtil;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
* @author [email protected]
*
*/
public class Example1TestCase {

private static final String NESTED_OPEN = PropertyResolver.ELEMENT_PROPERT_PREFIX;
private static final String NESTED_CLOSE = PropertyResolver.ELEMENT_PROPERT_SUFFIX;

@Test
public void resolveProperties() {

PropertyResolver propertyResolver = new IntrospectorPropertyResolver();

Property aliasesFirst = propertyResolver.getProperty(TypeFactory.valueOf(PersonDto.class), "aliases" + NESTED_OPEN + "[0]" + NESTED_CLOSE);

Assert.assertNotNull(aliasesFirst);
Assert.assertEquals(TypeFactory.valueOf(String.class), aliasesFirst.getType());
Assert.assertNotNull(aliasesFirst.getContainer());
Assert.assertEquals(TypeFactory.valueOf(String[][].class), aliasesFirst.getContainer().getType());
}

/**
* !
*/
@Test
public void nestedElements() {

MapperFactory mapperFactory = MappingUtil.getMapperFactory(true);

mapperFactory.classMap(Person.class, PersonDto.class)
.field("name.first", "firstName")
.field("name.last", "lastName")
.field("knownAliases" + NESTED_OPEN + "first" + NESTED_CLOSE, "aliases" + NESTED_OPEN + "[0]" + NESTED_CLOSE)
.field("knownAliases" + NESTED_OPEN + "last" + NESTED_CLOSE, "aliases" + NESTED_OPEN + "[1]" + NESTED_CLOSE)
.byDefault()
.register();

MapperFacade mapper = mapperFactory.getMapperFacade();

List<Name> aliases = new ArrayList<>();
aliases.add(new Name("Joe", "Williams"));
aliases.add(new Name("Terry", "Connor"));
Person source = new Person(new Name("John","Doe"), new Date(), aliases);

PersonDto dest = mapper.map(source, PersonDto.class);

Assert.assertNotNull(dest);
Assert.assertEquals(source.getName().getFirst(), dest.getFirstName());
Assert.assertEquals(source.getName().getLast(), dest.getLastName());
Assert.assertEquals(source.getKnownAliases().get(0).getFirst(), dest.getAliases()[0][0]);
Assert.assertEquals(source.getKnownAliases().get(0).getLast(), dest.getAliases()[0][1]);
Assert.assertEquals(source.getKnownAliases().get(1).getFirst(), dest.getAliases()[1][0]);
Assert.assertEquals(source.getKnownAliases().get(1).getLast(), dest.getAliases()[1][1]);

}

public static class Person {
private final Name name;
private final List<Name> knownAliases;
private final Date birthDate;

public Person(Name name, Date birthDate, List<Name> knownAliases) {
this.name = name;
this.birthDate = (Date) birthDate.clone();
this.knownAliases = new ArrayList<>(knownAliases);
}

public List<Name> getKnownAliases() {
return Collections.unmodifiableList(knownAliases);
}

public Name getName() {
return name;
}

public Date getBirthDate() {
return (Date) birthDate.clone();
}
}

public static class Name {
private final String first;
private final String last;

public Name(String first, String last) {
this.first = first;
this.last = last;
}

public String getFirst() {
return first;
}

public String getLast() {
return last;
}
}

public static class PersonDto {
private String firstName;
private String lastName;
private Date birthDate;
private String[][] aliases;

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public Date getBirthDate() {
return birthDate;
}

public String[][] getAliases() {
return aliases;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}

public void setAliases(String[][] aliases) {
this.aliases = aliases;
}
}
}
Loading

0 comments on commit 232848f

Please sign in to comment.