Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: CI

on:
push:
branches: [2.3.x, 2.7.x, 3.0.x, 3.1.x, 3.2.x, 3.3.x, 3.4.x, 3.5.x, 4.0.x, 4.1.x]
branches: [2.3.x, 2.7.x, 3.0.x, 3.1.x, 3.2.x, 3.3.x, 3.4.x, 3.5.x, 4.0.x, 4.1.x, "test/**", "feature/REQ-20261118-wandl-ip2region-issue10-35"]
pull_request:
branches: [2.3.x, 2.7.x, 3.0.x, 3.1.x, 3.2.x, 3.3.x, 3.4.x, 3.5.x, 4.0.x, 4.1.x]

Expand All @@ -15,19 +15,23 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v7
- name: Resolve toolchain
id: toolchain
env:
VERSION_LINE: ${{ github.base_ref || github.ref_name }}
shell: bash
run: |
if [[ "$VERSION_LINE" == test/* || "$VERSION_LINE" == "feature/REQ-20261118-wandl-ip2region-issue10-35" ]]; then
VERSION_LINE="$(python3 -c 'from xml.etree import ElementTree as E; version = E.parse("pom.xml").getroot().find("{*}version").text; print(".".join(version.split(".")[:3]))')"
fi
case "$VERSION_LINE" in
2.3.x|2.7.x) echo "java=8" >> "$GITHUB_OUTPUT" ;;
2.0.x|2.3.x|2.7.x) echo "java=8" >> "$GITHUB_OUTPUT" ;;
3.*) echo "java=17" >> "$GITHUB_OUTPUT" ;;
4.*) echo "java=21" >> "$GITHUB_OUTPUT" ;;
*) echo "Unsupported version line: $VERSION_LINE" >&2; exit 1 ;;
esac
- uses: actions/checkout@v7
echo "version-line=$VERSION_LINE" >> "$GITHUB_OUTPUT"
- uses: actions/setup-java@v5
with:
distribution: temurin
Expand All @@ -43,7 +47,7 @@ jobs:
printf '%s' "$MAVEN_SETTINGS_XML" > "$HOME/.m2/settings.xml"
- name: Build and verify with the version-line Maven
env:
VERSION_LINE: ${{ github.base_ref || github.ref_name }}
VERSION_LINE: ${{ steps.toolchain.outputs.version-line }}
shell: bash
run: |
if [ "$VERSION_LINE" = "4.1.x" ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.easy4j.ip2region.spring.boot.ext.XdbSearcher;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -29,11 +30,26 @@ public class IP2regionAutoConfiguration implements ResourceLoaderAware {
*/

@Bean
@ConditionalOnMissingBean
public XdbSearcher xdbSearcher(IP2regionProperties properties) throws IOException {
String ipv4Location = properties.isExternal() ? properties.getLocation() : XdbSearcher.DEFAULT_LOCATION;
return new XdbSearcher(resourceLoader, ipv4Location, properties.isIpv6Enabled(),
properties.getIpv6Location());
}
/**
* Creates a searcher for callers using the original factory signature.
* The supplied loader and IPv4 location take precedence for this direct call.
*
* @param properties IPv4 and IPv6 resource configuration
* @param loader resource loader supplied by the caller
* @return a searcher for the configured resources
* @throws IOException if the searcher cannot be initialized
*/
public XdbSearcher xdbSearcher(IP2regionProperties properties, ResourceLoader loader) throws IOException {
return new XdbSearcher(loader, properties.getLocation(), properties.isIpv6Enabled(),
properties.getIpv6Location());
}

/**
* <p>Ip2region template.</p>
* @param xdbSearcher the xdb searcher
Expand All @@ -42,6 +58,7 @@ public XdbSearcher xdbSearcher(IP2regionProperties properties) throws IOExceptio
*/

@Bean
@ConditionalOnMissingBean
public IP2regionTemplate ip2regionTemplate(XdbSearcher xdbSearcher) throws IOException {
return new IP2regionTemplate(xdbSearcher);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import io.github.easy4j.ip2region.spring.boot.ext.XdbSearcher;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* Tests for {@link IP2regionAutoConfiguration} and {@link IP2regionProperties}.
Expand Down Expand Up @@ -49,6 +53,38 @@ public void beansCreated() {
});
}

@Test
void preservesApplicationProvidedSearcherAndTemplate() throws Exception {
XdbSearcher searcher = mock(XdbSearcher.class);
IP2regionTemplate template = new IP2regionTemplate(searcher);

this.contextRunner.withBean("customSearcher", XdbSearcher.class, () -> searcher)
.withBean("customTemplate", IP2regionTemplate.class, () -> template)
.run(context -> {
assertThat(context).hasSingleBean(XdbSearcher.class).hasSingleBean(IP2regionTemplate.class);
assertThat(context.getBean(XdbSearcher.class)).isSameAs(searcher);
assertThat(context.getBean(IP2regionTemplate.class)).isSameAs(template);
});
}

@Test
void legacyFactoryUsesTheSuppliedResourceLoaderAndLocation() throws Exception {
String location = "classpath:legacy-ipv4.xdb";
ResourceLoader loader = mock(ResourceLoader.class);
when(loader.getResource(location)).thenReturn(new DefaultResourceLoader().getResource(XdbSearcher.DEFAULT_LOCATION));
IP2regionProperties properties = new IP2regionProperties();
properties.setLocation(location);
properties.setIpv6Enabled(false);

XdbSearcher searcher = new IP2regionAutoConfiguration().xdbSearcher(properties, loader);
try {
verify(loader).getResource(location);
assertThat(searcher.memorySearch("8.8.8.8")).isNotEqualTo(XdbSearcher.NOT_MATCH);
} finally {
searcher.destroy();
}
}

@Test
public void propertiesSettersWork() {
IP2regionProperties props = new IP2regionProperties();
Expand Down
Loading