View Javadoc
1   package ejava.projects.esales.xml;
2   
3   import ejava.projects.esales.dto.Account;
4   
5   import ejava.projects.esales.dto.ESales;
6   
7   import java.io.BufferedInputStream;
8   import java.io.File;
9   import java.io.FileInputStream;
10  import java.io.FilenameFilter;
11  import java.io.InputStream;
12  import java.lang.reflect.Method;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  import org.junit.Before;
17  import org.junit.Test;
18  import static org.junit.Assert.*;
19  
20  public class ESalesParserTest {
21  	private static final Log log = LogFactory.getLog(ESalesParser.class);
22  	private String inputDir = System.getProperty("inputDir");
23  	
24  	@Before
25  	public void setUp() {
26  		assertNotNull("inputDir not supplied", inputDir);
27  	}
28  	
29  	@Test
30  	public void testParser() throws Exception {
31  		File inDir = new File(inputDir);
32  		File[] files = inDir.listFiles(new FilenameFilter() {
33  			public boolean accept(File dir, String name) {
34  				return (name.startsWith("eSales-") &&
35  						name.endsWith(".xml"));
36  			}
37  		});
38  		for (File file : files) {
39  			testParser(file.getCanonicalPath());
40  		}
41  	}
42  	
43  	public void testParser(String inputFile) throws Exception {
44  		log.info("*** testParser:" + inputFile + " ***");
45  		
46  		InputStream is = new FileInputStream(inputFile);
47  		BufferedInputStream bis = new BufferedInputStream(is);
48  		ESalesParser parser = new ESalesParser(ESales.class, bis);
49  		Object object=null;
50  		do {
51  			object = parser.getObject("Address", "Account", "Auction", "Bid","Image");
52  			if (object != null) { dump(object); };
53  			if (object instanceof Account) {
54  				checkAccount((Account)object);
55  			}
56  		} while (object != null);
57  		bis.close();
58  	}
59  
60  	private void checkAccount(Account account) {
61  		assertNotNull("null login:" + account.getRefid(), account.getLogin());	
62  		assertTrue("account.addresses was empty:" + account.getRefid(), account.getAddress().size() > 0);
63  	}
64  
65  	private void dump(Object object) throws Exception {
66  		StringBuilder text = new StringBuilder();
67  		for (Method m : object.getClass().getDeclaredMethods()) {
68  			if (m.getName().startsWith("get")) {
69  				String propertyName =m.getName().substring("get".length());
70  				Object value = m.invoke(object, new Object[] {});
71  				text.append(object.getClass().getName() + "." +
72  						propertyName + "=" +
73  						value + "\n");
74  			}
75  		}
76  		log.debug(text);
77  	}
78  }