]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr/src/test/java/org/argeo/server/jcr/JcrResourceAdapterTest.java
Reset to a proper SNAPSHOT state
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr / src / test / java / org / argeo / server / jcr / JcrResourceAdapterTest.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.server.jcr;
18
19 import java.io.File;
20 import java.io.InputStream;
21 import java.text.SimpleDateFormat;
22 import java.util.Calendar;
23 import java.util.List;
24
25 import javax.jcr.Repository;
26
27 import org.apache.commons.io.IOUtils;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.jackrabbit.core.TransientRepository;
31 import org.argeo.jcr.JcrResourceAdapter;
32 import org.argeo.jcr.unit.AbstractJcrTestCase;
33 import org.springframework.core.io.ClassPathResource;
34 import org.springframework.core.io.Resource;
35
36 public class JcrResourceAdapterTest extends AbstractJcrTestCase {
37 private static SimpleDateFormat sdf = new SimpleDateFormat(
38 "yyyyMMdd:hhmmss.SSS");
39
40 private final static Log log = LogFactory
41 .getLog(JcrResourceAdapterTest.class);
42
43 private JcrResourceAdapter jra;
44
45 public void testCreate() throws Exception {
46 String basePath = "/test/subdir";
47 jra.mkdirs(basePath);
48 Resource res = new ClassPathResource("org/argeo/server/jcr/dummy00.xls");
49 String filePath = basePath + "/dummy.xml";
50 jra.create(filePath, res, "application/vnd.ms-excel");
51 InputStream in = jra.retrieve(filePath);
52 assertTrue(IOUtils.contentEquals(res.getInputStream(), in));
53 }
54
55 public void testVersioning() throws Exception {
56 String basePath = "/test/versions";
57 jra.mkdirs(basePath);
58 String filePath = basePath + "/dummy.xml";
59 Resource res00 = new ClassPathResource(
60 "org/argeo/server/jcr/dummy00.xls");
61 jra.create(filePath, res00, "application/vnd.ms-excel");
62 Resource res01 = new ClassPathResource(
63 "org/argeo/server/jcr/dummy01.xls");
64 jra.update(filePath, res01);
65 Resource res02 = new ClassPathResource(
66 "org/argeo/server/jcr/dummy02.xls");
67 jra.update(filePath, res02);
68
69 List<Calendar> versions = jra.listVersions(filePath);
70 log.debug("Versions of " + filePath);
71 int count = 0;
72 for (Calendar version : versions) {
73 log.debug(" " + (count == 0 ? "base" : count - 1) + "\t"
74 + sdf.format(version.getTime()));
75 count++;
76 }
77
78 assertEquals(4, versions.size());
79
80 InputStream in = jra.retrieve(filePath, 1);
81 assertTrue(IOUtils.contentEquals(res01.getInputStream(), in));
82 in = jra.retrieve(filePath, 0);
83 assertTrue(IOUtils.contentEquals(res00.getInputStream(), in));
84 in = jra.retrieve(filePath, 2);
85 assertTrue(IOUtils.contentEquals(res02.getInputStream(), in));
86 Resource res03 = new ClassPathResource(
87 "org/argeo/server/jcr/dummy03.xls");
88 jra.update(filePath, res03);
89 in = jra.retrieve(filePath, 1);
90 assertTrue(IOUtils.contentEquals(res01.getInputStream(), in));
91 }
92
93 @Override
94 protected void setUp() throws Exception {
95 log.debug("SET UP");
96 super.setUp();
97 jra = new JcrResourceAdapter();
98 jra.setSession(session());
99 }
100
101 @Override
102 protected void tearDown() throws Exception {
103 log.debug("TEAR DOWN");
104 super.tearDown();
105 }
106
107 protected File getRepositoryFile() throws Exception {
108 Resource res = new ClassPathResource(
109 "org/argeo/server/jcr/repository.xml");
110 return res.getFile();
111 }
112
113 protected Repository createRepository() throws Exception {
114 Repository repository = new TransientRepository(getRepositoryFile(),
115 getHomeDir());
116 return repository;
117 }
118
119 }