public void testSet() {
// Check that we can't create arrays of size smaller than 0.
boolean exceptionCaught = false;
try {
ArrayFS array = this.cas.createArrayFS(-1);
assertTrue(array != null);
} catch (CASRuntimeException e) {
exceptionCaught = true;
assertTrue(e.getMessageKey().equals(CASRuntimeException.ILLEGAL_ARRAY_SIZE));
}
assertTrue(exceptionCaught);
ArrayFS array = this.cas.createArrayFS(0);
assertTrue(array != null);
assertTrue(array.size() == 0);
exceptionCaught = false;
try {
array.get(0);
} catch (ArrayIndexOutOfBoundsException e) {
exceptionCaught = true;
}
assertTrue(exceptionCaught);
FeatureStructure fs1 = this.cas.createFS(this.ts.getType(CAS.TYPE_NAME_ANNOTATION));
FeatureStructure fs2 = this.cas.createFS(this.ts.getType(CAS.TYPE_NAME_TOP));
FeatureStructure fs3 = this.cas.createFS(this.ts.getType(CASTestSetup.TOKEN_TYPE));
array = this.cas.createArrayFS(3);
try {
array.set(0, fs1);
array.set(1, fs2);
array.set(2, fs3);
} catch (ArrayIndexOutOfBoundsException e) {
assertTrue(false);
}
exceptionCaught = false;
try {
array.set(-1, fs1);
} catch (ArrayIndexOutOfBoundsException e) {
exceptionCaught = true;
}
assertTrue(exceptionCaught);
exceptionCaught = false;
try {
array.set(4, fs1);
} catch (ArrayIndexOutOfBoundsException e) {
exceptionCaught = true;
}
assertTrue(exceptionCaught);
assertTrue(array.get(0).equals(fs1));
assertTrue(array.get(1).equals(fs2));
assertTrue(array.get(2).equals(fs3));
exceptionCaught = false;
try {
array.get(-1);
} catch (ArrayIndexOutOfBoundsException e) {
exceptionCaught = true;
}
assertTrue(exceptionCaught);
exceptionCaught = false;
try {
array.get(4);
} catch (ArrayIndexOutOfBoundsException e) {
exceptionCaught = true;
}
assertTrue(exceptionCaught);
}