/*
* Copyright 2002-2007 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.internna.iwebmvc.parsers;
import org.internna.iwebmvc.dao.DAO;
import org.internna.iwebmvc.model.Address;
import org.internna.iwebmvc.model.AddressType;
import org.internna.iwebmvc.model.Province;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring.xml", "/addresess.xml"})
public class AddressParserTest {
@Autowired private AddressParser addressParser;
@Autowired private DAO baseDao;
@Before
public void init() throws Exception {
Address address = new Address();
address.setCity("xx");
address.setNumber("1");
address.setStreet("whatever");
address.setPostalCode("23423");
address.setType(baseDao.first(AddressType.class));
address.setProvince(baseDao.first(Province.class));
baseDao.create(address);
}
@Test
public void testParse() {
assertNull(addressParser.parse(null));
Address address = new Address();
address.setProvince(new Province());
address.getProvince().setId(baseDao.first(Province.class).getId());
address.setType(new AddressType());
address.getType().setId(baseDao.first(AddressType.class).getId());
address = addressParser.parse(address);
assertNotNull(address.getProvince().getCountry());
assertNotNull(address.getType().getType());
address = new Address();
address.setId(baseDao.first(Address.class).getId());
address.setPostalCode("00001");
address = addressParser.parse(address);
assertEquals(address.getPostalCode(), "00001");
assertNull(address.getProvince());
address = new Address();
address.setId(baseDao.first(Address.class).getId());
address.setPostalCode("00001");
address.setProvince(baseDao.find(Province.class, 0, 2).get(1));
address = addressParser.parse(address);
assertFalse(address.getProvince().getId().equals(baseDao.first(Province.class).getId()));
}
}