Skip to content

Commit 1b0aa05

Browse files
committed
Jam Android testcases
1 parent 06e968a commit 1b0aa05

31 files changed

Lines changed: 1291 additions & 312 deletions

.idea/dictionaries/twisted.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jarRepositories.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,24 @@ android {
2323
exclude group:"org.jmonkeyengine",module:"jme3-desktop"
2424
}
2525
compileOptions {
26+
coreLibraryDesugaringEnabled true
2627
sourceCompatibility JavaVersion.VERSION_11
2728
targetCompatibility JavaVersion.VERSION_11
2829
}
2930
}
3031

3132
dependencies {
3233
implementation fileTree(dir: "libs", include: ["*.jar"])
34+
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
3335
implementation 'androidx.appcompat:appcompat:1.2.0'
3436
implementation 'com.google.android.material:material:1.3.0'
3537
testImplementation 'junit:junit:4.13.2'
3638
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
3739
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
38-
implementation "org.jmonkeyengine:jme3-core:3.4.0-beta4"
39-
implementation "org.jmonkeyengine:jme3-effects:3.4.0-beta4"
40-
implementation "org.jmonkeyengine:jme3-android-native:3.4.0-beta4"
40+
implementation "org.jmonkeyengine:jme3-core:3.4.0-SNAPSHOT"
41+
implementation "org.jmonkeyengine:jme3-effects:3.4.0-SNAPSHOT"
42+
implementation "org.jmonkeyengine:jme3-android-native:3.4.0-stable"
43+
implementation "org.jmonkeyengine:jme3-android:3.4.0-stable"
4144
implementation 'com.github.Scrappers-glitch:Superior-Extended-Engine:1.5.1-alpha12'
4245
implementation "androidx.work:work-runtime:2.5.0"
4346
implementation "androidx.concurrent:concurrent-futures:1.1.0"

app/src/androidTest/java/com/scrappers/dbtraining/ExampleInstrumentedTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.scrappers.dbtraining;
22

3+
import static org.junit.Assert.assertEquals;
4+
35
import android.content.Context;
46

5-
import androidx.test.platform.app.InstrumentationRegistry;
67
import androidx.test.ext.junit.runners.AndroidJUnit4;
8+
import androidx.test.platform.app.InstrumentationRegistry;
79

810
import org.junit.Test;
911
import org.junit.runner.RunWith;
1012

11-
import static org.junit.Assert.*;
12-
1313
/**
1414
* Instrumented test, which will execute on an Android device.
1515
*
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.scrappers.dbtraining.androidTerminal.command;
2+
3+
import com.scrappers.dbtraining.androidTerminal.terminal.Terminal;
4+
5+
public interface Command {
6+
void asyncExecution(final Runtime jvmRuntime, final Terminal.Permission permission) throws InterruptedException;
7+
void setProcess(final Process process);
8+
void setCommands(final String[] commands);
9+
void getProcess();
10+
Terminal.Permission getPermission();
11+
String asyncStringRead();
12+
byte[] asyncBufferRead();
13+
void asyncStringWrite(final String values);
14+
void asyncBufferWrite(final byte[] buffer);
15+
void setPermission(final Terminal.Permission permission);
16+
void setId(final String id);
17+
String getId();
18+
void setIndex(int index);
19+
int getIndex();
20+
enum BufferSize{
21+
ZERO(0), ONE_KILOS(1024), TWO_KILOS(ONE_KILOS.memory * 2), FOUR_KILOS(TWO_KILOS.memory * 2), SIX_KILOS(TWO_KILOS.memory * 3),
22+
EIGHT_KILOS(TWO_KILOS.memory * 4), ONE_MEGA(ONE_KILOS.memory * 1024), TWO_MEGA(ONE_MEGA.memory * 2);
23+
private int memory;
24+
BufferSize(final int memory){
25+
this.memory = memory;
26+
}
27+
28+
public int getMemory() {
29+
return memory;
30+
}
31+
32+
public void setMemory(int memory) {
33+
this.memory = memory;
34+
}
35+
}
36+
}
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
package com.scrappers.dbtraining.androidTerminal.command;
2+
3+
import android.view.View;
4+
5+
import com.scrappers.dbtraining.androidTerminal.terminal.AndroidTerminal;
6+
import com.scrappers.dbtraining.androidTerminal.terminal.CommandLineExecutor;
7+
import com.scrappers.dbtraining.androidTerminal.terminal.Terminal;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.util.concurrent.Callable;
13+
import java.util.concurrent.ExecutionException;
14+
import java.util.concurrent.Executors;
15+
import java.util.logging.Level;
16+
import java.util.logging.Logger;
17+
18+
public class CommandSet implements Command, WritableData{
19+
20+
private String[] commands;
21+
private String id;
22+
private Process process;
23+
private static final Object mutex0 = new Object();
24+
private Terminal.Permission permission;
25+
private boolean firstCommandSet = false;
26+
private static final Logger logger = Logger.getLogger(CommandSet.class.getName());
27+
private BufferSize bufferSize = BufferSize.ONE_KILOS;
28+
29+
public CommandSet(final CommandLineExecutor commandLineExecutor, final String setId, final BufferSize bufferSize){
30+
//use the permissions provided by the android terminal class as the default permission.
31+
this.permission = commandLineExecutor.getPermission();
32+
this.id = setId;
33+
this.bufferSize = bufferSize;
34+
synchronized (mutex0) {
35+
mutex0.notify();
36+
}
37+
}
38+
39+
public void setFirstCommandSet(boolean firstCommandSet) {
40+
this.firstCommandSet = firstCommandSet;
41+
}
42+
43+
public boolean isFirstCommandSet() {
44+
return firstCommandSet;
45+
}
46+
47+
@Override
48+
public void asyncExecution(Runtime jvmRuntime, Terminal.Permission permission) throws InterruptedException {
49+
//set the new permission specifically for this command set.
50+
permission = this.permission;
51+
//calling the synchronized on hte mutex obj would make the thread own this object monitor.
52+
synchronized (mutex0) {
53+
try {
54+
mutex0.wait();
55+
logger.log(Level.FINE, "Started executing command set with id : " + CommandSet.this.getId());
56+
//expand to accommodate --the 2 vital commands--
57+
final String[] finalCommands = new String[commands.length + 2];
58+
//copy values to the final commands args
59+
finalCommands[0] = permission.getPermission();
60+
finalCommands[1] = "-c";
61+
for (int i = 0; i < commands.length; i++) {
62+
//copy commands to the final commands args
63+
finalCommands[i + 2] = commands[i];
64+
}
65+
//assign the process to be used by input streams.
66+
CommandSet.this.process = jvmRuntime.exec(finalCommands);
67+
} catch (IOException e) {
68+
e.printStackTrace();
69+
}
70+
//use this object as a mutex
71+
//calling the synchronized on hte mutex obj would make the thread own this object monitor.
72+
mutex0.notifyAll();
73+
logger.log(Level.FINE, "Finished executing command set with id : " + CommandSet.this.getId());
74+
}
75+
}
76+
77+
@Override
78+
public void setProcess(Process process) {
79+
80+
}
81+
82+
@Override
83+
public void setCommands(String[] commands) {
84+
this.commands = commands;
85+
}
86+
87+
@Override
88+
public void getProcess() {
89+
90+
}
91+
92+
@Override
93+
public Terminal.Permission getPermission() {
94+
return permission;
95+
}
96+
97+
@Override
98+
public String asyncStringRead() {
99+
try {
100+
return Executors.newSingleThreadExecutor().submit(new AsyncStringReader()).get();
101+
} catch (ExecutionException | InterruptedException e) {
102+
throw new IllegalStateException("Cannot read from Streams. " + e.getMessage());
103+
}
104+
}
105+
106+
@Override
107+
public byte[] asyncBufferRead() {
108+
try {
109+
return (byte[]) Executors.newSingleThreadExecutor().submit(new AsyncByteReader()).get();
110+
} catch (ExecutionException | InterruptedException e) {
111+
throw new IllegalStateException("Cannot read from Streams. " + e.getMessage());
112+
}
113+
}
114+
115+
@Override
116+
public void asyncStringWrite(String values) {
117+
118+
}
119+
120+
@Override
121+
public void asyncBufferWrite(byte[] buffer) {
122+
123+
}
124+
125+
@Override
126+
public void setPermission(Terminal.Permission permission) {
127+
this.permission = permission;
128+
}
129+
130+
@Override
131+
public void setId(String id) {
132+
this.id = id;
133+
}
134+
135+
@Override
136+
public String getId() {
137+
return id;
138+
}
139+
140+
@Override
141+
public void setIndex(int index) {
142+
143+
}
144+
145+
@Override
146+
public int getIndex() {
147+
return 0;
148+
}
149+
150+
@Override
151+
public void writeToFile(File file) {
152+
153+
}
154+
155+
@Override
156+
public void writeToConsole(Logger logger) {
157+
158+
}
159+
160+
@Override
161+
public void printToWidget(View view) {
162+
163+
}
164+
165+
private class AsyncStringReader extends Reader<String> {
166+
@Override
167+
public String call() throws Exception {
168+
logger.log(Level.FINE, "Started reading command set output with id : "+CommandSet.this.getId());
169+
//use this object as a mutex
170+
waitForCommandExecution();
171+
final Process process = CommandSet.this.process;
172+
final InputStream inputStream = process.getInputStream();
173+
//use at least 2kbs buffer memory
174+
byte[] buffer = new byte[Math.max(bufferSize.getMemory(), 2048)];
175+
for (int i = 0; i < buffer.length; i++) {
176+
buffer[i] = (byte) inputStream.read();
177+
}
178+
buffer = clampBufferLen(buffer);
179+
releaseLocks();
180+
logger.log(Level.FINE, "Finished reading command set output with id : "+CommandSet.this.getId());
181+
return new String(buffer);
182+
}
183+
}
184+
private class AsyncByteReader extends Reader<byte[]> {
185+
@Override
186+
public byte[] call() throws Exception {
187+
logger.log(Level.FINE, "Started reading command set output with id : "+CommandSet.this.getId());
188+
//use this object as a mutex
189+
waitForCommandExecution();
190+
final Process process = CommandSet.this.process;
191+
final InputStream inputStream = process.getInputStream();
192+
byte[] buffer = new byte[2048];
193+
for (int i = 0; i < buffer.length; i++) {
194+
buffer[i] = (byte) inputStream.read();
195+
}
196+
buffer = clampBufferLen(buffer);
197+
releaseLocks();
198+
logger.log(Level.FINE, "Finished reading command set output with id : "+CommandSet.this.getId());
199+
return buffer;
200+
}
201+
}
202+
protected class AsyncWriter extends Thread{
203+
@Override
204+
public void run() {
205+
//use this object as a mutex
206+
waitForCommandExecution();
207+
}
208+
private void waitForCommandExecution(){
209+
try {
210+
synchronized (mutex0) {
211+
mutex0.wait();
212+
}
213+
} catch (InterruptedException e) {
214+
e.printStackTrace();
215+
}
216+
}
217+
}
218+
private abstract static class Reader<T> implements Callable<T>{
219+
protected void waitForCommandExecution() throws InterruptedException{
220+
synchronized (mutex0) {
221+
mutex0.wait();
222+
}
223+
}
224+
protected void releaseLocks(){
225+
synchronized (mutex0) {
226+
mutex0.notifyAll();
227+
}
228+
}
229+
/**
230+
* Clamps the buffer length to the only viable readable alphabetic based bytes.
231+
* @param buffer the byte buffer to clamp.
232+
* @return a new clamped buffer.
233+
*/
234+
protected synchronized byte[] clampBufferLen(byte[] buffer){
235+
final byte[] tempBuffer = new byte[buffer.length];
236+
int byteIndex = 0;
237+
int bufferLen = 0;
238+
//deep copy available values to a temporary buffer & recalculate the length of the initial buffer
239+
for(final byte val : buffer){
240+
if(val >= 0){
241+
//collect viable values & arrange them serially
242+
tempBuffer[byteIndex++] = val;
243+
++bufferLen;
244+
}
245+
}
246+
final byte[] newBuffer = new byte[bufferLen];
247+
for(int i = 0; i < newBuffer.length; i++){
248+
//deep copy data into the new buffer
249+
newBuffer[i] = tempBuffer[i];
250+
}
251+
//return a new clamped buffer to the available data
252+
return newBuffer;
253+
}
254+
}
255+
256+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.scrappers.dbtraining.androidTerminal.command;
2+
3+
import android.view.View;
4+
import java.io.File;
5+
import java.util.logging.Logger;
6+
7+
public interface WritableData extends Command{
8+
void writeToFile(final File file);
9+
void writeToConsole(final Logger logger);
10+
void printToWidget(final View view);
11+
}

0 commit comments

Comments
 (0)