Skip to content

Commit

Permalink
TRUNK-6224: Update replace ClassLoaderFileOpener with modern API (#4574)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibacher committed Mar 6, 2024
1 parent 8ed43b6 commit 4f99a34
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 130 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.liquibase;

import liquibase.resource.ClassLoaderResourceAccessor;
import liquibase.resource.InputStreamList;
import org.openmrs.util.OpenmrsClassLoader;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

/**
* A customization of Liquibase's {@link ClassLoaderResourceAccessor} which defaults to the OpenMRS ClassLoader and has
* special handling for our liquibase.xml files, which occur multiple times on the classpath.
*/
public class OpenmrsClassLoaderResourceAccessor extends ClassLoaderResourceAccessor {

public OpenmrsClassLoaderResourceAccessor() {
super(OpenmrsClassLoader.getInstance());
}

public OpenmrsClassLoaderResourceAccessor(ClassLoader classLoader) {
super(classLoader);
}

@Override
public InputStreamList openStreams(String relativeTo, String streamPath) throws IOException {
InputStreamList result = super.openStreams(relativeTo, streamPath);
if (result != null && !result.isEmpty() && result.size() > 1) {
try (InputStreamList oldResult = result) {
URI uri = oldResult.getURIs().get(0);
result = new InputStreamList(uri, uri.toURL().openStream());
}

}

return result;
}
}
69 changes: 0 additions & 69 deletions api/src/main/java/org/openmrs/util/ClassLoaderFileOpener.java

This file was deleted.

3 changes: 2 additions & 1 deletion api/src/main/java/org/openmrs/util/DatabaseUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.openmrs.liquibase.ChangeLogVersionFinder;
import org.openmrs.liquibase.ChangeSetExecutorCallback;
import org.openmrs.liquibase.LiquibaseProvider;
import org.openmrs.liquibase.OpenmrsClassLoaderResourceAccessor;
import org.openmrs.module.ModuleClassLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -879,7 +880,7 @@ private static CompositeResourceAccessor getCompositeResourceAccessor(ClassLoade
}
}

ResourceAccessor openmrsFO = new ClassLoaderFileOpener(classLoader);
ResourceAccessor openmrsFO = new OpenmrsClassLoaderResourceAccessor(classLoader);
ResourceAccessor fsFO = new FileSystemResourceAccessor(OpenmrsUtil.getApplicationDataDirectoryAsFile());
return new CompositeResourceAccessor(openmrsFO, fsFO);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import liquibase.resource.InputStreamList;
import liquibase.resource.ResourceAccessor;
import org.openmrs.api.context.Context;
import org.openmrs.util.ClassLoaderFileOpener;
import org.openmrs.liquibase.OpenmrsClassLoaderResourceAccessor;
import org.openmrs.util.OpenmrsClassLoader;
import org.openmrs.util.OpenmrsConstants;
import org.openmrs.util.OpenmrsUtil;
Expand All @@ -30,7 +30,6 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
Expand All @@ -40,7 +39,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.Set;

/**
* Executes (aka "source"s) the given file on the current database. <br>
Expand Down Expand Up @@ -94,7 +92,7 @@ public void execute(Database database) throws CustomChangeException {
try {
tmpOutputFile = File.createTempFile(sqlFile, "tmp");

fileOpener = new ClassLoaderFileOpener(OpenmrsClassLoader.getInstance());
fileOpener = new OpenmrsClassLoaderResourceAccessor(OpenmrsClassLoader.getInstance());
try (InputStreamList sqlFileInputStream = fileOpener.openStreams(null, sqlFile);
OutputStream outputStream = new FileOutputStream(tmpOutputFile)) {
if (sqlFileInputStream != null && !sqlFileInputStream.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.liquibase;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.Collections;

import liquibase.resource.InputStreamList;
import org.junit.jupiter.api.Test;
import org.openmrs.liquibase.OpenmrsClassLoaderResourceAccessor;
import org.openmrs.util.OpenmrsClassLoader;

public class OpenmrsClassLoaderResourceAccessorTest {

@Test
public void shouldGetSingleResourceAsStream() throws Exception {
ClassLoader classLoader = mock(ClassLoader.class);

when(classLoader.getResources(any()))
.thenReturn(OpenmrsClassLoader.getSystemClassLoader().getResources("TestingApplicationContext.xml"));


OpenmrsClassLoaderResourceAccessor classLoaderFileOpener = new OpenmrsClassLoaderResourceAccessor(classLoader);
try (InputStreamList inputStreamSet = classLoaderFileOpener.openStreams(null, "some path")) {
assertEquals(1, inputStreamSet.size());
}
}

@Test
public void shouldGetNoResourceAsStream() throws Exception {
ClassLoader classLoader = mock(ClassLoader.class);

when(classLoader.getResources(any()))
.thenReturn(Collections.emptyEnumeration());


try (OpenmrsClassLoaderResourceAccessor classLoaderFileOpener = new OpenmrsClassLoaderResourceAccessor(classLoader);
InputStreamList inputStreamSet = classLoaderFileOpener.openStreams(null, "")){
assertThat(inputStreamSet.size(), is(0));
}
}
}

This file was deleted.

0 comments on commit 4f99a34

Please sign in to comment.