]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/test/java/org/argeo/server/jcr/JcrResourceAdapterTest.java
Introduce JcrResource adapter
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jackrabbit / src / test / java / org / argeo / server / jcr / JcrResourceAdapterTest.java
1 package org.argeo.server.jcr;
2
3 import java.io.File;
4 import java.io.InputStream;
5 import java.text.SimpleDateFormat;
6 import java.util.Calendar;
7 import java.util.List;
8
9 import junit.framework.TestCase;
10
11 import org.apache.commons.io.FileUtils;
12 import org.apache.commons.io.IOUtils;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.apache.jackrabbit.core.TransientRepository;
16 import org.springframework.core.io.ClassPathResource;
17 import org.springframework.core.io.Resource;
18
19 public class JcrResourceAdapterTest extends TestCase {
20 private static SimpleDateFormat sdf = new SimpleDateFormat(
21 "yyyyMMdd:hhmmss.SSS");
22
23 private final static Log log = LogFactory
24 .getLog(JcrResourceAdapterTest.class);
25
26 private JcrResourceAdapter jra;
27 private TransientRepository repository;
28
29 public void testCreate() throws Exception {
30 String basePath = "/test/subdir";
31 jra.mkdirs(basePath);
32 Resource res = new ClassPathResource("org/argeo/server/jcr/dummy00.xls");
33 String filePath = basePath + "/dummy.xml";
34 jra.create(filePath, res, "application/vnd.ms-excel");
35 InputStream in = jra.retrieve(filePath);
36 assertTrue(IOUtils.contentEquals(res.getInputStream(), in));
37 }
38
39 public void testVersioning() throws Exception {
40 String basePath = "/test/versions";
41 jra.mkdirs(basePath);
42 String filePath = basePath + "/dummy.xml";
43 Resource res00 = new ClassPathResource(
44 "org/argeo/server/jcr/dummy00.xls");
45 jra.create(filePath, res00, "application/vnd.ms-excel");
46 Resource res01 = new ClassPathResource(
47 "org/argeo/server/jcr/dummy01.xls");
48 jra.update(filePath, res01);
49 Resource res02 = new ClassPathResource(
50 "org/argeo/server/jcr/dummy02.xls");
51 jra.update(filePath, res02);
52
53 List<Calendar> versions = jra.listVersions(filePath);
54 for (Calendar version : versions)
55 log.debug(sdf.format(version.getTime()));
56 assertEquals(4, versions.size());
57
58 InputStream in = jra.retrieve(filePath, 1);
59 assertTrue(IOUtils.contentEquals(res01.getInputStream(), in));
60 in = jra.retrieve(filePath, 0);
61 assertTrue(IOUtils.contentEquals(res00.getInputStream(), in));
62 in = jra.retrieve(filePath, 2);
63 assertTrue(IOUtils.contentEquals(res02.getInputStream(), in));
64 Resource res03 = new ClassPathResource(
65 "org/argeo/server/jcr/dummy03.xls");
66 jra.update(filePath, res03);
67 in = jra.retrieve(filePath, 1);
68 assertTrue(IOUtils.contentEquals(res01.getInputStream(), in));
69 }
70
71 @Override
72 protected void setUp() throws Exception {
73 File homeDir = new File(System.getProperty("java.io.tmpdir"),
74 JcrResourceAdapterTest.class.getSimpleName());
75 FileUtils.deleteDirectory(homeDir);
76 Resource res = new ClassPathResource(
77 "org/argeo/server/jcr/repository.xml");
78 repository = new TransientRepository(res.getFile(), homeDir);
79
80 jra = new JcrResourceAdapter();
81 jra.setRepository(repository);
82 jra.setUsername("demo");
83 jra.setPassword("demo");
84 jra.afterPropertiesSet();
85 }
86
87 @Override
88 protected void tearDown() throws Exception {
89 jra.destroy();
90 // repository.shutdown();
91 }
92
93 }