001/**
002 *
003 * Copyright 2019 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jxmpp.strings.testframework;
018
019import org.jxmpp.jid.Jid;
020import org.jxmpp.stringprep.XmppStringprepException;
021
022public abstract class ValidJidTestresult extends JidTestresult {
023
024        public final ValidJid validJid;
025        public final Jid jid;
026
027        protected ValidJidTestresult(XmppStringPrepper xmppStringPrepper, long startNanos, long stopNanos,
028                        ValidJid validJid, Jid jid) {
029                super(xmppStringPrepper, startNanos, stopNanos);
030                this.validJid = validJid;
031                this.jid = jid;
032        }
033
034        public static class Successful extends ValidJidTestresult {
035
036                protected Successful(XmppStringPrepper xmppStringPrepper, long startNanos, long stopNanos, ValidJid validJid,
037                                Jid jid) {
038                        super(xmppStringPrepper, startNanos, stopNanos, validJid, jid);
039                }
040
041        }
042
043        public abstract static class Failed extends ValidJidTestresult {
044
045                protected Failed(XmppStringPrepper xmppStringPrepper, long startNanos, long stopNanos, ValidJid validJid,
046                                Jid jid) {
047                        super(xmppStringPrepper, startNanos, stopNanos, validJid, jid);
048                }
049
050                protected StringBuilder buildToString() {
051                        StringBuilder sb = new StringBuilder();
052                        sb.append(xmppStringPrepper)
053                                .append(" failed to handle the following valid JID:\n")
054                                .append(validJid);
055                        return sb;
056                }
057        }
058
059        public static class FailedBecauseMismatch extends Failed {
060
061                public final boolean domainpartMismatch, localpartMismatch, resourcepartMismatch;
062
063                protected FailedBecauseMismatch(XmppStringPrepper xmppStringPrepper, long startNanos, long stopNanos, ValidJid validJid, Jid jid, boolean domainpartMismatch,
064                                boolean localpartMismatch, boolean resourcepartMismatch) {
065                        super(xmppStringPrepper, startNanos, stopNanos, validJid, jid);
066                        this.domainpartMismatch = domainpartMismatch;
067                        this.localpartMismatch = localpartMismatch;
068                        this.resourcepartMismatch = resourcepartMismatch;
069                }
070
071                @Override
072                public String toString() {
073                        StringBuilder sb = buildToString();
074                        sb.append("as it returned the following JID:\n")
075                                .append(jid).append('\n')
076                                .append("but there is a mismatch in the:\n");
077                        if (domainpartMismatch) {
078                                sb.append("- domainpart\n");
079                                sb.append("  expected: ").append(validJid.domainpart).append('\n');
080                                sb.append("  actual  : ").append(jid.getDomain()).append('\n');
081                        }
082                        if (localpartMismatch) {
083                                sb.append("- localpart\n");
084                                sb.append("  expected: ").append(validJid.localpart).append('\n');
085                                sb.append("  actual  : ").append(jid.getLocalpartOrThrow()).append('\n');
086                        }
087                        if (resourcepartMismatch) {
088                                sb.append("- resourcepart\n");
089                                sb.append("  expected: ").append(validJid.resourcepart).append('\n');
090                                sb.append("  actual  : ").append(jid.getResourceOrThrow()).append('\n');
091                        }
092                        return sb.toString();
093                }
094        }
095
096        public static class FailedBecauseException extends Failed {
097
098                public final XmppStringprepException xmppStringprepException;
099
100                protected FailedBecauseException(XmppStringPrepper xmppStringPrepper, long startNanos, long stopNanos,
101                                ValidJid validJid, XmppStringprepException xmppStringprepException) {
102                        super(xmppStringPrepper, startNanos, stopNanos, validJid, null);
103                        this.xmppStringprepException = xmppStringprepException;
104                }
105
106                @Override
107                public String toString() {
108                        StringBuilder sb = buildToString();
109                        sb.append("as it threw the following exception:\n")
110                                .append(xmppStringprepException)
111                                .append('\n');
112                        return sb.toString();
113                }
114        }
115}