PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/core/core/src/test/java/com/zenika/dorm/core/test/repository/PatternRepositoryTest.java

https://github.com/Zenika/DORM
Java | 90 lines | 76 code | 10 blank | 4 comment | 0 complexity | c5b1f18b73b8069c4f7026f3f7b4aacc MD5 | raw file
  1. package com.zenika.dorm.core.test.repository;
  2. import com.google.inject.AbstractModule;
  3. import com.google.inject.Guice;
  4. import com.google.inject.Injector;
  5. import com.zenika.dorm.core.model.DormResource;
  6. import com.zenika.dorm.core.model.impl.DefaultDormResource;
  7. import com.zenika.dorm.core.repository.DormRepositoryConfiguration;
  8. import com.zenika.dorm.core.repository.impl.DormRepositoryPatternAssociate;
  9. import com.zenika.dorm.core.service.spi.ExtensionFactoryServiceLoader;
  10. import com.zenika.dorm.core.test.model.DormMetadataTest;
  11. import org.apache.commons.io.FileUtils;
  12. import org.codehaus.jackson.map.ObjectMapper;
  13. import org.junit.After;
  14. import org.junit.Before;
  15. import org.junit.Test;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.net.URISyntaxException;
  19. import java.net.URL;
  20. import static org.fest.assertions.Assertions.*;
  21. /**
  22. * @author Antoine ROUAZE <antoine.rouaze AT zenika.com>
  23. */
  24. public class PatternRepositoryTest {
  25. private static final String BASE_PATH_REPOSITORY_TEST = "/home/erouan/repository_tmp";
  26. private static final String TEST_JAR = "/com/zenika/dorm/core/test/resources/zenika_test.jar";
  27. private File testJarRepo;
  28. @Before
  29. public void setUp() {
  30. try {
  31. File basePathTest = new File(BASE_PATH_REPOSITORY_TEST + "/com/group_test/antoine/zenika/1.2.0/tmp/");
  32. File basePathTest1 = new File(BASE_PATH_REPOSITORY_TEST + "/com/group_test/zenika_test/1.2.0/tmp/");
  33. basePathTest.mkdirs();
  34. basePathTest1.mkdirs();
  35. testJarRepo = new File(basePathTest1, "zenika_test-1.2.0.jar");
  36. File testJarRepo2 = new File(basePathTest, "zenika-1.2.0.jar");
  37. File testJar = new File(getClass().getResource(TEST_JAR).toURI());
  38. FileUtils.copyFile(testJar, testJarRepo);
  39. FileUtils.copyFile(testJar, testJarRepo2);
  40. } catch (URISyntaxException e) {
  41. throw new RuntimeException("URI syntax exception", e);
  42. } catch (IOException e) {
  43. throw new RuntimeException("IO error", e);
  44. }
  45. }
  46. @Test
  47. public void getTest() {
  48. DormMetadataTest dormMetadata = new DormMetadataTest("1.2.0", "zenika_test");
  49. dormMetadata.setGroupId("com.group_test");
  50. // dormMetadata.setAuthor("greg");
  51. DormResource expectedResource = new DefaultDormResource("zenika_test", "jar", testJarRepo);
  52. Injector injector = Guice.createInjector(new AbstractModule() {
  53. @Override
  54. protected void configure() {
  55. bind(ExtensionFactoryServiceLoader.class);
  56. bind(DormRepositoryConfiguration.class).toInstance(getConfiguration());
  57. }
  58. });
  59. DormRepositoryPatternAssociate repositoryPatternAssociate = injector.getInstance(DormRepositoryPatternAssociate.class);
  60. DormResource resultResource = repositoryPatternAssociate.get(dormMetadata, null);
  61. assertThat(resultResource).isEqualTo(expectedResource);
  62. }
  63. private DormRepositoryConfiguration getConfiguration() {
  64. ObjectMapper mapper = new ObjectMapper();
  65. URL url = getClass().getResource("/com/zenika/dorm/core/test/resources/test_repository_configuration.json");
  66. try {
  67. return mapper.readValue(url, DormRepositoryConfiguration.class);
  68. } catch (IOException e) {
  69. throw new RuntimeException("Unable to retrieve the configuration", e);
  70. }
  71. }
  72. @After
  73. public void tearDown() {
  74. try {
  75. FileUtils.deleteDirectory(new File(BASE_PATH_REPOSITORY_TEST));
  76. } catch (IOException e) {
  77. throw new RuntimeException("Unable to delete this directory: " + BASE_PATH_REPOSITORY_TEST, e);
  78. }
  79. }
  80. }