Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* This file is part of VisiCut.
* Copyright (C) 2011 - 2013 Thomas Oster <thomas.oster@rwth-aachen.de>
* RWTH Aachen University - 52062 Aachen, Germany
*
* VisiCut is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VisiCut is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with VisiCut. If not, see <http://www.gnu.org/licenses/>.
**/
package com.t_oster.liblasercut.laserscript;
import com.t_oster.liblasercut.PowerSpeedFocusProperty;
import com.t_oster.liblasercut.VectorPart;
import com.t_oster.liblasercut.laserscript.ScriptInterface;
import com.t_oster.liblasercut.laserscript.ScriptInterpreter;
import com.t_oster.liblasercut.laserscript.VectorPartScriptInterface;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.AccessControlException;
import java.util.LinkedList;
import java.util.List;
import javax.script.ScriptException;
import static org.junit.Assert.*;
import org.junit.Test;
/**
*
* @author Thomas Oster <thomas.oster@rwth-aachen.de>
*/
public class ScriptInterpreterTest
{
public ScriptInterpreterTest()
{
}
@Test
public void testSecurity() throws ScriptException, IOException
{
SecurityManager bak = System.getSecurityManager();
ScriptInterpreter instance = new ScriptInterpreter();
File f = new File("testfile");
if (f.exists())
{
f.delete();
}
try
{
instance.execute(new InputStreamReader(this.getClass().getResourceAsStream("SecurityTest.js")), new ScriptInterface(){
public void move(double x, double y)
{
}
public void line(double x, double y)
{
}
public void set(String property, Object value)
{
}
public Object get(String property)
{
return null;
}
public void echo(String text)
{
}
});
}
catch (ScriptException e)
{
assertFalse(f.exists());
return;
}
fail("No Exception thrown");
}
@Test
public void testVectorJobScriptInterface() throws ScriptException, IOException
{
String script = "function rectangle(x, y, width, height) {";
script += "move(x, y);";
script += "line(x+width, y);";
script += "line(x+width, y+height);";
script += "line(x, y+height);";
script += "line(x, y);";
script += "}";
script += "rectangle(0, 0, 20, 30);";
script += "rectangle(0, 0, 20, 30);";
ScriptInterpreter instance = new ScriptInterpreter();
VectorPart vp = new VectorPart(new PowerSpeedFocusProperty(), 500d);
VectorPartScriptInterface si = new VectorPartScriptInterface(vp, new AffineTransform());
instance.execute(script, si);
assertEquals(0, vp.getMinX());
assertEquals(0, vp.getMinY());
assertEquals(20, vp.getMaxX());
assertEquals(30, vp.getMaxY());
}
/**
* Test of execute method, of class ScriptInterpreter.
*/
@Test
public void testExecute() throws Exception
{
System.out.println("execute");
String script = "function rectangle(x, y, width, height) {";
script += "move(x, y);";
script += "line(x+width, y);";
script += "line(x+width, y+height);";
script += "line(x, y+height);";
script += "line(x, y);";
script += "}";
script += "rectangle(0, 0, 20, 30);";
script += "rectangle(0, 0, 20, 30);";
ScriptInterpreter instance = new ScriptInterpreter();
final List<String> steps = new LinkedList<String>();
instance.execute(script, new ScriptInterface(){
public void move(double x, double y)
{
assertTrue(x == 0 && y == 0);
steps.add("move ("+x+","+y+")");
}
public void line(double x, double y)
{
steps.add("line ("+x+","+y+")");
}
public void set(String property, Object value)
{
steps.add("set ("+property+","+value.toString()+")");
}
public Object get(String property)
{
steps.add("get ("+property+")");
return null;
}
public void echo(String text)
{
steps.add("echo ("+text+")");
}
});
assertEquals(10, steps.size());
}
}