use of com.github.bordertech.wcomponents.util.mock.MockFileItem in project wcomponents by BorderTech.
the class WFileWidget_Test method createFileItem.
/**
* @param fileName the file name in the file item
* @param size the size of the file in the file item
* @return a file item
*/
private static FileItem createFileItem(final String fileName, final int size) {
final byte[] testFile = new byte[size];
for (int i = 0; i < testFile.length; i++) {
testFile[i] = (byte) (i & 0xff);
}
MockFileItem fileItem = new MockFileItem();
fileItem.set(testFile);
fileItem.setName(fileName);
return fileItem;
}
use of com.github.bordertech.wcomponents.util.mock.MockFileItem in project wcomponents by BorderTech.
the class WMultiFileWidget_Test method createFileItem.
// @Test
// public void testDoHandleRequestAjaxUploadWithMultiFiles()
// {
// WMultiFileWidget widget = new WMultiFileWidget();
// widget.setLocked(true);
// setActiveContext(createUIContext());
//
// // Setup AJAX operation
// try
// {
// AjaxHelper.setCurrentOperationDetails(new AjaxOperation(widget.getId(), "X"), null);
//
// // Setup request with file1
// FileUploadMockRequest request = setupFileUploadRequest(widget, TEST_FILE_ITEM_1_2);
// boolean changed = widget.doHandleRequest(request);
// Assert.assertFalse("For ajax upload should have returned no change.", changed);
//
// // Check Uploaded file1
// Assert.assertEquals("Uploaded files should have 2 files", 2, widget.getValue().size());
// Assert.assertEquals("Uploaded files should contain file 1.", TEST_FILE_ITEM1.getName(), widget.getValue()
// .get(0).getFile().getName());
//
// Assert.assertEquals("Uploaded files should contain file 2.", TEST_FILE_ITEM2.getName(), widget.getValue()
// .get(1).getFile().getName());
//
// // Clear AJAX
// AjaxHelper.clearCurrentOperationDetails();
//
// // Process a "page" request
// changed = widget.doHandleRequest(request);
// Assert.assertTrue("After ajax uploads should have returned true.", changed);
//
// // Do prepare paint (should clear change flag)
// widget.preparePaintComponent(request);
//
// changed = widget.doHandleRequest(request);
// Assert.assertFalse("After ajax uploads and PreparePaint should have returned false.", changed);
//
// }
// finally
// {
// AjaxHelper.clearCurrentOperationDetails();
// }
// }
//
// @Test
// public void testDoHandleRequestPageRequest()
// {
// WMultiFileWidget widget = new WMultiFileWidget();
// widget.setLocked(true);
// setActiveContext(createUIContext());
//
// // Empty Request
// MockRequest request = new MockRequest();
// boolean changed = widget.doHandleRequest(request);
//
// Assert.assertFalse("For an empty request should have returned no change.", changed);
//
// // Set uploaded file file1, file2
// widget.setData(UPLOADED_1_2);
//
// // Check selected files are File 1 and File2
// Assert.assertEquals("Selected files should be file 1 and file2.", UPLOADED_1_2, widget.getSelectedFiles());
//
// // Setup request to select both files
// request = new MockRequest();
// request.setParameter(widget.getId() + ".selected", new String[] { "0", "1" });
// changed = widget.doHandleRequest(request);
//
// // Check selected files are still File 1 and File2
// Assert.assertFalse("Should have returned no change.", changed);
// Assert.assertEquals("Selected files should be file 1 and file2.", UPLOADED_1_2, widget.getSelectedFiles());
//
// // Setup request to only select file1
// request = new MockRequest();
// request.setParameter(widget.getId() + ".selected", new String[] { "0" });
// changed = widget.doHandleRequest(request);
//
// // Check selected files is File 1
// Assert.assertFalse("Should have returned no change.", changed);
// Assert.assertEquals("Selected file should be file 1.", UPLOADED_1, widget.getSelectedFiles());
//
// // Setup request to select no files
// request = new MockRequest();
// widget.doHandleRequest(request);
//
// // Check selected is empty
// Assert.assertTrue("Selected files should be empty.", widget.getSelectedFiles().isEmpty());
// }
//
// @Test
// public void testDoHandleRequestAjaxUpload()
// {
// WMultiFileWidget widget = new WMultiFileWidget();
// widget.setLocked(true);
// setActiveContext(createUIContext());
//
// // Setup AJAX operation
// try
// {
// AjaxHelper.setCurrentOperationDetails(new AjaxOperation(widget.getId(), "X"), null);
//
// // Setup request with file1
// FileUploadMockRequest request = setupFileUploadRequest(widget, TEST_FILE_ITEM1);
// boolean changed = widget.doHandleRequest(request);
// Assert.assertFalse("For ajax upload should have returned no change.", changed);
//
// // Check Uploaded file1
// Assert.assertEquals("Uploaded files should have 1 file", 1, widget.getValue().size());
// Assert.assertEquals("Uploaded files should contain file 1.", TEST_FILE_ITEM1.getName(), widget.getValue()
// .get(0).getFile().getName());
//
// // Setup request with file2
// request = setupFileUploadRequest(widget, TEST_FILE_ITEM2);
// changed = widget.doHandleRequest(request);
// Assert.assertFalse("For ajax upload should have returned no change.", changed);
//
// // Check Uploaded file1 and file2
// Assert.assertEquals("Uploaded files should have 2 files", 2, widget.getValue().size());
// Assert.assertEquals("Uploaded files should contain file 1.", TEST_FILE_ITEM1.getName(), widget.getValue()
// .get(0).getFile().getName());
// Assert.assertEquals("Uploaded files should contain file 2.", TEST_FILE_ITEM2.getName(), widget.getValue()
// .get(1).getFile().getName());
//
// // Clear AJAX
// AjaxHelper.clearCurrentOperationDetails();
//
// // Process a "page" request
// changed = widget.doHandleRequest(request);
// Assert.assertTrue("After ajax uploads should have returned true.", changed);
//
// // Do prepare paint (should clear change flag)
// widget.preparePaintComponent(request);
//
// changed = widget.doHandleRequest(request);
// Assert.assertFalse("After ajax uploads and PreparePaint should have returned false.", changed);
//
// }
// finally
// {
// AjaxHelper.clearCurrentOperationDetails();
// }
// }
//
// @Test
// public void testIsPresent()
// {
// WMultiFileWidget widget = new WMultiFileWidget();
// widget.setLocked(true);
//
// // Empty Request
// setActiveContext(createUIContext());
// MockRequest request = new MockRequest();
// Assert.assertFalse("IsPresent should return false", widget.isPresent(request));
//
// // Widget on the request
// request = new MockRequest();
// request.setParameter(widget.getId(), "x");
// Assert.assertTrue("IsPresent should return true", widget.isPresent(request));
//
// // Current AJAX operation
// // Setup AJAX operation - is target
// try
// {
// AjaxHelper.setCurrentOperationDetails(new AjaxOperation(widget.getId(), "X"), null);
//
// // Setup request with file1
// request = setupFileUploadRequest(widget, TEST_FILE_ITEM1);
// Assert.assertTrue("IsPresent should return true with ajax target", widget.isPresent(request));
// }
// finally
// {
// AjaxHelper.clearCurrentOperationDetails();
// }
// }
/**
* @param fileName the file name in the file item
* @param size the size of the file in the file item
* @return a file item
*/
private static FileItem createFileItem(final String fileName, final int size) {
final byte[] testFile = new byte[size];
for (int i = 0; i < testFile.length; i++) {
testFile[i] = (byte) (i & 0xff);
}
MockFileItem fileItem = new MockFileItem();
fileItem.set(testFile);
fileItem.setName(fileName);
return fileItem;
}
use of com.github.bordertech.wcomponents.util.mock.MockFileItem in project wcomponents by BorderTech.
the class WMultiFileWidgetRenderer_Test method testDoPaint.
@Test
public void testDoPaint() throws IOException, SAXException, XpathException {
WMultiFileWidget fileUpload = new WMultiFileWidget();
assertSchemaMatch(fileUpload);
assertXpathEvaluatesTo(fileUpload.getId(), "//ui:multifileupload/@id", fileUpload);
assertXpathEvaluatesTo("10240000", "//ui:multifileupload/@maxFileSize", fileUpload);
assertXpathNotExists("//ui:multifileupload/@disabled", fileUpload);
assertXpathNotExists("//ui:multifileupload/@hidden", fileUpload);
assertXpathNotExists("//ui:multifileupload/@required", fileUpload);
assertXpathNotExists("//ui:multifileupload/@readOnly", fileUpload);
assertXpathNotExists("//ui:multifileupload/@toolTip", fileUpload);
assertXpathNotExists("//ui:multifileupload/@accessibleText", fileUpload);
assertXpathNotExists("//ui:multifileupload/@acceptedMimeTypes", fileUpload);
assertXpathNotExists("//ui:multifileupload/@maxFiles", fileUpload);
assertXpathNotExists("//ui:multifileupload/ui:file", fileUpload);
fileUpload.setDisabled(true);
assertSchemaMatch(fileUpload);
assertXpathEvaluatesTo("true", "//ui:multifileupload/@disabled", fileUpload);
setFlag(fileUpload, ComponentModel.HIDE_FLAG, true);
assertSchemaMatch(fileUpload);
assertXpathEvaluatesTo("true", "//ui:multifileupload/@hidden", fileUpload);
fileUpload.setMandatory(true);
assertSchemaMatch(fileUpload);
assertXpathEvaluatesTo("true", "//ui:multifileupload/@required", fileUpload);
fileUpload.setToolTip("tooltip");
assertSchemaMatch(fileUpload);
assertXpathEvaluatesTo(fileUpload.getToolTip(), "//ui:multifileupload/@toolTip", fileUpload);
fileUpload.setAccessibleText("accessible");
assertSchemaMatch(fileUpload);
assertXpathEvaluatesTo(fileUpload.getAccessibleText(), "//ui:multifileupload/@accessibleText", fileUpload);
fileUpload.setFileTypes(new String[] { "a/b", "c/d" });
assertXpathEvaluatesTo("a/b,c/d", "//ui:multifileupload/@acceptedMimeTypes", fileUpload);
fileUpload.setMaxFileSize(12345);
assertXpathEvaluatesTo("12345", "//ui:multifileupload/@maxFileSize", fileUpload);
fileUpload.setMaxFiles(11);
assertXpathEvaluatesTo("11", "//ui:multifileupload/@maxFiles", fileUpload);
// Test file rendering
MockFileItem fileItem = new MockFileItem();
fileItem.setName("test.bin");
fileItem.setContentType("application/octet-stream");
fileItem.set(new byte[123]);
FileWidgetUpload file = new FileWidgetUpload("X", new FileItemWrap(fileItem));
fileUpload.setData(Arrays.asList(file));
assertSchemaMatch(fileUpload);
assertXpathEvaluatesTo("1", "count(//ui:multifileupload/ui:file)", fileUpload);
assertXpathEvaluatesTo("X", "//ui:multifileupload/ui:file/@id", fileUpload);
assertXpathEvaluatesTo(fileItem.getName(), "//ui:multifileupload/ui:file/@name", fileUpload);
assertXpathEvaluatesTo(fileItem.getContentType(), "//ui:multifileupload/ui:file/@type", fileUpload);
assertXpathEvaluatesTo(String.valueOf(fileItem.getSize()), "//ui:multifileupload/ui:file/@size", fileUpload);
}
Aggregations