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.Project;
023import org.jdrupes.builder.api.ResourceProvider;
024import org.jdrupes.builder.api.TestResult;
025
026/// Default implementation of a test result.
027///
028public class DefaultTestResult extends ResourceObject implements TestResult {
029
030    private final Project project;
031    private final ResourceProvider provider;
032    private boolean isFaulty;
033    private final long executed;
034    private final long failed;
035
036    /// Initializes a new default test result.
037    ///
038    /// @param project the project
039    /// @param provider the provider of the test result, used to check 
040    /// for equality
041    /// @param name the name
042    /// @param executed the executed
043    /// @param failed the failed
044    ///
045    @SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
046    protected DefaultTestResult(Project project, ResourceProvider provider,
047            String name, long executed, long failed) {
048        name(name);
049        this.project = project;
050        this.provider = provider;
051        this.executed = executed;
052        this.failed = failed;
053    }
054
055    @Override
056    public boolean isFaulty() {
057        return isFaulty;
058    }
059
060    @Override
061    public DefaultTestResult setFaulty() {
062        isFaulty = true;
063        return this;
064    }
065
066    @Override
067    public Project project() {
068        return project;
069    }
070
071    @Override
072    public long executed() {
073        return executed;
074    }
075
076    @Override
077    public long failed() {
078        return failed;
079    }
080
081    @Override
082    public int hashCode() {
083        final int prime = 31;
084        int result = super.hashCode();
085        result = prime * result + Objects.hash(provider);
086        return result;
087    }
088
089    @Override
090    public boolean equals(Object obj) {
091        if (this == obj) {
092            return true;
093        }
094        if (!super.equals(obj)) {
095            return false;
096        }
097        if (!(obj instanceof DefaultTestResult)) {
098            return false;
099        }
100        DefaultTestResult other = (DefaultTestResult) obj;
101        return Objects.equals(provider, other.provider);
102    }
103
104    /// To string.
105    ///
106    /// @return the string
107    ///
108    @Override
109    public String toString() {
110        String details;
111        if (failed() > 0) {
112            details = failed() + "/" + executed() + " failed";
113        } else {
114            details = executed() + " passed";
115        }
116        return TestResult.class.getSimpleName() + " from " + project().name()
117            + " (" + name().get() + "): " + details;
118    }
119}