0% found this document useful (0 votes)
19 views

Advanced Programming

The document describes a Java project structure with classes for blog posts and comments. It includes the code for these classes and unit tests for the post class. The task is to implement the PostApi interface for getting tags and last comments in a way that passes all tests for the correct class and fails tests for the incorrect classes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Advanced Programming

The document describes a Java project structure with classes for blog posts and comments. It includes the code for these classes and unit tests for the post class. The task is to implement the PostApi interface for getting tags and last comments in a way that passes all tests for the correct class and fails tests for the incorrect classes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

JAVA

Name: Ema Pedro


ID: S7514

TASK1 - See the content of the project:

• ● Class pl.wwsis.zpp.blog.App -- contains the main function with an example Spark


controller.
• ● File src/main/resources/spark/template/freemarker/hello.ftl -- view template used in
the controller above
• ● Class pl.wwsis.zpp.blog.model.Post (katalog src/main/java) -- a simple class to
represent a blog post
• ● Class pl.wwsis.zpp.blog.model.Comment (katalog src/main/java) --represents the
comment on the post
• ● Class pl.wwsis.zpp.blog.model.PostTest (katalog src/test/java) -- unit tests to the Post
class. Run the tests and study the code - the next exercise will be to write similar tests.

App:

package pl.wwsis.zpp.blog;

import static spark.Spark.get;

import java.util.HashMap;

import java.util.Map;

import spark.Request;

import spark.Response;

import spark.template.freemarker.FreeMarkerRoute;

