/*
* (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* Kevin Leturc
*/
package org.nuxeo.ecm.core.version.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import javax.inject.Inject;
import org.junit.runner.RunWith;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.event.EventService;
import org.nuxeo.ecm.core.test.CoreFeature;
import org.nuxeo.ecm.core.test.annotations.Granularity;
import org.nuxeo.ecm.core.test.annotations.RepositoryConfig;
import org.nuxeo.ecm.core.versioning.VersioningService;
import org.nuxeo.runtime.test.runner.Features;
import org.nuxeo.runtime.test.runner.FeaturesRunner;
import org.nuxeo.runtime.transaction.TransactionHelper;
/**
* @since 9.1
*/
@RunWith(FeaturesRunner.class)
@Features(CoreFeature.class)
@RepositoryConfig(cleanup = Granularity.METHOD)
public abstract class AbstractTestVersioning {
@Inject
protected CoreFeature coreFeature;
@Inject
protected VersioningService service;
@Inject
protected EventService eventService;
@Inject
protected CoreSession session;
protected void maybeSleepToNextSecond() {
coreFeature.getStorageConfiguration().maybeSleepToNextSecond();
}
protected void waitForAsyncCompletion() {
nextTransaction();
eventService.waitForAsyncCompletion();
}
protected void nextTransaction() {
if (TransactionHelper.isTransactionActiveOrMarkedRollback()) {
TransactionHelper.commitOrRollbackTransaction();
TransactionHelper.startTransaction();
}
}
protected long getMajor(DocumentModel doc) {
return getVersion(doc, VersioningService.MAJOR_VERSION_PROP);
}
protected long getMinor(DocumentModel doc) {
return getVersion(doc, VersioningService.MINOR_VERSION_PROP);
}
protected long getVersion(DocumentModel doc, String prop) {
Object propVal = doc.getPropertyValue(prop);
if (propVal == null || !(propVal instanceof Long)) {
return -1;
} else {
return ((Long) propVal).longValue();
}
}
protected void assertVersion(String expected, DocumentModel doc) throws Exception {
assertEquals(expected, getMajor(doc) + "." + getMinor(doc));
}
protected void assertLatestVersion(String expected, DocumentModel doc) throws Exception {
DocumentModel ver = doc.getCoreSession().getLastDocumentVersion(doc.getRef());
if (ver == null) {
assertNull(expected);
} else {
assertVersion(expected, ver);
}
}
protected void assertVersionLabel(String expected, DocumentModel doc) {
assertEquals(expected, service.getVersionLabel(doc));
}
}