comparison truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/interop/MessageStringTest.java @ 22135:e70b20f4bb00

Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Mon, 07 Sep 2015 17:07:20 +0200
parents
children dc83cc1f94f2
comparison
equal deleted inserted replaced
22134:025869c88840 22135:e70b20f4bb00
1 /*
2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.truffle.api.test.interop;
24
25 import com.oracle.truffle.api.interop.Message;
26 import java.lang.reflect.Field;
27 import java.lang.reflect.Method;
28 import java.lang.reflect.Modifier;
29 import java.util.Locale;
30 import static org.junit.Assert.*;
31 import org.junit.Test;
32
33 public class MessageStringTest {
34 @Test
35 public void testFields() throws Exception {
36 for (Field f : Message.class.getFields()) {
37 if (f.getType() != Message.class) {
38 continue;
39 }
40 if ((f.getModifiers() & Modifier.STATIC) == 0) {
41 continue;
42 }
43 Message msg = (Message) f.get(null);
44
45 String persistent = Message.toString(msg);
46 assertNotNull("Found name for " + f, persistent);
47 assertEquals("It is in upper case", persistent, persistent.toUpperCase(Locale.ENGLISH));
48
49 Message newMsg = Message.valueOf(persistent);
50
51 assertSame("Same for " + f, msg, newMsg);
52
53 assertEquals("Same toString()", persistent, msg.toString());
54 }
55 }
56
57 @Test
58 public void testFactoryMethods() throws Exception {
59 for (Method m : Message.class.getMethods()) {
60 if (m.getReturnType() != Message.class) {
61 continue;
62 }
63 if (!m.getName().startsWith("create")) {
64 continue;
65 }
66 if ((m.getModifiers() & Modifier.STATIC) == 0) {
67 continue;
68 }
69 Message msg = (Message) m.invoke(null, 0);
70
71 String persistent = Message.toString(msg);
72 assertNotNull("Found name for " + m, persistent);
73 assertEquals("It is in upper case", persistent, persistent.toUpperCase(Locale.ENGLISH));
74
75 Message newMsg = Message.valueOf(persistent);
76
77 assertEquals("Same for " + m, msg, newMsg);
78
79 assertEquals("Same toString()", persistent, msg.toString());
80 assertEquals("Same toString() for new one", persistent, newMsg.toString());
81 }
82 }
83
84 @Test
85 public void specialMessagePersitance() {
86 SpecialMsg msg = new SpecialMsg();
87 String persistent = Message.toString(msg);
88 Message newMsg = Message.valueOf(persistent);
89 assertEquals("Message reconstructed", msg, newMsg);
90 }
91
92 public static final class SpecialMsg extends Message {
93
94 @Override
95 public boolean equals(Object message) {
96 return message instanceof SpecialMsg;
97 }
98
99 @Override
100 public int hashCode() {
101 return 5425432;
102 }
103 }
104 }