Merge pull request #41 from ThomasRubini/reformat

This commit is contained in:
Thomas Rubini 2022-12-15 20:17:22 +01:00 committed by GitHub
commit 737b23f5a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 545 additions and 535 deletions

View File

@ -5,18 +5,20 @@ import com.beust.jcommander.JCommander;
/**
* Class to parse the command line arguments given by the user
*
* @author Capelier-Marla
*/
public class ArgParse {
/* distribution the user want */
/* distribution the user want */
private static String distribution;
private static String packet;
private static String packet;
/**
* Get the command line argument given by the user, parse it with the parser and store it in the corresponding variable
* @author Capelier-Marla
*
* @param args the command line arguments given by the user
* @author Capelier-Marla
*/
static void parseArguments(String[] args) {
// create JCommander and CommandLineParams objects
@ -24,43 +26,45 @@ public class ArgParse {
CommandLineParams params = new CommandLineParams();
// add argument required by the params to the JCommander object
jCommander.addObject(params);
try {
// parse the argument from list of String
jCommander.parse(args);
if(params.help) {
jCommander.setProgramName("PackageViewer");
jCommander.usage();
System.exit(0);
} else {
// store the argument parsed in the variable
packet = params.packet;
distribution = params.distribution;
}
try {
// parse the argument from list of String
jCommander.parse(args);
if (params.help) {
jCommander.setProgramName("PackageViewer");
jCommander.usage();
System.exit(0);
} else {
// store the argument parsed in the variable
packet = params.packet;
distribution = params.distribution;
}
} catch (Exception e) {
// if the parsing failed, print the error message and exit the program
System.out.println("You forgot something, please enter the package name and the distribution name if you want to search in a specific one");
jCommander.setProgramName("PackageViewer");
jCommander.usage();
System.exit(0);
}
} catch (Exception e) {
// if the parsing failed, print the error message and exit the program
System.out.println("You forgot something, please enter the package name and the distribution name if you want to search in a specific one");
jCommander.setProgramName("PackageViewer");
jCommander.usage();
System.exit(0);
}
}
/**
* Get the distribution name. If the user didn't give any or if we didn't parse it, return null
* @author Capelier-Marla
* @return String: the distribution name
*/
public static String getDistribution() {
return distribution;
}
/**
* Get the distribution name. If the user didn't give any or if we didn't parse it, return null
*
* @return String: the distribution name
* @author Capelier-Marla
*/
public static String getDistribution() {
return distribution;
}
/**
* Get the packet name, this one isn't optional
* @author Capelier-Marla
* @return String: the packet name
*/
public static String getPacket() {
return packet;
}
/**
* Get the packet name, this one isn't optional
*
* @return String: the packet name
* @author Capelier-Marla
*/
public static String getPacket() {
return packet;
}
}

View File

@ -4,29 +4,30 @@ import com.beust.jcommander.Parameter;
/**
* Class to store and get the command line arguments given by the user
*
* @author Capelier-Marla
*/
public class CommandLineParams {
/**
* Packet the user want to search, only parameter without names
*/
@Parameter(description = "Package to search",
required = true)
public String packet;
/**
* Packet the user want to search, only parameter without names
*/
@Parameter(description = "Package to search",
required = true)
public String packet;
/**
* Distribution the user want to search packages in
*/
@Parameter(names = {"--distro", "-d"},
description = "Linux distribution to search in")
public String distribution;
/**
* Distribution the user want to search packages in
*/
@Parameter(names = {"--distro", "-d"},
description = "Linux distribution to search in")
public String distribution;
/**
* Displays the help
*/
@Parameter(names = {"--help", "-h"},
description = "Display this help",
help = true)
public boolean help = false;
/**
* Displays the help
*/
@Parameter(names = {"--help", "-h"},
description = "Display this help",
help = true)
public boolean help = false;
}

View File

@ -9,55 +9,59 @@ import java.util.List;
/**
* Enum containing distribution information to get them by their name
*
* @author Capelier-Marla
*/
public enum DistributionEnum {
ARCH("arch", new ArchDistribution()),
FEDORA("fedora", new FedoraDistribution()),
;
ARCH("arch", new ArchDistribution()),
FEDORA("fedora", new FedoraDistribution()),
;
private final String name;
private final Distribution distributionConstructor;
private final String name;
private final Distribution distributionConstructor;
/**
* Constructor for enums
* @param name the name of the distribution
* @param distributionConstructor the instance of the distribution
* @author Capelier-Marla
*/
DistributionEnum(String name, Distribution distributionConstructor) {
this.name = name;
this.distributionConstructor = distributionConstructor;
}
/**
* Constructor for enums
*
* @param name the name of the distribution
* @param distributionConstructor the instance of the distribution
* @author Capelier-Marla
*/
DistributionEnum(String name, Distribution distributionConstructor) {
this.name = name;
this.distributionConstructor = distributionConstructor;
}
/**
* Get the distribution instance for the distribution requested in String
* @param name name of the distribution requested
* @return the instance of the distribution requested
* @author Capelier-Marla
*/
public static Distribution getDistributionConstructorByName(String name) {
// loop for all distributions stored in enum
for(var distrib : values()) {
// return the instance if it's the same as enum name
if(distrib.name.equals(name)) {
return distrib.distributionConstructor;
}
}
return null;
}
/**
* Get the distribution instance for the distribution requested in String
*
* @param name name of the distribution requested
* @return the instance of the distribution requested
* @author Capelier-Marla
*/
public static Distribution getDistributionConstructorByName(String name) {
// loop for all distributions stored in enum
for (var distrib : values()) {
// return the instance if it's the same as enum name
if (distrib.name.equals(name)) {
return distrib.distributionConstructor;
}
}
return null;
}
/**
* Get all distribution instances available in this enum
* @return the list of distribution instances
*/
public static List<Distribution> getAllDistributionsInstances() {
// create the set that will be returned
List<Distribution> result = new ArrayList<>();
// add all the distribution instances in the set
for(var distrib : values()) {
result.add(distrib.distributionConstructor);
}
return result;
}
/**
* Get all distribution instances available in this enum
*
* @return the list of distribution instances
*/
public static List<Distribution> getAllDistributionsInstances() {
// create the set that will be returned
List<Distribution> result = new ArrayList<>();
// add all the distribution instances in the set
for (var distrib : values()) {
result.add(distrib.distributionConstructor);
}
return result;
}
}

View File

@ -65,10 +65,10 @@ public class Pair<K, V> {
}
/**
* Returns a string representation of the pair
*
* @return String, string representation of the pair
*/
* Returns a string representation of the pair
*
* @return String, string representation of the pair
*/
@Override
public String toString() {
return "Pair{key=%s,value=%s}".formatted(first, second);

View File

@ -20,6 +20,7 @@ public class Searcher {
/**
* Get the list of all packages in the distribution set before
*
* @param packageName the name of the package wanted
* @return the list of all packages found
* @author Capelier-Marla
@ -28,11 +29,11 @@ public class Searcher {
// we add all instanced constructors in a list, only one if defined at creation of the object
List<Distribution> distributions;
if(distributionName == null) {
if (distributionName == null) {
distributions = DistributionEnum.getAllDistributionsInstances();
} else {
distributions = Collections.singletonList(DistributionEnum.getDistributionConstructorByName(distributionName));
if(distributions.get(0) == null) {
if (distributions.get(0) == null) {
System.out.println("Distribution non trouvée");
System.exit(0);
}
@ -48,7 +49,7 @@ public class Searcher {
}
// we get all packages waiting for them to be received
for(Future<List<SearchedPackage>> futurePackageList : listFuturePackagesList ) {
for (Future<List<SearchedPackage>> futurePackageList : listFuturePackagesList) {
try {
List<SearchedPackage> tempList = futurePackageList.get();
allPackages.addAll(tempList);
@ -61,7 +62,7 @@ public class Searcher {
public Package getPackage(SearchedPackage packetInput) {
if(distributionName == null) {
if (distributionName == null) {
distributionName = packetInput.getDistribution();
}
String packageName = packetInput.getName();

View File

@ -25,123 +25,123 @@ import java.util.logging.Logger;
*/
public class ArchDistribution extends AsyncRequestsParser implements Distribution {
/**
* Logger object used to split debug output and the application output
*/
private static final Logger logger = LoggerManager.getLogger("ArchDistribution");
/**
* Logger object used to split debug output and the application output
*/
private static final Logger logger = LoggerManager.getLogger("ArchDistribution");
/**
* This method remove all characters in the first string passed as
* parameter after one of the character in the second string if found
* in the first string
*
* @param str String, the string to trim
* @param trimAfterCharacters String, the character that delimits our string
* @return the string after being trimmed
*/
private static String trimAfterCharacters(String str, String trimAfterCharacters) {
for (char c : trimAfterCharacters.toCharArray()) {
int index = str.indexOf(c);
if (index > 0)
str = str.substring(index);
}
return str;
}
/**
* This method remove all characters in the first string passed as
* parameter after one of the character in the second string if found
* in the first string
*
* @param str String, the string to trim
* @param trimAfterCharacters String, the character that delimits our string
* @return the string after being trimmed
*/
private static String trimAfterCharacters(String str, String trimAfterCharacters) {
for (char c : trimAfterCharacters.toCharArray()) {
int index = str.indexOf(c);
if (index > 0)
str = str.substring(index);
}
return str;
}
/**
* This function return a package from arch package api in the form of a Pair
* Composed of a Package object, and a set of string containing the names of
* the dependencies of the package.
*
* @param packageName String, The package's exact name
* @return Pair of Package and Set of String
*/
@Override
public CompletableFuture<Pair<Package, Set<String>>> getPackageFromAPI(String packageName) {
// create a new http client and make a request to the arch research api
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest
.newBuilder(URI.create("https://archlinux.org/packages/search/json/?name=" + packageName)).build();
/**
* This function return a package from arch package api in the form of a Pair
* Composed of a Package object, and a set of string containing the names of
* the dependencies of the package.
*
* @param packageName String, The package's exact name
* @return Pair of Package and Set of String
*/
@Override
public CompletableFuture<Pair<Package, Set<String>>> getPackageFromAPI(String packageName) {
// create a new http client and make a request to the arch research api
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest
.newBuilder(URI.create("https://archlinux.org/packages/search/json/?name=" + packageName)).build();
CompletableFuture<Pair<Package, Set<String>>> futureResult = new CompletableFuture<>();
// send the request and when there's a response
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> {
//parse the json response
JSONObject json = new JSONObject(result.body());
CompletableFuture<Pair<Package, Set<String>>> futureResult = new CompletableFuture<>();
// send the request and when there's a response
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> {
//parse the json response
JSONObject json = new JSONObject(result.body());
// check if the response contains something
JSONArray resultsArrayJson = json.getJSONArray("results");
if (resultsArrayJson.length() == 0) {
// unknown package, probably an abstract dependency
futureResult.complete(null);
return;
}
JSONObject resultJson = resultsArrayJson.getJSONObject(0);
Set<String> dependenciesNames = new HashSet<>();
// parse dependencies without version requirements (bash >= 3.0) -> (bash)
for (Object dependency : resultJson.getJSONArray("depends")) {
dependenciesNames.add(trimAfterCharacters((String) dependency, "<>="));
}
//Create the package and store it and its set of deps in a pair
futureResult.complete(new Pair<>(
new Package(
resultJson.getString("pkgname"),
resultJson.getString("pkgver"),
resultJson.getString("repo"),
resultJson.getString("pkgdesc"),
"arch"),
dependenciesNames));
}).exceptionally(error -> {
error.printStackTrace();
logger.warning("Error while fetching package %s from the API : \n%s".formatted(packageName, error));
futureResult.complete(null);
return null;
});
// check if the response contains something
JSONArray resultsArrayJson = json.getJSONArray("results");
if (resultsArrayJson.length() == 0) {
// unknown package, probably an abstract dependency
futureResult.complete(null);
return;
}
JSONObject resultJson = resultsArrayJson.getJSONObject(0);
Set<String> dependenciesNames = new HashSet<>();
// parse dependencies without version requirements (bash >= 3.0) -> (bash)
for (Object dependency : resultJson.getJSONArray("depends")) {
dependenciesNames.add(trimAfterCharacters((String) dependency, "<>="));
}
//Create the package and store it and its set of deps in a pair
futureResult.complete(new Pair<>(
new Package(
resultJson.getString("pkgname"),
resultJson.getString("pkgver"),
resultJson.getString("repo"),
resultJson.getString("pkgdesc"),
"arch"),
dependenciesNames));
}).exceptionally(error -> {
error.printStackTrace();
logger.warning("Error while fetching package %s from the API : \n%s".formatted(packageName, error));
futureResult.complete(null);
return null;
});
return futureResult;
return futureResult;
}
}
/**
* Search for a package matching a pattern and return a list of packages and
* return a list of string matching this pattern.
*
* @param packageName String, the pattern to search in the repositories
* @return List of SearchedPackage objects
*/
public CompletableFuture<List<SearchedPackage>> searchPackage(String packageName) {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://archlinux.org/packages/search/json/?q=" + packageName))
.build();
/**
* Search for a package matching a pattern and return a list of packages and
* return a list of string matching this pattern.
*
* @param packageName String, the pattern to search in the repositories
* @return List of SearchedPackage objects
*/
public CompletableFuture<List<SearchedPackage>> searchPackage(String packageName) {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://archlinux.org/packages/search/json/?q=" + packageName))
.build();
CompletableFuture<List<SearchedPackage>> futureSearchedPackages = new CompletableFuture<>();
CompletableFuture<List<SearchedPackage>> futureSearchedPackages = new CompletableFuture<>();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> {
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> {
List<SearchedPackage> searchedPackagesList = new ArrayList<>();
JSONObject json = new JSONObject(result.body());
List<SearchedPackage> searchedPackagesList = new ArrayList<>();
JSONObject json = new JSONObject(result.body());
// iterate for every package in the list
for (Object searchResultObj : json.getJSONArray("results")) {
// convert object into String
JSONObject searchResultJson = (JSONObject) searchResultObj;
// add package into to list
searchedPackagesList.add(new SearchedPackage(
searchResultJson.getString("pkgname"),
searchResultJson.getString("pkgver"),
searchResultJson.getString("repo"),
searchResultJson.getString("pkgdesc"),
"arch"));
}
futureSearchedPackages.complete(searchedPackagesList);
}).exceptionally(error -> {
error.printStackTrace();
futureSearchedPackages.complete(Collections.emptyList());
return null;
});
// iterate for every package in the list
for (Object searchResultObj : json.getJSONArray("results")) {
// convert object into String
JSONObject searchResultJson = (JSONObject) searchResultObj;
// add package into to list
searchedPackagesList.add(new SearchedPackage(
searchResultJson.getString("pkgname"),
searchResultJson.getString("pkgver"),
searchResultJson.getString("repo"),
searchResultJson.getString("pkgdesc"),
"arch"));
}
futureSearchedPackages.complete(searchedPackagesList);
}).exceptionally(error -> {
error.printStackTrace();
futureSearchedPackages.complete(Collections.emptyList());
return null;
});
return futureSearchedPackages;
return futureSearchedPackages;
}
}
}

