Search in sources :

Example 6 with EntitySaxReader

use of org.apache.ofbiz.entity.util.EntitySaxReader in project ofbiz-framework by apache.

the class EntityXmlAssertTest method run.

@Override
public void run(TestResult result) {
    result.startTest(this);
    try {
        URL entityXmlURL = FlexibleLocation.resolveLocation(entityXmlUrlString);
        List<Object> errorMessages = new LinkedList<Object>();
        if ("assert".equals(this.action)) {
            EntityDataAssert.assertData(entityXmlURL, delegator, errorMessages);
        } else if ("load".equals(this.action)) {
            EntitySaxReader reader = new EntitySaxReader(delegator);
            reader.parse(entityXmlURL);
        } else {
            // uh oh, bad value
            result.addFailure(this, new AssertionFailedError("Bad value [" + this.action + "] for action attribute of entity-xml element"));
        }
        if (UtilValidate.isNotEmpty(errorMessages)) {
            for (Object failureMessage : errorMessages) {
                result.addFailure(this, new AssertionFailedError(failureMessage.toString()));
            }
        }
    } catch (Exception e) {
        result.addError(this, e);
    }
    result.endTest(this);
}
Also used : EntitySaxReader(org.apache.ofbiz.entity.util.EntitySaxReader) AssertionFailedError(junit.framework.AssertionFailedError) URL(java.net.URL) LinkedList(java.util.LinkedList)

Example 7 with EntitySaxReader

use of org.apache.ofbiz.entity.util.EntitySaxReader in project ofbiz-framework by apache.

the class EntityTestSuite method testEntitySaxReaderCreation.

/*
     * This test will verify that the LIMIT and OFFSET options can work properly.
     * Commented out because it makes the framework dependent on the content component
     */
/*public void testLimitOffsetOptions() throws Exception {
        String entityName = "Content";
        Datasource datasourceInfo = EntityConfig.getDatasource(delegator.getEntityHelper(entityName).getHelperName());
        if (UtilValidate.isEmpty(datasourceInfo.offsetStyle) || "none".equals(datasourceInfo.offsetStyle)) {
            Debug.logInfo("The offset-stype configured in datasource is " + datasourceInfo.offsetStyle +  ", this test is skipped.", module);
            return;
        } else {
            Debug.logInfo("The offset-stype configured in datasource is " + datasourceInfo.offsetStyle +  ".", module);
        }
        try {
            EntityFindOptions findOptions = new EntityFindOptions();
            long count = delegator.findCountByCondition("Content", null, null, null);
            Debug.logInfo("Content entity has " + count + " rows", module);
            int rowsPerPage = 10;
            // use rows/page as limit option
            findOptions.setLimit(rowsPerPage);
            int pages = (int) count/rowsPerPage;
            if (count > pages * rowsPerPage) {
                pages += 1;
            }
            Debug.logInfo("These rows will be displayed in " + pages + " pages, each page has " + rowsPerPage + " rows.", module);
            ModelEntity modelEntity = delegator.getModelEntity(entityName);

            long start = UtilDateTime.nowTimestamp().getTime();
            for (int page = 1; page <= pages; page++) {
                Debug.logInfo("Page " + page + ":", module);
                // set offset option
                findOptions.setOffset((page - 1) * rowsPerPage);
                EntityListIterator iterator = null;
                try {
                    iterator = delegator.getEntityHelper(entityName).findListIteratorByCondition(modelEntity, null, null, null, UtilMisc.toList("lastUpdatedStamp DESC"), findOptions);
                    while (iterator != null) {
                        GenericValue gv = iterator.next();
                        if (gv == null) {
                            break;
                        }
                        Debug.logInfo(gv.getString("contentId") + ": " + gv.getString("contentName") + "       (updated: " + gv.getTimestamp("lastUpdatedStamp") + ")", module);
                    }
                } catch (GenericEntityException e) {
                    Debug.logError(e, module);
                } finally {
                    if (iterator != null) {
                        iterator.close();
                    }
                }
            }
            long end = UtilDateTime.nowTimestamp().getTime();
            long time1 = end - start;
            Debug.logInfo("Time consumed WITH limit and offset option (ms): " + time1, module);

            start = UtilDateTime.nowTimestamp().getTime();
            for (int page = 1; page <= pages; page++) {
                Debug.logInfo("Page " + page + ":", module);
                EntityListIterator iterator = null;
                try {
                    iterator = ((GenericHelperDAO) delegator.getEntityHelper(entityName)).findListIteratorByCondition(modelEntity, null, null, null, UtilMisc.toList("lastUpdatedStamp DESC"), null);
                    if (iterator == null) {
                        continue;
                    }
                    iterator.setDelegator(delegator);
                    List<GenericValue> gvs = iterator.getCompleteList();
                    int fromIndex = (page - 1) * rowsPerPage;
                    int toIndex = fromIndex + rowsPerPage;
                    if (toIndex > count) {
                        toIndex = (int) count;
                    }
                    gvs = gvs.subList(fromIndex, toIndex);
                    for (GenericValue gv : gvs) {
                        Debug.logInfo(gv.getString("contentId") + ": " + gv.getString("contentName") + "       (updated: " + gv.getTimestamp("lastUpdatedStamp") + ")", module);
                    }
                } catch (GenericEntityException e) {
                    Debug.logError(e, module);
                } finally {
                    if (iterator != null) {
                        iterator.close();
                    }
                }
            }
            end = UtilDateTime.nowTimestamp().getTime();
            long time2 = end - start;
            Debug.logInfo("Time consumed WITHOUT limit and offset option (ms): " + time2, module);
            Debug.logInfo("Time saved (ms): " + (time2 - time1), module);
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
        }
    }*/
