/*
* 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 java.util.Date;
import org.internna.iwebmvc.dao.DAO;
import org.internna.iwebmvc.dao.SecurityDAO;
import org.internna.iwebmvc.model.Document;
import org.internna.iwebmvc.model.security.RoleImpl;
import org.internna.iwebmvc.model.security.UserImpl;
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 org.springframework.util.FileCopyUtils;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring.xml", "/documents.xml"})
public class DocumentParserTest {
@Autowired private DAO baseDao;
@Autowired private SecurityDAO securityDAO;
@Autowired private DocumentParser documentParser;
@Before
public void init() throws Exception {
securityDAO.createAuthority("admin");
UserImpl user = new UserImpl();
user.setUsername("iwebmvc");
user.setName("IWebMvc");
user.setPassword("iwebmvc");
user.addRole((RoleImpl) securityDAO.findAuthority("admin"));
securityDAO.createUser(user);
Document doc = new Document();
doc.setTemporal(false);
doc.setIdentifier("void image");
doc.setWidth(1);
doc.setHeight(1);
doc.setCreation(new Date());
doc.setMimeType("image/gif");
doc.setAuthor((UserImpl) securityDAO.findUser("iwebmvc"));
doc.setContents(FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("/void.gif")));
baseDao.create(doc);
}
@Test
public void testParse() throws Exception {
assertNull(documentParser.parse(null));
Document up = new Document();
assertNull(documentParser.parse(up));
up.setIdentifier("new upload");
up.setId(baseDao.first(Document.class).getId());
up = documentParser.parse(up);
assertNotNull(up.getAuthor());
assertEquals(up.getIdentifier(), "new upload");
}
}