This project has retired. For details please refer to its Attic page.
TestJythonWritableWrapper xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.giraph.jython;
19  
20  import org.apache.giraph.jython.wrappers.JythonWritableWrapper;
21  import org.junit.Test;
22  import org.python.core.PyClass;
23  import org.python.core.PyFunction;
24  import org.python.core.PyInteger;
25  import org.python.core.PyMethod;
26  import org.python.core.PyObject;
27  import org.python.util.PythonInterpreter;
28  
29  import java.io.ByteArrayInputStream;
30  import java.io.ByteArrayOutputStream;
31  import java.io.DataInputStream;
32  import java.io.DataOutputStream;
33  import java.io.IOException;
34  
35  import static org.junit.Assert.assertEquals;
36  import static org.junit.Assert.assertTrue;
37  
38  public class TestJythonWritableWrapper {
39    @Test
40    public void testWrap() throws IOException {
41      String jython =
42          "class Foo:\n" +
43          "    def __init__(self):\n" +
44          "        self.val = 17\n" +
45          "\n" +
46          "    def do_add(self, x):\n" +
47          "        self.val += x\n" +
48          "\n" +
49          "    def do_add_squared(self, x):\n" +
50          "        self.do_add(x * x)\n" +
51          "\n" +
52          "    def new_other(self):\n" +
53          "        self.other_val = 3\n" +
54          "\n" +
55          "def outside_add_squared(foo, x):\n" +
56          "    foo.do_add_squared(x)\n" +
57          "\n";
58  
59      PythonInterpreter interpreter = new PythonInterpreter();
60      interpreter.exec(jython);
61  
62      PyObject fooClass = interpreter.get("Foo");
63      assertTrue(fooClass instanceof PyClass);
64      PyObject foo = fooClass.__call__();
65  
66      PyObject fooVal = foo.__getattr__("val");
67      assertTrue(fooVal instanceof PyInteger);
68      PyInteger val = (PyInteger) fooVal;
69      assertEquals(17, val.getValue());
70  
71      PyObject function = interpreter.get("outside_add_squared");
72      assertTrue("method class: " + function.getClass(), function instanceof PyFunction);
73      function.__call__(foo, new PyInteger(3));
74  
75      fooVal = foo.__getattr__("val");
76      assertTrue(fooVal instanceof PyInteger);
77      val = (PyInteger) fooVal;
78      assertEquals(26, val.getValue());
79  
80      JythonWritableWrapper wrappedFoo = new JythonWritableWrapper(foo);
81      PyObject newOtherMethod = wrappedFoo.__getattr__("new_other");
82  
83      assertTrue(newOtherMethod instanceof PyMethod);
84      newOtherMethod.__call__();
85  
86      function.__call__(wrappedFoo, new PyInteger(2));
87  
88      fooVal = foo.__getattr__("val");
89      assertTrue(fooVal instanceof PyInteger);
90      val = (PyInteger) fooVal;
91      assertEquals(30, val.getValue());
92  
93      ByteArrayOutputStream baos = new ByteArrayOutputStream();
94      DataOutputStream dos = new DataOutputStream(baos);
95      wrappedFoo.write(dos);
96  
97      byte[] data = baos.toByteArray();
98      ByteArrayInputStream bais = new ByteArrayInputStream(data);
99      DataInputStream dis = new DataInputStream(bais);
100 
101     PyObject foo2 = fooClass.__call__();
102 
103     PyObject foo2Val = foo2.__getattr__("val");
104     assertTrue(foo2Val instanceof PyInteger);
105     PyInteger val2 = (PyInteger) foo2Val;
106     assertEquals(17, val2.getValue());
107 
108     JythonWritableWrapper wrappedFoo2 = new JythonWritableWrapper(foo2);
109 
110     foo2Val = wrappedFoo2.getPyObject().__getattr__("val");
111     assertTrue(foo2Val instanceof PyInteger);
112     val2 = (PyInteger) foo2Val;
113     assertEquals(17, val2.getValue());
114 
115     wrappedFoo2.readFields(dis);
116 
117     foo2Val = wrappedFoo2.getPyObject().__getattr__("val");
118     assertTrue(foo2Val instanceof PyInteger);
119     val2 = (PyInteger) foo2Val;
120     assertEquals(30, val2.getValue());
121   }
122 }