/*
     * Tests EntitySaxReader, verification loading data with tag create, create-update, create-replace, delete
     */
public void testEntitySaxReaderCreation() throws Exception {
    String xmlContentLoad = "<entity-engine-xml>" + "<TestingType testingTypeId=\"JUNIT-TEST\" description=\"junit test\"/>" + "<create>" + "    <TestingType testingTypeId=\"JUNIT-TEST2\" description=\"junit test\"/>" + "    <Testing testingId=\"T1\" testingTypeId=\"JUNIT-TEST\" testingName=\"First test\" testingSize=\"10\" testingDate=\"2010-01-01 00:00:00\"/>" + "</create>" + "<Testing testingId=\"T2\" testingTypeId=\"JUNIT-TEST2\" testingName=\"Second test\" testingSize=\"20\" testingDate=\"2010-02-01 00:00:00\"/>" + "</entity-engine-xml>";
    EntitySaxReader reader = new EntitySaxReader(delegator);
    long numberLoaded = reader.parse(xmlContentLoad);
    assertEquals("Create Entity loaded ", 4, numberLoaded);
    GenericValue t1 = EntityQuery.use(delegator).from("Testing").where("testingId", "T1").queryOne();
    GenericValue t2 = EntityQuery.use(delegator).from("Testing").where("testingId", "T2").cache(true).queryOne();
    assertNotNull("Create Testing(T1)", t1);
    assertEquals("Create Testing(T1).testingTypeId", "JUNIT-TEST", t1.getString("testingTypeId"));
    assertEquals("Create Testing(T1).testingName", "First test", t1.getString("testingName"));
    assertEquals("Create Testing(T1).testingSize", Long.valueOf(10), t1.getLong("testingSize"));
    assertEquals("Create Testing(T1).testingDate", UtilDateTime.toTimestamp("01/01/2010 00:00:00"), t1.getTimestamp("testingDate"));
    assertNotNull("Create Testing(T2)", t2);
    assertEquals("Create Testing(T2).testingTypeId", "JUNIT-TEST2", t2.getString("testingTypeId"));
    assertEquals("Create Testing(T2).testingName", "Second test", t2.getString("testingName"));
    assertEquals("Create Testing(T2).testingSize", Long.valueOf(20), t2.getLong("testingSize"));
    assertEquals("Create Testing(T2).testingDate", UtilDateTime.toTimestamp("02/01/2010 00:00:00"), t2.getTimestamp("testingDate"));
}
Also used : EntitySaxReader(org.apache.ofbiz.entity.util.EntitySaxReader) GenericValue(org.apache.ofbiz.entity.GenericValue)

