import java.awt.Point; import junit.framework.*; import junit.extensions.converter.*; import junit.extensions.converter.evaluator.*; public class BookTest extends TestCase { public BookTest(String name) { super(name); } public void testAgileBook() throws Exception { Converter converter = new DefaultConverter(); converter = new BeanConverter(converter, Book.class, "ISBN", "title", "author"); Book book = new Book("0-13-597444-5"); Object expected = new Object[] { "ISBN=0-13-597444-5", "title=Agile Softare Development", "author=Robert C. Martin", }; converter.assertEquals(expected, book, 1); } public void testSingleProperty() throws Exception { Converter converter = new DefaultConverter(); converter = new BeanConverter(converter, Book.class, "title"); converter.assertEquals("title=Agile Software Development", new Book("4-7561-3687-7")); } public void testPoint() throws Exception { Converter converter = new DefaultConverter(); converter = new ClassFieldValueConverter(converter, "", Point.class, "x", "y"); converter.assertEquals("", new Point(10, 20)); } public void testDesignPatterns() throws Exception { Converter converter = new DefaultConverter(); converter = new BeanConverter(converter, Book.class, "title", "authors"); converter.addEvaluator(new SortEvaluator("authors")); Book book = new Book("0-201-63361-2"); Object expected = new Object[] { "title=Design Patterns", new Property("authors", new Object[] { "Erich Gamma", "John Vlissides", "Ralph Johnson", "Richard Helm", }), }; converter.assertEquals(expected, book, 2); } public void testAddEvaluatorError() throws Exception { Converter converter = new DefaultConverter(); boolean failed; try { converter.addEvaluator(new SortEvaluator("authors")); failed = false; } catch (UnsupportedOperationException ex) { failed = true; } assertEquals(true, failed); } } class Book { private String isbn; public Book(String isbn) { this.isbn = isbn; } public String getISBN() { return isbn; } public String getTitle() { if (isbn.equals("0-201-63361-2")) return "Design Patterns"; return "Agile Software Development"; } public String getAuthor() { return "Robert C. Martin"; } public String[] getAuthors() { if (isbn.equals("0-201-63361-2")) return new String[] { "Erich Gamma", "Richard Helm", "Ralph Johnson", "John Vlissides", }; return new String[] {"Robert C. Martin"}; } }