-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TRUNK-6224: Update replace ClassLoaderFileOpener with modern API (#4574)
- Loading branch information
Showing
6 changed files
with
110 additions
and
130 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
api/src/main/java/org/openmrs/liquibase/OpenmrsClassLoaderResourceAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
69
api/src/main/java/org/openmrs/util/ClassLoaderFileOpener.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
api/src/test/java/org/openmrs/liquibase/OpenmrsClassLoaderResourceAccessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* 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()); | ||
|
||
OpenmrsClassLoaderResourceAccessor classLoaderFileOpener = new OpenmrsClassLoaderResourceAccessor(classLoader); | ||
try (InputStreamList inputStreamSet = classLoaderFileOpener.openStreams(null, "")){ | ||
assertThat(inputStreamSet.size(), is(0)); | ||
} | ||
} | ||
} |
56 changes: 0 additions & 56 deletions
56
api/src/test/java/org/openmrs/util/databasechange/ClassLoaderFileOpenerTest.java
This file was deleted.
Oops, something went wrong.