List of usage examples for org.apache.commons.lang RandomStringUtils randomAscii
public static String randomAscii(int count)
Creates a random string whose length is the number of characters specified.
Characters will be chosen from the set of characters whose ASCII value is between 32
and 126
(inclusive).
From source file:org.grycap.gpf4med.DownloadServiceTest.java
@Test public void test() { System.out.println("DownloadServiceTest.test()"); try {/*from w w w .ja v a 2 s . com*/ // prepare input requests final String[] uris = { "http://www.chromix.com/downloadarea/testimages/frontier_color57sb.jpg", "http://www.hutchcolor.com/Targets_&_images_to_go/GrayBoat.zip", "http://www.chromix.com/downloadarea/testimages/frontier_color57s.jpg" }; // CRC32 checksum computed with Apache Commons IO final ImmutableMap<String, Map.Entry<Long, Long>> chc32sums = new ImmutableMap.Builder<String, Map.Entry<Long, Long>>() .put("frontier_color57sb.jpg", new AbstractMap.SimpleEntry<Long, Long>(1885150690l, 205632l)) .put("GrayBoat.zip", new AbstractMap.SimpleEntry<Long, Long>(2944050653l, 1286661l)) .put("frontier_color57s.jpg", new AbstractMap.SimpleEntry<Long, Long>(3600934165l, 967643l)) .build(); // test download with encryption final ImmutableMap<URI, File> requests = new ImmutableMap.Builder<URI, File>() .put(new URI(uris[0]), uriToFile(uris[0])).put(new URI(uris[1]), uriToFile(uris[1])) .put(new URI(uris[2]), uriToFile(uris[2])).build(); final ImmutableMap<URI, File> pending = new DownloadService().download(requests, null, null, null); assertThat("service response is not null", pending, notNullValue()); assertThat("there are no pending requests", pending.size(), equalTo(0)); for (final Map.Entry<String, Map.Entry<Long, Long>> chc32sum : chc32sums.entrySet()) { final File file = new File(TEST_OUTPUT_DIR, chc32sum.getKey()); assertThat("file exists", file.exists(), equalTo(true)); assertThat("file size coincides", chc32sum.getValue().getValue().longValue(), equalTo(file.length())); assertThat("CRC32 checksum coincides", chc32sum.getValue().getKey().longValue(), equalTo(FileUtils.checksumCRC32(file))); } // test download with encryption FileUtils.deleteQuietly(TEST_OUTPUT_DIR); final FileEncryptionProvider encryptionProvider = FileEncryptionProvider .getInstance(RandomStringUtils.randomAscii(1024)); final ImmutableMap<URI, File> pending2 = new DownloadService().download(requests, null, encryptionProvider, null); assertThat("service response is not null", pending2, notNullValue()); assertThat("there are no pending requests", pending2.size(), equalTo(0)); for (final Map.Entry<String, Map.Entry<Long, Long>> chc32sum : chc32sums.entrySet()) { final File file = new File(TEST_OUTPUT_DIR, chc32sum.getKey()); assertThat("file exists", file.exists(), equalTo(true)); // decrypt file final File clearFile = new File(file.getCanonicalPath() + ".tmp"); encryptionProvider.decrypt(new FileInputStream(file), new FileOutputStream(clearFile)); // check file assertThat("file size concides", chc32sum.getValue().getValue().longValue(), equalTo(clearFile.length())); assertThat("CRC32 checksum coincides", chc32sum.getValue().getKey().longValue(), equalTo(FileUtils.checksumCRC32(clearFile))); } // test file validation fail final String anyFile = uris[0]; final FileValidator alwaysFailValidator = mock(FileValidator.class); when(alwaysFailValidator.isValid(any(File.class))).thenReturn(false); final ImmutableMap<URI, File> pending3 = new DownloadService().download( new ImmutableMap.Builder<URI, File>() .put(new URI(anyFile), new File(TEST_OUTPUT_DIR, "any_file")).build(), alwaysFailValidator, null, null); assertThat("service response is not null", pending3, notNullValue()); assertThat("read invalid file is expected to return a pending file", pending3.size(), equalTo(1)); } catch (Exception e) { e.printStackTrace(System.err); fail("DownloadServiceTest.test() failed: " + e.getMessage()); } finally { System.out.println("DownloadServiceTest.test() has finished"); } }
From source file:org.grycap.gpf4med.FileEncryptionProviderTest.java
@Test public void test() { System.out.println("FileEncryptionProviderTest.test()"); try {/*from w w w.j a v a2s . c o m*/ // prepare test file final File clearFile = new File(TEST_OUTPUT_DIR, "clear.txt"); final File encryptedFile = new File(TEST_OUTPUT_DIR, "encrypt.txt"); final File clearTestFile = new File(TEST_OUTPUT_DIR, "clear_test.txt"); final String message = RandomStringUtils.randomAscii(10007); FileUtils.write(clearFile, message); // test encryption FileInputStream inputStream = new FileInputStream(clearFile); FileOutputStream outputStream = new FileOutputStream(encryptedFile); final FileEncryptionProvider provider = FileEncryptionProvider .getInstance(RandomStringUtils.randomAscii(1024)); provider.encrypt(inputStream, outputStream); final String encryptedMessage = FileUtils.readFileToString(encryptedFile); assertThat("encrypted message is not null", encryptedMessage, notNullValue()); assertThat("encrypted message is not empty", StringUtils.isNotBlank(encryptedMessage)); assertThat("encrypted message does not coincide with clear message", message, not(equalTo(encryptedMessage))); // test decryption inputStream = new FileInputStream(encryptedFile); outputStream = new FileOutputStream(clearTestFile); provider.decrypt(inputStream, outputStream); final String clearMessage = FileUtils.readFileToString(clearTestFile); assertThat("clear message is not null", clearMessage, notNullValue()); assertThat("clear message is not empty", StringUtils.isNotBlank(clearMessage)); assertThat("clear message coincides with original message", message, equalTo(clearMessage)); } catch (Exception e) { e.printStackTrace(System.err); fail("FileEncryptionProviderTest.test() failed: " + e.getMessage()); } finally { System.out.println("FileEncryptionProviderTest.test() has finished"); } }
From source file:org.kitesdk.cli.commands.TestTarImportCommand.java
@BeforeClass public static void createTestInputFiles() throws IOException { TestTarImportCommand.cleanup();/* w ww .j a v a2 s.co m*/ Path testData = new Path(TEST_DATA_DIR); FileSystem testFS = testData.getFileSystem(new Configuration()); datasetUri = "dataset:file:" + System.getProperty("user.dir") + "/" + TEST_DATASET_DIR + "/" + TEST_DATASET_NAME; TarArchiveOutputStream tosNoCompression = null; TarArchiveOutputStream tosGzipCompression = null; TarArchiveOutputStream tosBzip2Compression = null; TarArchiveOutputStream tosLargeEntry = null; TarArchiveEntry tarArchiveEntry = null; try { // No compression tosNoCompression = new TarArchiveOutputStream(testFS.create(new Path(TAR_TEST_FILE), true)); writeToTarFile(tosNoCompression, TAR_TEST_ROOT_PREFIX + "/", null); // Gzip compression tosGzipCompression = new TarArchiveOutputStream( new GzipCompressorOutputStream(testFS.create(new Path(TAR_TEST_GZIP_FILE), true))); writeToTarFile(tosGzipCompression, TAR_TEST_GZIP_ROOT_PREFIX + "/", null); // BZip2 compression tosBzip2Compression = new TarArchiveOutputStream( new BZip2CompressorOutputStream(testFS.create(new Path(TAR_TEST_BZIP2_FILE), true))); writeToTarFile(tosBzip2Compression, TAR_TEST_BZIP2_ROOT_PREFIX + "/", null); // "Large" entry file (10000 bytes) tosLargeEntry = new TarArchiveOutputStream(testFS.create(new Path(TAR_TEST_LARGE_ENTRY_FILE), true)); String largeEntry = RandomStringUtils.randomAscii(10000); writeToTarFile(tosLargeEntry, "largeEntry", largeEntry); // Generate test files with random names and content Random random = new Random(1); for (int i = 0; i < NUM_TEST_FILES; ++i) { // Create random file and data int fNameLength = random.nextInt(MAX_FILENAME_LENGTH); int fContentLength = random.nextInt(MAX_FILECONTENT_LENGTH); String fName = RandomStringUtils.randomAlphanumeric(fNameLength); String fContent = RandomStringUtils.randomAscii(fContentLength); // Write the file to tarball writeToTarFile(tosNoCompression, TAR_TEST_ROOT_PREFIX + "/" + fName, fContent); writeToTarFile(tosGzipCompression, TAR_TEST_GZIP_ROOT_PREFIX + "/" + fName, fContent); writeToTarFile(tosBzip2Compression, TAR_TEST_BZIP2_ROOT_PREFIX + "/" + fName, fContent); System.out.println("Wrote " + fName + " [" + fContentLength + "]"); } } finally { IOUtils.closeStream(tosNoCompression); IOUtils.closeStream(tosGzipCompression); IOUtils.closeStream(tosBzip2Compression); IOUtils.closeStream(tosLargeEntry); } }
From source file:org.orcid.persistence.dao.SolrDaoTest.java
@Test public void queryStringSearchFamilyNameGivenNameTenRows() throws Exception { int numRecordsToCreate = 10; List<String> orcidsToGetStored = new ArrayList<String>(); for (int i = 0; i < numRecordsToCreate; i++) { OrcidSolrDocument orcidSolrDocument = buildSupplementaryOrcid(); orcidSolrDocument.setGivenNames(RandomStringUtils.randomAscii(20)); // format of orcid irrelevant to solr - just need to make them // different orcidSolrDocument.setOrcid(RandomStringUtils.randomAscii(20)); orcidsToGetStored.add(orcidSolrDocument.getOrcid()); persistOrcid(orcidSolrDocument); }// w w w . ja v a 2 s. com String familyNameOnlyQuery = "family-name:Family Name"; List<OrcidSolrResult> solrResults = solrDao.findByDocumentCriteria(familyNameOnlyQuery, null, null) .getResults(); assertTrue(solrResults.size() == 10); familyNameOnlyQuery = "{!start=0 rows=5} family-name:Family Name"; solrResults = solrDao.findByDocumentCriteria(familyNameOnlyQuery, null, null).getResults(); assertTrue(solrResults.size() == 5); }
From source file:org.rascalmpl.library.cobra.Arbitrary.java
public IValue arbStringAscii(IInteger length) { return values.string(RandomStringUtils.randomAscii(length.intValue())); }
From source file:org.sonar.server.computation.ComputationServiceTest.java
private ComputationStep mockStep() { ComputationStep step = mock(ComputationStep.class); when(step.getDescription()).thenReturn(RandomStringUtils.randomAscii(5)); return step; }
From source file:org.xwiki.test.ui.appwithinminutes.WizardTest.java
/** * Tests the application creation process from start to end. *//*w w w . j a v a 2s .c o m*/ @Test @IgnoreBrowsers({ @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146"), @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason = "See http://jira.xwiki.org/browse/XE-1177") }) public void testCreateApplication() { // Step 1 // Set the application location. appCreatePage.getLocationPicker().browseDocuments(); new DocumentPickerModal().selectDocument(getClass().getSimpleName(), this.testName.getMethodName(), "WebHome"); appCreatePage.getLocationPicker() .waitForLocation(Arrays.asList("", getClass().getSimpleName(), this.testName.getMethodName(), "")); // Enter the application name (random name), making sure we also use some special chars. // See XWIKI-11747: Impossible to create new entry with an application having UTF8 chars in its name String appName = RandomStringUtils.randomAscii(10) + "\u00E2"; String[] appPath = new String[] { getClass().getSimpleName(), this.testName.getMethodName(), appName }; appCreatePage.setApplicationName(appName); // Wait for the preview. appCreatePage.waitForApplicationNamePreview(); // Move to the next step. ApplicationClassEditPage classEditPage = appCreatePage.clickNextStep(); // Step 2 // Add a 'Short Text' field. ClassFieldEditPane fieldEditPane = classEditPage.addField("Short Text"); // Set the field pretty name and default value fieldEditPane.setPrettyName("City Name"); fieldEditPane.setDefaultValue("Paris"); // Move to the next step. ApplicationHomeEditPage homeEditPage = classEditPage.clickNextStep().waitUntilPageIsLoaded(); // Move back to the second step. classEditPage = homeEditPage.clickPreviousStep(); // Open the configuration panel and set the field name fieldEditPane = new ClassFieldEditPane("shortText1"); fieldEditPane.openConfigPanel(); fieldEditPane.setName("cityName"); // Move to the next step. homeEditPage = classEditPage.clickNextStep(); // Step 3 // Enter the application description. String appDescription = "Simple application to manage data about various cities"; homeEditPage.setDescription(appDescription); // Add the Short Text field from the previous step to the list of columns. homeEditPage.addLiveTableColumn("City Name"); // Click the finish button which should lead us to the application home page. ApplicationHomePage homePage = homeEditPage.clickFinish(); // Assert the application description is present. Assert.assertTrue(homePage.getContent().contains(appDescription)); // Add a new entry. String firstEntryName = RandomStringUtils.randomAscii(6); EntryNamePane entryNamePane = homePage.clickAddNewEntry(); entryNamePane.setName(firstEntryName); EntryEditPage entryEditPage = entryNamePane.clickAdd(); // Assert the pretty name and the default value of the Short Text field. // Apparently WebElement#getText() takes into account the text-transform CSS property. Assert.assertEquals("CITY NAME", entryEditPage.getLabel("cityName")); Assert.assertEquals("Paris", entryEditPage.getValue("cityName")); // Change the field value. entryEditPage.setValue("cityName", "London"); // Save and go back to the application home page. entryEditPage.clickSaveAndView().clickBreadcrumbLink(appName); homePage = new ApplicationHomePage(); // Assert the entry we have just created is listed in the live table. LiveTableElement entriesLiveTable = homePage.getEntriesLiveTable(); entriesLiveTable.waitUntilReady(); Assert.assertTrue(entriesLiveTable.hasRow("City Name", "London")); // Assert that only the entry we have just created is listed as child of the application home page. The rest of // the documents (class, template, sheet, preferences) should be marked as hidden. PagesLiveTableElement childrenLiveTable = homePage.viewChildren().getLiveTable(); childrenLiveTable.waitUntilReady(); Assert.assertEquals(1, childrenLiveTable.getRowCount()); Assert.assertTrue(childrenLiveTable.hasPageWithTitle(firstEntryName)); // Go back to the application home page. getDriver().navigate().back(); // Click the edit button. homePage.edit(); homeEditPage = new ApplicationHomeEditPage(); // Change the application description. appDescription = "The best app!"; homeEditPage.setDescription(appDescription); // Remove one of the live table columns. homeEditPage.removeLiveTableColumn("Actions"); // Save homePage = homeEditPage.clickSaveAndView(); // Assert that the application description has changed and that the column has been removed. Assert.assertTrue(homePage.getContent().contains(appDescription)); entriesLiveTable = homePage.getEntriesLiveTable(); entriesLiveTable.waitUntilReady(); Assert.assertFalse(entriesLiveTable.hasColumn("Actions")); // Click the link to edit the application. classEditPage = homePage.clickEditApplication(); // Drag a Number field. fieldEditPane = classEditPage.addField("Number"); // Set the field pretty name. fieldEditPane.setPrettyName("Population Size"); // Fast forward. homeEditPage = classEditPage.clickNextStep(); // Just wait for the WYSIWYG editor (which is used for setting the application description) to load so that the // page layout is stable before we click on the Finish button. homeEditPage.setDescription(appDescription); homePage = homeEditPage.clickFinish(); // Add a new entry. String secondEntryName = RandomStringUtils.randomAscii(6); entryNamePane = homePage.clickAddNewEntry(); entryNamePane.setName(secondEntryName); entryEditPage = entryNamePane.clickAdd(); // Assert the new field is displayed in the edit sheet (field name was auto-generated). // Apparently WebElement#getText() takes into account the text-transform CSS property. Assert.assertEquals("POPULATION SIZE", entryEditPage.getLabel("number1")); // Save and go back to the application home page. entryEditPage.clickSaveAndView().clickBreadcrumbLink(appName); homePage = new ApplicationHomePage(); // Assert both entries are displayed in the live table. entriesLiveTable = homePage.getEntriesLiveTable(); entriesLiveTable.waitUntilReady(); Assert.assertTrue(entriesLiveTable.hasRow("Page name", firstEntryName)); Assert.assertTrue(entriesLiveTable.hasRow("Page name", secondEntryName)); // Go to the App Within Minutes home page. AppWithinMinutesHomePage appWithinMinutesHomePage = AppWithinMinutesHomePage.gotoPage(); // Assert that the created application is listed in the live table. ApplicationsLiveTableElement appsLiveTable = appWithinMinutesHomePage.getAppsLiveTable(); appsLiveTable.waitUntilReady(); Assert.assertTrue(appsLiveTable.isApplicationListed(appPath)); // Delete the application entries. homePage = appsLiveTable.viewApplication(appPath); homePage.clickDeleteAllEntries().clickYes(); // Verify that the entries live table is empty. entriesLiveTable = homePage.getEntriesLiveTable(); entriesLiveTable.waitUntilReady(); Assert.assertEquals(0, entriesLiveTable.getRowCount()); // Delete the application. homePage.clickDeleteApplication().clickYes(); // Verify that the application is not listed anymore. appsLiveTable = AppWithinMinutesHomePage.gotoPage().getAppsLiveTable(); appsLiveTable.waitUntilReady(); Assert.assertFalse(appsLiveTable.isApplicationListed(appPath)); }
From source file:org.xwiki.test.ui.appwithinminutes.WizardTest.java
/** * @see XWIKI-7380: Cannot go back from step 2 to step 1 *///from ww w .ja v a2 s .co m @Test @IgnoreBrowsers({ @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146"), @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason = "See http://jira.xwiki.org/browse/XE-1177") }) public void testGoBackToFirstStep() { // Step 1 String appName = RandomStringUtils.randomAscii(6); appCreatePage.setApplicationName(appName); appCreatePage.waitForApplicationNamePreview(); // Step 2 ApplicationClassEditPage classEditPage = appCreatePage.clickNextStep(); classEditPage.addField("Short Text"); // Back to Step 1 appCreatePage = classEditPage.clickPreviousStep(); appCreatePage.setApplicationName(appName); appCreatePage.waitForApplicationNamePreview(); // Test that the application wasn't created. Assert.assertFalse(appCreatePage.getContent().contains(ApplicationNameTest.APP_NAME_USED_WARNING_MESSAGE)); // Step 2 again classEditPage = appCreatePage.clickNextStep(); Assert.assertTrue(classEditPage.getContent().contains(ClassEditorTest.EMPTY_CANVAS_HINT)); classEditPage.addField("Number"); // Step 3 and back to Step 2 classEditPage = classEditPage.clickNextStep().waitUntilPageIsLoaded().clickPreviousStep(); Assert.assertFalse(classEditPage.getContent().contains(ClassEditorTest.EMPTY_CANVAS_HINT)); Assert.assertFalse(classEditPage.hasPreviousStep()); }
From source file:sernet.verinice.encryption.test.CryptoTest.java
private char[] getPassword(int length) { return RandomStringUtils.randomAscii(length).toCharArray(); }