001/*
002 * JDrupes Builder
003 * Copyright (C) 2025 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.builder.core;
020
021import java.util.Objects;
022import org.jdrupes.builder.api.FaultAware;
023import org.jdrupes.builder.api.Project;
024import org.jdrupes.builder.api.ResourceProvider;
025import org.jdrupes.builder.api.TestResult;
026
027/// Default implementation of a test result.
028///
029public class DefaultTestResult extends ResourceObject implements TestResult {
030
031    private final Project project;
032    private final ResourceProvider provider;
033    private boolean isFaulty;
034    private final long executed;
035    private final long failed;
036
037    /// Initializes a new default test result.
038    ///
039    /// @param project the project
040    /// @param provider the provider of the test result, used to check 
041    /// for equality
042    /// @param name the name
043    /// @param executed the executed
044    /// @param failed the failed
045    ///
046    @SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
047    protected DefaultTestResult(Project project, ResourceProvider provider,
048            String name, long executed, long failed) {
049        name(name);
050        this.project = project;
051        this.provider = provider;
052        this.executed = executed;
053        this.failed = failed;
054    }
055
056    @Override
057    public boolean isFaulty() {
058        return isFaulty;
059    }
060
061    @Override
062    public FaultAware setFaulty() {
063        isFaulty = true;
064        return null;
065    }
066
067    @Override
068    public Project project() {
069        return project;
070    }
071
072    @Override
073    public long executed() {
074        return executed;
075    }
076
077    @Override
078    public long failed() {
079        return failed;
080    }
081
082    @Override
083    public int hashCode() {
084        final int prime = 31;
085        int result = super.hashCode();
086        result = prime * result + Objects.hash(provider);
087        return result;
088    }
089
090    @Override
091    public boolean equals(Object obj) {
092        if (this == obj) {
093            return true;
094        }
095        if (!super.equals(obj)) {
096            return false;
097        }
098        if (!(obj instanceof DefaultTestResult)) {
099            return false;
100        }
101        DefaultTestResult other = (DefaultTestResult) obj;
102        return Objects.equals(provider, other.provider);
103    }
104
105    /// To string.
106    ///
107    /// @return the string
108    ///
109    @Override
110    public String toString() {
111        String details;
112        if (failed() > 0) {
113            details = failed() + "/" + executed() + " failed";
114        } else {
115            details = executed() + " passed";
116        }
117        return TestResult.class.getSimpleName() + " from " + project().name()
118            + " (" + name().get() + "): " + details;
119    }
120}