Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/interop/MessageStringTest.java @ 22424:fec62e298245
Rename package truffle.api.test to truffle.api to enable package-protected API access for testing.
author | Christian Humer <christian.humer@oracle.com> |
---|---|
date | Wed, 02 Dec 2015 15:16:27 +0100 |
parents | truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/interop/MessageStringTest.java@6af6ca6c848e |
children | b39b603e2a1e |
comparison
equal
deleted
inserted
replaced
22423:70a10a9f28ad | 22424:fec62e298245 |
---|---|
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.interop; | |
24 | |
25 import static org.junit.Assert.assertEquals; | |
26 import static org.junit.Assert.assertNotNull; | |
27 import static org.junit.Assert.assertSame; | |
28 | |
29 import java.lang.reflect.Field; | |
30 import java.lang.reflect.Method; | |
31 import java.lang.reflect.Modifier; | |
32 import java.util.Locale; | |
33 | |
34 import org.junit.Test; | |
35 | |
36 import com.oracle.truffle.api.interop.Message; | |
37 | |
38 public class MessageStringTest { | |
39 | |
40 @Test | |
41 public void testFields() throws Exception { | |
42 for (Field f : Message.class.getFields()) { | |
43 if (f.getType() != Message.class) { | |
44 continue; | |
45 } | |
46 if ((f.getModifiers() & Modifier.STATIC) == 0) { | |
47 continue; | |
48 } | |
49 Message msg = (Message) f.get(null); | |
50 | |
51 String persistent = Message.toString(msg); | |
52 assertNotNull("Found name for " + f, persistent); | |
53 assertEquals("It is in upper case", persistent, persistent.toUpperCase(Locale.ENGLISH)); | |
54 | |
55 Message newMsg = Message.valueOf(persistent); | |
56 | |
57 assertSame("Same for " + f, msg, newMsg); | |
58 | |
59 assertEquals("Same toString()", persistent, msg.toString()); | |
60 } | |
61 } | |
62 | |
63 @Test | |
64 public void testFactoryMethods() throws Exception { | |
65 for (Method m : Message.class.getMethods()) { | |
66 if (m.getReturnType() != Message.class) { | |
67 continue; | |
68 } | |
69 if (!m.getName().startsWith("create")) { | |
70 continue; | |
71 } | |
72 if ((m.getModifiers() & Modifier.STATIC) == 0) { | |
73 continue; | |
74 } | |
75 Message msg = (Message) m.invoke(null, 0); | |
76 | |
77 String persistent = Message.toString(msg); | |
78 assertNotNull("Found name for " + m, persistent); | |
79 assertEquals("It is in upper case", persistent, persistent.toUpperCase(Locale.ENGLISH)); | |
80 | |
81 Message newMsg = Message.valueOf(persistent); | |
82 | |
83 assertEquals("Same for " + m, msg, newMsg); | |
84 | |
85 assertEquals("Same toString()", persistent, msg.toString()); | |
86 assertEquals("Same toString() for new one", persistent, newMsg.toString()); | |
87 } | |
88 } | |
89 | |
90 @Test | |
91 public void specialMessagePersitance() { | |
92 SpecialMsg msg = new SpecialMsg(); | |
93 String persistent = Message.toString(msg); | |
94 Message newMsg = Message.valueOf(persistent); | |
95 assertEquals("Message reconstructed", msg, newMsg); | |
96 } | |
97 | |
98 public static final class SpecialMsg extends Message { | |
99 | |
100 @Override | |
101 public boolean equals(Object message) { | |
102 return message instanceof SpecialMsg; | |
103 } | |
104 | |
105 @Override | |
106 public int hashCode() { | |
107 return 5425432; | |
108 } | |
109 } | |
110 } |