@Test public void testFindProperties() { // create identities, group and resource OLATResource res = JunitTestHelper.createRandomResource(); Identity id1 = JunitTestHelper.createAndPersistIdentityAsUser("prop-8-" + UUID.randomUUID().toString()); Identity id2 = JunitTestHelper.createAndPersistIdentityAsUser("prop-9-" + UUID.randomUUID().toString()); BusinessGroup group = businessGroupService.createBusinessGroup( id1, "a buddygroup", "a desc", -1, -1, false, false, null); dbInstance.commitAndCloseSession(); String category = "cat3"; String propertyName = "TestProperty3"; String textValue = "textValue3"; Property p = pm.createPropertyInstance( id1, group, res, category, propertyName, new Float(1.1), new Long(123456), "stringValue", textValue); pm.saveProperty(p); p = pm.createPropertyInstance( id2, group, res, category, propertyName, new Float(1.1), new Long(123456), "stringValue", textValue); pm.saveProperty(p); List<Property> propertyList = pm.findProperties( id1, group, res.getResourceableTypeName(), res.getResourceableId(), category, propertyName); assertEquals(1, propertyList.size()); assertEquals(propertyName, propertyList.get(0).getName()); assertEquals(textValue, propertyList.get(0).getTextValue()); int deletedCount1 = pm.deleteProperties(id1, group, res, category, propertyName); Assert.assertEquals(1, deletedCount1); int deletedCount2 = pm.deleteProperties(id2, group, res, category, propertyName); Assert.assertEquals(1, deletedCount2); }
@Test public void testRealWorldScoreFloatValues() { // delete everything from previous tests pm.deleteProperties(null, null, null, "test", "TestPropertyFloatValue1"); pm.deleteProperties(null, null, null, "test", "TestPropertyFloatValue2"); // Test setting and getting of a normal float value Property prop1 = pm.createPropertyInstance( null, null, null, "test", "TestPropertyFloatValue1", new Float(0.9), null, null, null); pm.saveProperty(prop1); Property prop2 = pm.createPropertyInstance( null, null, null, "test", "TestPropertyFloatValue2", new Float(0.1), null, null, null); pm.saveProperty(prop2); dbInstance.commitAndCloseSession(); prop1 = pm.findProperty(null, null, null, "test", "TestPropertyFloatValue1"); prop2 = pm.findProperty(null, null, null, "test", "TestPropertyFloatValue2"); // In course assessment 0.1 + 0.9 must exactly give 1 as a result assertEquals( 1, prop1.getFloatValue().floatValue() + prop2.getFloatValue().floatValue(), 0.00001); pm.deleteProperties(null, null, null, "test", "TestPropertyFloatValue1"); pm.deleteProperties(null, null, null, "test", "TestPropertyFloatValue2"); }
/** * testFloatValues THIS test does only success when you run it against mysql with FLOAT(65,30). * FLOAT(65,30) is mysql specific and if you let hibernate generate the tables automatic it will * result in FLOAT only and this test will fail. So this means that you have to populate the * tables yourself via the sql file. */ @Test public void testFloatValues() { // create identity, group and resource OLATResource res = JunitTestHelper.createRandomResource(); Identity identity = JunitTestHelper.createAndPersistIdentityAsUser("prop-6-" + UUID.randomUUID().toString()); BusinessGroup group = businessGroupService.createBusinessGroup( identity, "a buddygroup", "a desc", -1, -1, false, false, null); dbInstance.commitAndCloseSession(); Property original, copy; // gs:changed to FLOAT(65,30) to be compatible with hsqldb and auto generated ddl // Define my own MAX float value because the db precision changed from DECIMAL(78,36) to // DECIMAL(65,30) double floatMaxValue = 1E34; // 1E35 does failed with mysql 5.0.x original = pm.createPropertyInstance( identity, group, res, "cat", "TestProperty", new Float(1234534343424213.1324534533456), new Long(123456), "stringValue", "textValue"); pm.saveProperty(original); // DBFactory.getInstance().evict(original); dbInstance.commitAndCloseSession(); copy = pm.findProperty(identity, group, res, "cat", "TestProperty"); float f1F = original.getFloatValue().floatValue(); float copyF = copy.getFloatValue().floatValue(); assertEquals("values differ:" + f1F + ", and " + copyF, f1F, copyF, 0.0000000001f); pm.deleteProperties(identity, group, res, "cat", "TestProperty"); // note: on mysql 5.0, the standard installation is strict mode, which reports any data // truncation error as a real jdbc error. // -1e35 seems out of range for DECIMAL(65,30) so data truncation occurs??? // +-> 30 digits to the right side of decimal point // From mysql: // The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the // arguments in MySQL 5.1 are as follows: // M is the maximum number of digits (the precision). It has a range of 1 to 65. (Older versions // of MySQL allowed a range of 1 to 254.) // D is the number of digits to the right of the decimal point (the scale). It has a range of 0 // to 30 and must be no larger than M. original = pm.createPropertyInstance( identity, group, res, "cat", "TestProperty", new Float(-floatMaxValue), new Long(123456), "stringValue", "textValue"); pm.saveProperty(original); dbInstance.commitAndCloseSession(); copy = pm.findProperty( identity, group, res, "cat", "TestProperty"); // this one failes at the moment for hsqldb with: incompatible data // type in conversion: from SQL type DECIMAL to java.lang.Double, // value: // -9999999790214767953607394487959552.000000000000000000000000000000 assertTrue(original.getFloatValue().floatValue() == copy.getFloatValue().floatValue()); pm.deleteProperties(identity, group, res, "cat", "TestProperty"); original = pm.createPropertyInstance( identity, group, res, "cat", "TestProperty", new Float(floatMaxValue), new Long(123456), "stringValue", "textValue"); pm.saveProperty(original); dbInstance.commitAndCloseSession(); copy = pm.findProperty(identity, group, res, "cat", "TestProperty"); assertTrue(original.getFloatValue().floatValue() == copy.getFloatValue().floatValue()); pm.deleteProperties(identity, group, res, "cat", "TestProperty"); original = pm.createPropertyInstance( identity, group, res, "cat", "TestProperty", new Float(Long.MAX_VALUE), new Long(123456), "stringValue", "textValue"); pm.saveProperty(original); dbInstance.commitAndCloseSession(); copy = pm.findProperty(identity, group, res, "cat", "TestProperty"); assertTrue(original.getFloatValue().floatValue() == copy.getFloatValue().floatValue()); pm.deleteProperties(identity, group, res, "cat", "TestProperty"); original = pm.createPropertyInstance( identity, group, res, "cat", "TestProperty", new Float(Long.MIN_VALUE), new Long(123456), "stringValue", "textValue"); pm.saveProperty(original); dbInstance.commitAndCloseSession(); copy = pm.findProperty(identity, group, res, "cat", "TestProperty"); assertTrue(original.getFloatValue().floatValue() == copy.getFloatValue().floatValue()); pm.deleteProperties(identity, group, res, "cat", "TestProperty"); }