Example 8 with EntitySaxReader

use of org.apache.ofbiz.entity.util.EntitySaxReader in project ofbiz-framework by apache.

the class EntityData method exec.

@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
    List<Object> messages = errorListFma.get(methodContext.getEnvMap());
    if (messages == null) {
        messages = new LinkedList<Object>();
        errorListFma.put(methodContext.getEnvMap(), messages);
    }
    String location = this.locationFse.expandString(methodContext.getEnvMap());
    Delegator delegator = getDelegator(methodContext);
    URL dataUrl = null;
    try {
        dataUrl = FlexibleLocation.resolveLocation(location, methodContext.getLoader());
    } catch (MalformedURLException e) {
        messages.add("Could not find Entity Data document in resource: " + location + "; error was: " + e.toString());
    }
    if (dataUrl == null) {
        messages.add("Could not find Entity Data document in resource: " + location);
    }
    if ("assert".equals(mode)) {
        try {
            EntityDataAssert.assertData(dataUrl, delegator, messages);
        } catch (Exception e) {
            String xmlError = "Error checking/asserting XML Resource \"" + dataUrl.toExternalForm() + "\"; Error was: " + e.getMessage();
            messages.add(xmlError);
            Debug.logWarning(e, xmlError, module);
        }
    } else {
        try {
            EntitySaxReader reader = null;
            if (timeout > 0) {
                reader = new EntitySaxReader(delegator, timeout);
            } else {
                reader = new EntitySaxReader(delegator);
            }
            reader.parse(dataUrl);
        } catch (Exception e) {
            String xmlError = "Error loading XML Resource \"" + dataUrl.toExternalForm() + "\"; Error was: " + e.getMessage();
            messages.add(xmlError);
            Debug.logWarning(e, xmlError, module);
        }
    }
    return true;
}
Also used : EntitySaxReader(org.apache.ofbiz.entity.util.EntitySaxReader) MalformedURLException(java.net.MalformedURLException) Delegator(org.apache.ofbiz.entity.Delegator) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) MiniLangException(org.apache.ofbiz.minilang.MiniLangException)

Example 9 with EntitySaxReader

use of org.apache.ofbiz.entity.util.EntitySaxReader in project ofbiz-framework by apache.

the class EntityXmlAssertTest method countTestCases.

@Override
public int countTestCases() {
    int testCaseCount = 0;
    try {
        URL entityXmlURL = FlexibleLocation.resolveLocation(entityXmlUrlString);
        EntitySaxReader reader = new EntitySaxReader(delegator);
        testCaseCount += reader.parse(entityXmlURL);
    } catch (Exception e) {
        Debug.logError(e, "Error getting test case count", module);
    }
    return testCaseCount;
}
Also used : EntitySaxReader(org.apache.ofbiz.entity.util.EntitySaxReader) URL(java.net.URL)

Aggregations

EntitySaxReader (org.apache.ofbiz.entity.util.EntitySaxReader)9 GenericValue (org.apache.ofbiz.entity.GenericValue)5 URL (java.net.URL)4 MalformedURLException (java.net.MalformedURLException)2 Delegator (org.apache.ofbiz.entity.Delegator)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 LinkedList (java.util.LinkedList)1 Locale (java.util.Locale)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 AssertionFailedError (junit.framework.AssertionFailedError)1 GeneralException (org.apache.ofbiz.base.util.GeneralException)1 UtilURL (org.apache.ofbiz.base.util.UtilURL)1 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)1 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)1 MiniLangException (org.apache.ofbiz.minilang.MiniLangException)1 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)1 SAXException (org.xml.sax.SAXException)1