⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testattribute.java

📁 一个java操作xml的完整示例
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			attr.setValue(Double.toString(java.lang.Double.MAX_VALUE));
			assertTrue("incorrect double value", attr.getDoubleValue() == java.lang.Double.MAX_VALUE);

			attr.setValue(Double.toString(java.lang.Double.MIN_VALUE));
			assertTrue("incorrect double value", attr.getDoubleValue() == java.lang.Double.MIN_VALUE);

		} catch (DataConversionException e) {
			fail("couldn't convert boolean value");
		}

		try {
			attr.setValue("foo");
			fail("incorrectly returned double from non double value" + attr.getDoubleValue());

		} catch (DataConversionException e) {
		}

	}

    /**
     * Test floats returned from Attribute values. Checks that both correctly
     * formatted (java style) and incorrectly formatted float strings are
     * returned or raise a DataConversionException
     */
    public void test_TCM__float_getFloatValue() {
        Attribute attr = new Attribute("test", "1.00000009999e+10f");
        float flt = 1.00000009999e+10f;
        try {
            assertTrue("incorrect float conversion",
                    attr.getFloatValue() == flt);
        } catch (DataConversionException e) {
            fail("couldn't convert to float");
        }

        // test an invalid float

        attr.setValue("1.00000009999e");
        try {
            attr.getFloatValue();
            fail("incorrect float conversion from non float");
        } catch (DataConversionException e) {
        }

    }

    /**
 	 * Tests that Attribute can convert value strings to ints and
 	 * that is raises DataConversionException if it is not an int.
	 */
	public void test_TCM__int_getIntValue() {
        final Attribute attribute = new Attribute("test", "");
        int summand = 3;
        for(int i = 0; i < 28; i++) {
            summand <<= 1;
            final int value = Integer.MIN_VALUE + summand;

            attribute.setValue("" + value);
            try {
                assertEquals("incorrect int conversion", attribute.getIntValue(), value);
            } catch (final DataConversionException e) {
                fail("couldn't convert to int");
            }
        }

		//test an invalid int
        TCM__int_getIntValue_invalidInt("" + Long.MIN_VALUE);
        TCM__int_getIntValue_invalidInt("" + Long.MAX_VALUE);
        TCM__int_getIntValue_invalidInt("" + Float.MIN_VALUE);
        TCM__int_getIntValue_invalidInt("" + Float.MAX_VALUE);
        TCM__int_getIntValue_invalidInt("" + Double.MIN_VALUE);
        TCM__int_getIntValue_invalidInt("" + Double.MAX_VALUE);
	}

    private void TCM__int_getIntValue_invalidInt(final String value) {
        final Attribute attribute = new Attribute("test", value);
        try {
            attribute.getIntValue();
            fail("incorrect int conversion from non int");
        } catch (final DataConversionException e) {
        }
    }
    /**
	 * Test that Attribute returns a valid hashcode.
	 */
	public void test_TCM__int_hashCode() {
	    final Attribute attr = new Attribute("test", "value");
		//only an exception would be a problem
		int i = -1;
		try {
		    i = attr.hashCode();
		}
		catch(Exception e) {
		    fail("bad hashCode");
		}
		final Attribute attr2 = new Attribute("test", "value");

        //different Attributes, same text
		final int x = attr2.hashCode();
		assertFalse("Different Attributes with same value have same hashcode", x == i);

        final Attribute attr3 = new Attribute("test2", "value");
		//only an exception would be a problem
		final int y = attr3.hashCode();
		assertFalse("Different Attributes have same hashcode", y == x);
	}

    /**
	 * Test the convienience method for returning a long from an Attribute
	 */
	public void test_TCM__long_getLongValue() {
        final Attribute attribute = new Attribute("test", "");
        long summand = 3;
	    for(int i = 0; i < 60; i++) {
            summand <<= 1;
            final long value = Long.MIN_VALUE + summand;

            attribute.setValue("" + value);
    		try {
    			assertEquals("incorrect long conversion", attribute.getLongValue(), value);
    		} catch (final DataConversionException e) {
    			fail("couldn't convert to long");
    		}
        }

		//test an invalid long
        attribute.setValue("100000000000000000000000000");
		try {
            attribute.getLongValue();
			fail("incorrect long conversion from non long");
		} catch (final DataConversionException e) {
		}
	}

    /**
 	 * Test that an Attribute can clone itself correctly.  The test
 	 * covers the simple case and with the attribute using a namespace
 	 * and prefix.
	 */
	public void test_TCM__Object_clone() {
        TCM__Object_clone__default();

        TCM__Object_clone__attributeType(Attribute.UNDECLARED_TYPE);
        TCM__Object_clone__attributeType(Attribute.CDATA_TYPE);
        TCM__Object_clone__attributeType(Attribute.ID_TYPE);
        TCM__Object_clone__attributeType(Attribute.IDREF_TYPE);
        TCM__Object_clone__attributeType(Attribute.IDREFS_TYPE);
        TCM__Object_clone__attributeType(Attribute.ENTITY_TYPE);
        TCM__Object_clone__attributeType(Attribute.ENTITIES_TYPE);
        TCM__Object_clone__attributeType(Attribute.NMTOKEN_TYPE);
        TCM__Object_clone__attributeType(Attribute.NMTOKENS_TYPE);
        TCM__Object_clone__attributeType(Attribute.NOTATION_TYPE);
        TCM__Object_clone__attributeType(Attribute.ENUMERATED_TYPE);


        TCM__Object_clone__Namespace_default();

        TCM__Object_clone__Namespace_attributeType(Attribute.UNDECLARED_TYPE);
        TCM__Object_clone__Namespace_attributeType(Attribute.CDATA_TYPE);
        TCM__Object_clone__Namespace_attributeType(Attribute.ID_TYPE);
        TCM__Object_clone__Namespace_attributeType(Attribute.IDREF_TYPE);
        TCM__Object_clone__Namespace_attributeType(Attribute.IDREFS_TYPE);
        TCM__Object_clone__Namespace_attributeType(Attribute.ENTITY_TYPE);
        TCM__Object_clone__Namespace_attributeType(Attribute.ENTITIES_TYPE);
        TCM__Object_clone__Namespace_attributeType(Attribute.NMTOKEN_TYPE);
        TCM__Object_clone__Namespace_attributeType(Attribute.NMTOKENS_TYPE);
        TCM__Object_clone__Namespace_attributeType(Attribute.NOTATION_TYPE);
        TCM__Object_clone__Namespace_attributeType(Attribute.ENUMERATED_TYPE);
	}

    /**
     * Test that an Attribute can clone itself correctly. The test covers the
     * simple case with only name and value.
     */
    private void TCM__Object_clone__default() {
        final String attributeName = "test";
        final String attributeValue = "value";

        final Attribute attribute = new Attribute(attributeName, attributeValue);
        final Attribute clonedAttribute = (Attribute) attribute.clone();

        assertTrue("incorrect name in clone", clonedAttribute.getName().equals(attributeName));
        assertTrue("incorrect value in clone", clonedAttribute.getValue().equals(attributeValue));
        assertEquals("incoorect attribute type in clone", clonedAttribute.getAttributeType(), Attribute.UNDECLARED_TYPE);
    }

    /**
     * Test that an Attribute can clone itself correctly. The test covers the
     * simple case with only name, value and a given attribute type.
     */
    private void TCM__Object_clone__attributeType(final int attributeType) {
        final String attributeName = "test";
        final String attributeValue = "value";

        final Attribute attribute = new Attribute(attributeName, attributeValue, attributeType);
        final Attribute clonedAttribute = (Attribute) attribute.clone();

        assertTrue("incorrect name in clone", clonedAttribute.getName().equals(attributeName));
        assertTrue("incorrect value in clone", clonedAttribute.getValue().equals(attributeValue));
        assertEquals("incoorect attribute type in clone", clonedAttribute.getAttributeType(), attributeType);
    }

    /**
     * Test that an Attribute can clone itself correctly. The test covers the
     * case with name, value, prefix, namespace and a given attribute type.
     */
    private void TCM__Object_clone__Namespace_default() {
        final String prefix = "prefx";
        final String uri = "http://some.other.place";

        final Namespace namespace = Namespace.getNamespace(prefix, uri);

        final String attributeName = "test";
        final String attributeValue = "value";

        final Attribute attribute = new Attribute(attributeName, attributeValue, namespace);
        final Attribute clonedAttribute = (Attribute) attribute.clone();

        assertTrue("incorrect name in clone", clonedAttribute.getName().equals(attributeName));
        assertTrue("incorrect value in clone", clonedAttribute.getValue().equals(attributeValue));
        assertEquals("incoorect attribute type in clone", clonedAttribute.getAttributeType(), Attribute.UNDECLARED_TYPE);

        assertTrue("incorrect prefix in clone", clonedAttribute.getNamespacePrefix().equals(prefix));
        assertTrue("incorrect qualified name in clone", clonedAttribute.getQualifiedName().equals(prefix + ':' + attributeName));
        assertTrue("incorrect Namespace URI in clone", clonedAttribute.getNamespaceURI().equals(uri));
    }

    /**
     * Test that an Attribute can clone itself correctly. The test covers the
     * case with name, value, prefix, namespace and a given attribute type.
     */
    private void TCM__Object_clone__Namespace_attributeType(final int attributeType) {
        final String prefix = "prefx";
        final String uri = "http://some.other.place";

        final Namespace namespace = Namespace.getNamespace(prefix, uri);

        final String attributeName = "test";
        final String attributeValue = "value";

        final Attribute attribute = new Attribute(attributeName, attributeValue, attributeType, namespace);
        final Attribute clonedAttribute = (Attribute) attribute.clone();

        assertTrue("incorrect name in clone", clonedAttribute.getName().equals(attributeName));
        assertTrue("incorrect value in clone", clonedAttribute.getValue().equals(attributeValue));
        assertEquals("incoorect attribute type in clone", clonedAttribute.getAttributeType(), attributeType);

        assertTrue("incorrect prefix in clone", clonedAttribute.getNamespacePrefix().equals(prefix));
        assertTrue("incorrect qualified name in clone", clonedAttribute.getQualifiedName().equals(prefix + ':' + attributeName));
        assertTrue("incorrect Namespace URI in clone", clonedAttribute.getNamespaceURI().equals(uri));
    }

    /**
     * Test that setting an Attribute's value works correctly.
     */
    public void test_TCM__OrgJdomAttribute_setValue_String() {
    	final Namespace namespace = Namespace.getNamespace("prefx", "http://some.other.place");

    	final Attribute attribute = new Attribute("test", "value", namespace);

    	assertTrue("incorrect value before set", attribute.getValue().equals("value"));

        attribute.setValue("foo");
    	assertTrue("incorrect value after set", attribute.getValue().equals("foo"));

    	//test that the verifier is called
    	try {
            attribute.setValue(null);
    		fail("Attribute setValue didn't catch null  string");
    	} catch (final IllegalDataException e) {
    	}

    	try {
    		final char c= 0x11;
    		final StringBuffer buffer = new StringBuffer("hhhh");
            buffer.setCharAt(2, c);
    		attribute.setValue(buffer.toString());
    		fail("Attribute setValue didn't catch invalid comment string");
    	} catch (final IllegalDataException e) {
    	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -