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 java.io.BufferedWriter;
020import java.io.IOException;
021import java.io.OutputStreamWriter;
022import java.io.Writer;
023import java.nio.charset.StandardCharsets;
024import java.util.List;
025import java.util.concurrent.Callable;
026import java.util.stream.Collectors;
027
028import picocli.CommandLine;
029import picocli.CommandLine.Command;
030import picocli.CommandLine.Option;
031import picocli.CommandLine.TypeConversionException;
032
033@Command(name = "test-xmpp-strings")
034public class StringsTestframeworkMain implements Callable<Integer> {
035
036        @Option(names = {"-s", "--stringprepper"})
037        List<XmppStringPrepper> xmppStringPreppers;
038
039        @Override
040        public Integer call() throws IOException {
041                StringsTestframework.Configuration.Builder configurationBuilder = StringsTestframework.Configuration.builder();
042                if (xmppStringPreppers == null) {
043                        configurationBuilder.withAllKnownXmppStringpreppers();
044                } else {
045                        configurationBuilder.addXmppStringPreppers(xmppStringPreppers);
046                }
047
048                StringsTestframework.Configuration configuration = configurationBuilder.build();
049
050                StringsTestframework stringsTestframework = new StringsTestframework(configuration);
051
052                StringsTestframework.Result result = stringsTestframework.runTests();
053
054                Writer out = new BufferedWriter(new OutputStreamWriter(System.out, StandardCharsets.UTF_8));
055                result.writeResults(out);
056                out.flush();
057
058                int exitcode = result.wasSuccessful ? 0 : 1;
059                return exitcode;
060        }
061
062        /**
063         * Run the jXMPP Strings Testframework.
064         *
065         * @param args arguments to the framework (currently none).
066         * @throws IOException if an I/O error occurs.
067         */
068        public static void main(String[] args) throws IOException {
069                // For some yet unknown reason, the arguments passed with the test-xmpp-strings python script using gradle's
070                // '--arg' arrive as one argument. That is
071                // ./test-xmpp-strings -s simple
072                // yields
073                // args.length == 1
074                // args[0].equals("-s simple")
075                // Even though gradle has in JavaExec.setArgsString(String)
076                // setArgs(Arrays.asList(Commandline.translateCommandline(args)));
077                if (args.length == 1) {
078                        args = org.apache.tools.ant.types.Commandline.translateCommandline(args[0]);
079                }
080
081                CommandLine commandLine = new CommandLine(new StringsTestframeworkMain());
082                commandLine.registerConverter(XmppStringPrepper.class, s -> {
083                        XmppStringPrepper xmppStringPrepper = XmppStringPrepper.lookup(s);
084                        if (xmppStringPrepper == null) {
085                                List<XmppStringPrepper> knownXmppStringPreppers = XmppStringPrepper.getKnownXmppStringpreppers();
086
087                                String message = "XMPP string prepper named '" + s + "' unknown. Known preppers: "
088                                                + knownXmppStringPreppers.stream()
089                                                        .map(XmppStringPrepper::getName)
090                                                        .collect(Collectors.joining(", "));
091                                throw new TypeConversionException(message);
092                        }
093                        return xmppStringPrepper;
094                });
095                commandLine.setPosixClusteredShortOptionsAllowed(false);
096
097                int exitcode = commandLine.execute(args);
098                System.exit(exitcode);
099        }
100}