XlsDetector extends POIFSContainerDetector implements Detector { @Override public Format detect(Metadata metadata, TikaInputStream inputStream) throws IOException { Format result = null; MediaType mediaType = super.detect(inputStream, metadata); if (mediaType == null || StringUtils.equals(mediaType.toString(), FormatUtils.UNKNOWN_MEDIA_TYPE)) { mediaType = zipContainerDetector.detect(inputStream, new Metadata()); } if (mediaType != null) { String mediaTypeName = mediaType.toString(); if (StringUtils.startsWith(mediaTypeName, XlsFormatFamily.MEDIA_TYPE) || StringUtils.equals(mediaTypeName, OLD_XLS_MEDIA_TYPE)) { result = new Format(xlsFormatFamily, FormatUtils.DEFAULT_ENCODING); } } return result; }  @Override Format detect(Metadata metadata, TikaInputStream inputStream);  }
@Test(expected = IllegalArgumentException.class) public void should_not_read_null_input_stream() throws Exception { xlsDetector.detect(null); }
@Test public void should_detect_old_xls_format() throws Exception { try (InputStream inputStream = this.getClass().getResourceAsStream("test.xls")) { Format actual = xlsDetector.detect(inputStream); assertTrue(actual.getFormatFamily() instanceof XlsFormatFamily); assertTrue(StringUtils.equals("UTF-8", actual.getEncoding())); } }
@Test public void should_detect_new_xls_format() throws Exception { try (InputStream inputStream = this.getClass().getResourceAsStream("test_new.xlsx")) { Format actual = xlsDetector.detect(inputStream); assertTrue(actual.getFormatFamily() instanceof XlsFormatFamily); assertTrue(StringUtils.equals("UTF-8", actual.getEncoding())); } }
@Test public void read_xls_that_can_be_parsed_as_csv_TDP_375() throws Exception { String fileName = "TDP-375_xsl_read_as_csv.xls"; try (InputStream inputStream = this.getClass().getResourceAsStream(fileName)) { Format actual = xlsDetector.detect(inputStream); Assert.assertNotNull(actual); assertTrue(actual.getFormatFamily() instanceof XlsFormatFamily); assertEquals(XlsFormatFamily.MEDIA_TYPE, actual.getFormatFamily().getMediaType()); assertTrue(StringUtils.equals("UTF-8", actual.getEncoding())); } }