BUG-1346: fix wrong prefix generation 43/9143/3
authorTimotej Kubas <tkubas@cisco.com>
Fri, 18 Jul 2014 14:12:49 +0000 (16:12 +0200)
committerRobert Varga <rovarga@cisco.com>
Fri, 18 Jul 2014 22:31:42 +0000 (22:31 +0000)
Java promotion rules are slightly different than C, so the expression
got promoted to int, which obviously did the wrong thing. Add an
explicit cast to prevent it.

+test

Change-Id: Id9e708ca86b7267b84b2a3db5ad52aeef651763d
Signed-off-by: Robert Varga <rovarga@cisco.com>
Signed-off-by: Timotej Kubas <tkubas@cisco.com>
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/RandomPrefix.java
yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/RandomPrefixTest.java [new file with mode: 0644]

index 82928944ec2c14e2fd0de6e0ed96bad40d83fd04..223157a223eee681231840c8b0d436dbe2ca0f83 100644 (file)
@@ -31,7 +31,7 @@ final class RandomPrefix {
                 do {
                     final StringBuilder sb = new StringBuilder();
                     for (int i = 0; i < 4; i++) {
-                        sb.append('a' + random.nextInt(25));
+                        sb.append((char)('a' + random.nextInt(25)));
                     }
 
                     prefix = sb.toString();
@@ -43,4 +43,4 @@ final class RandomPrefix {
 
         return prefix + ':' + qname.getLocalName();
     }
-}
\ No newline at end of file
+}
diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/RandomPrefixTest.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/RandomPrefixTest.java
new file mode 100644 (file)
index 0000000..eb32e4d
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.impl.codec.xml;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.impl.codec.xml.RandomPrefix;
+
+/**
+ * @author tkubas
+ */
+public class RandomPrefixTest {
+    private RandomPrefix randomPrefix;
+
+    /**
+     * setup {@link #randomPrefix} instance
+     */
+    @Before
+    public void setUp() {
+        randomPrefix = new RandomPrefix();
+    }
+    /**
+     * Test method for {@link org.opendaylight.yangtools.yang.data.impl.codec.xml.RandomPrefix#encodeQName(QName)}.
+     */
+    @Test
+    public void testEncodeQName() {
+        QName node = QName.create("","2013-06-07","node");
+        String encodedQName = randomPrefix.encodeQName(node);
+        Assert.assertNotNull(encodedQName);
+        Assert.assertTrue("prefix is expected to contain 4 small letters as prefix but result is: "+encodedQName,
+                encodedQName.matches("[a-z]{4}:node"));
+    }
+
+}