View File

@ -24,128 +24,128 @@ import java.util.logging.Logger;
*/
public class FedoraDistribution extends AsyncRequestsParser implements Distribution {
/**
* Logger object used to split debug output and the application output
*/
private static final Logger logger = LoggerManager.getLogger("FedoraDistribution");
/**
* Logger object used to split debug output and the application output
*/
private static final Logger logger = LoggerManager.getLogger("FedoraDistribution");
/**
* This function return a package from Fedora metadata api in the form of a
* Pair Composed of a Package object, and a set of string containing the
* names of the dependencies of the package.
*
* @param packageName String, The package's exact name
* @return Pair of Package and Set of String
*/
protected CompletableFuture<Pair<Package, Set<String>>> getPackageFromAPI(String packageName) {
// create a new http client
HttpClient client = HttpClient.newHttpClient();
// and create its url
String url = "https://mdapi.fedoraproject.org/rawhide/pkg/" + packageName + "";
HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
/**
* This function return a package from Fedora metadata api in the form of a
* Pair Composed of a Package object, and a set of string containing the
* names of the dependencies of the package.
*
* @param packageName String, The package's exact name
* @return Pair of Package and Set of String
*/
protected CompletableFuture<Pair<Package, Set<String>>> getPackageFromAPI(String packageName) {
// create a new http client
HttpClient client = HttpClient.newHttpClient();
// and create its url
String url = "https://mdapi.fedoraproject.org/rawhide/pkg/" + packageName + "";
HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
CompletableFuture<Pair<Package, Set<String>>> futureResult = new CompletableFuture<>();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> {
CompletableFuture<Pair<Package, Set<String>>> futureResult = new CompletableFuture<>();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> {
String body = result.body();
String body = result.body();
if (body.contains("404: Not Found")) {
futureResult.complete(null);
return;
}
if (body.contains("404: Not Found")) {
futureResult.complete(null);
return;
}
JSONObject json = new JSONObject(result.body());
JSONObject json = new JSONObject(result.body());
// get infos
Set<String> dependenciesNames = new HashSet<>();
// get infos
Set<String> dependenciesNames = new HashSet<>();
for (Object depPackageObj : json.getJSONArray("requires")) {
// convert object into String
JSONObject depPackageJson = (JSONObject) depPackageObj;
// add package into Package List
String depName = depPackageJson.getString("name");
if (depName.contains(".so"))
continue;
if (depName.contains("/"))
continue;
dependenciesNames.add(depName);
}
for (Object depPackageObj : json.getJSONArray("requires")) {
// convert object into String
JSONObject depPackageJson = (JSONObject) depPackageObj;
// add package into Package List
String depName = depPackageJson.getString("name");
if (depName.contains(".so"))
continue;
if (depName.contains("/"))
continue;
dependenciesNames.add(depName);
}
futureResult.complete(new Pair<>(
new Package(
json.getString("basename"),
json.getString("version"),
"rawhide",
json.getString("summary"),
"fedora"
),
dependenciesNames
));
}).exceptionally(error->{
error.printStackTrace();
logger.warning("Error while fetching package %s from the API : \n%s".formatted(packageName, error));
futureResult.complete(null);
return null;
});
// if there's an error, return an empty string
return futureResult;
}
futureResult.complete(new Pair<>(
new Package(
json.getString("basename"),
json.getString("version"),
"rawhide",
json.getString("summary"),
"fedora"
),
dependenciesNames
));
}).exceptionally(error -> {
error.printStackTrace();
logger.warning("Error while fetching package %s from the API : \n%s".formatted(packageName, error));
futureResult.complete(null);
return null;
});
// if there's an error, return an empty string
return futureResult;
}
/**
* Search for a package matching a pattern and return a list of packages and
* return a list of string matching this pattern.
*
* @param packageName String, the pattern to search in the repositories
* @return List of SearchedPackage objects
*/
@Override
public CompletableFuture<List<SearchedPackage>> searchPackage(String packageName) {
// create a new http client and make a request to the fedora research api
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(
"https://src.fedoraproject.org/api/0/projects?namepace=rpms&per_page=100&short=true&pattern=*"
+ packageName + "*"))
.build();
/**
* Search for a package matching a pattern and return a list of packages and
* return a list of string matching this pattern.
*
* @param packageName String, the pattern to search in the repositories
* @return List of SearchedPackage objects
*/
@Override
public CompletableFuture<List<SearchedPackage>> searchPackage(String packageName) {
// create a new http client and make a request to the fedora research api
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(
"https://src.fedoraproject.org/api/0/projects?namepace=rpms&per_page=100&short=true&pattern=*"
+ packageName + "*"))
.build();
CompletableFuture<List<SearchedPackage>> futureSearchedPackages = new CompletableFuture<>();
CompletableFuture<List<SearchedPackage>> futureSearchedPackages = new CompletableFuture<>();
// send the request and when there's a response
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> {
//parse the json response
JSONObject json = new JSONObject(result.body());
// send the request and when there's a response
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> {
//parse the json response
JSONObject json = new JSONObject(result.body());
List<SearchedPackage> searchedPackagesList = new ArrayList<>();
List<SearchedPackage> searchedPackagesList = new ArrayList<>();
// iterate for every package in the list
for (Object searchResultObj : json.getJSONArray("projects")) {
// convert object into String
JSONObject searchResultJson = (JSONObject) searchResultObj;
// iterate for every package in the list
for (Object searchResultObj : json.getJSONArray("projects")) {
// convert object into String
JSONObject searchResultJson = (JSONObject) searchResultObj;
// get infos
String name = searchResultJson.getString("name");
// get infos
String name = searchResultJson.getString("name");
// do not include fork projects in the list
if(!name.startsWith("fork/")){
// do not include fork projects in the list
if (!name.startsWith("fork/")) {
// add package into to list
searchedPackagesList.add(new SearchedPackage(
name,
null,
"rawhide",
searchResultJson.getString("description"),
"fedora"
));
}
// add package into to list
searchedPackagesList.add(new SearchedPackage(
name,
null,
"rawhide",
searchResultJson.getString("description"),
"fedora"
));
}
}
futureSearchedPackages.complete(searchedPackagesList);
}).exceptionally(error -> {
error.printStackTrace();
futureSearchedPackages.complete(Collections.emptyList());
return null;
});
}
futureSearchedPackages.complete(searchedPackagesList);
}).exceptionally(error -> {
error.printStackTrace();
futureSearchedPackages.complete(Collections.emptyList());
return null;
});
return futureSearchedPackages;
}
return futureSearchedPackages;
}
}

