1 package org.apache.archiva.metadata.repository.jcr;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.model.MetadataFacetFactory;
24 import org.apache.archiva.metadata.repository.AbstractMetadataRepositoryTest;
25 import org.apache.archiva.metadata.repository.DefaultMetadataResolver;
26 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
27 import org.apache.archiva.metadata.repository.MetadataSessionException;
28 import org.apache.archiva.metadata.repository.RepositorySession;
29 import org.apache.jackrabbit.oak.segment.file.InvalidFileStoreVersionException;
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
35 import javax.jcr.Node;
36 import javax.jcr.NodeIterator;
37 import javax.jcr.RepositoryException;
38 import javax.jcr.Session;
39 import javax.jcr.query.QueryResult;
40 import javax.jcr.query.Row;
41 import javax.jcr.query.RowIterator;
42 import java.io.IOException;
43 import java.nio.file.Files;
44 import java.nio.file.Path;
45 import java.nio.file.Paths;
46 import java.util.Collection;
49 import static org.apache.archiva.metadata.repository.jcr.JcrConstants.PROJECT_VERSION_NODE_TYPE;
50 import static org.assertj.core.api.Assertions.assertThat;
53 * Using static sessionFactory and repository, because initialization is expensive if we rebuild the whole repository for
56 public class JcrMetadataRepositoryTest
57 extends AbstractMetadataRepositoryTest
60 private static JcrRepositorySessionFactory sessionFactory;
61 private static JcrMetadataRepository repository;
64 public JcrMetadataRepository getRepository( )
70 public JcrRepositorySessionFactory getSessionFactory( )
72 return sessionFactory;
76 public static void setupSpec( ) throws IOException, InvalidFileStoreVersionException
78 Path directory = Paths.get( "target/test-repositories" );
79 if ( Files.exists( directory ) )
81 org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
84 Map<String, MetadataFacetFactory> factories = createTestMetadataFacetFactories( );
85 JcrRepositorySessionFactory jcrSessionFactory = new JcrRepositorySessionFactory( );
86 jcrSessionFactory.setMetadataResolver( new DefaultMetadataResolver( ) );
87 jcrSessionFactory.setMetadataFacetFactories( factories );
89 jcrSessionFactory.open( );
90 sessionFactory = jcrSessionFactory;
91 repository = jcrSessionFactory.getMetadataRepository( );
96 public void setup() throws MetadataRepositoryException, RepositoryException, MetadataSessionException
98 try( JcrRepositorySession session = (JcrRepositorySession) getSessionFactory().createSession() ) {
99 Session jcrSession = session.getJcrSession( );
100 if (jcrSession.itemExists( "/repositories/test" ))
102 jcrSession.removeItem( "/repositories/test" );
109 public static void stopSpec( )
112 if ( repository != null )
118 catch ( Throwable e )
123 if ( sessionFactory != null )
127 sessionFactory.close( );
129 catch ( Throwable e )
138 public void testSearchArtifactsByKey( )
141 try ( RepositorySession session = sessionFactory.createSession( ) )
143 createArtifactWithData( session );
148 try ( RepositorySession session = sessionFactory.createSession( ) )
150 session.refreshAndDiscard( );
151 Session jcrSession = ( (JcrRepositorySession) session ).getJcrSession( );
152 assertThat(jcrSession.propertyExists( "/repositories/test/content/mytest/myproject/1.0/url" )).isTrue();
154 Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, "url", TEST_URL, false );
155 assertThat( artifactsByProperty ).isNotNull( ).isNotEmpty( );
163 public void testSearchArtifactsByKeyExact()
165 try (RepositorySession session = sessionFactory.createSession())
167 createArtifactWithData( session );
169 try (RepositorySession session = sessionFactory.createSession())
171 session.refreshAndDiscard();
173 Session jcrSession = ( (JcrRepositorySession) session ).getJcrSession( );
174 assertThat(jcrSession.propertyExists( "/repositories/test/content/mytest/myproject/1.0/url" )).isTrue();
175 Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, "url", TEST_URL, true);
176 assertThat(artifactsByProperty).describedAs( "Artifact search by url=%s must give a result.", TEST_URL ).isNotNull().isNotEmpty();
177 artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, "org.name", "pache", true );
178 assertThat( artifactsByProperty ).describedAs( "Artifact search by text org.name='pache' must be empty" ).isNotNull( ).isEmpty( );