Changeset 4925


Ignore:
Timestamp:
May 8, 2009, 1:30:36 PM (12 years ago)
Author:
Nicklas Nordborg
Message:

References #742: Refactor BioAssaySetExporter?

The new exporter structure is in place. The old export and plug-in has not been touched yet.

Location:
trunk/src
Files:
17 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/util/ConsoleProgressReporter.java

    r4889 r4925  
    5757  {
    5858    if (needNewLine) System.out.print(useNewLine ? "\n" : "\r");
    59     System.out.print("["+percent + "%]\t" + message == null ? "" : message);
     59    System.out.print("["+percent + "%]\t" + (message == null ? "" : message));
    6060    needNewLine = true;
    6161  }
  • trunk/src/core/net/sf/basedb/util/Values.java

    r4515 r4925  
    393393  }
    394394 
    395   public static final <T> String getString(Collection<T> values, String deliminator, boolean skipNull, Formatter<T> formatter)
     395  public static final <T> String getString(Collection<T> values, String deliminator, boolean skipNull, Formatter<? super T> formatter)
    396396  {
    397397    StringBuilder sb = new StringBuilder();
  • trunk/src/core/net/sf/basedb/util/formatter/NameableFormatter.java

    r4901 r4925  
    3333  implements Formatter<Nameable>
    3434{
     35  private String nullValue = "";
    3536
     37  /**
     38    Create a new formatter. Null values are formatted as an empty string.
     39  */
     40  public NameableFormatter()
     41  {}
     42 
     43  /**
     44    Create a new formatter.
     45    @param nullValue The string to return if a null item is
     46      passed to {@link #format(Nameable)}
     47  */
     48  public NameableFormatter(String nullValue)
     49  {
     50    this.nullValue = nullValue;
     51  }
     52 
     53  /*
     54    From the Formatter interface
     55    -------------------------------------------
     56  */
    3657  @Override
    3758  public String format(Nameable nameableItem)
    3859  {   
    39     return nameableItem == null ? "" : nameableItem.getName();
     60    return nameableItem == null ? nullValue : nameableItem.getName();
    4061  }
    4162
     63  /**
     64    @throws UnsupportedOperationException Always
     65  */
    4266  @Override
    4367  public Nameable parseString(String value)
    4468  {
    45     // TODO Auto-generated method stub
    46     return null;
     69    throw new UnsupportedOperationException("parseString: " + value);
    4770  }
     71  // -------------------------------------------
    4872
    4973}
  • trunk/src/test/TestBioAsssaySetExporter.java

    r4917 r4925  
     1/*
     2 $Id$
     3
     4 Copyright (C) 2007 Johan Enell
     5
     6 This file is part of BASE - BioArray Software Environment.
     7 Available at http://base.thep.lu.se/
     8
     9 BASE is free software; you can redistribute it and/or modify it
     10 under the terms of the GNU General Public License as published by
     11 the Free Software Foundation; either version 3 of the License, or
     12 (at your option) any later version.
     13
     14 BASE is distributed in the hope that it will be useful, but
     15 WITHOUT ANY WARRANTY; without even the implied warranty of
     16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     17 General Public License for more details.
     18
     19 You should have received a copy of the GNU General Public License
     20 along with BASE. If not, see <http://www.gnu.org/licenses/>.
     21*/
    122import net.sf.basedb.core.Annotation;
    223import net.sf.basedb.core.AnnotationSet;
     
    1334import net.sf.basedb.core.ItemQuery;
    1435import net.sf.basedb.core.Platform;
    15 import net.sf.basedb.core.ProgressReporter;
    1636import net.sf.basedb.core.RawBioAssay;
    1737import net.sf.basedb.core.RawDataBatcher;
     
    2141import net.sf.basedb.core.SystemItems;
    2242import net.sf.basedb.core.Type;
     43import net.sf.basedb.core.VirtualColumn;
    2344import net.sf.basedb.core.data.RawData;
    2445import net.sf.basedb.core.data.ReporterData;
     46import net.sf.basedb.core.query.Dynamic;
    2547import net.sf.basedb.core.query.Expressions;
    2648import net.sf.basedb.core.query.Hql;
     49import net.sf.basedb.core.query.Orders;
    2750import net.sf.basedb.core.query.Restrictions;
    28 import net.sf.basedb.plugins.BioAssaySetExporter;
    2951import net.sf.basedb.util.ConsoleProgressReporter;
     52import net.sf.basedb.util.basefile.BaseFileWriter;
     53import net.sf.basedb.util.export.spotdata.BaseFileExporter;
     54import net.sf.basedb.util.export.spotdata.DynamicField;
     55import net.sf.basedb.util.export.spotdata.ExportableFieldFactory;
     56import net.sf.basedb.util.export.spotdata.MatrixBaseFileExporter;
     57import net.sf.basedb.util.export.spotdata.SerialBaseFileExporter;
    3058import net.sf.basedb.util.parser.FlatFileParser;
    3159import net.sf.basedb.util.parser.FlatFileParser.Line;
    3260
     61import java.io.OutputStreamWriter;
    3362import java.util.ArrayList;
    3463import java.util.Arrays;
    35 import java.util.Collections;
    36 import java.util.HashSet;
    37 import java.util.Set;
    38 import java.util.TreeMap;
    39 import java.util.regex.Matcher;
     64import java.util.List;
    4065import java.util.regex.Pattern;
    4166
    42 /*
    43  $Id$
    44 
    45  Copyright (C) 2007 Johan Enell
    46 
    47  This file is part of BASE - BioArray Software Environment.
    48  Available at http://base.thep.lu.se/
    49 
    50  BASE is free software; you can redistribute it and/or modify it
    51  under the terms of the GNU General Public License as published by
    52  the Free Software Foundation; either version 3 of the License, or
    53  (at your option) any later version.
    54 
    55  BASE is distributed in the hope that it will be useful, but
    56  WITHOUT ANY WARRANTY; without even the implied warranty of
    57  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    58  General Public License for more details.
    59 
    60  You should have received a copy of the GNU General Public License
    61  along with BASE. If not, see <http://www.gnu.org/licenses/>.
    62  */
    63 
    6467/**
    6568
    66  @author johan
     69 @author johan, nicklas
    6770 @version 2.3
    6871 @base.modified $Date$
     
    7174{
    7275  static boolean ok = true;
    73  
    74   static ArrayList<String> readlines = new ArrayList<String>();
    7576 
    7677  public static void main(String[] args)
     
    319320      ReporterBatcher rb = ReporterBatcher.getNew(dc);
    320321      ReporterData r = Reporter.getNew(name);
     322      r.setName(name);
    321323      rb.insert(r);
    322324      rb.close();
     
    374376      BioAssaySet bas = BioAssaySet.getById(dc, bioAssaySetId);
    375377      File file = File.getById(dc, fileId);
    376      
    377       // I use a TreeMap so I can predict the order of the parameters
    378       TreeMap<String, String> parameters = new TreeMap<String, String>();
    379       parameters.put("b", "some parameter");
    380       parameters.put("a", "some other parameter");
    381      
    382       ArrayList<String> rep = new ArrayList<String>();
    383       rep.add("rep.id");
    384       rep.add("rep.name");
    385       rep.add("rep.position");
    386      
    387       ArrayList<String> spot = new ArrayList<String>();
     378      ItemQuery<BioAssay> query = bas.getBioAssays();
     379      query.order(Orders.asc(Hql.property("name")));
     380      query.order(Orders.asc(Hql.property("id")));
     381      List<BioAssay> assays = new ArrayList<BioAssay>(query.list(dc));
     382     
     383      // Initialise exporter
     384      BaseFileExporter bfe = matrix ? new MatrixBaseFileExporter() : new SerialBaseFileExporter();
     385      bfe.setDbControl(dc);
     386      bfe.setSource(bas);
     387      bfe.setAverageOnReporter(mergeReporters);
     388      if (!TestUtil.getSilent()) bfe.setProgressReporter(new ConsoleProgressReporter());
     389      bfe.setWriter(new BaseFileWriter(new OutputStreamWriter(file.getUploadStream(false))));
     390     
     391      // Parameters that go in the 'settings' section
     392      bfe.setParameter("b", "some parameter");
     393      bfe.setParameter("a", "some other parameter");
     394
     395      // Configure the reporter fields to export
     396      List<DynamicField> reporterFields = new ArrayList<DynamicField>();
     397      reporterFields.add(ExportableFieldFactory.reporter("id", null, Formula.AverageMethod.NONE, null));
     398      reporterFields.add(ExportableFieldFactory.reporter("name", null, Formula.AverageMethod.NONE, null));
     399      reporterFields.add(ExportableFieldFactory.simple(Dynamic.column(VirtualColumn.POSITION), "position", Formula.AverageMethod.NONE, null));
     400      bfe.addReporterFields(reporterFields);
     401
     402      // Configure the spot fields to export
    388403      ItemQuery<Formula> ratio = Formula.getQuery(Formula.Type.COLUMN_EXPRESSION,
    389         bas.getRawDataType(), bas.getIntensityTransform());
    390       ratio.restrict(Restrictions.eq(Hql.property("name"), Expressions.string("Ratio, ch1 / ch2")));
    391       if (ratio.count(dc) < 1)
    392       {
    393         throw new Exception("Cant find formula 'Ratio, ch1 / ch2'");
    394       }
    395       spot.add("intensity1");
    396       spot.add("raw.row");
    397       spot.add("raw.col");
    398       spot.add("raw.flags");
    399       spot.add("frm."+ratio.list(dc).get(0).getId());
    400      
    401       //Create the expected header values values
    402       //Assays section
    403       Set<String> assaysAnnotationColumns = Collections.singleton("testExportAnnotation");
    404       HashSet<String> assaysColumns = new HashSet<String>(assaysAnnotationColumns);
    405       assaysColumns.add("id");
    406       assaysColumns.add("name");
    407       Set<String> assaysCount = Collections.singleton("2");
     404          bas.getRawDataType(), bas.getIntensityTransform());
     405        ratio.restrict(Restrictions.eq(Hql.property("name"), Expressions.string("Ratio, ch1 / ch2")));
     406        if (ratio.count(dc) < 1)
     407        {
     408          throw new Exception("Cant find formula 'Ratio, ch1 / ch2'");
     409        }
     410     
     411      List<DynamicField> spotFields = new ArrayList<DynamicField>();
     412      spotFields.add(ExportableFieldFactory.channel(1, "intensity1", null));
     413      spotFields.add(ExportableFieldFactory.rawData("row", null, Formula.AverageMethod.NONE, null));
     414      spotFields.add(ExportableFieldFactory.rawData("flags", null, Formula.AverageMethod.NONE, null));
     415      spotFields.add(ExportableFieldFactory.formula(ratio.list(dc).get(0), null, null));
     416      bfe.addSpotFields(spotFields);
    408417     
    409418      //Spots section
    410       Set<String> spotsChannels = Collections.singleton("2");
    411       HashSet<String> spotsAssayFields = new HashSet<String>();
    412       spotsAssayFields.add("intensity1");
    413       spotsAssayFields.add("row");
    414       spotsAssayFields.add("col");
    415       spotsAssayFields.add("flags");
    416       spotsAssayFields.add("Ratio, ch1 / ch2");
    417       HashSet<String> spotsColumns = new HashSet<String>();
    418       spotsColumns.add("reporter");
    419       spotsColumns.add("name");
    420       spotsColumns.add("position");
    421       spotsColumns.add("assayData");
    422       HashSet<String> spotsAssays = new HashSet<String>();
    423       for (BioAssay ba : bas.getBioAssays().list(dc))
    424       {
    425         spotsAssays.add(String.valueOf(ba.getId()));
    426       }
    427       Set<String> spotsCount;
    428       if (matrix)
    429       {
    430         if (mergeReporters)
    431         {
    432           spotsCount = Collections.singleton("2");
    433         }
    434         else
    435         {
    436           spotsCount = Collections.singleton("6");
    437         }
    438       }
    439       else
    440       {
    441         if (mergeReporters)
    442         {
    443           spotsCount = Collections.singleton("2");
    444         }
    445         else
    446         {
    447           spotsCount = Collections.singleton("5");
    448         }
    449       }
    450      
    451       BioAssaySetExporter exporter = new BioAssaySetExporter();
    452       ProgressReporter progress = null;
    453       if (!TestUtil.getSilent()) progress = new ConsoleProgressReporter();
    454       if (matrix)
    455       {
    456         exporter.exportBaseFileMatrix(bas, file, parameters, rep, spot,
    457             mergeReporters, progress);
    458       }
    459       else
    460       {
    461         exporter.exportBaseFileSerial(bas, file, parameters, rep, spot,
    462             mergeReporters, progress);
    463       }
    464       dc.commit();
    465      
    466       dc = TestUtil.getDbControl();
    467      
     419      bfe.doExport();
     420      if (!TestUtil.getSilent()) write("");
     421      dc.commit();
     422     
     423      // Now it is time to test if the result is what we expected
     424      dc = TestUtil.getDbControl();
    468425      ffp = new FlatFileParser();
    469426      ffp.setSectionRegexp(Pattern.compile("section[\\t](.*)"));
     
    475432      ffp.setUseNullIfEmpty(false);
    476433
    477       ArrayList<String[]> rows = new ArrayList<String[]>();
    478      
    479       readlines.clear();
     434      List<Pattern[]> rowPatterns;
     435     
     436      // Test BASEfile marker
     437      ffp.parseHeaders();
     438      TestFlatFileParser.testLine(ffp.getLine(0), "BASEfile");
     439
    480440      // Test settings section
    481       ffp.parseHeaders();
    482       for (Line l : ffp.getLines())
    483       {
    484         readlines.add(l.toString());
    485       }
    486       TestFlatFileParser.testLine(ffp.getLine(0), "BASEfile");
    487441      TestFlatFileParser.testSection(ffp.nextSection(), "settings");
    488442      ffp.parseHeaders();
    489       for (Line l : ffp.getLines())
    490       {
    491         readlines.add(l.toString());
    492       }
    493       TestFlatFileParser.testHeaderLine(ffp.getLine(0), "a", "some other parameter");
    494       TestFlatFileParser.testHeaderLine(ffp.getLine(1), "b", "some parameter");
    495      
    496       readlines.clear();
     443      TestFlatFileParser.testHeaderLine(ffp.getLine(0), "b", "some parameter");
     444      TestFlatFileParser.testHeaderLine(ffp.getLine(1), "a", "some other parameter");
     445
    497446      // Test assays section
    498       TestFlatFileParser.testSection(ffp.nextSection(), "assays");
     447      Line section = ffp.nextSection();
     448      TestFlatFileParser.testSection(section, "assays");
    499449      ffp.parseHeaders();
    500       for (Line l : ffp.getLines())
    501       {
    502         readlines.add(l.toString());
    503       }
    504      
    505       test_baseFileSectionHeader("annotationColumns", ffp.getHeader("annotationColumns"), assaysAnnotationColumns);
    506       test_baseFileSectionHeader("columns", ffp.getHeader("columns"), assaysColumns);
    507       test_baseFileSectionHeader("count", ffp.getHeader("count"), assaysCount);
    508       rows.add(null);
    509       rows.add(null);
    510       test_baseFileSectionData(ffp, rows);
    511      
    512       readlines.clear();
     450      TestFlatFileParser.testHeaderLine(ffp.getLine(0), "columns", "\\t", "id", "name", "testExportAnnotation");
     451      TestFlatFileParser.testHeaderLine(ffp.getLine(1), "annotationColumns", "\\t", "testExportAnnotation");
     452      TestFlatFileParser.testHeaderLine(ffp.getLine(2), "count", "2");
     453      rowPatterns = new ArrayList<Pattern[]>(2);
     454      rowPatterns.add(null);
     455      rowPatterns.add(null);
     456      TestFlatFileParser.testDataSection(ffp, rowPatterns, section);
     457     
    513458      // Test spot section(s)
    514459      int numOfSections = matrix ? 1 : (int) bas.getBioAssays().count(dc);
    515       Line section = ffp.nextSection();
    516       while (section != null && numOfSections > 0)
     460      String spotsCount;
     461      if (matrix)
     462      {
     463        spotsCount = mergeReporters ? "2" : "6";
     464      }
     465      else
     466      {
     467        spotsCount = mergeReporters ? "2" : "5";
     468      }
     469     
     470      rowPatterns.clear();
     471      if (mergeReporters)
     472      {
     473        Pattern[] p = new Pattern[matrix ? 11 : 7];
     474        p[0] = Pattern.compile("\\d+"); // Reporter id
     475        p[1] = Pattern.compile("exportreporter\\d"); // Reporter name
     476        p[2] = Pattern.compile("[123456]"); // Position
     477        p[3] = Pattern.compile("[14](\\.0)?"); // spot intensity
     478        p[4] = Pattern.compile(""); // row
     479        p[5] = p[4];            // flags
     480        p[6] = Pattern.compile("0\\.5"); // Ratio
     481        if (matrix)
     482        {
     483          for (int i = 7; i < 11; ++i) // Repeat for second assay
     484          {
     485            p[i] = p[i-4];
     486          }
     487        }
     488        rowPatterns.add(p);
     489        rowPatterns.add(p);
     490      }
     491      else
     492      {
     493        if (matrix)
     494        {
     495          Pattern[] p1 = new Pattern[11];
     496          p1[0] = Pattern.compile("\\d+"); // Reporter id
     497          p1[1] = Pattern.compile("exportreporter\\d"); // Reporter name
     498          p1[2] = Pattern.compile("[1234]");  // Position
     499          p1[3] = Pattern.compile("1?(\\.0)?");  // Spot intensity
     500          p1[4] = Pattern.compile("[01]");  // Row
     501          p1[5] = Pattern.compile("0"); // Flags
     502          p1[6] = Pattern.compile("0\\.5");  // Ratio
     503         
     504          Pattern[] p2 = new Pattern[11];
     505          p2[0] = p1[0];
     506          p2[1] = p1[1];
     507          p2[2] = Pattern.compile("[56]");  // Position
     508          p2[3] = Pattern.compile("(10(\\.0)?)|");  // Spot intensity
     509          p2[4] = Pattern.compile("2|");  // Row
     510          p2[5] = Pattern.compile("-50|");  // Flags
     511          p2[6] = Pattern.compile("0\\.5|"); //Ratio
     512          for (int i = 7; i < 11; ++i)
     513          {
     514            p1[i] = p1[i-4];
     515            p2[i] = p2[i-4];
     516          }
     517          rowPatterns.add(p1);
     518          rowPatterns.add(p1);
     519          rowPatterns.add(p1);
     520          rowPatterns.add(p1);
     521          rowPatterns.add(p2);
     522          rowPatterns.add(p2);
     523        }
     524        else
     525        {
     526          Pattern[] p = new Pattern[7];
     527          p[0] = Pattern.compile("\\d+"); // Reporter id
     528          p[1] = Pattern.compile("exportreporter\\d"); // Reporter name
     529          p[2] = Pattern.compile("[123456]"); // Position
     530          p[3] = Pattern.compile("10?(\\.0)?");  // Spot intensity
     531          p[4] = Pattern.compile("[012]");  // Row
     532          p[5] = Pattern.compile("(0)|(-50)");  // Flags
     533          p[6] = Pattern.compile("0\\.5");  // Ratio
     534          rowPatterns.add(p);
     535          rowPatterns.add(p);
     536          rowPatterns.add(p);
     537          rowPatterns.add(p);
     538          rowPatterns.add(p);
     539        }
     540      }
     541
     542      section = ffp.nextSection();
     543      int sectionIndex = 0;
     544      while (section != null)
    517545      {
    518546        TestFlatFileParser.testSection(section, "spots");
    519547        ffp.parseHeaders();
    520         for (Line l : ffp.getLines()) readlines.add(l.toString());
    521         test_baseFileSectionHeader("channels", ffp.getHeader("channels"), spotsChannels);
    522         test_baseFileSectionHeader("assayFields", ffp.getHeader("assayFields"), spotsAssayFields);
    523         test_baseFileSectionHeader("columns", ffp.getHeader("columns"), spotsColumns);
    524         test_baseFileSectionHeader("assays", ffp.getHeader("assays"), spotsAssays);
    525         test_baseFileSectionHeader("count", ffp.getHeader("count"), spotsCount);
    526 
    527         rows.clear();
     548        List<String> expectedAssays = new ArrayList<String>();
    528549        if (matrix)
    529550        {
    530           if (mergeReporters)
     551          for (BioAssay ba : assays)
    531552          {
    532             String[] cols = {"\\d+","New reporter","[123456]","[14](\\.0)?","","","","0\\.5","[14](\\.0)?","","","","0\\.5"};
    533             rows.add(cols);
    534             rows.add(cols);
    535           }
    536           else
    537           {
    538             String[] cols1 = {"\\d+","New reporter","[1234]","1(\\.0)?","[01]","[01]","0","0\\.5","1(\\.0)?","[01]","[01]","0","0\\.5"};
    539             String[] cols2 = {"\\d+","New reporter","[56]","(10(\\.0)?)|","2|","0|","-50|","0\\.5|","(10(\\.0)?)|","2|","0|","-50|","0\\.5|"};
    540             rows.add(cols1);
    541             rows.add(cols1);
    542             rows.add(cols1);
    543             rows.add(cols1);
    544             rows.add(cols2);
    545             rows.add(cols2);
     553            expectedAssays.add(Integer.toString(ba.getId()));
    546554          }
    547555        }
    548556        else
    549557        {
    550           if (mergeReporters)
    551           {
    552             String[] cols = {"\\d+","New reporter","[123456]","[14](\\.0)?","","","","0\\.5"};
    553             rows.add(cols);
    554             rows.add(cols);
    555           }
    556           else
    557           {
    558             String[] cols = {"\\d+","New reporter","[123456]","10?(\\.0)?","[012]","[01]","(0)|(-50)","0\\.5"};
    559             rows.add(cols);
    560             rows.add(cols);
    561             rows.add(cols);
    562             rows.add(cols);
    563             rows.add(cols);
    564           }
     558          expectedAssays.add(Integer.toString(assays.get(sectionIndex).getId()));
    565559        }
    566         test_baseFileSectionData(ffp, rows);
     560        TestFlatFileParser.testHeaderLine(ffp.getLine(0), "channels", "2");
     561        TestFlatFileParser.testHeaderLine(ffp.getLine(1), "assays", "\\t", expectedAssays);
     562        TestFlatFileParser.testHeaderLine(ffp.getLine(2), "columns", "\\t", "id", "name", "position", "assayData");
     563        TestFlatFileParser.testHeaderLine(ffp.getLine(3), "assayFields", "\\t", "intensity1", "row", "flags", "Ratio, ch1 / ch2");
     564        TestFlatFileParser.testHeaderLine(ffp.getLine(4), "count", spotsCount);
     565        TestFlatFileParser.testDataSection(ffp, rowPatterns, section);
    567566       
    568567        section = ffp.nextSection();
    569         --numOfSections;
     568        sectionIndex++;
    570569      }
    571570      write("--Exporting basefile [matrix:"+matrix+", mergeReporters:"+mergeReporters+"] SUCCEEDED");
     
    573572    catch (Throwable ex)
    574573    {
    575       for (String line : readlines)
    576       {
    577         write("["+line+"]");
    578       }
    579574      write("--Exporting basefile [matrix:"+matrix+", mergeReporters:"+mergeReporters+"] FAILED");
    580575      ex.printStackTrace();
     
    587582  }
    588583 
    589   private static ArrayList<FlatFileParser.Data> test_baseFileSectionData(FlatFileParser ffp, ArrayList<String[]> rows)
    590     throws Exception
    591   {
    592     ArrayList<FlatFileParser.Data> datalines = new ArrayList<FlatFileParser.Data>(rows.size());
    593     while (ffp.hasMoreData())
    594     {
    595       FlatFileParser.Data dataline = ffp.nextData();
    596       readlines.add(dataline.toString());
    597       datalines.add(dataline);
    598     }
    599    
    600     if (datalines.size() != rows.size())
    601     {
    602       throw new Exception("Wrong number of data lines. Found "+datalines.size()+" expected "+rows.size());
    603     }
    604    
    605     for (int i = 0; i < rows.size(); ++i)
    606     {
    607       FlatFileParser.Data dataline = datalines.get(i);
    608      
    609       String[] row = rows.get(i);
    610       if (row != null)
    611       {
    612         if (dataline.columns() != row.length)
    613         {
    614           throw new Exception("Wrong number of columns. Found "+dataline.columns()+" expected "+row.length+". ["+dataline+"]");
    615         }
    616        
    617         for (int j = 0; j < row.length; ++j)
    618         {
    619           String col = row[j];
    620           Pattern p = Pattern.compile(col);
    621           Matcher m = p.matcher(dataline.get(j));
    622           if (!m.matches())
    623           {
    624             throw new Exception("The value["+dataline.get(j)+"] at column number "+(j+1)+" dosent match the pattern '"+p+"'. ["+dataline+"]");
    625           }
    626         }
    627       }
    628     }
    629    
    630     return datalines;
    631   }
    632 
    633   private static void test_baseFileSectionHeader(String header, String found, Set<String> expected)
    634     throws Exception
    635   {
    636     HashSet<String> foundSet = new HashSet<String>(Arrays.asList(found.split("\\t")));
    637     if (!expected.containsAll(foundSet))
    638     {
    639       throw new Exception("Expected values '"+expected+"' didnt contain the header values '"+foundSet+"' for header '"+header+"'");
    640     }
    641   }
    642 
    643584  static void write(String message)
    644585  {
  • trunk/src/test/TestFlatFileParser.java

    r4889 r4925  
     1import java.io.IOException;
    12import java.io.InputStream;
    23import java.util.Arrays;
    34import java.util.HashSet;
     5import java.util.List;
    46import java.util.Set;
     7import java.util.regex.Matcher;
    58import java.util.regex.Pattern;
    69
     10import net.sf.basedb.core.BaseException;
    711import net.sf.basedb.util.FileUtil;
    812import net.sf.basedb.util.parser.FlatFileParser;
    913import net.sf.basedb.util.parser.Mapper;
     14import net.sf.basedb.util.parser.FlatFileParser.Data;
    1015import net.sf.basedb.util.parser.FlatFileParser.Line;
    1116
     
    199204  }
    200205
    201   public static void testHeaderLine(Line line, String name, String value) throws Exception
     206  public static void testHeaderLine(Line line, String name, String value)
    202207  {
    203208    if (!line.type().equals(FlatFileParser.LineType.HEADER))
    204       throw new Exception("Line is no header: "+line.toString());
     209    {
     210      throw new BaseException("Line is no header: "+line);
     211    }
    205212    if (!line.name().equals(name))
    206       throw new Exception("Cant find name '"+name+"' in line '"+line.line()+"'");
     213    {
     214      throw new BaseException("Expected header name '"+name+"' on line '"+line+"'");
     215    }
    207216    if (!line.value().equals(value))
    208       throw new Exception("Cant find value '"+value+"' in line '"+line.line()+"'");
    209   }
    210 
    211   public static void testLine(Line line, String name) throws Exception
     217    {
     218      throw new BaseException("Expected header value '"+value+"' on line '"+line+"'");
     219    }
     220  }
     221
     222  public static void testHeaderLine(Line line, String name, String splitter, String... expected)
     223  {
     224    testHeaderLine(line, name, splitter, Arrays.asList(expected));
     225  }
     226 
     227  public static void testHeaderLine(Line line, String name, String splitter, List<String> expected)
     228  {
     229    if (!line.type().equals(FlatFileParser.LineType.HEADER))
     230    {
     231      throw new BaseException("Line is no header: "+line);
     232    }
     233    if (!line.name().equals(name))
     234    {
     235      throw new BaseException("Expected header name '"+name+"' on line '"+line+"'");
     236    }
     237    String[] lineValues = line.value().split(splitter);
     238    for (int i = 0; i < lineValues.length; ++i)
     239    {
     240      String fnd = lineValues[i];
     241      String exp = expected.get(i);
     242      if (!fnd.equals(exp))
     243      {
     244        throw new BaseException("Expected header value '" + exp + "' at index " + i + " on line '" + line + "'" );
     245      }
     246    }
     247  }
     248
     249 
     250  public static void testLine(Line line, String name)
    212251  {
    213252    if (!line.line().equals(name))
    214       throw new Exception("Line '"+line.line()+"' doesnt match '"+name+"'");
    215   }
    216 
    217   public static void testSection(Line line, String name) throws Exception
     253    {
     254      throw new BaseException("Line '"+line+"' doesn't match '"+name+"'");
     255    }
     256  }
     257
     258  public static void testSection(Line line, String name)
    218259  {
    219260    if (!line.type().equals(FlatFileParser.LineType.SECTION))
    220       throw new Exception("Line is no section: "+line.toString());
     261    {
     262      throw new BaseException("Line is no section: "+line);
     263    }
    221264    if (!line.name().equals(name))
    222       throw new Exception("Cant find sectionname '"+name+"' in line '"+line.line()+"'");
    223   }
     265    {
     266      throw new BaseException("Expected section name '"+name+"' on line '"+line+"'");
     267    }
     268  }
     269 
     270  public static void testDataSection(FlatFileParser ffp, List<Pattern[]> patterns, Line section)
     271    throws IOException
     272  {
     273    int index = 0;
     274    while (ffp.hasMoreData())
     275    {
     276      Data data = ffp.nextData();
     277      if (index < patterns.size())
     278      {
     279        Pattern[] p = patterns.get(index);
     280        if (p != null) testDataLine(data, p);
     281      }
     282      index++;
     283    }
     284   
     285    if (index != patterns.size())
     286    {
     287      throw new BaseException("Unexpected number of data lines. Found "+index+
     288          "; expected "+patterns.size() + " in section " + section);
     289    }
     290  }
     291 
     292  public static void testDataLine(Data data, Pattern[] regexp)
     293  {
     294    if (data.columns() != regexp.length)
     295    {
     296      throw new BaseException("Expected "+regexp.length+" columns on line "+data);
     297    }
     298    for (int i = 0; i < regexp.length; ++i)
     299    {
     300      Pattern p = regexp[i];
     301      if (p != null)
     302      {
     303        String value = data.get(i);
     304        Matcher m = p.matcher(value);
     305        if (!m.matches())
     306        {
     307          throw new BaseException("Value '"+value+"' at column " + i + " dosen't match the pattern '"+p+"' on line: " + data);
     308        }
     309      }
     310    }
     311  }
     312 
    224313}
Note: See TracChangeset for help on using the changeset viewer.