Changeset 3068


Ignore:
Timestamp:
01/19/12 09:13:49 (4 months ago)
Author:
kasper
Message:

Ticket #749: Added CLI output type options.

Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • AnalyzerBeans/trunk/cli/src/main/java/org/eobjects/analyzer/cli/CliArguments.java

    r2719 r3068  
    9393        private String tableName; 
    9494 
     95        @Option(name = "-ot", aliases = { "--output-type" }, usage = "How to represent the result of the job") 
     96        private CliOutputType outputType; 
     97 
     98        @Option(name = "-of", aliases = { "--output-file" }, usage = "File in which to save the result of the job", required = false) 
     99        private File outputFile; 
     100 
    95101        @Option(name = "-v", aliases = { "-var", "--variable" }, multiValued = true) 
    96102        private Map<String, String> variableOverrides; 
     
    134140        } 
    135141 
     142        public File getOutputFile() { 
     143                return outputFile; 
     144        } 
     145 
     146        public CliOutputType getOutputType() { 
     147                if (outputFile == null) { 
     148                        return CliOutputType.TEXT; 
     149                } 
     150                return outputType; 
     151        } 
     152 
    136153        /** 
    137154         * Gets whether the arguments have been sufficiently set to execute a CLI 
  • AnalyzerBeans/trunk/cli/src/main/java/org/eobjects/analyzer/cli/CliProgressAnalysisListener.java

    r3040 r3068  
    3838 
    3939import org.eobjects.metamodel.schema.Table; 
     40import org.slf4j.Logger; 
     41import org.slf4j.LoggerFactory; 
    4042 
    4143final class CliProgressAnalysisListener implements AnalysisListener { 
     44 
     45        private static final Logger logger = LoggerFactory.getLogger(CliProgressAnalysisListener.class); 
    4246 
    4347        private Map<Table, AtomicInteger> rowCounts = new HashMap<Table, AtomicInteger>(); 
     
    5458        public void rowProcessingBegin(AnalysisJob job, RowProcessingMetrics metrics) { 
    5559                Table table = metrics.getTable(); 
    56                 System.out.println("Analyzing rows from table: " + table.getName()); 
     60                logger.info("Analyzing rows from table: {}", table.getName()); 
    5761                rowCounts.put(table, new AtomicInteger(0)); 
    5862        } 
     
    7579        @Override 
    7680        public void rowProcessingSuccess(AnalysisJob job, RowProcessingMetrics metrics) { 
    77                 System.out.println("Done processing rows from table: " + metrics.getTable().getName()); 
     81                logger.info("Done processing rows from table: {}", metrics.getTable().getName()); 
    7882        } 
    7983 
  • AnalyzerBeans/trunk/cli/src/main/java/org/eobjects/analyzer/cli/CliRunner.java

    r2817 r3068  
    2121 
    2222import java.io.BufferedInputStream; 
     23import java.io.Closeable; 
    2324import java.io.File; 
    2425import java.io.FileInputStream; 
     
    5556import org.eobjects.metamodel.schema.Schema; 
    5657import org.eobjects.metamodel.schema.Table; 
     58import org.eobjects.metamodel.util.FileHelper; 
    5759import org.slf4j.Logger; 
    5860import org.slf4j.LoggerFactory; 
     
    6365 * @author Kasper SÞrensen 
    6466 */ 
    65 public final class CliRunner { 
     67public final class CliRunner implements Closeable { 
    6668 
    6769        private final static Logger logger = LoggerFactory.getLogger(CliRunner.class); 
     
    6971        private final CliArguments _arguments; 
    7072        private final PrintWriter _out; 
    71  
    72         public CliRunner(CliArguments arguments, PrintWriter out) { 
     73        private final boolean _closeOut; 
     74 
     75        /** 
     76         * Alternative constructor that will specifically specifies the output 
     77         * writer. Should be used only for testing, since normally the CliArguments 
     78         * should be used to decide which outputwriter to use 
     79         *  
     80         * @param arguments 
     81         * @param out 
     82         */ 
     83        protected CliRunner(CliArguments arguments, PrintWriter out) { 
    7384                _arguments = arguments; 
    74                 _out = out; 
     85                if (out == null) { 
     86                        final File outputFile = arguments.getOutputFile(); 
     87                        if (outputFile == null) { 
     88                                _out = new PrintWriter(System.out); 
     89                        } else { 
     90                                _out = new PrintWriter(FileHelper.getWriter(outputFile)); 
     91                        } 
     92                        _closeOut = true; 
     93                } else { 
     94                        _out = out; 
     95                        _closeOut = false; 
     96                } 
     97        } 
     98 
     99        public CliRunner(CliArguments arguments) { 
     100                this(arguments, null); 
    75101        } 
    76102 
     
    293319                                throwable.printStackTrace(_out); 
    294320                        } 
    295                          
     321 
    296322                        throw errors.get(0); 
    297323                } 
     
    397423                } 
    398424        } 
     425 
     426        @Override 
     427        public void close() { 
     428                if (_closeOut) { 
     429                        FileHelper.safeClose(_out); 
     430                } 
     431        } 
    399432} 
  • AnalyzerBeans/trunk/cli/src/main/java/org/eobjects/analyzer/cli/Main.java

    r2817 r3068  
    2222import java.io.PrintWriter; 
    2323 
     24import org.eobjects.metamodel.util.FileHelper; 
     25 
    2426/** 
    2527 * Main class for the AnalyzerBeans Command-line interface (CLI). 
     
    2830 */ 
    2931public final class Main { 
    30  
    31         private static PrintWriter out = new PrintWriter(System.out); 
    3232 
    3333        /** 
     
    4141                CliArguments arguments = CliArguments.parse(args); 
    4242                if (arguments.isSet() && !arguments.isUsageMode()) { 
    43                         CliRunner runner = new CliRunner(arguments, out); 
    44                         runner.run(); 
     43                        CliRunner runner = new CliRunner(arguments); 
     44                        try { 
     45                                runner.run(); 
     46                        } finally { 
     47                                runner.close(); 
     48                        } 
    4549                } else { 
    46                         CliArguments.printUsage(out); 
     50                        PrintWriter out = new PrintWriter(System.out); 
     51                        printUsage(out); 
     52                        FileHelper.safeClose(out); 
    4753                } 
    48                 out.flush(); 
    4954        } 
    5055 
    51         public static void setOut(PrintWriter out) { 
    52                 Main.out = out; 
     56        private static void printUsage(PrintWriter out) { 
     57                CliArguments.printUsage(out); 
    5358        } 
    5459} 
  • AnalyzerBeans/trunk/cli/src/test/java/org/eobjects/analyzer/cli/MainTest.java

    r3053 r3068  
    2020package org.eobjects.analyzer.cli; 
    2121 
    22 import java.io.PrintWriter; 
     22import java.io.IOException; 
     23import java.io.OutputStream; 
     24import java.io.PrintStream; 
    2325import java.io.StringWriter; 
    2426 
    2527import junit.framework.TestCase; 
    2628 
     29import org.apache.log4j.PropertyConfigurator; 
     30 
    2731public class MainTest extends TestCase { 
    2832 
     33        private StringWriter _stringWriter; 
     34        private PrintStream _originalSysOut; 
     35 
     36        @Override 
     37        protected void setUp() throws Exception { 
     38                _stringWriter = new StringWriter(); 
     39                _originalSysOut = System.out; 
     40                useAsSystemOut(_stringWriter); 
     41                 
     42                PropertyConfigurator.configure("src/test/resources/log4j.xml"); 
     43        } 
     44 
     45        private void useAsSystemOut(StringWriter stringWriter) { 
     46                OutputStream out = new OutputStream() { 
     47                        @Override 
     48                        public void write(int b) throws IOException { 
     49                                _stringWriter.write(b); 
     50                        } 
     51                }; 
     52                System.setOut(new PrintStream(out)); 
     53        } 
     54 
     55        @Override 
     56        protected void tearDown() throws Exception { 
     57                super.tearDown(); 
     58                System.setOut(_originalSysOut); 
     59        } 
     60 
    2961        public void testUsage() throws Throwable { 
    30                 StringWriter stringWriter = new StringWriter(); 
    31                 Main.setOut(new PrintWriter(stringWriter)); 
    32  
    3362                Main.main("-usage".split(" ")); 
    3463 
    35                 String out1 = stringWriter.toString(); 
     64                String out1 = _stringWriter.toString(); 
    3665 
    3766                String[] lines = out1.split("\n"); 
    3867 
    39                 assertEquals(8, lines.length); 
     68                assertEquals(10, lines.length); 
    4069 
    4170                assertEquals( 
     
    5382                assertEquals("DATASTORES | SCHEMAS | TABLES | COLUMNS]                   : configuration", lines[5].trim()); 
    5483                assertEquals( 
     84                                "-of (--output-file) FILE                                   : File in which to save the result of the job", 
     85                                lines[6].trim()); 
     86                assertEquals("-ot (--output-type) [TEXT | HTML | SERIALIZED]             : How to represent the result of the job", 
     87                                lines[7].trim()); 
     88                assertEquals( 
    5589                                "-s (-schema, --schema-name) VAL                            : Name of schema when printing a list of tables or columns", 
    56                                 lines[6].trim()); 
     90                                lines[8].trim()); 
    5791                assertEquals( 
    5892                                "-t (-table, --table-name) VAL                              : Name of table when printing a list of columns", 
    59                                 lines[7].trim()); 
     93                                lines[9].trim()); 
    6094 
    6195                // again without the -usage flag 
    62                 stringWriter = new StringWriter(); 
    63                 Main.setOut(new PrintWriter(stringWriter)); 
     96                _stringWriter = new StringWriter(); 
     97                useAsSystemOut(_stringWriter); 
    6498                Main.main(new String[0]); 
    6599 
    66                 String out2 = stringWriter.toString(); 
     100                String out2 = _stringWriter.toString(); 
    67101                assertEquals(out1, out2); 
    68102        } 
    69103 
    70104        public void testListDatastores() throws Throwable { 
    71                 StringWriter stringWriter = new StringWriter(); 
    72                 Main.setOut(new PrintWriter(stringWriter)); 
    73  
    74105                Main.main("-conf examples/conf.xml -list DATASTORES".split(" ")); 
    75106 
    76                 String out = stringWriter.toString().replaceAll("\r\n", "\n"); 
     107                String out = _stringWriter.toString().replaceAll("\r\n", "\n"); 
    77108                assertEquals("Datastores:\n-----------\nall_datastores\nemployees_csv\norderdb\n", out); 
    78109        } 
    79110 
    80111        public void testListSchemas() throws Throwable { 
    81                 StringWriter stringWriter = new StringWriter(); 
    82                 Main.setOut(new PrintWriter(stringWriter)); 
    83  
    84112                Main.main("-conf examples/conf.xml -ds orderdb -list SCHEMAS".split(" ")); 
    85113 
    86                 String out = stringWriter.toString().replaceAll("\r\n", "\n"); 
     114                String out = _stringWriter.toString().replaceAll("\r\n", "\n"); 
    87115                assertEquals("Schemas:\n" + "--------\n" + "INFORMATION_SCHEMA\n" + "PUBLIC\n", out); 
    88116        } 
    89117 
    90118        public void testListTables() throws Throwable { 
    91                 StringWriter stringWriter = new StringWriter(); 
    92                 Main.setOut(new PrintWriter(stringWriter)); 
    93  
    94119                Main.main("-conf examples/conf.xml -ds orderdb -schema PUBLIC -list TABLES".split(" ")); 
    95120 
    96                 String out = stringWriter.toString().replaceAll("\r\n", "\n"); 
     121                String out = _stringWriter.toString().replaceAll("\r\n", "\n"); 
    97122                assertEquals( 
    98123                                "Tables:\n-------\nCUSTOMERS\nCUSTOMER_W_TER\nDEPARTMENT_MANAGERS\nDIM_TIME\nEMPLOYEES\nOFFICES\nORDERDETAILS\nORDERFACT\nORDERS\nPAYMENTS\nPRODUCTS\nQUADRANT_ACTUALS\nTRIAL_BALANCE\n", 
     
    101126 
    102127        public void testListColumns() throws Throwable { 
    103                 StringWriter stringWriter = new StringWriter(); 
    104                 Main.setOut(new PrintWriter(stringWriter)); 
    105  
    106128                Main.main("-conf examples/conf.xml -ds orderdb -schema PUBLIC -table EMPLOYEES -list COLUMNS".split(" ")); 
    107129 
    108                 String out = stringWriter.toString().replaceAll("\r\n", "\n"); 
     130                String out = _stringWriter.toString().replaceAll("\r\n", "\n"); 
    109131                assertEquals( 
    110132                                "Columns:\n--------\nEMPLOYEENUMBER\nLASTNAME\nFIRSTNAME\nEXTENSION\nEMAIL\nOFFICECODE\nREPORTSTO\nJOBTITLE\n", 
     
    113135 
    114136        public void testListTransformers() throws Throwable { 
    115                 StringWriter stringWriter = new StringWriter(); 
    116                 Main.setOut(new PrintWriter(stringWriter)); 
    117  
    118137                Main.main("-conf examples/conf.xml -list TRANSFORMERS".split(" ")); 
    119138 
    120                 String out = stringWriter.toString().replaceAll("\r\n", "\n"); 
     139                String out = _stringWriter.toString().replaceAll("\r\n", "\n"); 
    121140                String[] lines = out.split("\n"); 
    122141 
     
    128147 
    129148        public void testListFilters() throws Throwable { 
    130                 StringWriter stringWriter = new StringWriter(); 
    131                 Main.setOut(new PrintWriter(stringWriter)); 
    132  
    133149                Main.main("-conf examples/conf.xml -list FILTERS".split(" ")); 
    134150 
    135                 String out = stringWriter.toString().replaceAll("\r\n", "\n"); 
     151                String out = _stringWriter.toString().replaceAll("\r\n", "\n"); 
    136152                String[] lines = out.split("\n"); 
    137153 
     
    143159 
    144160        public void testListAnalyzers() throws Throwable { 
    145                 StringWriter stringWriter = new StringWriter(); 
    146                 Main.setOut(new PrintWriter(stringWriter)); 
    147  
    148161                Main.main("-conf examples/conf.xml -list ANALYZERS".split(" ")); 
    149162 
    150                 String out = stringWriter.toString().replaceAll("\r\n", "\n"); 
     163                String out = _stringWriter.toString().replaceAll("\r\n", "\n"); 
    151164                String[] lines = out.split("\n"); 
    152165 
     
    158171 
    159172        public void testListExplorers() throws Throwable { 
    160                 StringWriter stringWriter = new StringWriter(); 
    161                 Main.setOut(new PrintWriter(stringWriter)); 
    162  
    163173                Main.main("-conf examples/conf.xml -list EXPLORERS".split(" ")); 
    164174 
    165                 String out = stringWriter.toString().replaceAll("\r\n", "\n"); 
     175                String out = _stringWriter.toString().replaceAll("\r\n", "\n"); 
    166176                String[] lines = out.split("\n"); 
    167177 
     
    172182 
    173183        public void testExampleEmployeesJob() throws Throwable { 
    174                 StringWriter stringWriter = new StringWriter(); 
    175                 Main.setOut(new PrintWriter(stringWriter)); 
    176184                Main.main("-conf examples/conf.xml -job examples/employees_job.xml".split(" ")); 
    177185 
    178                 String out = stringWriter.toString().replaceAll("\r\n", "\n"); 
     186                String out = _stringWriter.toString().replaceAll("\r\n", "\n"); 
    179187                String[] lines = out.split("\n"); 
    180                 assertEquals("SUCCESS!", lines[0]); 
    181188 
    182189                assertTrue(out.indexOf("Top values:\n" + " - company.com: 4\n" + " - eobjects.org: 2") != -1); 
     
    184191                assertTrue(lines.length > 80); 
    185192                assertTrue(lines.length < 90); 
     193 
     194                assertEquals("SUCCESS!", lines[0]); 
    186195        } 
    187196} 
  • DataCleaner/trunk/core/src/main/java/org/eobjects/datacleaner/bootstrap/Bootstrap.java

    r3028 r3068  
    9494                        if (arguments.isUsageMode()) { 
    9595                                final PrintWriter out = new PrintWriter(System.out); 
    96                                 CliArguments.printUsage(out); 
     96                                try { 
     97                                        CliArguments.printUsage(out); 
     98                                } finally { 
     99                                        FileHelper.safeClose(out); 
     100                                } 
    97101 
    98102                                exitCommandLine(null, 1); 
     
    123127                if (cliMode) { 
    124128 
    125                         final PrintWriter out = new PrintWriter(System.out); 
    126129                        // run in CLI mode 
    127130 
    128131                        int exitCode = 0; 
     132                        final CliArguments arguments = _options.getCommandLineArguments(); 
     133                        final CliRunner runner = new CliRunner(arguments); 
    129134                        try { 
    130                                 final CliArguments arguments = _options.getCommandLineArguments(); 
    131                                 final CliRunner runner = new CliRunner(arguments, out); 
    132135                                runner.run(configuration); 
    133136                        } catch (Throwable e) { 
     
    135138                                exitCode = 1; 
    136139                        } finally { 
    137                                 out.flush(); 
     140                                runner.close(); 
    138141                                exitCommandLine(configuration, exitCode); 
    139142                        } 
     
    143146                        final MacOSManager macOsManager = injector.getInstance(MacOSManager.class); 
    144147                        macOsManager.init(); 
    145                          
     148 
    146149                        // run in GUI mode 
    147150                        final AnalysisJobBuilderWindow analysisJobBuilderWindow = injector.getInstance(AnalysisJobBuilderWindow.class); 
Note: See TracChangeset for help on using the changeset viewer.