com.devnexus.ting.core.dao.DocumentDaoTest.java Source code

Java tutorial

Introduction

Here is the source code for com.devnexus.ting.core.dao.DocumentDaoTest.java

Source

/*
 * Copyright 2002-2013 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 com.devnexus.ting.core.dao;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.transaction.annotation.Transactional;

import com.devnexus.ting.model.FileData;
import com.devnexus.ting.repository.FileDataRepository;

/**
 * @author Gunnar Hillert
 */
@Transactional
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class DocumentDaoTest extends BaseDaoIntegrationTest {

    @Autowired
    private FileDataRepository documentDao;

    /**
     * Test to verify that the seed data is correctly populated. Typically there
     * should be 10 industries in the system:
     *
     */
    @Test
    public void testInsertDocument() throws IOException {

        FileData document = new FileData();
        document.setFileModified(new Date());
        document.setName("cartman.jpg");
        document.setType("image/jpeg");

        InputStream is = DocumentDaoTest.class.getResourceAsStream("/images/speakers/hans_dockter.jpg");

        byte[] imageData = IOUtils.toByteArray(is);
        document.setFileSize(Long.valueOf(imageData.length));
        document.setFileData(imageData);

        FileData savedDocument = documentDao.save(document);
        super.entityManager.flush();

        FileData docFromDb = documentDao.getOne(savedDocument.getId());

        Assert.assertEquals("cartman.jpg", docFromDb.getName());

    }

}