/core/core/src/test/java/com/zenika/dorm/core/test/repository/PatternRepositoryTest.java
Java | 90 lines | 76 code | 10 blank | 4 comment | 0 complexity | c5b1f18b73b8069c4f7026f3f7b4aacc MD5 | raw file
- package com.zenika.dorm.core.test.repository;
- import com.google.inject.AbstractModule;
- import com.google.inject.Guice;
- import com.google.inject.Injector;
- import com.zenika.dorm.core.model.DormResource;
- import com.zenika.dorm.core.model.impl.DefaultDormResource;
- import com.zenika.dorm.core.repository.DormRepositoryConfiguration;
- import com.zenika.dorm.core.repository.impl.DormRepositoryPatternAssociate;
- import com.zenika.dorm.core.service.spi.ExtensionFactoryServiceLoader;
- import com.zenika.dorm.core.test.model.DormMetadataTest;
- import org.apache.commons.io.FileUtils;
- import org.codehaus.jackson.map.ObjectMapper;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import java.io.File;
- import java.io.IOException;
- import java.net.URISyntaxException;
- import java.net.URL;
- import static org.fest.assertions.Assertions.*;
- /**
- * @author Antoine ROUAZE <antoine.rouaze AT zenika.com>
- */
- public class PatternRepositoryTest {
- private static final String BASE_PATH_REPOSITORY_TEST = "/home/erouan/repository_tmp";
- private static final String TEST_JAR = "/com/zenika/dorm/core/test/resources/zenika_test.jar";
- private File testJarRepo;
- @Before
- public void setUp() {
- try {
- File basePathTest = new File(BASE_PATH_REPOSITORY_TEST + "/com/group_test/antoine/zenika/1.2.0/tmp/");
- File basePathTest1 = new File(BASE_PATH_REPOSITORY_TEST + "/com/group_test/zenika_test/1.2.0/tmp/");
- basePathTest.mkdirs();
- basePathTest1.mkdirs();
- testJarRepo = new File(basePathTest1, "zenika_test-1.2.0.jar");
- File testJarRepo2 = new File(basePathTest, "zenika-1.2.0.jar");
- File testJar = new File(getClass().getResource(TEST_JAR).toURI());
- FileUtils.copyFile(testJar, testJarRepo);
- FileUtils.copyFile(testJar, testJarRepo2);
- } catch (URISyntaxException e) {
- throw new RuntimeException("URI syntax exception", e);
- } catch (IOException e) {
- throw new RuntimeException("IO error", e);
- }
- }
- @Test
- public void getTest() {
- DormMetadataTest dormMetadata = new DormMetadataTest("1.2.0", "zenika_test");
- dormMetadata.setGroupId("com.group_test");
- // dormMetadata.setAuthor("greg");
- DormResource expectedResource = new DefaultDormResource("zenika_test", "jar", testJarRepo);
- Injector injector = Guice.createInjector(new AbstractModule() {
- @Override
- protected void configure() {
- bind(ExtensionFactoryServiceLoader.class);
- bind(DormRepositoryConfiguration.class).toInstance(getConfiguration());
- }
- });
- DormRepositoryPatternAssociate repositoryPatternAssociate = injector.getInstance(DormRepositoryPatternAssociate.class);
- DormResource resultResource = repositoryPatternAssociate.get(dormMetadata, null);
- assertThat(resultResource).isEqualTo(expectedResource);
- }
- private DormRepositoryConfiguration getConfiguration() {
- ObjectMapper mapper = new ObjectMapper();
- URL url = getClass().getResource("/com/zenika/dorm/core/test/resources/test_repository_configuration.json");
- try {
- return mapper.readValue(url, DormRepositoryConfiguration.class);
- } catch (IOException e) {
- throw new RuntimeException("Unable to retrieve the configuration", e);
- }
- }
- @After
- public void tearDown() {
- try {
- FileUtils.deleteDirectory(new File(BASE_PATH_REPOSITORY_TEST));
- } catch (IOException e) {
- throw new RuntimeException("Unable to delete this directory: " + BASE_PATH_REPOSITORY_TEST, e);
- }
- }
- }