### Question:
StringSubstitutionFactory { public static StringSubstitutor createStringSubstitutor(InputStream stringsubs) throws StringSubstitutionException { return new StringSubstitutionEngine(stringsubs); }  static StringSubstitutor createStringSubstitutor(InputStream stringsubs);  }### Answer:
@Test public void testStringSubstitutorInvalidStream() { StringBuffer pathBuffer = new StringBuffer(); pathBuffer.append(TestStringSubstitutionFactory.class.getPackage().getName().replace(".", "/")); pathBuffer.append(File.separator); pathBuffer.append(this.getClass().getSimpleName()); pathBuffer.append(".class"); InputStream invalidStream = TestStringSubstitutionFactory.class.getClassLoader().getResourceAsStream(pathBuffer.toString()); try { StringSubstitutionFactory.createStringSubstitutor(invalidStream); } catch (StringSubstitutionException e) { return; } Assert.fail("No exception thrown for invalid stream."); }
@Test public void testStringSubstitutorNullStream() { try { StringSubstitutionFactory.createStringSubstitutor(null); } catch (StringSubstitutionException e) { return; } Assert.fail("No exception thrown for null stream."); }
@Test public void testStringSubstitutorValidStream() { InputStream invalidStream = TestStringSubstitutionFactory.class.getClassLoader(). getResourceAsStream(_stringSubsPath); try { StringSubstitutor substitutor = StringSubstitutionFactory.createStringSubstitutor(invalidStream); substitutor.setAttributePreprocessor(new AttributePreprocessorImpl(_substitutionMap)); backUpTestFile(); substitutor.substituteAll(); for (Group group : substitutor.getStringSubsDefinition().getGroup()) { if (group.getId().equals(VALID_GROUP_ID)) { validateSubstitutedArchiveEntries(group); for (FileEntry fileEntry : group.getFileEntry()) { if (fileEntry.getName().equalsIgnoreCase(_testFileName) && !validateTestFile(new File(fileEntry.getName()))) { Assert.fail("Substitution failed in the test file."); break; } } } } restoreTestFile(); } catch (StringSubstitutionException e) { Assert.fail("Exception occurred during string substitution process.", e); } }
@Test public void testStringSubstitutorInvalidStream() { StringBuilder pathBuffer = new StringBuilder(); pathBuffer.append(TestStringSubstitutionFactory.class.getPackage().getName().replace(".", "/")); pathBuffer.append(File.separator); pathBuffer.append(this.getClass().getSimpleName()); pathBuffer.append(".class"); InputStream invalidStream = TestStringSubstitutionFactory.class.getClassLoader().getResourceAsStream(pathBuffer.toString()); try { StringSubstitutionFactory.createStringSubstitutor(invalidStream); } catch (StringSubstitutionException e) { return; } Assert.fail("No exception thrown for invalid stream."); }
@Test public void testStringSubstitutorValidStream() { InputStream invalidStream = TestStringSubstitutionFactory.class.getClassLoader().getResourceAsStream(STRING_SUBS_PATH); try { StringSubstitutor substitutor = StringSubstitutionFactory.createStringSubstitutor(invalidStream); substitutor.setAttributePreprocessor(new AttributePreprocessorImpl(substitutionMap)); backUpTestFile(); substitutor.substituteAll(); for (Group group : substitutor.getStringSubsDefinition().getGroup()) { if (group.getId().equals(VALID_GROUP_ID)) { validateSubstitutedArchiveEntries(group); for (FileEntry fileEntry : group.getFileEntry()) { if (fileEntry.getName().equalsIgnoreCase(TEST_FILE_NAME) && !validateTestFile(new File(fileEntry.getName()))) { Assert.fail("Substitution failed in the test file."); break; } } } } restoreTestFile(); } catch (StringSubstitutionException e) { Assert.fail("Exception occurred during string substitution process.", e); } }