View File

@ -6,6 +6,7 @@ import fr.packageviewer.pack.SearchedPackage;
import java.util.List;
public interface Frontend {
SearchedPackage askUserToChoosePackage(List<SearchedPackage> packets);
void showPackageTree(Package packet, int depth);
SearchedPackage askUserToChoosePackage(List<SearchedPackage> packets);
void showPackageTree(Package packet, int depth);
}

View File

@ -2,15 +2,11 @@ package fr.packageviewer.frontend;
public class FrontendFactory {
public static Frontend get(String name){
switch(name){
case "terminal":{
return new FrontendTerminal();
}
default:{
throw new IllegalArgumentException("Invalid frontend");
}
}
}
public static Frontend get(String name) {
if (name.equals("terminal")) {
return new FrontendTerminal();
}
throw new IllegalArgumentException("Invalid frontend");
}
}

View File

@ -6,15 +6,16 @@ import fr.packageviewer.pack.SearchedPackage;
import java.util.List;
import java.util.Scanner;
public class FrontendTerminal implements Frontend{
public class FrontendTerminal implements Frontend {
/**
/**
* Check if the String given is a number
*
* @param i the String given
* @return true if the String is a number
* @author Capelier-Marla
*/
private static boolean isNumeric(String i) {
private static boolean isNumeric(String i) {
try {
Integer.parseInt(i);
return true;
@ -23,21 +24,21 @@ public class FrontendTerminal implements Frontend{
}
}
@Override
public SearchedPackage askUserToChoosePackage(List<SearchedPackage> packets) {
// list all packages in reverse order
@Override
public SearchedPackage askUserToChoosePackage(List<SearchedPackage> packets) {
// list all packages in reverse order
for (int i = packets.size(); i-- > 0; ) {
SearchedPackage searchedPacket = packets.get(i);
System.out.printf("%s - %s/%s/%s %s%n\t%s%n",
i,
searchedPacket.getDistribution(),
searchedPacket.getRepo(),
searchedPacket.getName(),
searchedPacket.getVersion()==null?"":searchedPacket.getVersion(),
searchedPacket.getDescription());
i,
searchedPacket.getDistribution(),
searchedPacket.getRepo(),
searchedPacket.getName(),
searchedPacket.getVersion() == null ? "" : searchedPacket.getVersion(),
searchedPacket.getDescription());
}
System.out.printf("Pick a package to see in details (0-%s) : ", packets.size()-1);
System.out.printf("Pick a package to see in details (0-%s) : ", packets.size() - 1);
Scanner input = new Scanner(System.in);
// we create vars for the loop
@ -49,9 +50,9 @@ public class FrontendTerminal implements Frontend{
packetNumberString = input.nextLine();
// reset notValid to false, we set it in true only if something is wrong
notValid = false;
if(isNumeric(packetNumberString)) {
if (isNumeric(packetNumberString)) {
packetNumber = Integer.parseInt(packetNumberString);
if(packetNumber < 0 || packetNumber >= packets.size()) {
if (packetNumber < 0 || packetNumber >= packets.size()) {
// this number is too big or too small
System.out.println("Enter a valid number");
notValid = true;
@ -61,22 +62,22 @@ public class FrontendTerminal implements Frontend{
System.out.println("Enter a valid number");
notValid = true;
}
} while(notValid);
} while (notValid);
input.close();
return packets.get(packetNumber);
}
return packets.get(packetNumber);
}
@Override
public void showPackageTree(Package packet, int depth) {
@Override
public void showPackageTree(Package packet, int depth) {
System.out.printf("%s%s / %s : %s%n",
" ".repeat(depth),
packet.getName(),
packet.getVersion(),
packet.getDescription());
for (Package dep : packet.getDeps()) {
showPackageTree(dep, depth+1);
}
}
" ".repeat(depth),
packet.getName(),
packet.getVersion(),
packet.getDescription());
for (Package dep : packet.getDeps()) {
showPackageTree(dep, depth + 1);
}
}
}

View File

@ -10,68 +10,68 @@ import java.util.List;
* @version 1.0
*/
public class Package extends SearchedPackage {
/**
* List of package storing all the dependencies of the package
*/
private final List<Package> deps;
/**
* List of package storing all the dependencies of the package
*/
private final List<Package> deps;
/**
* Getter for the deps attribute
*
* @return List, List of package storing all the dependencies of the package
*/
public List<Package> getDeps() {
return deps;
}
/**
* Getter for the deps attribute
*
* @return List, List of package storing all the dependencies of the package
*/
public List<Package> getDeps() {
return deps;
}
/**
* This method adds to the dependency list the package passed as parameter.
*
* @param pack Package, the package to add as dependency
*/
public void addDep(Package pack) {
deps.add(pack);
}
/**
* This method adds to the dependency list the package passed as parameter.
*
* @param pack Package, the package to add as dependency
*/
public void addDep(Package pack) {
deps.add(pack);
}
/**
* Second constructor for the Package class, allows to create a package
* without supplying a list of dependencies.
*
* @param name String, name of the package
* @param version String, version of the package
* @param repo String, repository where the package is located
* @param description String, description of the package
* @param distribution String, the distribution where this specific package
* belongs
*/
public Package(String name, String version, String repo, String description, String distribution) {
this(name, version, repo, description, distribution, new ArrayList<>());
}
/**
* Second constructor for the Package class, allows to create a package
* without supplying a list of dependencies.
*
* @param name String, name of the package
* @param version String, version of the package
* @param repo String, repository where the package is located
* @param description String, description of the package
* @param distribution String, the distribution where this specific package
* belongs
*/
public Package(String name, String version, String repo, String description, String distribution) {
this(name, version, repo, description, distribution, new ArrayList<>());
}
/**
* Main constructor for the Package class
*
* @param name String, name of the package
* @param version String, version of the package
* @param repo String, repository where the package is located
* @param description String, description of the package
* @param distribution String, the distribution where this specific package
* belongs
* @param deps List of Package, dependencies of the package
*/
public Package(String name, String version, String repo, String description, String distribution,
List<Package> deps) {
super(name, version, repo, description, distribution);
this.deps = deps;
}
/**
* Main constructor for the Package class
*
* @param name String, name of the package
* @param version String, version of the package
* @param repo String, repository where the package is located
* @param description String, description of the package
* @param distribution String, the distribution where this specific package
* belongs
* @param deps List of Package, dependencies of the package
*/
public Package(String name, String version, String repo, String description, String distribution,
List<Package> deps) {
super(name, version, repo, description, distribution);
this.deps = deps;
}
/**
* Returns a string representation of the package
*
* @return String, string representation of the package
*/
@Override
public String toString() {
return "Package{%s,deps=%s}".formatted(super.toString(), deps);
}
/**
* Returns a string representation of the package
*
* @return String, string representation of the package
*/
@Override
public String toString() {
return "Package{%s,deps=%s}".formatted(super.toString(), deps);
}
}

View File

@ -8,98 +8,98 @@ package fr.packageviewer.pack;
* @version 1.0
*/
public class SearchedPackage {
/**
* Name of the package
*/
private final String name;
/**
* Version of the package
*/
private final String version;
/**
* Repository where the package is located
*/
private final String repo;
/**
* Description of the package
*/
private final String description;
/**
* Distribution where this specific package belongs
*/
private final String distribution;
/**
* Name of the package
*/
private final String name;
/**
* Version of the package
*/
private final String version;
/**
* Repository where the package is located
*/
private final String repo;
/**
* Description of the package
*/
private final String description;
/**
* Distribution where this specific package belongs
*/
private final String distribution;
/**
* Getter for the name attribute
*
* @return String, the name of the package
*/
public String getName() {
return name;
}
/**
* Getter for the name attribute
*
* @return String, the name of the package
*/
public String getName() {
return name;
}
/**
* Getter for the version attribute
*
* @return String, the version of the package
*/
public String getVersion() {
return version;
}
/**
* Getter for the version attribute
*
* @return String, the version of the package
*/
public String getVersion() {
return version;
}
/**
* Getter for the repo attribute
*
* @return String, repository where the package is located
*/
public String getRepo() {
return repo;
}
/**
* Getter for the repo attribute
*
* @return String, repository where the package is located
*/
public String getRepo() {
return repo;
}
/**
* Getter for the description attribute
*
* @return String, Description of the package
*/
public String getDescription() {
return description;
}
/**
* Getter for the description attribute
*
* @return String, Description of the package
*/
public String getDescription() {
return description;
}
/**
* Getter for the distribution attribute
*
* @return String, distribution where this specific package belongs
*/
public String getDistribution() {
return distribution;
}
/**
* Getter for the distribution attribute
*
* @return String, distribution where this specific package belongs
*/
public String getDistribution() {
return distribution;
}
/**
* Constructor for the SearchedPackage class
*
* @param name String, name of the package
* @param version String, version of the package
* @param repo String, repository where the package is located
* @param description String, description of the package
* @param distribution String, the distribution where this specific package
* belongs
*/
public SearchedPackage(String name, String version, String repo, String description, String distribution) {
this.name = name;
this.version = version;
this.repo = repo;
this.description = description;
this.distribution = distribution;
}
/**
* Constructor for the SearchedPackage class
*
* @param name String, name of the package
* @param version String, version of the package
* @param repo String, repository where the package is located
* @param description String, description of the package
* @param distribution String, the distribution where this specific package
* belongs
*/
public SearchedPackage(String name, String version, String repo, String description, String distribution) {
this.name = name;
this.version = version;
this.repo = repo;
this.description = description;
this.distribution = distribution;
}
/**
* Returns a string representation of the package
*
* @return String, string representation of the package
*/
@Override
public String toString() {
return "SearchedPackage{name=%s,version=%s,repo=%s,description=%s,distribution=%s}".formatted(name, version,
repo, description, distribution);
}
/**
* Returns a string representation of the package
*
* @return String, string representation of the package
*/
@Override
public String toString() {
return "SearchedPackage{name=%s,version=%s,repo=%s,description=%s,distribution=%s}".formatted(name, version,
repo, description, distribution);
}
}

View File

@ -18,7 +18,6 @@ import java.util.logging.Logger;
*
* @author R.Thomas
* @version 1.0
*
*/
public abstract class AsyncRequestsParser {
/**

View File

@ -14,19 +14,21 @@ import java.util.concurrent.Future;
public abstract class DistroTest<T extends Distribution> {
protected abstract T createInstance();
protected List<SearchedPackage> helperSearchPackage(String packageName){
protected List<SearchedPackage> helperSearchPackage(String packageName) {
Distribution distribution = createInstance();
Future<List<SearchedPackage>> future = distribution.searchPackage(packageName);
try{
try {
return future.get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
}
protected Package helperGetPackageTree(String packageName, int depth){
protected Package helperGetPackageTree(String packageName, int depth) {
Distribution distribution = createInstance();
Future<Package> future = distribution.getPackageTree(packageName, depth);
try{
try {
return future.get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
@ -34,7 +36,7 @@ public abstract class DistroTest<T extends Distribution> {
}
@Test
public void testBasicQueryDoNotFail(){
public void testBasicQueryDoNotFail() {
helperGetPackageTree("bash", 0);
}
@ -56,7 +58,7 @@ public abstract class DistroTest<T extends Distribution> {
public void testQueryWithDepth1hasOneLevelOfDeps() {
Package pack = helperGetPackageTree("bash", 1);
Assertions.assertNotEquals(pack.getDeps().size(), 0);
for(Package dep : pack.getDeps()){
for (Package dep : pack.getDeps()) {
Assertions.assertEquals(dep.getDeps().size(), 0);
}
}
@ -83,10 +85,11 @@ public abstract class DistroTest<T extends Distribution> {
public void testThatBashSearchReturnsResults() {
Assertions.assertNotEquals(helperSearchPackage("bash").size(), 0);
}
@Test
public void testThatBashSearchContainsBash() {
for(SearchedPackage pack : helperSearchPackage("bash")){
if(pack.getName().equals("bash")){
for (SearchedPackage pack : helperSearchPackage("bash")) {
if (pack.getName().equals("bash")) {
Assertions.assertTrue(true);
return;
}