]> git.argeo.org Git - lgpl/argeo-commons.git/blob - JcrResourceAdapterTest.java
525208c4391edaa477c2ce04418b506e7ab7c202
[lgpl/argeo-commons.git] / 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 org.apache.commons.io.IOUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.argeo.jcr.JcrResourceAdapter;
29 import org.argeo.server.jackrabbit.unit.AbstractJcrTestCase;
30 import org.springframework.core.io.ClassPathResource;
31 import org.springframework.core.io.Resource;
32
33 public class JcrResourceAdapterTest extends AbstractJcrTestCase {
34 private static SimpleDateFormat sdf = new SimpleDateFormat(
35 "yyyyMMdd:hhmmss.SSS");
36
37 private final static Log log = LogFactory
38 .getLog(JcrResourceAdapterTest.class);
39
40 private JcrResourceAdapter jra;
41
42 public void testCreate() throws Exception {
43 String basePath = "/test/subdir";
44 jra.mkdirs(basePath);
45 Resource res = new ClassPathResource("org/argeo/server/jcr/dummy00.xls");
46 String filePath = basePath + "/dummy.xml";
47 jra.create(filePath, res, "application/vnd.ms-excel");
48 InputStream in = jra.retrieve(filePath);
49 assertTrue(IOUtils.contentEquals(res.getInputStream(), in));
50 }
51
52 public void testVersioning() throws Exception {
53 String basePath = "/test/versions";
54 jra.mkdirs(basePath);
55 String filePath = basePath + "/dummy.xml";
56 Resource res00 = new ClassPathResource(
57 "org/argeo/server/jcr/dummy00.xls");
58 jra.create(filePath, res00, "application/vnd.ms-excel");
59 Resource res01 = new ClassPathResource(
60 "org/argeo/server/jcr/dummy01.xls");
61 jra.update(filePath, res01);
62 Resource res02 = new ClassPathResource(
63 "org/argeo/server/jcr/dummy02.xls");
64 jra.update(filePath, res02);
65
66 List<Calendar> versions = jra.listVersions(filePath);
67 log.debug("Versions of " + filePath);
68 int count = 0;
69 for (Calendar version : versions) {
70 log.debug(" " + (count == 0 ? "base" : count - 1) + "\t"
71 + sdf.format(version.getTime()));
72 count++;
73 }
74
75 assertEquals(4, versions.size());
76
77 InputStream in = jra.retrieve(filePath, 1);
78 assertTrue(IOUtils.contentEquals(res01.getInputStream(), in));
79 in = jra.retrieve(filePath, 0);
80 assertTrue(IOUtils.contentEquals(res00.getInputStream(), in));
81 in = jra.retrieve(filePath, 2);
82 assertTrue(IOUtils.contentEquals(res02.getInputStream(), in));
83 Resource res03 = new ClassPathResource(
84 "org/argeo/server/jcr/dummy03.xls");
85 jra.update(filePath, res03);
86 in = jra.retrieve(filePath, 1);
87 assertTrue(IOUtils.contentEquals(res01.getInputStream(), in));
88 }
89
90 @Override
91 protected void setUp() throws Exception {
92 log.debug("SET UP");
93 super.setUp();
94 jra = new JcrResourceAdapter();
95 jra.setSession(session());
96 }
97
98 @Override
99 protected void tearDown() throws Exception {
100 log.debug("TEAR DOWN");
101 super.tearDown();
102 }
103
104 protected File getRepositoryFile() throws Exception {
105 Resource res = new ClassPathResource(
106 "org/argeo/server/jcr/repository.xml");
107 return res.getFile();
108 }
109
110 }