SampleCodeReader { public RetrievedCodeSample readSample(final String serviceName, final String sampleId) throws SampleRetrievalException { ValidationUtils.assertInputNotEmpty(serviceName, "Sample service name must be provided"); ValidationUtils.assertInputNotEmpty(sampleId, "Sample ID must be provided"); String targetSampleFile = String.format("/%s/%s.xml", sampleDir, serviceName); InputStream in = getClass().getResourceAsStream(targetSampleFile); if (in == null) { return null; } SampleCodeXMLParser parser = new SampleCodeXMLParser(sampleId); try { saxParser.parse(in, parser); } catch (Exception e) { throw new SampleRetrievalException("Could not parse sample", serviceName, sampleId, e); } String sampleContent = parser.getSampleContent(); String sampleTitle = parser.getSampleTitle(); String sampleDescription = parser.getSampleDescription(); if (ValidationUtils.areNullOrEmpty(sampleContent, sampleTitle, sampleDescription)) { return null; } return new RetrievedCodeSample(sampleId, serviceName, sampleTitle, sampleDescription, sampleContent); } SampleCodeReader(final String sampleDir); RetrievedCodeSample readSample(final String serviceName,
            final String sampleId);  }
@Test public void testRetrieveValidSample() throws Exception { RetrievedCodeSample sample = reader.readSample(SERVICE_NAME, SAMPLE_ID); assertEquals(sample.getId(), SAMPLE_ID); assertEquals(sample.getService(), SERVICE_NAME); assertEquals(sample.getTitle(), "Foo Sample"); assertEquals(sample.getDescription(), "A foo sample!"); assertEquals(sample.getContent(), "String foo = \"foo!\";"); }
@Test(expected = IllegalArgumentException.class) public void testRetrieveSampleNullService() throws SampleRetrievalException { reader.readSample(null, SAMPLE_ID); }
@Test(expected = IllegalArgumentException.class) public void testRetrieveSampleEmptyService() throws SampleRetrievalException { reader.readSample("", SAMPLE_ID); }
@Test(expected = IllegalArgumentException.class) public void testRetrieveSampleNullSample() throws SampleRetrievalException { reader.readSample(SERVICE_NAME, null); }
@Test(expected = IllegalArgumentException.class) public void testRetrieveSampleEmptySample() throws SampleRetrievalException { reader.readSample(SERVICE_NAME, ""); }
@Test public void testRetrieveNonExistantSample() throws SampleRetrievalException { Assert.assertNull(reader.readSample(SERVICE_NAME, "BogusSample")); }
@Test public void testRetrieveSampleWithMissingTitle() throws SampleRetrievalException { Assert.assertNull(reader.readSample(SERVICE_NAME, "MissingTitleSample")); }
@Test public void testRetrieveSampleWithEmptyTitle() throws SampleRetrievalException { Assert.assertNull(reader.readSample(SERVICE_NAME, "EmptyTitleSample")); }
@Test public void testRetrieveSampleWithMissingDescription() throws SampleRetrievalException { Assert.assertNull(reader.readSample(SERVICE_NAME, "MissingDescriptionSample")); }
@Test public void testRetrieveSampleWithEmptyDescription() throws SampleRetrievalException { Assert.assertNull(reader.readSample(SERVICE_NAME, "EmptyDescriptionSample")); }
@Test public void testRetrieveSampleWithMissingContent() throws SampleRetrievalException { Assert.assertNull(reader.readSample(SERVICE_NAME, "MissingContentSample")); }
@Test public void testRetrieveSampleWithEmptyContent() throws SampleRetrievalException { Assert.assertNull(reader.readSample(SERVICE_NAME, "EmptyContentSample")); }