|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 2021, 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.espresso.staticobject.test; |
| 24 | + |
| 25 | +import com.oracle.truffle.espresso.staticobject.DefaultStaticObject; |
| 26 | +import com.oracle.truffle.espresso.staticobject.DefaultStaticProperty; |
| 27 | +import com.oracle.truffle.espresso.staticobject.StaticProperty; |
| 28 | +import com.oracle.truffle.espresso.staticobject.StaticPropertyKind; |
| 29 | +import com.oracle.truffle.espresso.staticobject.StaticShape; |
| 30 | +import org.junit.Assert; |
| 31 | +import org.junit.Assume; |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +import java.lang.reflect.Field; |
| 35 | +import java.lang.reflect.Modifier; |
| 36 | + |
| 37 | +public class BuilderPropertyTest extends StaticObjectTest { |
| 38 | + @Test |
| 39 | + public void sameBuilderSameProperty() { |
| 40 | + StaticShape.Builder builder = StaticShape.newBuilder(this); |
| 41 | + StaticProperty property = new DefaultStaticProperty("property", StaticPropertyKind.Int, false); |
| 42 | + builder.property(property); |
| 43 | + try { |
| 44 | + // You cannot add the same property twice |
| 45 | + builder.property(property); |
| 46 | + Assert.fail(); |
| 47 | + } catch (IllegalArgumentException e) { |
| 48 | + // You cannot add the same property twice |
| 49 | + Assert.assertEquals("This builder already contains a property with id 'property'", e.getMessage()); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void sameBuilderSameName() throws IllegalArgumentException { |
| 55 | + StaticShape.Builder builder = StaticShape.newBuilder(this); |
| 56 | + StaticProperty p1 = new DefaultStaticProperty("property", StaticPropertyKind.Int, false); |
| 57 | + StaticProperty p2 = new DefaultStaticProperty("property", StaticPropertyKind.Int, false); |
| 58 | + builder.property(p1); |
| 59 | + try { |
| 60 | + // You cannot add two properties with the same name |
| 61 | + builder.property(p2); |
| 62 | + Assert.fail(); |
| 63 | + } catch (IllegalArgumentException e) { |
| 64 | + Assert.assertEquals("This builder already contains a property with id 'property'", e.getMessage()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void differentBuildersSameProperty() { |
| 70 | + StaticShape.Builder b1 = StaticShape.newBuilder(this); |
| 71 | + StaticShape.Builder b2 = StaticShape.newBuilder(this); |
| 72 | + StaticProperty property = new DefaultStaticProperty("property", StaticPropertyKind.Int, false); |
| 73 | + b1.property(property); |
| 74 | + b2.property(property); |
| 75 | + b1.build(); |
| 76 | + try { |
| 77 | + // You cannot build shapes that share properties |
| 78 | + b2.build(); |
| 79 | + Assert.fail(); |
| 80 | + } catch (RuntimeException e) { |
| 81 | + Assert.assertEquals("Attempt to reinitialize the offset of static property 'property' of kind 'Int'.\nWas it added to more than one builder or multiple times to the same builder?", |
| 82 | + e.getMessage()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void propertyName() throws NoSuchFieldException { |
| 88 | + Assume.assumeFalse(ARRAY_BASED_STORAGE); |
| 89 | + |
| 90 | + StaticShape.Builder builder = StaticShape.newBuilder(this); |
| 91 | + StaticProperty property = new DefaultStaticProperty("property", StaticPropertyKind.Int, false); |
| 92 | + builder.property(property); |
| 93 | + StaticShape<DefaultStaticObject.Factory> shape = builder.build(); |
| 94 | + DefaultStaticObject object = shape.getFactory().create(); |
| 95 | + object.getClass().getField(guessGeneratedFieldName(property)); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void propertyFinal() throws NoSuchFieldException { |
| 100 | + Assume.assumeFalse(ARRAY_BASED_STORAGE); |
| 101 | + |
| 102 | + StaticShape.Builder builder = StaticShape.newBuilder(this); |
| 103 | + StaticProperty p1 = new DefaultStaticProperty("p1", StaticPropertyKind.Int, true); |
| 104 | + StaticProperty p2 = new DefaultStaticProperty("p2", StaticPropertyKind.Int, false); |
| 105 | + builder.property(p1); |
| 106 | + builder.property(p2); |
| 107 | + StaticShape<DefaultStaticObject.Factory> shape = builder.build(); |
| 108 | + DefaultStaticObject object = shape.getFactory().create(); |
| 109 | + Field f1 = object.getClass().getField(guessGeneratedFieldName(p1)); |
| 110 | + Field f2 = object.getClass().getField(guessGeneratedFieldName(p2)); |
| 111 | + Assert.assertTrue(Modifier.isFinal(f1.getModifiers())); |
| 112 | + Assert.assertFalse(Modifier.isFinal(f2.getModifiers())); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void propertyKind() throws NoSuchFieldException { |
| 117 | + Assume.assumeFalse(ARRAY_BASED_STORAGE); |
| 118 | + |
| 119 | + StaticShape.Builder builder = StaticShape.newBuilder(this); |
| 120 | + StaticPropertyKind[] kinds = StaticPropertyKind.values(); |
| 121 | + StaticProperty[] properties = new StaticProperty[kinds.length]; |
| 122 | + for (int i = 0; i < properties.length; i++) { |
| 123 | + properties[i] = new DefaultStaticProperty(kinds[i].name(), kinds[i], false); |
| 124 | + builder.property(properties[i]); |
| 125 | + } |
| 126 | + StaticShape<DefaultStaticObject.Factory> shape = builder.build(); |
| 127 | + DefaultStaticObject object = shape.getFactory().create(); |
| 128 | + for (int i = 0; i < properties.length; i++) { |
| 129 | + Class<?> expectedType; |
| 130 | + switch (kinds[i]) { |
| 131 | + case Boolean: |
| 132 | + expectedType = boolean.class; |
| 133 | + break; |
| 134 | + case Byte: |
| 135 | + expectedType = byte.class; |
| 136 | + break; |
| 137 | + case Char: |
| 138 | + expectedType = char.class; |
| 139 | + break; |
| 140 | + case Double: |
| 141 | + expectedType = double.class; |
| 142 | + break; |
| 143 | + case Float: |
| 144 | + expectedType = float.class; |
| 145 | + break; |
| 146 | + case Int: |
| 147 | + expectedType = int.class; |
| 148 | + break; |
| 149 | + case Long: |
| 150 | + expectedType = long.class; |
| 151 | + break; |
| 152 | + case Object: |
| 153 | + expectedType = Object.class; |
| 154 | + break; |
| 155 | + case Short: |
| 156 | + expectedType = short.class; |
| 157 | + break; |
| 158 | + default: |
| 159 | + expectedType = null; |
| 160 | + Assert.fail("Unexpected type: " + kinds[i]); |
| 161 | + } |
| 162 | + Assert.assertEquals(expectedType, object.getClass().getField(guessGeneratedFieldName(properties[i])).getType()); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments