diff --git a/src/main/java/com/artipie/rpm/ModifiableRepository.java b/src/main/java/com/artipie/rpm/ModifiableRepository.java deleted file mode 100644 index 57c5357..0000000 --- a/src/main/java/com/artipie/rpm/ModifiableRepository.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm; - -import com.artipie.rpm.misc.UncheckedConsumer; -import com.artipie.rpm.pkg.FilePackage; -import com.artipie.rpm.pkg.InvalidPackageException; -import com.artipie.rpm.pkg.Metadata; -import com.artipie.rpm.pkg.Package; -import com.artipie.rpm.pkg.PackageOutput; -import com.artipie.rpm.pkg.Repodata; -import com.jcabi.log.Logger; -import java.io.Closeable; -import java.io.IOException; -import java.nio.file.Path; -import java.util.List; - -/** - * Repository aggregate {@link PackageOutput}, decorator for {@link Repository}. It accepts repo - * metadata, checks whether package is already exists in metadata files, if not, - * it adds package data to metadata. On save it also extracts not existing packages from - * metadatas and saves summary {@code repomd.xml} file. - * @since 0.6 - */ -public final class ModifiableRepository implements PackageOutput { - - /** - * Origin. - */ - private final Repository origin; - - /** - * List of existing packages ids (checksums) from primary.xml. - */ - private final List existing; - - /** - * Metadata outputs. - */ - private final List metadata; - - /** - * Digest algorithm. - */ - private final Digest digest; - - /** - * Ctor. - * @param existing Existing packages hexes list - * @param metadata Metadata files - * @param digest Hashing algorithm - */ - public ModifiableRepository(final List existing, final List metadata, - final Digest digest) { - this.existing = existing; - this.metadata = metadata; - this.digest = digest; - this.origin = new Repository(metadata, digest); - } - - /** - * Update itself using package metadata. - * @param pkg Package - * @return Itself - * @throws IOException On error - */ - public ModifiableRepository update(final FilePackage pkg) throws IOException { - final String hex = new FileChecksum(pkg.path(), this.digest).hex(); - if (!this.existing.remove(hex)) { - try { - this.origin.update(pkg.parsed()); - } catch (final InvalidPackageException ex) { - Logger.warn(this, "Failed parsing '%s': %[exception]s", pkg.path(), ex); - } - } - return this; - } - - /** - * Clears records about packages that does not present in the repository any more - * from metadata files. - * @return Itself - */ - public ModifiableRepository clear() { - this.metadata.stream().parallel().forEach( - new UncheckedConsumer<>(meta -> meta.brush(this.existing)) - ); - return this; - } - - @Override - public void accept(final Package.Meta meta) throws IOException { - this.origin.accept(meta); - } - - @Override - public void close() { - this.metadata.stream().parallel().forEach( - new UncheckedConsumer<>(Closeable::close) - ); - } - - /** - * Save metadata files and gzip. - * @param repodata Repodata - * @return All metadata files - * @throws IOException On error - */ - public List save(final Repodata repodata) throws IOException { - return this.origin.save(repodata); - } -} diff --git a/src/main/java/com/artipie/rpm/Repository.java b/src/main/java/com/artipie/rpm/Repository.java deleted file mode 100644 index 0972d68..0000000 --- a/src/main/java/com/artipie/rpm/Repository.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm; - -import com.artipie.rpm.meta.XmlRepomd; -import com.artipie.rpm.misc.UncheckedFunc; -import com.artipie.rpm.pkg.Metadata; -import com.artipie.rpm.pkg.Package; -import com.artipie.rpm.pkg.PackageOutput; -import com.artipie.rpm.pkg.Repodata; -import com.jcabi.aspects.Tv; -import com.jcabi.log.Logger; -import java.io.IOException; -import java.nio.file.Path; -import java.util.List; -import java.util.stream.Collectors; - -/** - * Repository aggregate {@link PackageOutput}. It accepts package metadata - * and proxies to all outputs. On complete, it saves summary metadata to - * {@code repomd.xml} file. - * @since 0.6 - */ -final class Repository implements PackageOutput { - - /** - * Metadata outputs. - */ - private final List metadata; - - /** - * Digest algorithm. - */ - private final Digest digest; - - /** - * Ctor. - * @param files Metadata files outputs - * @param digest Digest algorithm - */ - Repository(final List files, final Digest digest) { - this.metadata = files; - this.digest = digest; - } - - /** - * Update itself using package metadata. - * @param pkg Package - * @return Itself - * @throws IOException On error - */ - public Repository update(final Package pkg) throws IOException { - pkg.save(this, this.digest); - return this; - } - - @Override - public void accept(final Package.Meta meta) throws IOException { - new PackageOutput.Multiple(this.metadata).accept(meta); - } - - @Override - public void close() throws IOException { - new PackageOutput.Multiple(this.metadata).close(); - Logger.info(this, "repository closed"); - } - - /** - * Save metadata files and gzip. - * @param repodata Repository repodata - * @return All metadata files - * @throws IOException On error - */ - public List save(final Repodata repodata) throws IOException { - try (XmlRepomd repomd = repodata.createRepomd()) { - repomd.begin(System.currentTimeMillis() / Tv.THOUSAND); - final List outs = this.metadata.stream() - .map(new UncheckedFunc<>(meta -> meta.save(repodata, this.digest, repomd))) - .collect(Collectors.toList()); - outs.add(repomd.file()); - return outs; - } - } -} diff --git a/src/main/java/com/artipie/rpm/meta/XmlFilelists.java b/src/main/java/com/artipie/rpm/meta/XmlFilelists.java deleted file mode 100644 index ea60e4f..0000000 --- a/src/main/java/com/artipie/rpm/meta/XmlFilelists.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.meta; - -import java.io.Closeable; -import java.io.IOException; -import java.nio.file.Path; -import java.util.Arrays; -import java.util.Set; -import java.util.stream.Collectors; -import javax.xml.stream.XMLStreamException; - -/** - * XML {@code filelists.xml} metadata file imperative writer. - *

- * This object is not thread safe and depends on order of method calls. - *

- * @since 0.4 - */ -@SuppressWarnings("PMD.AvoidDuplicateLiterals") -public final class XmlFilelists implements Closeable { - - /** - * Xml file. - */ - private final XmlFile xml; - - /** - * XmlPackagesFile writer. - */ - private final XmlPackagesFile packages; - - /** - * Ctor. - * @param file Path to write filelists.xml - */ - public XmlFilelists(final Path file) { - this(new XmlFile(file)); - } - - /** - * Primary ctor. - * @param xml XML file - */ - public XmlFilelists(final XmlFile xml) { - this.xml = xml; - this.packages = new XmlPackagesFile(xml, XmlPackage.FILELISTS); - } - - /** - * Starts processing of RPMs. - * @return Self - * @throws XMLStreamException when XML generation causes error - */ - public XmlFilelists startPackages() throws XMLStreamException { - this.packages.startPackages(); - return this; - } - - /** - * Start new package. - * @param name Package name - * @param arch Package arch - * @param checksum Package checksum - * @return Package modifier - * @throws XMLStreamException On XML error - */ - public Package startPackage(final String name, final String arch, final String checksum) - throws XMLStreamException { - this.xml.writeStartElement("package"); - this.xml.writeAttribute("pkgid", checksum); - this.xml.writeAttribute("name", name); - this.xml.writeAttribute("arch", arch); - return new Package(this, this.xml); - } - - @Override - public void close() throws IOException { - this.packages.close(); - } - - /** - * Package writer. - * @since 0.6 - */ - public static final class Package { - - /** - * Filelists reference. - */ - private final XmlFilelists filelists; - - /** - * XML stream. - */ - private final XmlFile xml; - - /** - * Ctor. - * @param filelists Filelists - * @param xml XML stream - */ - Package(final XmlFilelists filelists, final XmlFile xml) { - this.filelists = filelists; - this.xml = xml; - } - - /** - * Add version. - * @param epoch Epoch - * @param ver Version - * @param rel Release - * @return Self - * @throws XMLStreamException On XML error - */ - public Package version(final int epoch, final String ver, final String rel) - throws XMLStreamException { - this.xml.writeEmptyElement("version"); - this.xml.writeAttribute("epoch", String.valueOf(epoch)); - this.xml.writeAttribute("ver", ver); - this.xml.writeAttribute("rel", rel); - return this; - } - - /** - * Add package files. - * @param files Files - * @param dirs Dirs - * @param did Directory ids - * @return Self - * @throws XMLStreamException On failure - */ - @SuppressWarnings("PMD.UseVarargs") - public Package files(final String[] files, final String[] dirs, final int[] did) - throws XMLStreamException { - final Set dirset = Arrays.stream(dirs).collect(Collectors.toSet()); - for (int idx = 0; idx < files.length; idx += 1) { - final String fle = files[idx]; - if (fle.isEmpty() || fle.charAt(0) == '.') { - continue; - } - final String path = String.format("%s%s", dirs[did[idx]], fle); - this.xml.writeStartElement("file"); - if (dirset.contains(String.format("%s/", path))) { - this.xml.writeAttribute("type", "dir"); - } - this.xml.writeCharacters(path); - this.xml.writeEndElement(); - } - return this; - } - - /** - * Close a package. - * @return Filelists - * @throws XMLStreamException On error - */ - public XmlFilelists close() throws XMLStreamException { - this.xml.writeEndElement(); - return this.filelists; - } - } -} diff --git a/src/main/java/com/artipie/rpm/meta/XmlMetaJoin.java b/src/main/java/com/artipie/rpm/meta/XmlMetaJoin.java deleted file mode 100644 index 425c257..0000000 --- a/src/main/java/com/artipie/rpm/meta/XmlMetaJoin.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.meta; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Joins two meta xml-files. - * @since 0.9 - */ -public final class XmlMetaJoin { - - /** - * Xml header patter. - */ - private static final Pattern HEADER = Pattern.compile("<\\?xml.*?>"); - - /** - * How many lines to check for xml header and open tag. - */ - private static final int MAX = 5; - - /** - * Tag. - */ - private final String tag; - - /** - * Ctor. - * @param tag Metatag - */ - public XmlMetaJoin(final String tag) { - this.tag = tag; - } - - /** - * Appends data from part to target. - * @param target Target - * @param part File to append - * @throws IOException On error - */ - @SuppressWarnings({"PMD.PrematureDeclaration", "PMD.GuardLogStatement"}) - public void merge(final Path target, final Path part) throws IOException { - final Path res = target.getParent().resolve( - String.format("%s.merged", target.getFileName().toString()) - ); - try (BufferedWriter out = Files.newBufferedWriter(res)) { - this.writeFirstPart(target, out); - this.writeSecondPart(part, out); - } catch (final IOException err) { - Files.delete(res); - throw err; - } - Files.move(res, target, StandardCopyOption.REPLACE_EXISTING); - } - - /** - * Writes the first part. - * @param target What to write - * @param writer Where to write - * @throws IOException On error - */ - @SuppressWarnings("PMD.AssignmentInOperand") - private void writeFirstPart(final Path target, final BufferedWriter writer) - throws IOException { - try (BufferedReader in = Files.newBufferedReader(target)) { - String line; - final String close = String.format("", this.tag); - while ((line = in.readLine()) != null) { - if (line.contains(close)) { - line = line.replace(close, ""); - } - writer.append(line); - writer.newLine(); - } - } - } - - /** - * Writes the first part. - * @param target What to write - * @param writer Where to write - * @throws IOException On error - */ - @SuppressWarnings("PMD.AssignmentInOperand") - private void writeSecondPart(final Path target, final BufferedWriter writer) - throws IOException { - try (BufferedReader in = Files.newBufferedReader(target)) { - String line; - int cnt = 0; - boolean found = false; - final Pattern pattern = Pattern.compile(String.format("<%s.*?>", this.tag)); - while ((line = in.readLine()) != null) { - if (cnt >= XmlMetaJoin.MAX && !found) { - throw new IOException("Failed to merge xml, header not found in part"); - } - if (cnt < XmlMetaJoin.MAX && !found) { - final Matcher mheader = XmlMetaJoin.HEADER.matcher(line); - if (mheader.find()) { - line = mheader.replaceAll(""); - } - final Matcher matcher = pattern.matcher(line); - if (matcher.find()) { - line = matcher.replaceAll(""); - found = true; - } - } - cnt = cnt + 1; - writer.append(line); - writer.newLine(); - } - } - } -} diff --git a/src/main/java/com/artipie/rpm/meta/XmlOthers.java b/src/main/java/com/artipie/rpm/meta/XmlOthers.java deleted file mode 100644 index 857451d..0000000 --- a/src/main/java/com/artipie/rpm/meta/XmlOthers.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.meta; - -import java.io.Closeable; -import java.io.IOException; -import java.nio.file.Path; -import java.util.List; -import javax.xml.stream.XMLStreamException; - -/** - * XML {@code others.xml} metadata imperative writer. - *

- * This object is not thread safe and depends on order of method calls. - *

- * @since 0.6 - */ -@SuppressWarnings("PMD.AvoidDuplicateLiterals") -public final class XmlOthers implements Closeable { - - /** - * Xml file. - */ - private final XmlFile xml; - - /** - * XmlPackagesFile writer. - */ - private final XmlPackagesFile packages; - - /** - * Ctor. - * @param file Path to write other.xml - */ - public XmlOthers(final Path file) { - this(new XmlFile(file)); - } - - /** - * Primary ctor. - * @param xml Xml file - */ - private XmlOthers(final XmlFile xml) { - this.xml = xml; - this.packages = new XmlPackagesFile(xml, XmlPackage.OTHER); - } - - /** - * Start packages. - * @return Self - * @throws XMLStreamException On error - */ - public XmlOthers startPackages() throws XMLStreamException { - this.packages.startPackages(); - return this; - } - - /** - * Add new package. - * @param name Package name - * @param arch Package arch - * @param checksum Package checksum - * @return Package writer - * @throws XMLStreamException On error - */ - public XmlOthers.Package addPackage(final String name, final String arch, final String checksum) - throws XMLStreamException { - this.xml.writeStartElement("package"); - this.xml.writeAttribute("pkgid", checksum); - this.xml.writeAttribute("name", name); - this.xml.writeAttribute("arch", arch); - return new XmlOthers.Package(this, this.xml); - } - - @Override - public void close() throws IOException { - this.packages.close(); - } - - /** - * Package writer. - * @since 0.6 - */ - public static final class Package { - - /** - * Others reference. - */ - private final XmlOthers others; - - /** - * XML writer. - */ - private final XmlFile xml; - - /** - * Ctor. - * @param others Others reference - * @param xml XML writer - */ - Package(final XmlOthers others, final XmlFile xml) { - this.others = others; - this.xml = xml; - } - - /** - * Add version. - * @param eopch Epoch seconds - * @param ver Version string - * @param rel Release name - * @return Self - * @throws XMLStreamException On error - */ - public Package version(final int eopch, final String ver, final String rel) - throws XMLStreamException { - this.xml.writeEmptyElement("version"); - this.xml.writeAttribute("epoch", String.valueOf(eopch)); - this.xml.writeAttribute("ver", ver); - this.xml.writeAttribute("rel", rel); - return this; - } - - /** - * Add changelog. - * @param changelogs List of changelog items - * @return Self - * @throws XMLStreamException On error - */ - public Package changelog(final List changelogs) throws XMLStreamException { - for (final String changelog : changelogs) { - final ChangelogEntry entry = new ChangelogEntry(changelog); - this.changelog(entry.author(), entry.date(), entry.content()); - } - return this; - } - - /** - * Adds changelog tag to others.xml file. - * @param author Changelog author - * @param date Epoch seconds - * @param content Changelog content - * @return Self - * @throws XMLStreamException On error - */ - public Package changelog(final String author, final int date, final String content) - throws XMLStreamException { - this.xml.writeStartElement("changelog"); - this.xml.writeAttribute("date", String.valueOf(date)); - this.xml.writeAttribute("author", String.valueOf(author)); - this.xml.writeCharacters(content); - this.xml.writeEndElement(); - return this; - } - - /** - * Close packages. - * @return Others - * @throws XMLStreamException On error - */ - public XmlOthers close() throws XMLStreamException { - this.xml.writeEndElement(); - return this.others; - } - } -} diff --git a/src/main/java/com/artipie/rpm/meta/XmlPackage.java b/src/main/java/com/artipie/rpm/meta/XmlPackage.java index 698ddb7..7ed9dd8 100644 --- a/src/main/java/com/artipie/rpm/meta/XmlPackage.java +++ b/src/main/java/com/artipie/rpm/meta/XmlPackage.java @@ -4,12 +4,6 @@ */ package com.artipie.rpm.meta; -import com.artipie.rpm.pkg.FilelistsOutput; -import com.artipie.rpm.pkg.OthersOutput; -import com.artipie.rpm.pkg.PackageOutput; -import com.artipie.rpm.pkg.PrimaryOutput; -import java.io.IOException; -import java.nio.file.Files; import java.util.Locale; import java.util.Map; import org.cactoos.map.MapEntry; @@ -30,12 +24,7 @@ public enum XmlPackage { new MapEntry<>("", "http://linux.duke.edu/metadata/common"), new MapEntry<>("rpm", "http://linux.duke.edu/metadata/rpm") ) - ) { - @Override - public PackageOutput.FileOutput output() throws IOException { - return new PrimaryOutput(Files.createTempFile(this.tempPrefix(), XmlPackage.SUFFIX)); - } - }, + ), /** * Metadata other.xml. @@ -43,12 +32,7 @@ public PackageOutput.FileOutput output() throws IOException { OTHER( "otherdata", new MapOf(new MapEntry<>("", "http://linux.duke.edu/metadata/other")) - ) { - @Override - public PackageOutput.FileOutput output() throws IOException { - return new OthersOutput(Files.createTempFile(this.tempPrefix(), XmlPackage.SUFFIX)); - } - }, + ), /** * Metadata filelists.xml. @@ -56,12 +40,7 @@ public PackageOutput.FileOutput output() throws IOException { FILELISTS( "filelists", new MapOf(new MapEntry<>("", "http://linux.duke.edu/metadata/filelists")) - ) { - @Override - public PackageOutput.FileOutput output() throws IOException { - return new FilelistsOutput(Files.createTempFile(this.tempPrefix(), XmlPackage.SUFFIX)); - } - }; + ); /** * File suffix. @@ -121,13 +100,6 @@ public Map xmlNamespaces() { return this.namespaces; } - /** - * File output for the xml package. - * @return FileOutput instance - * @throws IOException On error - */ - public abstract PackageOutput.FileOutput output() throws IOException; - /** * List of XmlPackage. * @since 0.10 diff --git a/src/main/java/com/artipie/rpm/meta/XmlPackagesFile.java b/src/main/java/com/artipie/rpm/meta/XmlPackagesFile.java deleted file mode 100644 index 40ced50..0000000 --- a/src/main/java/com/artipie/rpm/meta/XmlPackagesFile.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.meta; - -import java.io.Closeable; -import java.nio.charset.StandardCharsets; -import java.util.Map; -import javax.xml.stream.XMLStreamException; - -/** - * Starting tag and ending tag writer for metadata writers. - * - * @since 0.6 - */ -public final class XmlPackagesFile implements Closeable { - - /** - * Packages count attribute in the xml. - */ - private static final String PACKAGES_ATTR = "packages"; - - /** - * Xml file. - */ - private final XmlFile xml; - - /** - * Metadata. - */ - private final XmlPackage mtd; - - /** - * Ctor. - * @param xml Xml file - * @param mtd Metadata - */ - public XmlPackagesFile(final XmlFile xml, final XmlPackage mtd) { - this.xml = xml; - this.mtd = mtd; - } - - /** - * Start packages section. - * @throws XMLStreamException On error - */ - public void startPackages() throws XMLStreamException { - this.xml.writeStartDocument(StandardCharsets.UTF_8.displayName(), "1.0"); - this.xml.writeStartElement(this.mtd.tag()); - for (final Map.Entry namespace: this.mtd.xmlNamespaces().entrySet()) { - this.xml.writeNamespace(namespace.getKey(), namespace.getValue()); - } - this.xml.writeAttribute(XmlPackagesFile.PACKAGES_ATTR, "-1"); - } - - @Override - public void close() { - try { - this.xml.writeEndElement(); - this.xml.writeEndDocument(); - this.xml.close(); - } catch (final XMLStreamException err) { - throw new XmlException("Failed to close", err); - } - } -} diff --git a/src/main/java/com/artipie/rpm/meta/XmlPrimary.java b/src/main/java/com/artipie/rpm/meta/XmlPrimary.java deleted file mode 100644 index 2243bff..0000000 --- a/src/main/java/com/artipie/rpm/meta/XmlPrimary.java +++ /dev/null @@ -1,504 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.meta; - -import com.artipie.rpm.misc.UncheckedConsumer; -import com.artipie.rpm.pkg.HeaderTags; -import java.io.Closeable; -import java.nio.file.Path; -import java.util.Arrays; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; -import javax.xml.stream.XMLStreamException; - -/** - * XML {@code primary.xml} metadata imperative writer. - *

- * This object is not thread safe and depends on order of method calls. - *

- * - * @since 0.6 - */ -@SuppressWarnings({"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals"}) -public final class XmlPrimary implements Closeable { - - /** - * Xml primary namespace. - */ - private static final String NAMESPACE = XmlPackage.PRIMARY.xmlNamespaces().get("rpm"); - - /** - * Xml file. - */ - private final XmlFile xml; - - /** - * XmlPackagesFile writer. - */ - private final XmlPackagesFile packages; - - /** - * Ctor. - * @param path Path to write primary.xml - */ - public XmlPrimary(final Path path) { - this(new XmlFile(path)); - } - - /** - * Primary ctor. - * @param xml Xml file - */ - @SuppressWarnings("unchecked") - public XmlPrimary(final XmlFile xml) { - this.xml = xml; - this.packages = new XmlPackagesFile(xml, XmlPackage.PRIMARY); - } - - /** - * Start packages section. - * @return Self - * @throws XMLStreamException On error - */ - public XmlPrimary startPackages() throws XMLStreamException { - this.packages.startPackages(); - return this; - } - - /** - * Start writing a package. - * @return Package writer - * @throws XMLStreamException On error - */ - public Package startPackage() throws XMLStreamException { - this.xml.writeStartElement("package"); - this.xml.writeAttribute("type", "rpm"); - return new Package(this.xml, this); - } - - @Override - public void close() { - this.packages.close(); - } - - /** - * XML package writer. - * @since 0.6 - */ - public static final class Package { - - /** - * XML stream. - */ - private final XmlFile xml; - - /** - * Primary. - */ - private final XmlPrimary primary; - - /** - * Ctor. - * @param xml XML stream - * @param primary XML primary - */ - public Package(final XmlFile xml, final XmlPrimary primary) { - this.xml = xml; - this.primary = primary; - } - - /** - * Set package name. - * @param name Name of the package - * @return Self - * @throws XMLStreamException On XML failure - */ - public Package name(final String name) throws XMLStreamException { - this.xml.writeStartElement("name"); - this.xml.writeCharacters(name); - this.xml.writeEndElement(); - return this; - } - - /** - * Set package arch. - * @param arch Arch name - * @return Self - * @throws XMLStreamException On XML failure - */ - public Package arch(final String arch) throws XMLStreamException { - this.xml.writeStartElement("arch"); - this.xml.writeCharacters(arch); - this.xml.writeEndElement(); - return this; - } - - /** - * Set package version. - * @param epoch Epoch millis - * @param ver Version string - * @param rel Release name - * @return Self - * @throws XMLStreamException On XML failure - */ - public Package version(final int epoch, final String ver, final String rel) - throws XMLStreamException { - this.xml.writeEmptyElement("version"); - this.xml.writeAttribute("epoch", String.valueOf(epoch)); - this.xml.writeAttribute("ver", ver); - this.xml.writeAttribute("rel", rel); - return this; - } - - /** - * Set package checksum. - * @param type Digest type - * @param id Package id - * @param sum Sum hex - * @return Self - * @throws XMLStreamException On XML failure - */ - public Package checksum(final String type, final String id, final String sum) - throws XMLStreamException { - this.xml.writeStartElement("checksum"); - this.xml.writeAttribute("type", type); - this.xml.writeAttribute("pkgid", id); - this.xml.writeCharacters(sum); - this.xml.writeEndElement(); - return this; - } - - /** - * Set package summary. - * @param text Summary text - * @return Self - * @throws XMLStreamException On failure - */ - public Package summary(final String text) throws XMLStreamException { - this.xml.writeStartElement("summary"); - this.xml.writeCharacters(text); - this.xml.writeEndElement(); - return this; - } - - /** - * Set package description. - * @param text Description text - * @return Self - * @throws XMLStreamException On XML failure - */ - public Package description(final String text) throws XMLStreamException { - this.xml.writeStartElement("description"); - this.xml.writeCharacters(text); - this.xml.writeEndElement(); - return this; - } - - /** - * Set packager name. - * @param name Name - * @return Self - * @throws XMLStreamException On XML failure - */ - public Package packager(final String name) throws XMLStreamException { - this.xml.writeStartElement("packager"); - this.xml.writeCharacters(name); - this.xml.writeEndElement(); - return this; - } - - /** - * Set package URL. - * @param url URL string - * @return Self - * @throws XMLStreamException On XML failure - */ - public Package url(final String url) throws XMLStreamException { - this.xml.writeStartElement("url"); - this.xml.writeCharacters(url); - this.xml.writeEndElement(); - return this; - } - - /** - * Set package time. - * @param file File timestamp - * @param build Build timestamp - * @return Self - * @throws XMLStreamException On failure - */ - public Package time(final int file, final int build) throws XMLStreamException { - this.xml.writeEmptyElement("time"); - this.xml.writeAttribute("file", String.valueOf(file)); - this.xml.writeAttribute("build", String.valueOf(build)); - return this; - } - - /** - * Set package size. - * @param pkg Package size - * @param installed Installed size - * @param archive Archive size - * @return Self - * @throws XMLStreamException On failure - */ - public Package size(final long pkg, final int installed, final int archive) - throws XMLStreamException { - this.xml.writeEmptyElement("size"); - this.xml.writeAttribute("package", String.valueOf(pkg)); - this.xml.writeAttribute("installed", String.valueOf(installed)); - this.xml.writeAttribute("archive", String.valueOf(archive)); - return this; - } - - /** - * Set package location. - * @param href Location href - * @return Self - * @throws XMLStreamException On failure - */ - public Package location(final String href) throws XMLStreamException { - this.xml.writeEmptyElement("location"); - this.xml.writeAttribute("href", href); - return this; - } - - /** - * Set package files. - * @param files Files - * @param dirs Dirs - * @param did Directory ids - * @return Self - * @throws XMLStreamException On failure - */ - @SuppressWarnings("PMD.UseVarargs") - public Package files(final String[] files, final String[] dirs, final int[] did) - throws XMLStreamException { - final Set dirset = Arrays.stream(dirs).collect(Collectors.toSet()); - for (int idx = 0; idx < files.length; idx += 1) { - final String fle = files[idx]; - if (fle.isEmpty() || fle.charAt(0) == '.') { - continue; - } - final String path = String.format("%s%s", dirs[did[idx]], fle); - this.xml.writeStartElement("file"); - if (dirset.contains(String.format("%s/", path))) { - this.xml.writeAttribute("type", "dir"); - } - this.xml.writeCharacters(path); - this.xml.writeEndElement(); - } - return this; - } - - /** - * Start format section. - * @return Format writer. - * @throws XMLStreamException On failure - */ - public Format startFormat() throws XMLStreamException { - this.xml.writeStartElement("format"); - return new Format(this.xml, this); - } - - /** - * Close format. - * @return Priamry reference - * @throws XMLStreamException On error - */ - public XmlPrimary close() throws XMLStreamException { - this.xml.writeEndElement(); - return this.primary; - } - } - - /** - * Format package writer. - * @since 0.6 - */ - public static final class Format { - - /** - * XML writer. - */ - private final XmlFile xml; - - /** - * Primary package reference. - */ - private final XmlPrimary.Package pkg; - - /** - * Ctor. - * @param xml XML writer - * @param pkg Pacakge reference - */ - Format(final XmlFile xml, final XmlPrimary.Package pkg) { - this.xml = xml; - this.pkg = pkg; - } - - /** - * Add license. - * @param license License name - * @return Self - * @throws XMLStreamException On XML failure - */ - public Format license(final String license) throws XMLStreamException { - return this.writeElem("license", license); - } - - /** - * Add vendor. - * @param vendor Vendor name - * @return Self - * @throws XMLStreamException On failure - */ - public Format vendor(final String vendor) throws XMLStreamException { - return this.writeElem("vendor", vendor); - } - - /** - * Add group. - * @param group Group name - * @return Self - * @throws XMLStreamException On XML failure - */ - public Format group(final String group) throws XMLStreamException { - return this.writeElem("group", group); - } - - /** - * Add build host. - * @param host Host name - * @return Self - * @throws XMLStreamException On XML failure - */ - public Format buildHost(final String host) throws XMLStreamException { - return this.writeElem("buildhost", host); - } - - /** - * Add source RPM. - * @param source Source RPM name - * @return Self - * @throws XMLStreamException On XML failure - */ - public Format sourceRpm(final String source) throws XMLStreamException { - return this.writeElem("sourcerpm", source); - } - - /** - * Add header range. - * @param start Range start - * @param end Range end - * @return Self - * @throws XMLStreamException On XML failure - */ - public Format headerRange(final int start, final int end) throws XMLStreamException { - this.xml.writeEmptyElement(XmlPrimary.NAMESPACE, "header-range"); - this.xml.writeAttribute("start", String.valueOf(start)); - this.xml.writeAttribute("end", String.valueOf(end)); - return this; - } - - /** - * Adds `provides` element. - * @param names Libraries list provided by the package - * @param versions Versions - * @return Self - * @throws XMLStreamException On XML error - * @todo #220:30min We need to experimentally check that names and versions do not - * mix up in `provides`: download oxygen-gtk2-1.3.4-3.el7.x86_64.rpm from test bundle100 - * (see TestBundle.class), add it to resources folder and create xml primary from it. - * This rpm has not full `provides` information, entry tags are: - * - rpm:entry name="liboxygen-gtk.so()(64bit)" - * - rpm:entry name="oxygen-gtk2" flags="EQ" epoch="0" ver="1.3.4" rel="3.el7" - * - rpm:entry name="oxygen-gtk2(x86-64)" flags="EQ" epoch="0" ver="1.3.4" rel="3.el7" - * Make sure we write it the same way. - * @todo #220:30min Provides entry also can have `flags`, `epoch` and `rel` attributes. - * Find a way to obtain this information from rpm and add it here. Do not forget about - * test. - */ - public Format provides(final List names, final List versions) - throws XMLStreamException { - this.xml.writeStartElement(XmlPrimary.NAMESPACE, "provides"); - for (int ind = 0; ind < names.size(); ind = ind + 1) { - this.xml.writeStartElement(XmlPrimary.NAMESPACE, "entry"); - this.xml.writeAttribute("name", names.get(ind)); - if (ind < versions.size() && !versions.get(ind).ver().isEmpty()) { - this.writeEntryAttr(versions, ind); - } - this.xml.writeEndElement(); - } - this.xml.writeEndElement(); - return this; - } - - /** - * Add list of requires. - * @param requires Requires entries - * @param versions Versions entries - * @return Self - * @throws XMLStreamException On XML error - */ - public Format requires(final List requires, - final List versions) throws XMLStreamException { - this.xml.writeStartElement(XmlPrimary.NAMESPACE, "requires"); - for (int ind = 0; ind < requires.size(); ind = ind + 1) { - if (!requires.get(ind).startsWith("rpmlib(")) { - this.xml.writeStartElement(XmlPrimary.NAMESPACE, "entry"); - this.xml.writeAttribute("name", requires.get(ind)); - if (ind < versions.size() && !versions.get(ind).ver().isEmpty()) { - this.writeEntryAttr(versions, ind); - } - this.xml.writeEndElement(); - } - } - this.xml.writeEndElement(); - return this; - } - - /** - * Close format section. - * @return Parent package - * @throws XMLStreamException On XML error - */ - public Package close() throws XMLStreamException { - this.xml.writeEndElement(); - return this.pkg; - } - - /** - * Write standard format element. - * @param name Element name - * @param value Element value - * @return Itself - * @throws XMLStreamException On error - */ - private Format writeElem(final String name, final String value) throws XMLStreamException { - this.xml.writeStartElement(XmlPrimary.NAMESPACE, name); - this.xml.writeCharacters(value); - this.xml.writeEndElement(); - return this; - } - - /** - * Write entry tag attributes ver, epoch and rel. - * @param versions Versions list - * @param ind Current index - * @throws XMLStreamException On error - */ - private void writeEntryAttr(final List versions, final int ind) - throws XMLStreamException { - this.xml.writeAttribute("ver", versions.get(ind).ver()); - this.xml.writeAttribute("epoch", versions.get(ind).epoch()); - versions.get(ind).rel().ifPresent( - new UncheckedConsumer<>(rel -> this.xml.writeAttribute("rel", rel)) - ); - } - } -} diff --git a/src/main/java/com/artipie/rpm/pkg/FilePackage.java b/src/main/java/com/artipie/rpm/pkg/FilePackage.java index edc97ca..b00b597 100644 --- a/src/main/java/com/artipie/rpm/pkg/FilePackage.java +++ b/src/main/java/com/artipie/rpm/pkg/FilePackage.java @@ -23,61 +23,6 @@ */ public final class FilePackage implements Package { - /** - * The RPM file. - */ - private final Path file; - - /** - * The RPM file location relatively to the updated repository. - */ - private final String location; - - /** - * Ctor. - * @param path The path - * @param location File relative location - */ - public FilePackage(final Path path, final String location) { - this.file = path; - this.location = location; - } - - /** - * Get path. - * @return Path - */ - public Path path() { - return this.file; - } - - @Override - public void save(final PackageOutput out, final Digest digest) throws IOException { - out.accept( - new FilePackage.Headers( - new FilePackageHeader(this.file).header(), this.file, digest, this.location - ) - ); - Files.delete(this.file); - } - - @Override - public String toString() { - return String.format("%s[%s]", this.getClass().getSimpleName(), this.file.getFileName()); - } - - /** - * Parsed file package. - * @return Parsed package - * @throws InvalidPackageException In case package is invalid. - * @throws IOException On error - */ - public Package parsed() throws InvalidPackageException, IOException { - return new ParsedFilePackage( - new FilePackageHeader(this.file).header(), this.file, this.location - ); - } - /** * File package metadata. * @since 0.6 diff --git a/src/main/java/com/artipie/rpm/pkg/FilelistsOutput.java b/src/main/java/com/artipie/rpm/pkg/FilelistsOutput.java deleted file mode 100644 index f037774..0000000 --- a/src/main/java/com/artipie/rpm/pkg/FilelistsOutput.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.rpm.meta.XmlException; -import com.artipie.rpm.meta.XmlFilelists; -import com.artipie.rpm.meta.XmlMaid; -import com.artipie.rpm.meta.XmlPackage; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import javax.xml.stream.XMLStreamException; - -/** - * Package output for {@code filelists.xml}. - * @since 0.6 - */ -public final class FilelistsOutput implements PackageOutput.FileOutput { - - /** - * Path to write filelists.xml. - */ - private final Path path; - - /** - * Temporary file to modify. - */ - private final Path tmp; - - /** - * XML stream processor. - */ - private final XmlFilelists xml; - - /** - * Ctor. - * @param file Path to write filelists.xml - */ - public FilelistsOutput(final Path file) { - this.path = file; - this.tmp = file.getParent().resolve( - String.format("%s.part", file.getFileName().toString()) - ); - this.xml = new XmlFilelists(this.tmp); - } - - /** - * Start packages. - * @return Self - * @throws IOException On failure - */ - public FilelistsOutput start() { - try { - this.xml.startPackages(); - } catch (final XMLStreamException err) { - throw new XmlException("Failed to start packages", err); - } - return this; - } - - @Override - public void accept(final Package.Meta meta) throws IOException { - final HeaderTags tags = new HeaderTags(meta); - try { - this.xml.startPackage( - tags.name(), tags.arch(), meta.checksum().hex() - ).version(tags.epoch(), tags.version(), tags.release()) - .files( - tags.baseNames().toArray(new String[0]), - tags.dirNames().toArray(new String[0]), - tags.dirIndexes() - ).close(); - } catch (final XMLStreamException err) { - throw new XmlException("Failed to add package", err); - } - } - - @Override - public void close() throws IOException { - this.xml.close(); - Files.move(this.tmp, this.path, StandardCopyOption.REPLACE_EXISTING); - } - - @Override - public Path file() { - return this.path; - } - - @Override - public XmlMaid maid() { - return new XmlMaid.ByPkgidAttr(this.path); - } - - @Override - public String tag() { - return XmlPackage.FILELISTS.tag(); - } -} diff --git a/src/main/java/com/artipie/rpm/pkg/Metadata.java b/src/main/java/com/artipie/rpm/pkg/Metadata.java deleted file mode 100644 index acfc8cc..0000000 --- a/src/main/java/com/artipie/rpm/pkg/Metadata.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.rpm.Digest; -import com.artipie.rpm.meta.XmlRepomd; -import java.io.IOException; -import java.nio.file.Path; -import java.util.List; - -/** - * Metadata output. - * @since 0.8 - */ -public interface Metadata extends PackageOutput { - - /** - * Brushes metadata by cleaning not existing packages and setting packages count. - * @param ids Ids of the packages to clear - * @throws IOException When error occurs - */ - void brush(List ids) throws IOException; - - /** - * Save metadata to repomd, produce gzipped output. - * @param repodata Repository repodata - * @param digest Digest - * @param repomd Repomd to update - * @return Gzip metadata file - * @throws IOException On error - * @checkstyle ParameterNumberCheck (3 lines) - */ - Path save(Repodata repodata, Digest digest, XmlRepomd repomd) - throws IOException; - - /** - * Underling metadata file output. - * @return File output - */ - PackageOutput.FileOutput output(); - -} diff --git a/src/main/java/com/artipie/rpm/pkg/MetadataFile.java b/src/main/java/com/artipie/rpm/pkg/MetadataFile.java deleted file mode 100644 index 4d4d85a..0000000 --- a/src/main/java/com/artipie/rpm/pkg/MetadataFile.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.rpm.Digest; -import com.artipie.rpm.FileChecksum; -import com.artipie.rpm.meta.XmlAlter; -import com.artipie.rpm.meta.XmlException; -import com.artipie.rpm.meta.XmlPackage; -import com.artipie.rpm.meta.XmlRepomd; -import com.jcabi.log.Logger; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; -import java.util.concurrent.atomic.AtomicLong; -import java.util.zip.GZIPOutputStream; -import javax.xml.stream.XMLStreamException; - -/** - * Metadata file. It's a decorator for {@link PackageOutput}, - * so it should be used to accept metadata from {@link Package} - * and it proxies metadata to underlying output. After closing it saves - * all metadata to {@code repomd.xml}. - * @since 0.6 - */ -public final class MetadataFile implements Metadata { - - /** - * Metadata type. - */ - private final XmlPackage type; - - /** - * Output. - */ - private final FileOutput out; - - /** - * Packages count. - */ - private final AtomicLong cnt; - - /** - * Ctor. - * @param type Metadata type - * @param out Output - */ - public MetadataFile(final XmlPackage type, final FileOutput out) { - this.type = type; - this.out = out; - this.cnt = new AtomicLong(); - } - - @Override - public void accept(final Package.Meta meta) throws IOException { - Logger.debug(this, "accepting %s", this.type); - this.out.accept(meta); - this.cnt.incrementAndGet(); - } - - @Override - public void close() throws IOException { - this.out.close(); - Logger.info(this, "output %s closed", this.out); - } - - @Override - public void brush(final List ids) throws IOException { - new XmlAlter.File(this.out.file()).pkgAttr(this.out.tag(), String.valueOf(this.cnt.get())); - } - - @Override - public Path save(final Repodata repodata, final Digest digest, final XmlRepomd repomd) - throws IOException { - final Path open = this.out.file(); - Path gzip = Files.createTempFile(repodata.temp(), "", ".gz"); - MetadataFile.gzip(open, gzip); - gzip = Files.move(gzip, repodata.metadata(this.type, gzip)); - Logger.info(this, "gzipped %s to %s", open, gzip); - try (XmlRepomd.Data data = repomd.beginData(this.type.lowercase())) { - data.gzipChecksum(new FileChecksum(gzip, digest)); - data.openChecksum(new FileChecksum(open, digest)); - data.location(String.format("repodata/%s", gzip.getFileName())); - data.gzipSize(Files.size(gzip)); - data.openSize(Files.size(open)); - } catch (final XMLStreamException err) { - throw new XmlException("Failed to update repomd.xml", err); - } - Files.delete(open); - return gzip; - } - - @Override - public FileOutput output() { - return this.out; - } - - @Override - public String toString() { - return String.format("MetadataFile: %s", this.type); - } - - /** - * Gzip a file. - * - * @param input Source file - * @param output Target file - * @throws IOException On error - */ - private static void gzip(final Path input, final Path output) throws IOException { - try (InputStream fis = Files.newInputStream(input); - OutputStream fos = Files.newOutputStream(output); - GZIPOutputStream gzos = new GZIPOutputStream(fos)) { - // @checkstyle MagicNumberCheck (1 line) - final byte[] buffer = new byte[1024 * 8]; - while (true) { - final int length = fis.read(buffer); - if (length < 0) { - break; - } - gzos.write(buffer, 0, length); - } - gzos.finish(); - } - } -} diff --git a/src/main/java/com/artipie/rpm/pkg/ModifiableMetadata.java b/src/main/java/com/artipie/rpm/pkg/ModifiableMetadata.java deleted file mode 100644 index 4599364..0000000 --- a/src/main/java/com/artipie/rpm/pkg/ModifiableMetadata.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.rpm.Digest; -import com.artipie.rpm.meta.PackagesCount; -import com.artipie.rpm.meta.XmlAlter; -import com.artipie.rpm.meta.XmlMetaJoin; -import com.artipie.rpm.meta.XmlRepomd; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import java.util.List; -import java.util.Optional; - -/** - * Modifiable package. - * @since 0.8 - */ -public final class ModifiableMetadata implements Metadata { - - /** - * Origin. - */ - private final Metadata origin; - - /** - * Old metadata file from modifiable repository. - */ - private final PrecedingMetadata preceding; - - /** - * Packages count. - */ - private long cnt; - - /** - * Ctor. - * @param origin Origin - * @param preceding Old metadata from existing repository - */ - public ModifiableMetadata(final Metadata origin, final PrecedingMetadata preceding) { - this.origin = origin; - this.preceding = preceding; - this.cnt = 0L; - } - - @Override - public void brush(final List pkgs) throws IOException { - final Optional existed = this.preceding.findAndUnzip(); - if (existed.isPresent()) { - final Path previous = existed.get(); - if (this.cnt > 0) { - new XmlMetaJoin(this.origin.output().tag()) - .merge(this.origin.output().file(), previous); - } else { - Files.copy( - previous, this.origin.output().file(), StandardCopyOption.REPLACE_EXISTING - ); - } - if (pkgs.isEmpty()) { - this.cnt = this.cnt + new PackagesCount(previous).value(); - } else { - this.cnt = this.origin.output().maid().clean(pkgs); - } - } - new XmlAlter.File(this.origin.output().file()).pkgAttr( - this.origin.output().tag(), String.valueOf(this.cnt) - ); - } - - @Override - public Path save(final Repodata repodata, final Digest digest, - final XmlRepomd repomd) throws IOException { - return this.origin.save(repodata, digest, repomd); - } - - @Override - public FileOutput output() { - return this.origin.output(); - } - - @Override - public void accept(final Package.Meta meta) throws IOException { - this.origin.accept(meta); - this.cnt = this.cnt + 1; - } - - @Override - public void close() throws IOException { - this.origin.close(); - } - - @Override - public String toString() { - return this.origin.toString(); - } -} diff --git a/src/main/java/com/artipie/rpm/pkg/OthersOutput.java b/src/main/java/com/artipie/rpm/pkg/OthersOutput.java deleted file mode 100644 index db203ed..0000000 --- a/src/main/java/com/artipie/rpm/pkg/OthersOutput.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.rpm.meta.XmlException; -import com.artipie.rpm.meta.XmlMaid; -import com.artipie.rpm.meta.XmlOthers; -import com.artipie.rpm.meta.XmlPackage; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import javax.xml.stream.XMLStreamException; - -/** - * Generates other.xml metadata file. - * @since 0.4 - */ -public final class OthersOutput implements PackageOutput.FileOutput { - - /** - * Path to write other.xml. - */ - private final Path path; - - /** - * Temporary file to partly processed other.xml. - */ - private final Path tmp; - - /** - * XML of others. - */ - private final XmlOthers xml; - - /** - * Ctor. - * @param file Path to write filelists.xml - */ - public OthersOutput(final Path file) { - this.path = file; - this.tmp = file.getParent().resolve( - String.format("%s.part", file.getFileName().toString()) - ); - this.xml = new XmlOthers(this.tmp); - } - - /** - * Starts processing of RPMs. - * @return Self - */ - public OthersOutput start() { - try { - this.xml.startPackages(); - } catch (final XMLStreamException err) { - throw new XmlException("Failed to start packages", err); - } - return this; - } - - @Override - public void accept(final Package.Meta meta) throws IOException { - final HeaderTags tags = new HeaderTags(meta); - try { - this.xml.addPackage( - tags.name(), tags.arch(), - meta.checksum().hex() - ).version(tags.epoch(), tags.version(), tags.release()) - .changelog(tags.changelog()).close(); - } catch (final XMLStreamException err) { - throw new XmlException("Failed to add package", err); - } - } - - @Override - public void close() throws IOException { - this.xml.close(); - Files.move(this.tmp, this.path, StandardCopyOption.REPLACE_EXISTING); - } - - @Override - public Path file() { - return this.path; - } - - @Override - public XmlMaid maid() { - return new XmlMaid.ByPkgidAttr(this.path); - } - - @Override - public String tag() { - return XmlPackage.OTHER.tag(); - } -} diff --git a/src/main/java/com/artipie/rpm/pkg/Package.java b/src/main/java/com/artipie/rpm/pkg/Package.java index 178f736..9de3f75 100644 --- a/src/main/java/com/artipie/rpm/pkg/Package.java +++ b/src/main/java/com/artipie/rpm/pkg/Package.java @@ -4,7 +4,6 @@ */ package com.artipie.rpm.pkg; -import com.artipie.rpm.Digest; import java.io.IOException; import java.util.List; import org.redline_rpm.header.Header; @@ -16,14 +15,6 @@ */ public interface Package { - /** - * Save package to output using digest. - * @param out Output to save - * @param digest Digest to use - * @throws IOException On error - */ - void save(PackageOutput out, Digest digest) throws IOException; - /** * Package metadata. * @since 0.6 diff --git a/src/main/java/com/artipie/rpm/pkg/PackageOutput.java b/src/main/java/com/artipie/rpm/pkg/PackageOutput.java deleted file mode 100644 index 07b36d8..0000000 --- a/src/main/java/com/artipie/rpm/pkg/PackageOutput.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.rpm.meta.XmlMaid; -import com.artipie.rpm.misc.UncheckedConsumer; -import java.io.Closeable; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Arrays; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import java.util.stream.StreamSupport; - -/** - * RPM package output. - * @since 0.6 - */ -public interface PackageOutput extends Closeable { - - /** - * Accept package metadata. - * @param meta Metadata - * @throws IOException On error - */ - void accept(Package.Meta meta) throws IOException; - - /** - * File output implementation. - * @since 0.6 - */ - interface FileOutput extends PackageOutput { - - /** - * File path. - * @return Path - */ - Path file(); - - /** - * Returns {@link XmlMaid} instance. - * @return Xml maid - */ - XmlMaid maid(); - - /** - * File tag. - * @return String tag - */ - String tag(); - - /** - * Start packages. - * @return Self - * @throws IOException On failure - */ - FileOutput start() throws IOException; - - /** - * Fake {@link FileOutput}. - * - * @since 1.0 - */ - final class Fake implements FileOutput { - - /** - * File path. - */ - private final Path file; - - /** - * Was package output accepted? - */ - private boolean accepted; - - /** - * Ctor. - * - * @param file File path - */ - public Fake(final Path file) { - this.file = file; - this.accepted = false; - } - - @Override - public Fake start() throws IOException { - Files.write( - this.file, - Arrays.asList("content") - ); - return this; - } - - @Override - public void accept(final Package.Meta meta) { - this.accepted = true; - } - - @Override - public void close() { - // nothing - } - - @Override - public Path file() { - return this.file; - } - - @Override - public XmlMaid maid() { - return null; - } - - @Override - public String tag() { - return "fake"; - } - - /** - * Was package output accepted? - * @return True if {@link Fake#accept(Package.Meta)} was called - */ - public boolean isAccepted() { - return this.accepted; - } - } - } - - /** - * Multiple outputs. - * @since 0.6 - */ - final class Multiple implements PackageOutput { - - /** - * List of outputs. - */ - private final Iterable list; - - /** - * Ctor. - * @param outs Outputs - */ - public Multiple(final Metadata... outs) { - this(Arrays.asList(outs)); - } - - /** - * Ctor. - * @param outs Outputs - */ - public Multiple(final Iterable outs) { - this.list = outs; - } - - @Override - public void accept(final Package.Meta meta) { - StreamSupport.stream(this.list.spliterator(), true).forEach( - new UncheckedConsumer<>(out -> out.accept(meta)) - ); - } - - @Override - public void close() throws IOException { - final List errors = new LinkedList<>(); - for (final Metadata out : this.list) { - try { - out.close(); - out.brush(Collections.emptyList()); - } catch (final IOException err) { - errors.add(err); - } - } - if (!errors.isEmpty()) { - final IOException exc = new IOException("Couldn't close underlying outputs"); - errors.forEach(exc::addSuppressed); - throw exc; - } - } - } -} diff --git a/src/main/java/com/artipie/rpm/pkg/ParsedFilePackage.java b/src/main/java/com/artipie/rpm/pkg/ParsedFilePackage.java deleted file mode 100644 index 6d28875..0000000 --- a/src/main/java/com/artipie/rpm/pkg/ParsedFilePackage.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.rpm.Digest; -import com.jcabi.log.Logger; -import java.io.IOException; -import java.nio.file.Path; -import org.redline_rpm.header.Header; - -/** - * Package with parsed RPM headers. - * @since 0.8 - */ -final class ParsedFilePackage implements Package { - - /** - * Package metadata. - */ - private final Header header; - - /** - * Package file. - */ - private final Path file; - - /** - * The RPM file location relatively to the updated repository. - */ - private final String location; - - /** - * Ctor. - * @param meta Package metadata - * @param file File path - * @param location File relative location - */ - ParsedFilePackage(final Header meta, final Path file, final String location) { - this.header = meta; - this.file = file; - this.location = location; - } - - @Override - public void save(final PackageOutput out, final Digest digest) throws IOException { - Logger.debug(this, "accepting %s", this.file.getFileName()); - out.accept(new FilePackage.Headers(this.header, this.file, digest, this.location)); - } -} diff --git a/src/main/java/com/artipie/rpm/pkg/PrecedingMetadata.java b/src/main/java/com/artipie/rpm/pkg/PrecedingMetadata.java deleted file mode 100644 index a2acf41..0000000 --- a/src/main/java/com/artipie/rpm/pkg/PrecedingMetadata.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.rpm.files.Gzip; -import com.artipie.rpm.meta.XmlPackage; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Optional; -import java.util.stream.Stream; - -/** - * Metadata file from RPM repository being updated. - * @since 0.11 - */ -public interface PrecedingMetadata { - - /** - * Does the metadata exists? - * @return True is metadata exists - * @throws IOException On error - */ - boolean exists() throws IOException; - - /** - * Path to unzipped metadata file if found. - * @return Path - * @throws IOException On error - */ - Optional findAndUnzip() throws IOException; - - /** - * From directory {@link PrecedingMetadata} implementation. - * @since 0.11 - */ - final class FromDir implements PrecedingMetadata { - - /** - * Xml package type. - */ - private final XmlPackage type; - - /** - * Directory path. - */ - private final Path dir; - - /** - * Ctor. - * @param type Xml package type - * @param dir Directory - */ - public FromDir(final XmlPackage type, final Path dir) { - this.type = type; - this.dir = dir; - } - - @Override - public boolean exists() throws IOException { - return this.find().isPresent(); - } - - @Override - public Optional findAndUnzip() throws IOException { - final Optional metadata = this.find(); - final Optional res; - if (metadata.isPresent()) { - final Path unziped = Files.createTempFile( - this.dir, String.format("old-%s", this.type.lowercase()), ".xml" - ); - new Gzip(metadata.get()).unpack(unziped); - res = Optional.of(unziped); - } else { - res = Optional.empty(); - } - return res; - } - - /** - * Path to the metadata file if found. - * @return Path - * @throws IOException On error - */ - private Optional find() throws IOException { - try (Stream files = Files.walk(this.dir)) { - return files.filter( - path -> path.getFileName().toString() - .contains(String.format("%s.xml.gz", this.type.lowercase())) - ).findFirst(); - } - } - } - -} diff --git a/src/main/java/com/artipie/rpm/pkg/PrimaryOutput.java b/src/main/java/com/artipie/rpm/pkg/PrimaryOutput.java deleted file mode 100644 index 88301e6..0000000 --- a/src/main/java/com/artipie/rpm/pkg/PrimaryOutput.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.rpm.meta.XmlException; -import com.artipie.rpm.meta.XmlMaid; -import com.artipie.rpm.meta.XmlPackage; -import com.artipie.rpm.meta.XmlPrimary; -import com.artipie.rpm.meta.XmlPrimaryMaid; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import javax.xml.stream.XMLStreamException; - -/** - * Package output for {@code primary} metadata. - * @since 0.4 - */ -public final class PrimaryOutput implements PackageOutput.FileOutput { - - /** - * Path to write primary.xml. - */ - private final Path path; - - /** - * Temporary file. - */ - private final Path tmp; - - /** - * XML primary file. - */ - private final XmlPrimary xml; - - /** - * Ctor. - * @param file Path to write primary.xml - */ - public PrimaryOutput(final Path file) { - this.path = file; - this.tmp = file.getParent().resolve( - String.format("%s.part", file.getFileName().toString()) - ); - this.xml = new XmlPrimary(this.tmp); - } - - /** - * Starts processing of RPMs. - * @return Self - */ - public PrimaryOutput start() { - try { - this.xml.startPackages(); - } catch (final XMLStreamException err) { - throw new XmlException("Failed to start packages", err); - } - return this; - } - - @Override - public void accept(final Package.Meta meta) throws IOException { - final HeaderTags tags = new HeaderTags(meta); - try { - this.xml.startPackage() - .name(tags.name()) - .arch(tags.arch()) - .version(tags.epoch(), tags.version(), tags.release()) - .checksum( - meta.checksum().digest().type(), "YES", - meta.checksum().hex() - ).summary(tags.summary()) - .description(tags.description()) - .packager(tags.packager()) - .url(tags.url()) - .time(tags.fileTimes(), tags.buildTime()) - .size(meta.size(), tags.installedSize(), tags.archiveSize()) - .location(meta.href()) - .startFormat() - .license(tags.license()) - .vendor(tags.vendor()) - .group(tags.group()) - .buildHost(tags.buildHost()) - .sourceRpm(tags.sourceRmp()) - .headerRange(meta.range()[0], meta.range()[1]) - .provides(tags.providesNames(), tags.providesVer()) - .requires(tags.requires(), tags.requiresVer()) - .close() - .files( - tags.baseNames().toArray(new String[0]), - tags.dirNames().toArray(new String[0]), - tags.dirIndexes() - ).close(); - } catch (final XMLStreamException err) { - throw new XmlException("Failed to update XML", err); - } - } - - @Override - public void close() throws IOException { - this.xml.close(); - Files.move(this.tmp, this.path, StandardCopyOption.REPLACE_EXISTING); - } - - @Override - public Path file() { - return this.path; - } - - @Override - public XmlMaid maid() { - return new XmlPrimaryMaid(this.path); - } - - @Override - public String tag() { - return XmlPackage.PRIMARY.tag(); - } -} diff --git a/src/main/java/com/artipie/rpm/pkg/Repodata.java b/src/main/java/com/artipie/rpm/pkg/Repodata.java deleted file mode 100644 index 411fae9..0000000 --- a/src/main/java/com/artipie/rpm/pkg/Repodata.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.rpm.NamingPolicy; -import com.artipie.rpm.meta.XmlPackage; -import com.artipie.rpm.meta.XmlRepomd; -import java.io.IOException; -import java.nio.file.Path; - -/** - * Repodata creates repomd and files for resulting repository metadata files. - * @since 0.11 - */ -public interface Repodata { - - /** - * Creates {@link XmlRepomd} instance. - * @return Xml repomd - * @throws IOException On error - */ - XmlRepomd createRepomd() throws IOException; - - /** - * Temp dir to store metadata files. - * @return Path - */ - Path temp(); - - /** - * Path to save resulting metadata. - * @param type Xml package type - * @param gzip Gziped metadata - * @return Path - * @throws IOException On error - */ - Path metadata(XmlPackage type, Path gzip) throws IOException; - - /** - * Temp repodata. - * @since 0.11 - */ - final class Temp implements Repodata { - - /** - * Naming policy. - */ - private final NamingPolicy policy; - - /** - * Temp directory. - */ - private final Path tmp; - - /** - * Ctor. - * @param naming Naming policy - * @param tmp Temp directory - */ - public Temp(final NamingPolicy naming, final Path tmp) { - this.policy = naming; - this.tmp = tmp; - } - - @Override - public XmlRepomd createRepomd() throws IOException { - final Path repomd = this.tmp.resolve("repomd.xml"); - repomd.toFile().createNewFile(); - return new XmlRepomd(repomd); - } - - @Override - public Path temp() { - return this.tmp; - } - - @Override - public Path metadata(final XmlPackage type, final Path gzip) throws IOException { - return this.tmp.resolve( - String.format("%s.xml.gz", this.policy.name(type.lowercase(), gzip)) - ); - } - } - -} diff --git a/src/test/java/com/artipie/rpm/meta/XmlFilelistsTest.java b/src/test/java/com/artipie/rpm/meta/XmlFilelistsTest.java deleted file mode 100644 index a1a6299..0000000 --- a/src/test/java/com/artipie/rpm/meta/XmlFilelistsTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.meta; - -import com.jcabi.matchers.XhtmlMatchers; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import org.hamcrest.MatcherAssert; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -/** - * Tests for {@link XmlFilelists}. - * - * @since 0.6.3 - */ -public final class XmlFilelistsTest { - - @Test - public void writesFile(@TempDir final Path temp) throws Exception { - final Path file = temp.resolve("filelists.xml"); - try (XmlFilelists list = new XmlFilelists(file)) { - list.startPackages() - .startPackage("packagename", "packagearch", "packagechecksun") - .version(1, "packageversion", "packagerel") - .files( - new String[] {"file" }, - new String[] {"dir" }, - new int[] {0 } - ).close(); - } - MatcherAssert.assertThat( - new String(Files.readAllBytes(file), StandardCharsets.UTF_8), - XhtmlMatchers.hasXPaths( - // @checkstyle LineLengthCheck (2 line) - "/*[local-name()='filelists']/*[local-name()='package' and @pkgid='packagechecksun' and @name='packagename' and @arch='packagearch']/*[local-name()='version' and @epoch='1' and @ver='packageversion' and @rel='packagerel']", - "/*[local-name()='filelists']/*[local-name()='package' and @pkgid='packagechecksun' and @name='packagename' and @arch='packagearch']/*[local-name()='file' and text()='dirfile']" - ) - ); - } -} diff --git a/src/test/java/com/artipie/rpm/meta/XmlMetaJoinITCase.java b/src/test/java/com/artipie/rpm/meta/XmlMetaJoinITCase.java deleted file mode 100644 index 86760d8..0000000 --- a/src/test/java/com/artipie/rpm/meta/XmlMetaJoinITCase.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.meta; - -import com.artipie.asto.test.TestResource; -import com.artipie.rpm.TimingExtension; -import com.artipie.rpm.files.Gzip; -import com.artipie.rpm.files.TestBundle; -import com.artipie.rpm.hm.IsXmlEqual; -import com.artipie.rpm.misc.FileInDir; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Locale; -import org.apache.commons.io.FileUtils; -import org.hamcrest.MatcherAssert; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.EnabledIfSystemProperty; -import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.api.io.TempDir; - -/** - * Test for {@link XmlMetaJoin}. - * @since 0.9 - */ -@SuppressWarnings("PMD.AvoidDuplicateLiterals") -@EnabledIfSystemProperty(named = "it.longtests.enabled", matches = "true") -@ExtendWith(TimingExtension.class) -class XmlMetaJoinITCase { - /** - * Temporary directory for all tests. - * @checkstyle VisibilityModifierCheck (3 lines) - */ - @TempDir - static Path tmp; - - /** - * Gzipped bundle of RPMs. - */ - private static Path bundle; - - /** - * Test repo. - */ - private static Path repo; - - /** - * Resources dir. - */ - private static final Path FILELISTS = - new TestResource("repodata/filelists.xml.example").asPath(); - - @BeforeAll - static void setUpClass() throws Exception { - XmlMetaJoinITCase.bundle = new TestBundle( - TestBundle.Size.valueOf( - System.getProperty("it.longtests.size", "hundred") - .toUpperCase(Locale.US) - ) - ).load(XmlMetaJoinITCase.tmp); - } - - @BeforeEach - void setUp() throws Exception { - XmlMetaJoinITCase.repo = Files.createDirectory(XmlMetaJoinITCase.tmp.resolve("repo")); - new Gzip(XmlMetaJoinITCase.bundle).unpackTar(XmlMetaJoinITCase.repo); - } - - @AfterEach - void tearDown() throws Exception { - FileUtils.deleteDirectory(XmlMetaJoinITCase.repo.toFile()); - } - - @Test - void joinsBigMetadataWithSmall() throws IOException { - final Path fast = XmlMetaJoinITCase.repo.resolve("big.filelists.xml"); - new Gzip(new FileInDir(XmlMetaJoinITCase.repo).find("filelists.xml.gz")).unpack(fast); - new XmlMetaJoin("filelists").merge(fast, XmlMetaJoinITCase.FILELISTS); - final Path result = XmlMetaJoinITCase.repo.resolve("res.filelists.xml"); - new Gzip( - new FileInDir(XmlMetaJoinITCase.repo).find("filelists.xml.gz") - ).unpack(result); - new XmlStreamJoin("filelists").merge( - result, - XmlMetaJoinITCase.FILELISTS - ); - MatcherAssert.assertThat( - fast, - new IsXmlEqual(result) - ); - } - - @Test - void joinsSmallMetadataWithBig() throws IOException { - final Path big = XmlMetaJoinITCase.repo.resolve("big.filelists.xml"); - final Path fast = XmlMetaJoinITCase.repo.resolve("fast.filelists.xml"); - Files.copy(XmlMetaJoinITCase.FILELISTS, fast); - new Gzip(new FileInDir(XmlMetaJoinITCase.repo).find("filelists.xml.gz")).unpack(big); - new XmlMetaJoin("filelists").merge(fast, big); - final Path result = XmlMetaJoinITCase.repo.resolve("res.filelists.xml"); - Files.copy(XmlMetaJoinITCase.FILELISTS, result); - new XmlStreamJoin("filelists").merge(result, big); - MatcherAssert.assertThat( - fast, - new IsXmlEqual(result) - ); - } -} diff --git a/src/test/java/com/artipie/rpm/meta/XmlMetaJoinTest.java b/src/test/java/com/artipie/rpm/meta/XmlMetaJoinTest.java deleted file mode 100644 index 451a311..0000000 --- a/src/test/java/com/artipie/rpm/meta/XmlMetaJoinTest.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.meta; - -import com.artipie.asto.test.TestResource; -import com.artipie.rpm.hm.IsXmlEqual; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import org.hamcrest.MatcherAssert; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -/** - * Test for {@link XmlMetaJoin}. - * @since 0.9 - */ -@SuppressWarnings("PMD.AvoidDuplicateLiterals") -class XmlMetaJoinTest { - - @Test - void joinsTwoMetaXmlFiles(@TempDir final Path temp) throws IOException { - final Path file = Files.copy( - new TestResource("repodata/primary.xml.example.first").asPath(), - temp.resolve("target.xml") - ); - new XmlMetaJoin("metadata").merge( - file, new TestResource("repodata/primary.xml.example.second").asPath() - ); - MatcherAssert.assertThat( - file, - new IsXmlEqual(new TestResource("repodata/primary.xml.example").asPath()) - ); - } - - @Test - void joinsWhenXmlIsNotProperlySeparatedWithLineBreaks(@TempDir final Path temp) - throws IOException { - final Path target = temp.resolve("res.xml"); - final Path part = temp.resolve("part.xml"); - Files.write( - target, - String.join( - "\n", - "", - "1", - "2" - ).getBytes() - ); - Files.write( - part, - String.join( - "\n", - " ", - "2", - "3" - ).getBytes() - ); - new XmlMetaJoin("parent").merge(target, part); - MatcherAssert.assertThat( - target, - new IsXmlEqual( - String.join( - "\n", - "", - "", - "1223", - "" - ) - ) - ); - } - - @Test - void joinsOneLineXmls(@TempDir final Path temp) throws IOException { - final Path target = temp.resolve("target.xml"); - final Path part = temp.resolve("part.xml"); - Files.write( - target, - String.join( - "", - "", - "", - "A", - "B", - "" - ).getBytes() - ); - Files.write( - part, - String.join( - "", - "", - "", - "C", - "D", - "" - ).getBytes() - ); - new XmlMetaJoin("parent").merge(target, part); - MatcherAssert.assertThat( - target, - new IsXmlEqual( - String.join( - "\n", - "", - "", - "ABCD", - "" - ) - ) - ); - } - -} diff --git a/src/test/java/com/artipie/rpm/meta/XmlOthersTest.java b/src/test/java/com/artipie/rpm/meta/XmlOthersTest.java deleted file mode 100644 index 417534b..0000000 --- a/src/test/java/com/artipie/rpm/meta/XmlOthersTest.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.meta; - -import com.jcabi.matchers.XhtmlMatchers; -import com.jcabi.xml.XMLDocument; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import org.cactoos.list.ListOf; -import org.hamcrest.MatcherAssert; -import org.hamcrest.core.IsEqual; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -/** - * Testcase for {@link XmlOthers} class. - * @since 0.6 - */ -public class XmlOthersTest { - - /** - * Test package added successfully. - * @param tmp Temp path - * @throws Exception - * @checkstyle - */ - @Test - void canAddPackage(@TempDir final Path tmp) throws Exception { - final Path xml = tmp.resolve("others.xml"); - try (XmlOthers others = new XmlOthers(xml)) { - others.startPackages(); - others.addPackage( - "aom", "aarch64", - "7ae" - ).close(); - } - MatcherAssert.assertThat( - new String(Files.readAllBytes(xml), StandardCharsets.UTF_8), - // @checkstyle LineLengthCheck (1 line) - XhtmlMatchers.hasXPath("/*[local-name()='otherdata']/*[local-name()='package' and @name='aom' and @pkgid='7ae' and @arch='aarch64']") - ); - } - - /** - * Test adding a version for the package. - * @param tmp Temp path - * @throws Exception - */ - @Test - void canAddVersion(@TempDir final Path tmp) throws Exception { - final Path xml = tmp.resolve("other.xml"); - try (XmlOthers others = new XmlOthers(xml)) { - others.startPackages(); - others.addPackage( - "aaa", "bbb", - "ccc" - ).version(0, "1.0.0", "8.20190810git9666276.el8").close(); - } - MatcherAssert.assertThat( - new String(Files.readAllBytes(xml), StandardCharsets.UTF_8), - // @checkstyle LineLengthCheck (1 line) - XhtmlMatchers.hasXPath("/*[local-name()='otherdata']/*[local-name()='package']/*[local-name()='version' and @epoch='0' and @ver='1.0.0' and @rel='8.20190810git9666276.el8']") - ); - } - - /** - * Test adding a changelog for the package. - * @param tmp Temp path - * @throws Exception - */ - @Test - void canAddChangelog(@TempDir final Path tmp) throws Exception { - final Path xml = tmp.resolve("changelog.xml"); - try (XmlOthers others = new XmlOthers(xml)) { - others.startPackages(); - others.addPackage( - "ddd", "eee", - "fff" - ).version(0, "2.0.0", "8.20190810git9666276.el9") - .changelog("Paulo Lobo", 0, "Test for changelog generation") - .close(); - } - MatcherAssert.assertThat( - new String(Files.readAllBytes(xml), StandardCharsets.UTF_8), - // @checkstyle LineLengthCheck (1 line) - XhtmlMatchers.hasXPath("/*[local-name()='otherdata']/*[local-name()='package']/*[local-name()='changelog' and @date='0' and @author='Paulo Lobo' and text()='Test for changelog generation']") - ); - } - - /** - * Test adding a changelog list for the package. - * @param tmp Temp path - * @throws Exception - */ - @Test - void canAddMultipleChangelogs(@TempDir final Path tmp) throws Exception { - final Path xml = tmp.resolve("multiple-changelog.xml"); - try (XmlOthers others = new XmlOthers(xml)) { - others.startPackages(); - others.addPackage( - "ggg", "hhh", - "iii" - ).version(0, "3.0.0", "9.20190810git9666276.el10") - .changelog( - // @checkstyle LineLengthCheck (5 lines) - new ListOf<>( - "* Wed May 13 2020 John Doe - 0.1-2\n- Second artipie package", - "* Tue May 31 2016 Jane Doe - 0.1-1\n- First artipie package\n- Example second item in the changelog for version-release 0.1-1" - ) - ) - .close(); - } - MatcherAssert.assertThat( - new String(Files.readAllBytes(xml), StandardCharsets.UTF_8), - // @checkstyle LineLengthCheck (5 lines) - XhtmlMatchers.hasXPaths( - "/*[local-name()='otherdata']/*[local-name()='package']/*[local-name()='changelog' and @date='1589328000' and @author='John Doe ' and text()='- 0.1-2\n- Second artipie package']", - "/*[local-name()='otherdata']/*[local-name()='package']/*[local-name()='changelog' and @date='1464652800' and @author='Jane Doe ' and text()='- 0.1-1\n- First artipie package\n- Example second item in the changelog for version-release 0.1-1']" - ) - ); - MatcherAssert.assertThat( - // @checkstyle LineLengthCheck (2 lines) - new XMLDocument(xml) - .xpath("count(/*[local-name()='otherdata']/*[local-name()='package']/*[local-name()='changelog'])") - .get(0), - new IsEqual<>("2") - ); - } - -} diff --git a/src/test/java/com/artipie/rpm/meta/XmlPackagesFileTest.java b/src/test/java/com/artipie/rpm/meta/XmlPackagesFileTest.java deleted file mode 100644 index ee5a888..0000000 --- a/src/test/java/com/artipie/rpm/meta/XmlPackagesFileTest.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.meta; - -import com.jcabi.matchers.XhtmlMatchers; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import org.hamcrest.MatcherAssert; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -/** - * Tests for {@link XmlPackagesFile}. - * - * @since 0.6.3 - */ -public final class XmlPackagesFileTest { - - @Test - public void writesCorrectTag(@TempDir final Path temp) throws Exception { - final Path file = temp.resolve("tag.xml"); - try (XmlPackagesFile packs = new XmlPackagesFile(new XmlFile(file), XmlPackage.OTHER)) { - packs.startPackages(); - } - XmlPackagesFileTest.assertion( - file, String.format("/*[name()='%s']", XmlPackage.OTHER.tag()) - ); - } - - @Test - public void writesCorrectNamespace(@TempDir final Path temp) throws Exception { - final Path file = temp.resolve("name.xml"); - try (XmlPackagesFile packs = new XmlPackagesFile(new XmlFile(file), XmlPackage.FILELISTS)) { - packs.startPackages(); - } - XmlPackagesFileTest.assertion( - file, - String.format("/*[namespace-uri(.)='%s']", XmlPackage.FILELISTS.xmlNamespaces().get("")) - ); - } - - private static void assertion(final Path file, final String expected) throws IOException { - MatcherAssert.assertThat( - new String(Files.readAllBytes(file), StandardCharsets.UTF_8), - XhtmlMatchers.hasXPath(expected) - ); - } -} diff --git a/src/test/java/com/artipie/rpm/meta/XmlPrimaryTest.java b/src/test/java/com/artipie/rpm/meta/XmlPrimaryTest.java deleted file mode 100644 index 317492a..0000000 --- a/src/test/java/com/artipie/rpm/meta/XmlPrimaryTest.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.meta; - -import com.artipie.rpm.pkg.HeaderTags; -import com.jcabi.matchers.XhtmlMatchers; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import org.cactoos.list.ListOf; -import org.hamcrest.MatcherAssert; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -/** - * Tests for {@link XmlPrimary}. - * - * @since 0.6 - */ -public final class XmlPrimaryTest { - @Test - public void writesFile(@TempDir final Path temp) throws Exception { - final Path file = temp.resolve("primary.xml"); - try (XmlPrimary prim = new XmlPrimary(file)) { - // @checkstyle MagicNumberCheck (30 lines) - prim.startPackages() - .startPackage() - .name("primary") - .arch("arch") - .version(0, "0.0.1", "8.20190810git9666276.el8") - .summary("Package test for XmlPrimary") - .description("This is a package to test XmlPrimary behavior") - .packager("artipie") - .url("http://giuthub.com/artipie/rpm-adapter") - .time(5, 7) - .size(15L, 9, 2) - .location("http://github.com/artipie/rpm-adapter/primary.rpm") - .files( - new String[] {"file" }, - new String[] {"dir" }, - new int[] {0 } - ).startFormat() - .license("license") - .vendor("vendor") - .group("Unspecified") - .buildHost("http://giuthub.com/artipie/rpm-adapter/buildhost") - .sourceRpm("primary.src.rpm") - .headerRange(3, 8) - .provides( - new ListOf("abs"), - new ListOf(new HeaderTags.Version("1.0.0")) - ) - .requires( - new ListOf<>( - "ld-linux-aarch64.so.1()(64bit)" - ), - new ListOf(new HeaderTags.Version("1.2.0")) - ) - .close() - .close(); - } - MatcherAssert.assertThat( - new String(Files.readAllBytes(file), StandardCharsets.UTF_8), - // @checkstyle LineLengthCheck (25 lines) - XhtmlMatchers.hasXPaths( - "/*[local-name()='metadata']/*[local-name()='package' and @type='rpm']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='name' and text()='primary']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='arch' and text()='arch']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='version' and @epoch='0' and @ver='0.0.1' and @rel='8.20190810git9666276.el8']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='summary' and text()='Package test for XmlPrimary']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='description' and text()='This is a package to test XmlPrimary behavior']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='packager' and text()='artipie']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='url' and text()='http://giuthub.com/artipie/rpm-adapter']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='time' and @file='5' and @build='7']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='size' and @package='15' and @installed='9' and @archive='2']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='location' and @href='http://github.com/artipie/rpm-adapter/primary.rpm']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='file' and text()='dirfile']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='format']/*[name()='rpm:license' and text()='license']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='format']/*[name()='rpm:vendor' and text()='vendor']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='format']/*[name()='rpm:group' and text()='Unspecified']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='format']/*[name()='rpm:buildhost' and text()='http://giuthub.com/artipie/rpm-adapter/buildhost']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='format']/*[name()='rpm:sourcerpm' and text()='primary.src.rpm']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='format']/*[name()='rpm:header-range' and @start='3' and @end='8']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='format']/*[name()='rpm:requires']/*[name()='rpm:entry' and @name='ld-linux-aarch64.so.1()(64bit)' and @ver='1.2.0' and @epoch='0']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='format']/*[name()='rpm:provides']/*[name()='rpm:entry' and @name='abs' and @ver='1.0.0' and @epoch='0']" - ) - ); - } -} diff --git a/src/test/java/com/artipie/rpm/meta/XmlStreamJoin.java b/src/test/java/com/artipie/rpm/meta/XmlStreamJoin.java deleted file mode 100644 index 5abfc87..0000000 --- a/src/test/java/com/artipie/rpm/meta/XmlStreamJoin.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.meta; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import javax.xml.stream.XMLEventFactory; -import javax.xml.stream.XMLEventReader; -import javax.xml.stream.XMLEventWriter; -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.events.XMLEvent; - -/** - * Joins xml files by tag using xml-streams. - * @since 0.9 - */ -public final class XmlStreamJoin { - - /** - * Tag. - */ - private final String tag; - - /** - * Ctor. - * @param tag Xml tag - */ - public XmlStreamJoin(final String tag) { - this.tag = tag; - } - - /** - * Appends data from part to target. - * @param target Target - * @param part File to append - * @throws IOException On error - */ - @SuppressWarnings({"PMD.PrematureDeclaration", "PMD.GuardLogStatement"}) - public void merge(final Path target, final Path part) throws IOException { - final Path res = target.getParent().resolve( - String.format("%s.joined", target.getFileName().toString()) - ); - try (OutputStream out = Files.newOutputStream(res)) { - final XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(out); - this.writeFirstPart(target, writer); - this.writeSecondPart(part, writer); - writer.close(); - } catch (final XMLStreamException ex) { - Files.delete(res); - throw new XmlException(ex); - } - Files.move(res, target, StandardCopyOption.REPLACE_EXISTING); - } - - /** - * Writes the first part. - * @param target What to write - * @param writer Where to write - * @throws IOException On error - * @throws XMLStreamException On error - */ - private void writeFirstPart(final Path target, final XMLEventWriter writer) - throws IOException, XMLStreamException { - try (InputStream in = Files.newInputStream(target)) { - final XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(in); - writer.add(reader.nextEvent()); - writer.add(XMLEventFactory.newFactory().createSpace("\n")); - while (reader.hasNext()) { - final XMLEvent event = reader.nextEvent(); - if (!(event.isEndElement() - && event.asEndElement().getName().getLocalPart().equals(this.tag)) - && !event.isEndDocument() - ) { - writer.add(event); - } - } - reader.close(); - } - } - - /** - * Writes second part. - * @param part What to write - * @param writer Where to write - * @throws IOException On error - * @throws XMLStreamException On error - */ - private void writeSecondPart(final Path part, final XMLEventWriter writer) - throws IOException, XMLStreamException { - try (InputStream in = Files.newInputStream(part)) { - final XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(in); - while (reader.hasNext()) { - final XMLEvent event = reader.nextEvent(); - if (!(event.isStartElement() - && event.asStartElement().getName().getLocalPart().equals(this.tag)) - && !event.isStartDocument() - ) { - writer.add(event); - } - } - reader.close(); - } - } -} diff --git a/src/test/java/com/artipie/rpm/meta/XmlStreamJoinTest.java b/src/test/java/com/artipie/rpm/meta/XmlStreamJoinTest.java deleted file mode 100644 index 70f292c..0000000 --- a/src/test/java/com/artipie/rpm/meta/XmlStreamJoinTest.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.meta; - -import com.artipie.asto.test.TestResource; -import com.artipie.rpm.hm.IsXmlEqual; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import org.hamcrest.MatcherAssert; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -/** - * Test for {@link XmlStreamJoin}. - * @since 0.9 - */ -@SuppressWarnings("PMD.AvoidDuplicateLiterals") -public class XmlStreamJoinTest { - - @Test - void joinsTwoMetaXmlFiles(@TempDir final Path temp) throws IOException { - final Path file = Files.copy( - new TestResource("repodata/primary.xml.example.first").asPath(), - temp.resolve("target.xml") - ); - new XmlStreamJoin("metadata").merge( - file, new TestResource("repodata/primary.xml.example.second").asPath() - ); - MatcherAssert.assertThat( - file, - new IsXmlEqual(new TestResource("repodata/primary.xml.example").asPath()) - ); - } - - @Test - void joinsWhenXmlIsNotProperlySeparatedWithLineBreaks(@TempDir final Path temp) - throws IOException { - final Path target = temp.resolve("res.xml"); - final Path part = temp.resolve("part.xml"); - Files.write( - target, - String.join( - "\n", - "", - "1", - "2" - ).getBytes() - ); - Files.write( - part, - String.join( - "\n", - "", - "2", - "3" - ).getBytes() - ); - new XmlStreamJoin("parent").merge(target, part); - MatcherAssert.assertThat( - target, - new IsXmlEqual( - String.join( - "\n", - "", - "", - "1223", - "" - ) - ) - ); - } - -} diff --git a/src/test/java/com/artipie/rpm/pkg/FilePackageHeadersTest.java b/src/test/java/com/artipie/rpm/pkg/FilePackageHeadersTest.java index d6a28df..39d3615 100644 --- a/src/test/java/com/artipie/rpm/pkg/FilePackageHeadersTest.java +++ b/src/test/java/com/artipie/rpm/pkg/FilePackageHeadersTest.java @@ -4,15 +4,12 @@ */ package com.artipie.rpm.pkg; -import com.artipie.asto.ArtipieIOException; import com.artipie.rpm.Digest; -import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import org.hamcrest.MatcherAssert; import org.hamcrest.core.IsEqual; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.redline_rpm.header.Header; @@ -103,21 +100,6 @@ public void parseMissingIntHeader(@TempDir final Path unused) { ); } - @Test - public void failParseInvalidPackageFile(@TempDir final Path tmp) throws Exception { - final Path invalid = tmp.resolve("invalid.rpm"); - Files.write(invalid, "invalid".getBytes()); - final FilePackage pack = new FilePackage(invalid, "invalid"); - Assertions.assertThrows(InvalidPackageException.class, pack::parsed); - } - - @Test - public void failParseNotExistingPackageFile(@TempDir final Path tmp) { - final String fake = "not-exists.rpm"; - final FilePackage pack = new FilePackage(tmp.resolve(fake), fake); - Assertions.assertThrows(ArtipieIOException.class, pack::parsed); - } - @Test public void returnsProvidedLocation() { final String location = "subdir/some.rpm"; diff --git a/src/test/java/com/artipie/rpm/pkg/FilePackageTest.java b/src/test/java/com/artipie/rpm/pkg/FilePackageTest.java deleted file mode 100644 index 5b92006..0000000 --- a/src/test/java/com/artipie/rpm/pkg/FilePackageTest.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.asto.ArtipieIOException; -import com.artipie.rpm.Digest; -import com.artipie.rpm.TestRpm; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import org.hamcrest.MatcherAssert; -import org.hamcrest.core.IsEqual; -import org.hamcrest.core.IsInstanceOf; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -/** - * Test for {@link FilePackage}. - * @since 0.11 - */ -class FilePackageTest { - - @Test - void returnsPath() { - final Path path = Paths.get("some/path/package.rpm"); - MatcherAssert.assertThat( - new FilePackage(path, path.getFileName().toString()).path(), - new IsEqual<>(path) - ); - } - - @Test - void returnsParsedFile() throws IOException { - final Path path = new TestRpm.Abc().path(); - MatcherAssert.assertThat( - new FilePackage(path, path.getFileName().toString()).parsed(), - new IsInstanceOf(ParsedFilePackage.class) - ); - } - - @Test - void throwsExceptionIfFileDoesNotExists() { - Assertions.assertThrows( - ArtipieIOException.class, - () -> new FilePackage(Paths.get("some/file"), "file").parsed() - ); - } - - @Test - void throwsExceptionIfFileIsInvalid(@TempDir final Path temp) throws IOException { - final Path rpm = temp.resolve("bad.rpm"); - Files.write(rpm, new TestRpm.Invalid().bytes()); - Assertions.assertThrows( - InvalidPackageException.class, - () -> new FilePackage(rpm, rpm.getFileName().toString()).parsed() - ); - } - - @Test - void canPresentItselfAsString() { - final String name = "package.rpm"; - MatcherAssert.assertThat( - new FilePackage(Paths.get("urs/some_dir/", name), name).toString(), - new IsEqual<>(String.format("FilePackage[%s]", name)) - ); - } - - @Test - void onSaveCallsAcceptAndDeletesFile(@TempDir final Path temp) throws IOException { - final Path rpm = temp.resolve("test.rpm"); - Files.copy(new TestRpm.Libdeflt().path(), rpm); - final PackageOutput.FileOutput.Fake out = - new PackageOutput.FileOutput.Fake(temp.resolve("any")); - new FilePackage(rpm, rpm.getFileName().toString()).save(out, Digest.SHA256); - MatcherAssert.assertThat( - "PackageOutput was accepted", - out.isAccepted(), - new IsEqual<>(true) - ); - MatcherAssert.assertThat( - "Rpm package was removed", - rpm.toFile().exists(), - new IsEqual<>(false) - ); - } - -} diff --git a/src/test/java/com/artipie/rpm/pkg/FilelistsOutputTest.java b/src/test/java/com/artipie/rpm/pkg/FilelistsOutputTest.java deleted file mode 100644 index 3b0e442..0000000 --- a/src/test/java/com/artipie/rpm/pkg/FilelistsOutputTest.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.rpm.Digest; -import com.artipie.rpm.TestRpm; -import com.artipie.rpm.meta.XmlPackage; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import org.hamcrest.MatcherAssert; -import org.hamcrest.core.IsEqual; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; -import org.xmlunit.diff.DefaultNodeMatcher; -import org.xmlunit.diff.ElementSelectors; -import org.xmlunit.matchers.CompareMatcher; - -/** - * Test for {@link FilelistsOutput}. - * - * @since 0.11 - */ -@SuppressWarnings("PMD.AvoidDuplicateLiterals") -class FilelistsOutputTest { - @Test - void createsFileslistsForAbc(@TempDir final Path temp) throws IOException { - final Path res = temp.resolve("filelists.xml"); - final TestRpm.Abc abc = new TestRpm.Abc(); - try (PackageOutput.FileOutput fileslists = new FilelistsOutput(res)) { - fileslists.start(); - final Path rpm = abc.path(); - fileslists.accept( - new FilePackage.Headers(new FilePackageHeader(rpm).header(), rpm, Digest.SHA256) - ); - } - MatcherAssert.assertThat( - Files.readAllBytes(res), - CompareMatcher.isSimilarTo(Files.readAllBytes(abc.metadata(XmlPackage.FILELISTS))) - .ignoreWhitespace() - .normalizeWhitespace() - .withNodeFilter( - node -> !("file".equals(node.getLocalName()) - && "/usr/lib/.build-id".equals(node.getTextContent())) - ).withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)) - ); - } - - @Test - void checkFile(@TempDir final Path temp) throws IOException { - final Path res = temp.resolve("filelists.xml"); - try (PackageOutput.FileOutput fileslists = new FilelistsOutput(res)) { - fileslists.start(); - MatcherAssert.assertThat( - fileslists.file(), - new IsEqual<>(res) - ); - } - } - - @Test - void checkTag(@TempDir final Path temp) throws IOException { - final Path res = temp.resolve("fileslists.xml"); - try (PackageOutput.FileOutput fileslists = new FilelistsOutput(res)) { - fileslists.start(); - MatcherAssert.assertThat( - fileslists.tag(), - new IsEqual<>("filelists") - ); - } - } - - @Test - void createsFilelistForLibdeflt(@TempDir final Path temp) throws IOException { - final Path res = temp.resolve("fileslists.xml"); - final TestRpm.Libdeflt libdeflt = new TestRpm.Libdeflt(); - try (PackageOutput.FileOutput fileslists = new FilelistsOutput(res)) { - fileslists.start(); - final Path rpm = libdeflt.path(); - fileslists.accept( - new FilePackage.Headers(new FilePackageHeader(rpm).header(), rpm, Digest.SHA256) - ); - } - MatcherAssert.assertThat( - Files.readAllBytes(res), - CompareMatcher.isSimilarTo(Files.readAllBytes(libdeflt.metadata(XmlPackage.FILELISTS))) - .ignoreWhitespace() - .normalizeWhitespace() - .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)) - ); - } -} diff --git a/src/test/java/com/artipie/rpm/pkg/MetadataFileTest.java b/src/test/java/com/artipie/rpm/pkg/MetadataFileTest.java deleted file mode 100644 index 4817454..0000000 --- a/src/test/java/com/artipie/rpm/pkg/MetadataFileTest.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.rpm.Digest; -import com.artipie.rpm.FileChecksum; -import com.artipie.rpm.NamingPolicy; -import com.artipie.rpm.meta.XmlPackage; -import com.artipie.rpm.meta.XmlRepomd; -import com.jcabi.matchers.XhtmlMatchers; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import org.hamcrest.MatcherAssert; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -/** - * Tests for {@link MetadataFile}. - * - * @since 0.6.3 - */ -public final class MetadataFileTest { - - @Test - public void updatesRepomdOnSave(@TempDir final Path tmp) throws Exception { - final Path fake = tmp.resolve("fake.xml"); - try ( - MetadataFile meta = new MetadataFile( - XmlPackage.PRIMARY, new PackageOutput.FileOutput.Fake(fake).start() - ) - ) { - final XmlRepomd repomd = new XmlRepomd(tmp.resolve("repomd.xml")); - repomd.begin(System.currentTimeMillis()); - final String openhex = new FileChecksum(fake, Digest.SHA1).hex(); - final long size = Files.size(fake); - final Path gzip = meta.save( - new Repodata.Temp(new NamingPolicy.HashPrefixed(Digest.SHA1), tmp), - Digest.SHA1, repomd - ); - repomd.close(); - final String hex = new FileChecksum(gzip, Digest.SHA1).hex(); - MatcherAssert.assertThat( - new String(Files.readAllBytes(repomd.file()), StandardCharsets.UTF_8), - // @checkstyle LineLengthCheck (10 lines) - XhtmlMatchers.hasXPaths( - "/*[local-name()='repomd']/*[local-name()='revision']", - String.format("/*[local-name()='repomd']/*[local-name()='data' and @type='primary']/*[local-name()='checksum' and @type='sha' and text()='%s']", hex), - String.format("/*[local-name()='repomd']/*[local-name()='data' and @type='primary']/*[local-name()='open-checksum' and @type='sha' and text()='%s']", openhex), - String.format("/*[local-name()='repomd']/*[local-name()='data' and @type='primary']/*[local-name()='location' and @href='repodata/%s']", String.format("%s-%s.xml.gz", hex, XmlPackage.PRIMARY.lowercase())), - String.format("/*[local-name()='repomd']/*[local-name()='data' and @type='primary']/*[local-name()='size' and text()='%d']", Files.size(gzip)), - String.format("/*[local-name()='repomd']/*[local-name()='data' and @type='primary']/*[local-name()='open-size' and text()='%d']", size) - ) - ); - Files.deleteIfExists(gzip); - } - } -} diff --git a/src/test/java/com/artipie/rpm/pkg/ModifiableMetadataTest.java b/src/test/java/com/artipie/rpm/pkg/ModifiableMetadataTest.java deleted file mode 100644 index 11561ae..0000000 --- a/src/test/java/com/artipie/rpm/pkg/ModifiableMetadataTest.java +++ /dev/null @@ -1,222 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.asto.test.TestResource; -import com.artipie.rpm.Digest; -import com.artipie.rpm.StandardNamingPolicy; -import com.artipie.rpm.TestRpm; -import com.artipie.rpm.hm.NodeHasPkgCount; -import com.artipie.rpm.meta.XmlPackage; -import com.artipie.rpm.meta.XmlRepomd; -import com.jcabi.aspects.Tv; -import com.jcabi.matchers.XhtmlMatchers; -import com.jcabi.xml.XMLDocument; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Collections; -import java.util.Optional; -import org.cactoos.list.ListOf; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.hamcrest.core.IsNot; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -/** - * Test for {@link ModifiableMetadata}. - * @since 0.9 - * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) - * @checkstyle MagicNumberCheck (500 lines) - */ -@SuppressWarnings("PMD.AvoidDuplicateLiterals") -class ModifiableMetadataTest { - - @Test - void generatesMetadataFileWhenRpmsWereRemovedAndAdded(@TempDir final Path temp) - throws IOException { - final Path res = temp.resolve("primary.xml"); - res.toFile().createNewFile(); - final Path part = temp.resolve("part.primary.xml"); - Files.copy(new TestResource("repodata/primary.xml.example").asPath(), part); - final ModifiableMetadata mtd = new ModifiableMetadata( - new MetadataFile(XmlPackage.PRIMARY, new PrimaryOutput(res).start()), - this.preceding(Optional.of(part)) - ); - final Path rpm = new TestRpm.Abc().path(); - mtd.accept( - new FilePackage.Headers(new FilePackageHeader(rpm).header(), rpm, Digest.SHA256) - ); - mtd.close(); - mtd.brush( - new ListOf("7eaefd1cb4f9740558da7f12f9cb5a6141a47f5d064a98d46c29959869af1a44") - ); - MatcherAssert.assertThat( - "Has 'abc' and 'nginx' packages, writes `packages` attribute correctly", - new XMLDocument(res), - Matchers.allOf( - XhtmlMatchers.hasXPath( - //@checkstyle LineLengthCheck (3 lines) - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='name' and text()='abc']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='name' and text()='nginx']" - ), - new NodeHasPkgCount(2, XmlPackage.PRIMARY.tag()) - ) - ); - MatcherAssert.assertThat( - "Does not have 'aom' package", - new String(Files.readAllBytes(res), StandardCharsets.UTF_8), - new IsNot<>( - XhtmlMatchers.hasXPath( - //@checkstyle LineLengthCheck (1 line) - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='name' and text()='aom']" - ) - ) - ); - } - - @Test - void generatesMetadataFileWhenRpmsWereRemoved(@TempDir final Path temp) throws IOException { - final Path res = temp.resolve("other.xml"); - res.toFile().createNewFile(); - final Path part = temp.resolve("part.other.xml"); - Files.copy(new TestResource("repodata/other.xml.example").asPath(), part); - final ModifiableMetadata mtd = new ModifiableMetadata( - new MetadataFile(XmlPackage.OTHER, new OthersOutput(res).start()), - this.preceding(Optional.of(part)) - ); - mtd.close(); - mtd.brush( - new ListOf("54f1d9a1114fa85cd748174c57986004857b800fe9545fbf23af53f4791b31e2") - ); - MatcherAssert.assertThat( - "Has 'aom' package, writes `packages` attribute correctly", - new XMLDocument(res), - Matchers.allOf( - XhtmlMatchers.hasXPath( - "/*[local-name()='otherdata']/*[local-name()='package' and @name='aom']" - ), - new NodeHasPkgCount(1, XmlPackage.OTHER.tag()) - ) - ); - MatcherAssert.assertThat( - "Does not have 'nginx' package", - new String(Files.readAllBytes(res), StandardCharsets.UTF_8), - new IsNot<>( - XhtmlMatchers.hasXPath( - "/*[local-name()='otherdata']/*[local-name()='package' and @name='nginx']" - ) - ) - ); - } - - @Test - void generatesMetadataFileWhenRpmsWereAdded(@TempDir final Path temp) throws IOException { - final Path res = temp.resolve("filelists.xml"); - res.toFile().createNewFile(); - final Path part = temp.resolve("part.filelists.xml"); - Files.copy(new TestResource("repodata/filelists.xml.example").asPath(), part); - final ModifiableMetadata mtd = new ModifiableMetadata( - new MetadataFile(XmlPackage.FILELISTS, new FilelistsOutput(res).start()), - this.preceding(Optional.of(part)) - ); - final Path rpm = new TestRpm.Abc().path(); - mtd.accept( - new FilePackage.Headers(new FilePackageHeader(rpm).header(), rpm, Digest.SHA256) - ); - mtd.close(); - mtd.brush(Collections.emptyList()); - MatcherAssert.assertThat( - "Has 'aom', 'nginx' and 'abc' packages, writes `packages` attribute correctly", - new XMLDocument(res), - Matchers.allOf( - XhtmlMatchers.hasXPath( - "/*[local-name()='filelists']/*[local-name()='package' and @name='aom']", - "/*[local-name()='filelists']/*[local-name()='package' and @name='nginx']", - "/*[local-name()='filelists']/*[local-name()='package' and @name='abc']" - ), - new NodeHasPkgCount(3, XmlPackage.FILELISTS.tag()) - ) - ); - } - - @Test - void generatesMetadataFileForNewRepo(@TempDir final Path temp) throws IOException { - final Path res = temp.resolve("primary.xml"); - res.toFile().createNewFile(); - final ModifiableMetadata mtd = new ModifiableMetadata( - new MetadataFile(XmlPackage.PRIMARY, new PrimaryOutput(res).start()), - this.preceding(Optional.empty()) - ); - final Path abc = new TestRpm.Abc().path(); - mtd.accept( - new FilePackage.Headers(new FilePackageHeader(abc).header(), abc, Digest.SHA256) - ); - final Path libdeflt = new TestRpm.Libdeflt().path(); - mtd.accept( - new FilePackage.Headers( - new FilePackageHeader(libdeflt).header(), libdeflt, Digest.SHA256 - ) - ); - mtd.close(); - mtd.brush(Collections.emptyList()); - MatcherAssert.assertThat( - "Has 'libdeflt1_0' and 'abc' packages, writes `packages` attribute correctly", - new XMLDocument(res), - Matchers.allOf( - XhtmlMatchers.hasXPath( - //@checkstyle LineLengthCheck (2 lines) - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='name' and text()='abc']", - "/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='name' and text()='libdeflt1_0']" - ), - new NodeHasPkgCount(2, XmlPackage.PRIMARY.tag()) - ) - ); - } - - @Test - void savesItselfToRepomd(@TempDir final Path temp) throws IOException { - final Path filelists = temp.resolve("test.filelists.xml"); - final Path part = temp.resolve("part.filelists.xml"); - Files.copy(new TestResource("repodata/filelists.xml.example").asPath(), part); - filelists.toFile().createNewFile(); - final ModifiableMetadata mtd = new ModifiableMetadata( - new MetadataFile(XmlPackage.FILELISTS, new FilelistsOutput(filelists).start()), - this.preceding(Optional.of(part)) - ); - mtd.close(); - final Path repomd = temp.resolve("repomd.xml"); - try (XmlRepomd xml = new XmlRepomd(repomd)) { - xml.begin(System.currentTimeMillis() / Tv.THOUSAND); - mtd.save(new Repodata.Temp(StandardNamingPolicy.PLAIN, temp), Digest.SHA256, xml); - } - MatcherAssert.assertThat( - new String(Files.readAllBytes(repomd), Charset.defaultCharset()), - XhtmlMatchers.hasXPaths( - //@checkstyle LineLengthCheck (1 line) - "/*[namespace-uri()='http://linux.duke.edu/metadata/repo' and local-name()='repomd']", - "/*[name()='repomd']/*[name()='revision']", - "/*[name()='repomd']/*[name()='data' and @type='filelists']" - ) - ); - } - - private PrecedingMetadata preceding(final Optional part) { - return new PrecedingMetadata() { - @Override - public boolean exists() { - return true; - } - - @Override - public Optional findAndUnzip() { - return part; - } - }; - } -} diff --git a/src/test/java/com/artipie/rpm/pkg/OthersOutputTest.java b/src/test/java/com/artipie/rpm/pkg/OthersOutputTest.java deleted file mode 100644 index e9c3524..0000000 --- a/src/test/java/com/artipie/rpm/pkg/OthersOutputTest.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.rpm.Digest; -import com.artipie.rpm.TestRpm; -import com.artipie.rpm.meta.XmlPackage; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import org.hamcrest.MatcherAssert; -import org.hamcrest.core.IsEqual; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; -import org.xmlunit.matchers.CompareMatcher; - -/** - * Test for {@link OthersOutput}. - * - * @since 0.11 - */ -@SuppressWarnings("PMD.AvoidDuplicateLiterals") -class OthersOutputTest { - @Test - void createsOthersForAbc(@TempDir final Path temp) throws IOException { - final Path res = temp.resolve("others.xml"); - final TestRpm.Abc abc = new TestRpm.Abc(); - try (PackageOutput.FileOutput others = new OthersOutput(res)) { - others.start(); - final Path rpm = abc.path(); - others.accept( - new FilePackage.Headers( - new FilePackageHeader(rpm).header(), rpm, - Digest.SHA256, rpm.getFileName().toString() - ) - ); - } - MatcherAssert.assertThat( - Files.readAllBytes(res), - CompareMatcher.isSimilarTo(Files.readAllBytes(abc.metadata(XmlPackage.OTHER))) - .ignoreWhitespace() - .normalizeWhitespace() - .withNodeFilter( - node -> !"changelog".equals(node.getLocalName()) - ) - ); - } - - @Test - void checkFile(@TempDir final Path temp) throws IOException { - final Path res = temp.resolve("others.xml"); - try (PackageOutput.FileOutput others = new OthersOutput(res)) { - others.start(); - MatcherAssert.assertThat( - others.file(), - new IsEqual<>(res) - ); - } - } - - @Test - void checkTag(@TempDir final Path temp) throws IOException { - final Path res = temp.resolve("others.xml"); - try (PackageOutput.FileOutput others = new OthersOutput(res)) { - others.start(); - MatcherAssert.assertThat( - others.tag(), - new IsEqual<>("otherdata") - ); - } - } - - @Test - void createsOthersForLibdeflt(@TempDir final Path temp) throws IOException { - final Path res = temp.resolve("others.xml"); - final TestRpm.Libdeflt libdeflt = new TestRpm.Libdeflt(); - try (PackageOutput.FileOutput others = new OthersOutput(res)) { - others.start(); - final Path rpm = libdeflt.path(); - others.accept( - new FilePackage.Headers( - new FilePackageHeader(rpm).header(), rpm, - Digest.SHA256, rpm.getFileName().toString() - ) - ); - } - MatcherAssert.assertThat( - Files.readAllBytes(res), - CompareMatcher.isSimilarTo(Files.readAllBytes(libdeflt.metadata(XmlPackage.OTHER))) - .ignoreWhitespace() - .normalizeWhitespace() - .withNodeFilter( - node -> !"changelog".equals(node.getLocalName()) - ) - ); - } -} diff --git a/src/test/java/com/artipie/rpm/pkg/PrecedingMetadataFromDirTest.java b/src/test/java/com/artipie/rpm/pkg/PrecedingMetadataFromDirTest.java deleted file mode 100644 index 8e6c45f..0000000 --- a/src/test/java/com/artipie/rpm/pkg/PrecedingMetadataFromDirTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.asto.test.TestResource; -import com.artipie.rpm.meta.XmlPackage; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Optional; -import org.cactoos.list.ListOf; -import org.hamcrest.Matcher; -import org.hamcrest.MatcherAssert; -import org.hamcrest.core.AllOf; -import org.hamcrest.core.IsEqual; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; -import org.llorllale.cactoos.matchers.MatcherOf; - -/** - * Test for {@link PrecedingMetadata.FromDir}. - * @since 0.11 - * @checkstyle LeftCurlyCheck (500 lines) - */ -class PrecedingMetadataFromDirTest { - - @Test - void returnsTrueIfFileExists(@TempDir final Path temp) throws IOException { - final XmlPackage pckg = XmlPackage.PRIMARY; - this.copyExampleToTemp(temp, pckg); - MatcherAssert.assertThat( - new PrecedingMetadata.FromDir(pckg, temp).exists(), - new IsEqual<>(true) - ); - } - - @Test - void unzipIfFileExists(@TempDir final Path temp) throws IOException { - final XmlPackage pckg = XmlPackage.PRIMARY; - this.copyExampleToTemp(temp, pckg); - final Optional unziped = new PrecedingMetadata.FromDir(pckg, temp).findAndUnzip(); - MatcherAssert.assertThat( - "Metadata found", - unziped.isPresent(), - new IsEqual<>(true) - ); - MatcherAssert.assertThat( - "Metadata unzipped to parent directory", - unziped.get().getParent(), - new IsEqual<>(temp) - ); - } - - @Test - void doesNotFindIfNoMetadataExists(@TempDir final Path temp) throws IOException { - temp.resolve("some.txt").toFile().createNewFile(); - MatcherAssert.assertThat( - new PrecedingMetadata.FromDir(XmlPackage.FILELISTS, temp), - new AllOf( - new ListOf>( - new MatcherOf( - fromDir -> !fromDir.exists() - ), - new MatcherOf( - meta -> !meta.findAndUnzip().isPresent() - ) - ) - ) - ); - } - - private void copyExampleToTemp(final Path temp, final XmlPackage pckg) throws IOException { - Files.copy( - new TestResource("repodata/primary.xml.gz.example").asPath(), - temp.resolve(String.format("%s.xml.gz", pckg.lowercase())) - ); - } - -} diff --git a/src/test/java/com/artipie/rpm/pkg/PrimaryOutputTest.java b/src/test/java/com/artipie/rpm/pkg/PrimaryOutputTest.java deleted file mode 100644 index 1d75bfa..0000000 --- a/src/test/java/com/artipie/rpm/pkg/PrimaryOutputTest.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * The MIT License (MIT) Copyright (c) 2020-2021 artipie.com - * https://github.com/artipie/rpm-adapter/LICENSE.txt - */ -package com.artipie.rpm.pkg; - -import com.artipie.rpm.Digest; -import com.artipie.rpm.TestRpm; -import com.artipie.rpm.meta.XmlPackage; -import com.artipie.rpm.meta.XmlPrimaryMaid; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import org.hamcrest.MatcherAssert; -import org.hamcrest.core.IsEqual; -import org.hamcrest.core.IsInstanceOf; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; -import org.xmlunit.matchers.CompareMatcher; - -/** - * Test for {@link PrimaryOutput}. - * @since 0.10 - */ -@SuppressWarnings("PMD.AvoidDuplicateLiterals") -class PrimaryOutputTest { - - @Test - void createsPrimaryForAbc(@TempDir final Path temp) throws IOException { - final Path res = temp.resolve("primary.xml"); - try (PackageOutput.FileOutput primary = new PrimaryOutput(res)) { - primary.start(); - final Path rpm = new TestRpm.Abc().path(); - primary.accept( - new FilePackage.Headers( - new FilePackageHeader(rpm).header(), rpm, - Digest.SHA256, rpm.getFileName().toString() - ) - ); - } - MatcherAssert.assertThat( - Files.readAllBytes(res), - CompareMatcher.isIdenticalTo( - Files.readAllBytes(new TestRpm.Abc().metadata(XmlPackage.PRIMARY)) - ).ignoreWhitespace() - .ignoreElementContentWhitespace() - .normalizeWhitespace() - .withNodeFilter( - node -> !"file".equals(node.getLocalName()) - && !"provides".equals(node.getLocalName()) - && !"requires".equals(node.getLocalName()) - ).withAttributeFilter( - attr -> !"file".equals(attr.getName()) && !"archive".equals(attr.getName()) - ) - ); - } - - @Test - void checkFile(@TempDir final Path temp) throws IOException { - final Path res = temp.resolve("primary.xml"); - try (PackageOutput.FileOutput primary = new PrimaryOutput(res)) { - primary.start(); - MatcherAssert.assertThat( - primary.file(), - new IsEqual<>(res) - ); - } - } - - @Test - void checkTag(@TempDir final Path temp) throws IOException { - final Path res = temp.resolve("primary.xml"); - try (PackageOutput.FileOutput primary = new PrimaryOutput(res)) { - primary.start(); - MatcherAssert.assertThat( - primary.tag(), - new IsEqual<>("metadata") - ); - } - } - - @Test - void createsPrimaryForLibdeflt(@TempDir final Path temp) throws IOException { - final Path res = temp.resolve("primary.xml"); - try (PackageOutput.FileOutput primary = new PrimaryOutput(res)) { - primary.start(); - final Path rpm = new TestRpm.Libdeflt().path(); - primary.accept( - new FilePackage.Headers( - new FilePackageHeader(rpm).header(), rpm, - Digest.SHA256, rpm.getFileName().toString() - ) - ); - } - MatcherAssert.assertThat( - Files.readAllBytes(res), - CompareMatcher.isIdenticalTo( - Files.readAllBytes(new TestRpm.Libdeflt().metadata(XmlPackage.PRIMARY)) - ).ignoreWhitespace() - .ignoreElementContentWhitespace() - .normalizeWhitespace() - .withNodeFilter( - node -> !"file".equals(node.getLocalName()) - && !"provides".equals(node.getLocalName()) - && !"requires".equals(node.getLocalName()) - ).withAttributeFilter( - attr -> !"file".equals(attr.getName()) && !"archive".equals(attr.getName()) - ) - ); - } - - @Test - void createsCorrectMaidInstance(@TempDir final Path temp) throws IOException { - try (PrimaryOutput output = new PrimaryOutput(temp.resolve("fake.xml"))) { - output.start(); - MatcherAssert.assertThat( - output.maid(), - new IsInstanceOf(XmlPrimaryMaid.class) - ); - } - } -}