Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[client] Fix test generation overriding main components #559

Merged
merged 4 commits into from
Jan 28, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
import static java.util.stream.Collectors.toList;

import java.io.FileNotFoundException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.FilerException;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.tools.FileObject;
import javax.tools.StandardLocation;

import io.avaje.http.generator.core.APContext;
Expand Down Expand Up @@ -64,32 +64,40 @@ void read() {
}
}

private List<String> loadMetaInf() {
private Set<String> loadMetaInf() {
var set = new HashSet<String>();
try {
final FileObject fileObject = filer().getResource(StandardLocation.CLASS_OUTPUT, "", Constants.META_INF_COMPONENT);
if (fileObject != null) {
final List<String> lines = new ArrayList<>();
final Reader reader = fileObject.openReader(true);
final LineNumberReader lineReader = new LineNumberReader(reader);
String line;
while ((line = lineReader.readLine()) != null) {
line = line.trim();
if (!line.isEmpty()) {
lines.add(line);
}
}
return lines;
}
addLines(mainMetaInfURI(), set);
addLines(metaInfURI(), set);
} catch (final IOException e) {
logWarn("Error reading services file: " + e.getMessage());
}
return set;
}

private static void addLines(URI uri, HashSet<String> set) {
try (var lines = Files.lines(Path.of(uri))) {
lines.forEach(set::add);
} catch (FileNotFoundException | NoSuchFileException e) {
// logDebug("no services file yet");

} catch (final FilerException e) {
logDebug("FilerException reading services file");

} catch (final Exception e) {
} catch (Exception e) {
logWarn("Error reading services file: " + e.getMessage());
}
return Collections.emptyList();
}

private static URI mainMetaInfURI() throws IOException {
return URI.create(
metaInfURI()
.toString()
.replaceFirst("java/test", "java/main")
.replaceFirst("test-classes", "classes"));
}

private static URI metaInfURI() throws IOException {
return filer()
.getResource(StandardLocation.CLASS_OUTPUT, "", Constants.META_INF_COMPONENT)
.toUri();
}
}
14 changes: 14 additions & 0 deletions tests/test-client/src/test/java/example/github/Dummy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package example.github;

import java.util.List;

import io.avaje.http.api.Client;
import io.avaje.http.api.Get;
import io.avaje.http.client.HttpException;

@Client
public interface Dummy {

@Get("users/{user}/repos")
List<Repo> listRepos(String user, String other) throws HttpException;
}
Loading