Java tutorial
/* Copyright 2006 - 2010 Under Dusken Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /** * @author Erlend Hamnaberg<erlenha@underdusken.no> * @author Marvin B. Lillehaug <lillehau@underdusken.no> * @version $Id$ */ package no.dusken.aranea.service; import no.dusken.aranea.model.Article; import no.dusken.aranea.model.Issue; import no.dusken.aranea.model.Person; import no.dusken.aranea.model.Section; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.LinkedList; import java.util.List; import static junit.framework.Assert.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ "/META-INF/integrationTest.xml" }) @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) @Transactional public class TestArticleServiceImpl { @Autowired private ArticleService articleService; @Autowired private PersonService personService; @Autowired private IssueService issueService; @Autowired private SectionService sectionService; private Issue issue; private Article article; private Section section; @Before public void onSetup() { issue = new Issue(); issue.setIssue(1); Calendar from = new GregorianCalendar(2007, 1, 1); Calendar to = new GregorianCalendar(2007, 5, 1); issue.setFromDate(from); issue.setToDate(to); issue.setTimeCreated(to); issue.setDescription("#UD 01"); issue = issueService.saveOrUpdate(issue); section = new Section(); section.setName("test"); section.setUrl("test"); section = sectionService.saveOrUpdate(section); article = new Article(); article.setTitle("Tittel"); article.setSummary("Ingress"); article.setTimeCreated(to); article.setText("Lorem Ipsum"); article.setParent(section); article.setIssue(issue); article = articleService.saveOrUpdate(article); } @Test public void testEntity() { Article a = articleService.getEntity(article.getID()); assertNotNull("Get article was null", a); assertEquals("Get ID of article was incorrect", a.getID(), article.getID()); } @Test public void testGetArticlesByYear() { List<Article> list = articleService.getArticlesByYear(2007, 20, 0); assertNotNull("Get articles by year", list); assertEquals("Get number of articles by year", list.size(), 1L); } @Test public void testSaveArticle() { Article a = new Article(); a.setIssue(issue); a.setParent(section); a.setTitle("A fine title"); a.setText("A long and informative text, written by our fine journalists."); a = articleService.saveOrUpdate(a); assertNotNull("saveOrUpdate returned null!", a); assertNotNull("Saved article does not have ID", a.getID()); Article createdArticle; createdArticle = articleService.getEntity(a.getID()); assertNotNull("Article was not at once visible after merging", createdArticle); assertEquals(createdArticle.getTitle(), a.getTitle()); } @Test public void testGetPublishedArticlesByPerson() { Person p = getArticles(); List<Article> articles = articleService.getPublishedArticlesByPerson(p); assertEquals("Wrong number of articles returned.", 1, articles.size()); } @Test public void testDeleteArticles() { Person p = getArticles(); List<Article> articles = articleService.getPublishedArticlesByPerson(p); for (Article a : articles) { boolean successfullDelete = articleService.remove(a); assertTrue("Could not delete article", successfullDelete); } articles = articleService.getPublishedArticlesByPerson(p); assertEquals("Wrong number of articles returned.", 0, articles.size()); List<Person> persons = personService.getPersons(); assertTrue("Persons deleted", persons.size() > 0); } private Person getArticles() { Article a = new Article(); a.setText("ff"); a.setTitle("ff"); Person p = getPerson(); List authors = new LinkedList(); authors.add(p); a.setAuthors(authors); a.setPublished(true); Section s = new Section(); s.setName("fa"); a.setParent(s); articleService.saveOrUpdate(a); Article b = new Article(); b.setText("ff"); b.setTitle("ff"); b.setPublished(false); s.setName("fa"); b.setParent(s); b.setAuthors(authors); articleService.saveOrUpdate(b); return p; } private Person getPerson() { Person p = new Person(); p.setFirstname("g"); p.setUsername("asf"); p.setSurname("f"); p = personService.saveOrUpdate(p); return p; } }