@Test public void mixed() { InputStream stream = RecipeTest.class.getResourceAsStream("/recipes/mixed.txt"); Recipe recipe = Recipe.readFromStream(stream); assertNotNull(recipe); assertEquals("punch", recipe.id); assertEquals("Punch", recipe.title); assertEquals("Juice of 3 lemons\n" + "1 orange\n" + "1 pint grape juice\n" + "1 cup sugar\n" + "1 cup water\n" + "1 pine apple juice\n" + "Mix all together and strain. Add large piece of ice.", recipe.description); }
public static Recipe readFromStream(InputStream stream) { if (stream == null) { return null; } BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); String id = null; String title = null; StringBuilder descBuilder = new StringBuilder(); try { for (String line = reader.readLine(); line != null; line = reader.readLine()) { if (line.startsWith(ID_PREFIX)) { id = line.substring(ID_PREFIX.length()); continue; } if (line.startsWith(TITLE_PREFIX)) { title = line.substring(TITLE_PREFIX.length()); continue; } if (descBuilder.length() > 0) { descBuilder.append("\n"); } descBuilder.append(line); } } catch (IOException e) { return null; } if (id == null || title == null || descBuilder.length() == 0) { return null; } return new Recipe(id, title, descBuilder.toString()); }
Recipe { public static Recipe readFromStream(InputStream stream) { if (stream == null) { return null; } BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); String id = null; String title = null; StringBuilder descBuilder = new StringBuilder(); try { for (String line = reader.readLine(); line != null; line = reader.readLine()) { if (line.startsWith(ID_PREFIX)) { id = line.substring(ID_PREFIX.length()); continue; } if (line.startsWith(TITLE_PREFIX)) { title = line.substring(TITLE_PREFIX.length()); continue; } if (descBuilder.length() > 0) { descBuilder.append("\n"); } descBuilder.append(line); } } catch (IOException e) { return null; } if (id == null || title == null || descBuilder.length() == 0) { return null; } return new Recipe(id, title, descBuilder.toString()); } }
Recipe { public static Recipe readFromStream(InputStream stream) { if (stream == null) { return null; } BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); String id = null; String title = null; StringBuilder descBuilder = new StringBuilder(); try { for (String line = reader.readLine(); line != null; line = reader.readLine()) { if (line.startsWith(ID_PREFIX)) { id = line.substring(ID_PREFIX.length()); continue; } if (line.startsWith(TITLE_PREFIX)) { title = line.substring(TITLE_PREFIX.length()); continue; } if (descBuilder.length() > 0) { descBuilder.append("\n"); } descBuilder.append(line); } } catch (IOException e) { return null; } if (id == null || title == null || descBuilder.length() == 0) { return null; } return new Recipe(id, title, descBuilder.toString()); } private  Recipe(String id, String title, String description); }
Recipe { public static Recipe readFromStream(InputStream stream) { if (stream == null) { return null; } BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); String id = null; String title = null; StringBuilder descBuilder = new StringBuilder(); try { for (String line = reader.readLine(); line != null; line = reader.readLine()) { if (line.startsWith(ID_PREFIX)) { id = line.substring(ID_PREFIX.length()); continue; } if (line.startsWith(TITLE_PREFIX)) { title = line.substring(TITLE_PREFIX.length()); continue; } if (descBuilder.length() > 0) { descBuilder.append("\n"); } descBuilder.append(line); } } catch (IOException e) { return null; } if (id == null || title == null || descBuilder.length() == 0) { return null; } return new Recipe(id, title, descBuilder.toString()); } private  Recipe(String id, String title, String description); static Recipe readFromStream(InputStream stream); }
Recipe { public static Recipe readFromStream(InputStream stream) { if (stream == null) { return null; } BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); String id = null; String title = null; StringBuilder descBuilder = new StringBuilder(); try { for (String line = reader.readLine(); line != null; line = reader.readLine()) { if (line.startsWith(ID_PREFIX)) { id = line.substring(ID_PREFIX.length()); continue; } if (line.startsWith(TITLE_PREFIX)) { title = line.substring(TITLE_PREFIX.length()); continue; } if (descBuilder.length() > 0) { descBuilder.append("\n"); } descBuilder.append(line); } } catch (IOException e) { return null; } if (id == null || title == null || descBuilder.length() == 0) { return null; } return new Recipe(id, title, descBuilder.toString()); } private  Recipe(String id, String title, String description); static Recipe readFromStream(InputStream stream); final String id; final String title; final String description; }