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.jid.parts.Domainpart;
021import org.jxmpp.jid.parts.Localpart;
022import org.jxmpp.jid.parts.Resourcepart;
023import org.jxmpp.stringprep.XmppStringprepException;
024
025public abstract class InvalidJidTestresult extends JidTestresult {
026
027        public final InvalidJid invalidJid;
028
029        protected InvalidJidTestresult(XmppStringPrepper xmppStringPrepper, long startNanos, long stopNanos, InvalidJid invalidJid) {
030                super(xmppStringPrepper, startNanos, stopNanos);
031                this.invalidJid = invalidJid;
032        }
033
034        public static class Successful extends InvalidJidTestresult {
035
036                public final XmppStringprepException xmppStringprepException;
037
038                protected Successful(XmppStringPrepper xmppStringPrepper, long startNanos, long stopNanos,
039                                InvalidJid invalidJid, XmppStringprepException xmppStringprepException) {
040                        super(xmppStringPrepper, startNanos, stopNanos, invalidJid);
041                        this.xmppStringprepException = xmppStringprepException;
042                }
043
044        }
045
046        public static class Failed extends InvalidJidTestresult {
047
048                public final Jid jid;
049
050                protected Failed(XmppStringPrepper xmppStringPrepper, long startNanos, long stopNanos, InvalidJid invalidJid,
051                                Jid jid) {
052                        super(xmppStringPrepper, startNanos, stopNanos, invalidJid);
053                        this.jid = jid;
054                }
055
056                @Override
057                public String toString() {
058                        StringBuilder sb = new StringBuilder();
059                        sb.append(xmppStringPrepper)
060                        .append(" failed to handle the following invalid JID:\n")
061                        .append(invalidJid).append('\n')
062                        .append("as it produced the following JID (when it should have thrown an exception):\n")
063                        .append(jid).append('\n');
064                        Localpart localpart = jid.getLocalpartOrNull();
065                        if (localpart != null) {
066                                sb.append("- localpart: ").append(localpart).append('\n');
067                        }
068                        Domainpart domainpart = jid.getDomain();
069                        sb.append("- domainpart: ").append(domainpart).append('\n');
070                        Resourcepart resourcepart = jid.getResourceOrNull();
071                        if (resourcepart != null) {
072                                sb.append("- resourcepart: ").append(resourcepart).append('\n');
073                        }
074                        return sb.toString();
075                }
076        }
077
078}