1 5 package org.exoplatform.services.xml.transform; 6 7 import junit.framework.TestCase; 8 import org.xml.sax.helpers.XMLReaderFactory ; 9 import org.xml.sax.InputSource ; 10 import org.xml.sax.XMLReader ; 11 12 import org.exoplatform.container.PortalContainer; 13 import org.exoplatform.container.RootContainer; 14 import java.io.InputStream ; 15 import java.util.Date ; 16 17 import java.text.SimpleDateFormat ; 18 import java.text.DateFormat ; 19 import org.apache.commons.logging.Log ; 20 import org.exoplatform.services.log.LogService; 21 import org.exoplatform.services.xml.resolving.XMLResolvingService; 22 23 24 29 public class BaseTest extends TestCase { 30 private final String DATE_PATTERN = "yy-MM-DD_HH-mm-ss"; 31 private DateFormat dateFormat; 32 33 35 protected String getTimeStamp(){ 36 return dateFormat.format(new Date ()); 37 } 38 39 protected Log getLog(){ 40 Log log= null; 41 try { 42 LogService service = (LogService) RootContainer.getInstance(). 43 getComponentInstanceOfType(LogService.class); 44 log = service.getLog("org.exoplatform.services.xml"); 45 service.setLogLevel("org.exoplatform.services.xml", 46 LogService.DEBUG, true); 47 log = service.getLog(this.getClass()); 48 } catch (Exception ex) { 50 51 } 52 return log; 53 } 54 55 56 public BaseTest(){ 57 dateFormat = new SimpleDateFormat (DATE_PATTERN); 58 } 59 60 61 protected void validateXML(InputStream input) throws Exception { 62 XMLResolvingService resolvingService = 63 (XMLResolvingService) PortalContainer.getInstance(). 64 getComponentInstanceOfType(XMLResolvingService.class); 65 assertNotNull("XMLResolvingService", resolvingService); 66 67 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 68 xmlReader.setEntityResolver(resolvingService.getEntityResolver()); 69 assertNotNull("resolvingService.getEntityResolver()", 70 resolvingService.getEntityResolver()); 71 getLog().debug("resolvingService class is "+resolvingService.getClass().getName()); 72 73 InputSource src = resolvingService.getEntityResolver(). 74 resolveEntity("-//W3C//DTD XHTML 1.0 Transitional//EN", 75 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"); 76 assertNotNull("Not resolved InputSource entity", src); 77 78 xmlReader.setFeature("http://xml.org/sax/features/validation", 79 true); try { 82 xmlReader.parse(new InputSource (input)); 83 } catch (org.xml.sax.SAXParseException ex) { 84 fail("Document is not valid XML. See: \n" + ex.getMessage()); 85 } 86 } 87 88 } 89 | Popular Tags |