public class App {

public static void main(String[] args) {


get(new FreeMarkerRoute("/hello") {

@Override

public Object handle(Request request, Response response) {

Map<String, Object> attributes = new HashMap<String, Object>();

attributes.put("message", "Hello, World!");

return modelAndView(attributes, "hello.ftl");

});

Comment:

package pl.wwsis.zpp.blog.model;

import java.util.Date;

public class Comment {

private String author;

private Date date;

private String text;


public Comment(String author, String text) {

this.author = author;

this.date = new Date();

this.text = text;

public String getAuthor() {

return author;

public Date getDate() {

return date;

public String getText() {

return text;

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((author == null) ? 0 : author.hashCode());

result = prime * result + ((date == null) ? 0 : date.hashCode());


result = prime * result + ((text == null) ? 0 : text.hashCode());

return result;

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Comment other = (Comment) obj;

if (author == null) {

if (other.author != null)

return false;

} else if (!author.equals(other.author))

return false;

if (date == null) {

if (other.date != null)

return false;

} else if (!date.equals(other.date))

return false;

if (text == null) {
if (other.text != null)

return false;

} else if (!text.equals(other.text))

return false;

return true;

Post:

package pl.wwsis.zpp.blog.model;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Date;

import java.util.List;

public class Post {

private Date date;

private String title;

private String text;

protected List<Comment> comments = new ArrayList<Comment>();


public Post(String title, String text) {

this.date = new Date();

this.title = title;

this.text = text;

public void addComment(Comment comment) {

comments.add(comment);

public Date getDate() {

return date;

public String getTitle() {

return title;

public String getText() {

return text;

public List<Comment> getComments() {

return Collections.unmodifiableList(comments);
}

PostTest:

package pl.wwsis.zpp.blog.model;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.Test;

public class PostTest {

Post p = new Post("test title", "test content");

@Test

public void newPostShouldHaveEmptyListOfComments() {

assertNotNull(p.getComments());

assertEquals(0, p.getComments().size());

@Test

public void newPostShouldHaveDateSet() {

assertNotNull(p.getDate());
}

@Test

public void addComment_shouldLinkCommentToThePost() {

Comment comment = new Comment("tester", "test comment");

p.addComment(comment);

assertEquals(1, p.getComments().size());

assertTrue(p.getComments().contains(comment));

@Test

public void returnedCommentListShouldBeUnmodifiable() {

List<Comment> comments = p.getComments();

try {

comments.add(new Comment("tester", "test comment")); // not allowed,


comments can only be added using the addComment method of the Post class

fail("exception was expected!");

} catch (Exception e) {

// ok

}
TASK2:

In package pl.wwsis.zpp.blog.model.alternatives there is an interface PostApi,


describing extended API of post:
● Method getTags returns the set of tags assigned to the post. A tag is a simple text
label.
● Method addTag adds a tag to a post, ie when we add a tag to a post, it should be an
element of the set returned by getTags. Additionally, duplicates are not allowed, i.e.
even if we call the addTag method several times with the same tag, it will be added only
once. What's more, each post can have a maximum of 5 tags - after the addition
becomes the fifth, subsequent calls to the addTag method have no effect.
● Method getLastComments returns a list of the 3 most recent (by date) comments.
They are additionally sorted from newest to oldest
In the package pl.wwsis.zpp.blog.model.alternatives (folder src/main/java) there are 8
implementations of interface described above (classes Post1...Post8). Only one of them
is correct, i.e. meets the requirements described above.
The task is to write a JUnit tests suite such that:
● none of the incorrect implementations will pass it (i.e. at least one test will not pass for
each invalid class)
● correct implementation will pass all tests
Class pl.wwsis.zpp.blog.model.alternatives.PostSpecificationTest (folder src/test/java)
contains a test class skeleton. In line 13, a post is created that should be tested.
Changing the class name (from Post1 to e.g. Post2) will allow you to run tests for a
different version.
Post1:

package pl.wwsis.zpp.blog.model.alternatives;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import pl.wwsis.zpp.blog.model.Comment;
import pl.wwsis.zpp.blog.model.Post;

public class Post1 extends Post implements PostApi {

private Set<String> tags = new HashSet<String>();

public Post1(String title, String text) {


super(title, text);
}

public void addTag(String tag) {


if (tags.size() <= 5) {
tags.add(tag);
}
}

public Set<String> getTags() {


return Collections.unmodifiableSet(tags);
}

public List<Comment> getLastComments() {


Collections.sort(comments, new Comparator<Comment>() {
public int compare(Comment o1, Comment o2) {
return o2.getDate().compareTo(o1.getDate());
}
});
if (comments.size() > 3) {
return Arrays.asList(comments.get(0), comments.get(1),
comments.get(2));
} else {
return getComments();
}
}

Post2:

package pl.wwsis.zpp.blog.model.alternatives;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import pl.wwsis.zpp.blog.model.Comment;
import pl.wwsis.zpp.blog.model.Post;

public class Post2 extends Post implements PostApi {

private Map<String, Boolean> tags = new HashMap<String, Boolean>();

public Post2(String title, String text) {


super(title, text);
}

public void addTag(String tag) {


if (tags.size() < 5) {
tags.put(tag, true);
}
}

public Set<String> getTags() {


return tags.keySet();
}

public List<Comment> getLastComments() {


Collections.sort(comments, new Comparator<Comment>() {
public int compare(Comment o1, Comment o2) {
return o2.getDate().compareTo(o1.getDate());
}
});
if (comments.size() < 4) {
return getComments();
} else {
return Arrays.asList(comments.get(0), comments.get(1),
comments.get(2));
}
}

Post3:

package pl.wwsis.zpp.blog.model.alternatives;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import pl.wwsis.zpp.blog.model.Comment;
import pl.wwsis.zpp.blog.model.Post;

public class Post3 extends Post implements PostApi {

private Collection<String> tags = new ArrayList<String>();

public Post3(String title, String text) {


super(title, text);
}

public void addTag(String tag) {


if (tags.size() >=5) {
return;
} else {
tags.add(tag);
}
}

public Set<String> getTags() {


return Collections.unmodifiableSet(new HashSet<String>(tags));
}
public List<Comment> getLastComments() {
Collections.sort(comments, new Comparator<Comment>() {
public int compare(Comment o1, Comment o2) {
return o2.getDate().compareTo(o1.getDate());
}
});
if (comments.size() <= 3) {
return getComments();
} else {
return Arrays.asList(comments.get(0), comments.get(1),
comments.get(2));
}
}

Post4:

package pl.wwsis.zpp.blog.model.alternatives;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import pl.wwsis.zpp.blog.model.Comment;
import pl.wwsis.zpp.blog.model.Post;

public class Post4 extends Post implements PostApi {

private Map<String, String> tags = new HashMap<String, String>();

public Post4(String title, String text) {


super(title, text);
}

public void addTag(String tag) {


if (tags.values().size() ==5) {
return;
} else {
tags.put(tag, tag);
}
}

public Set<String> getTags() {


return new HashSet<String>(tags.values());
}

public List<Comment> getLastComments() {


Collections.sort(comments, new Comparator<Comment>() {
public int compare(Comment o1, Comment o2) {
return o2.getDate().compareTo(o1.getDate());
}
});
if (comments.size() < 4) {
return getComments();
} else {
return Arrays.asList(comments.get(0), comments.get(1),
comments.get(2));
}
}

Post5:

package pl.wwsis.zpp.blog.model.alternatives;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import pl.wwsis.zpp.blog.model.Comment;
import pl.wwsis.zpp.blog.model.Post;

public class Post5 extends Post implements PostApi{

private List<String> tags = new ArrayList<String>();

public Post5(String title, String text) {


super(title, text);
}
public void addTag(String tag) {
if (!tags.contains(tag) && tags.size() != 5) {
tags.add(tag);
}
}

public Set<String> getTags() {


return Collections.unmodifiableSet(new HashSet<String>(tags));
}

public List<Comment> getLastComments() {


Collections.sort(comments, new Comparator<Comment>() {
public int compare(Comment o1, Comment o2) {
return o2.getDate().compareTo(o1.getDate());
}
});
return getComments();
}

Post6:

package pl.wwsis.zpp.blog.model.alternatives;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import pl.wwsis.zpp.blog.model.Comment;
import pl.wwsis.zpp.blog.model.Post;

public class Post6 extends Post implements PostApi {

private Set<String> tags = new HashSet<String>();

public Post6(String title, String text) {


super(title, text);
}

public void addTag(String tag) {


if (tags.size() != 5) {
tags.add(tag);
}
}

public Set<String> getTags() {


return Collections.unmodifiableSet(new HashSet<String>());
}

public List<Comment> getLastComments() {


Collections.sort(comments, new Comparator<Comment>() {
public int compare(Comment o1, Comment o2) {
return o2.getDate().compareTo(o1.getDate());
}
});
if (comments.size() <= 3) {
return getComments();
} else {
return Arrays.asList(comments.get(0), comments.get(1),
comments.get(2));
}
}

Post7:

package pl.wwsis.zpp.blog.model.alternatives;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import pl.wwsis.zpp.blog.model.Comment;
import pl.wwsis.zpp.blog.model.Post;

public class Post7 extends Post implements PostApi {

private Map<String, Boolean> tags = new HashMap<String, Boolean>();

public Post7(String title, String text) {


super(title, text);
}

public void addTag(String tag) {


if (tags.size() < 5) {
tags.put(tag, true);
}
}

public Set<String> getTags() {


return tags.keySet();
}

public List<Comment> getLastComments() {


Collections.sort(comments, new Comparator<Comment>() {
public int compare(Comment o1, Comment o2) {
return o2.getDate().compareTo(o1.getDate());
}
});
if (comments.size() <= 3) {
return getComments();
} else {
return Arrays.asList(comments.get(1), comments.get(3),
comments.get(2));
}
}

Post8:

package pl.wwsis.zpp.blog.model.alternatives;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import pl.wwsis.zpp.blog.model.Comment;
import pl.wwsis.zpp.blog.model.Post;

public class Post8 extends Post implements PostApi {


private Map<String, Boolean> tags = new HashMap<String, Boolean>();

public Post8(String title, String text) {


super(title, text);
}

public void addTag(String tag) {


if (tags.size() < 5) {
tags.put(tag, true);
}
}

public Set<String> getTags() {


return tags.keySet();
}

public List<Comment> getLastComments() {


Collections.sort(comments, new Comparator<Comment>() {
public int compare(Comment o1, Comment o2) {
return o1.getDate().compareTo(o2.getDate());
}
});
if (comments.size() < 4) {
return getComments();
} else {
return Arrays.asList(comments.get(0), comments.get(1),
comments.get(2));
}
}

}
PostApi:

package pl.wwsis.zpp.blog.model.alternatives;

import java.util.List;
import java.util.Set;

import pl.wwsis.zpp.blog.model.Comment;

public interface PostApi {

void addTag(String tag);


Set<String> getTags();

List<Comment> getLastComments();

List<Comment> getComments();
void addComment(Comment c);
}

PostSpecificationTest:

package pl.wwsis.zpp.blog.model.alternatives;

import org.junit.Test;
import static org.junit.Assert.*;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.Collection;

public class PostSpecificationTest {


PostApi post = new Post1("test title", "test text"); // replace with Post2, Post3 etc. to test
other implementations

}
Test

You might also like