Java Code Examples for com.google.common.base.Charsets

The following are top voted examples for showing how to use com.google.common.base.Charsets. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: flume-release-1.7.0   File: EncryptionTestUtils.java   Source Code and License 6 votes vote down vote up
public static void createKeyStore(File keyStoreFile, File keyStorePasswordFile,
                                  Map<String, File> keyAliasPassword) throws Exception {
  KeyStore ks = KeyStore.getInstance("jceks");
  ks.load(null);
  List<String> keysWithSeperatePasswords = Lists.newArrayList();
  for (String alias : keyAliasPassword.keySet()) {
    Key key = newKey();
    char[] password = null;
    File passwordFile = keyAliasPassword.get(alias);
    if (passwordFile == null) {
      password = Files.toString(keyStorePasswordFile, Charsets.UTF_8).toCharArray();
    } else {
      keysWithSeperatePasswords.add(alias);
      password = Files.toString(passwordFile, Charsets.UTF_8).toCharArray();
    }
    ks.setKeyEntry(alias, key, password, null);
  }
  char[] keyStorePassword = Files.toString(keyStorePasswordFile, Charsets.UTF_8).toCharArray();
  FileOutputStream outputStream = new FileOutputStream(keyStoreFile);
  ks.store(outputStream, keyStorePassword);
  outputStream.close();
}
 
Example 2
Project: dotwebstack-framework   File: FileConfigurationBackendTest.java   Source Code and License 6 votes vote down vote up
@Test
public void loadPrefixes_ThrowConfigurationException_FoundUnknownPrefix() throws Exception {
  // Arrange
  Resource resource = mock(Resource.class);
  when(resource.getInputStream()).thenReturn(
      new ByteArrayInputStream(new String("@prefix dbeerpedia: <http://dbeerpedia.org#> .\n"
          + "@prefix elmo: <http://dotwebstack.org/def/elmo#> .\n"
          + "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n"
          + "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n"
          + "this is not a valid prefix").getBytes(Charsets.UTF_8)));
  when(resource.getFilename()).thenReturn("_prefixes.trig");
  when(((ResourcePatternResolver) resourceLoader).getResources(any())).thenReturn(
      new Resource[] {resource});

  // Assert
  thrown.expect(ConfigurationException.class);
  thrown.expectMessage("Found unknown prefix format <this is not a valid prefix> at line <5>");

  // Act
  backend.loadResources();
}
 
Example 3
Project: flume-release-1.7.0   File: TestTimestampInterceptor.java   Source Code and License 6 votes vote down vote up
/**
 * Ensure timestamp is NOT overwritten when preserveExistingTimestamp == true
 */
@Test
public void testPreserve() throws ClassNotFoundException,
    InstantiationException, IllegalAccessException {

  Context ctx = new Context();
  ctx.put("preserveExisting", "true");

  InterceptorBuilderFactory factory = new InterceptorBuilderFactory();
  Interceptor.Builder builder = InterceptorBuilderFactory.newInstance(
      InterceptorType.TIMESTAMP.toString());
  builder.configure(ctx);
  Interceptor interceptor = builder.build();

  long originalTs = 1L;
  Event event = EventBuilder.withBody("test event", Charsets.UTF_8);
  event.getHeaders().put(Constants.TIMESTAMP, Long.toString(originalTs));
  Assert.assertEquals(Long.toString(originalTs),
      event.getHeaders().get(Constants.TIMESTAMP));

  Long now = System.currentTimeMillis();
  event = interceptor.intercept(event);
  String timestampStr = event.getHeaders().get(Constants.TIMESTAMP);
  Assert.assertNotNull(timestampStr);
  Assert.assertTrue(Long.parseLong(timestampStr) == originalTs);
}
 
Example 4
Project: hadoop   File: TestTableMapping.java   Source Code and License 6 votes vote down vote up
@Test(timeout=60000)
public void testBadFile() throws IOException {
  File mapFile = File.createTempFile(getClass().getSimpleName() +
      ".testBadFile", ".txt");
  Files.write("bad contents", mapFile, Charsets.UTF_8);
  mapFile.deleteOnExit();
  TableMapping mapping = new TableMapping();

  Configuration conf = new Configuration();
  conf.set(NET_TOPOLOGY_TABLE_MAPPING_FILE_KEY, mapFile.getCanonicalPath());
  mapping.setConf(conf);

  List<String> names = new ArrayList<String>();
  names.add(hostName1);
  names.add(hostName2);

  List<String> result = mapping.resolve(names);
  assertEquals(names.size(), result.size());
  assertEquals(result.get(0), NetworkTopology.DEFAULT_RACK);
  assertEquals(result.get(1), NetworkTopology.DEFAULT_RACK);
}
 
Example 5
Project: flume-release-1.7.0   File: TestTaildirEventReader.java   Source Code and License 6 votes vote down vote up
@Test
public void testNewLineBoundaries() throws IOException {
  File f1 = new File(tmpDir, "file1");
  Files.write("file1line1\nfile1line2\rfile1line2\nfile1line3\r\nfile1line4\n",
              f1, Charsets.UTF_8);

  ReliableTaildirEventReader reader = getReader();
  List<String> out = Lists.newArrayList();
  for (TailFile tf : reader.getTailFiles().values()) {
    out.addAll(bodiesAsStrings(reader.readEvents(tf, 5)));
    reader.commit();
  }
  assertEquals(4, out.size());
  //Should treat \n as line boundary
  assertTrue(out.contains("file1line1"));
  //Should not treat \r as line boundary
  assertTrue(out.contains("file1line2\rfile1line2"));
  //Should treat \r\n as line boundary
  assertTrue(out.contains("file1line3"));
  assertTrue(out.contains("file1line4"));
}
 
Example 6
Project: kafka-junit   File: KafkaTestUtils.java   Source Code and License 6 votes vote down vote up
/**
 * Produce randomly generated records into the defined kafka namespace.
 *
 * @param numberOfRecords how many records to produce
 * @param topicName the namespace name to produce into.
 * @param partitionId the partition to produce into.
 * @return List of ProducedKafkaRecords.
 */
public List<ProducedKafkaRecord<byte[], byte[]>> produceRecords(
    final int numberOfRecords,
    final String topicName,
    final int partitionId
) {
    Map<byte[], byte[]> keysAndValues = new HashMap<>();

    // Generate random & unique data
    for (int x = 0; x < numberOfRecords; x++) {
        // Construct key and value
        long timeStamp = Clock.systemUTC().millis();
        String key = "key" + timeStamp;
        String value = "value" + timeStamp;

        // Add to map
        keysAndValues.put(key.getBytes(Charsets.UTF_8), value.getBytes(Charsets.UTF_8));
    }

    return produceRecords(keysAndValues, topicName, partitionId);
}
 
Example 7
Project: n4js   File: EclipseBasedProjectModelSetup.java   Source Code and License 6 votes vote down vote up
private void createManifest(String projectName, String string) throws CoreException, UnsupportedEncodingException {
	IProject project = workspace.getProject(projectName);
	IFile manifestFile = project.getFile(IN4JSProject.N4MF_MANIFEST);
	@SuppressWarnings("resource")
	StringInputStream content = new StringInputStream(string, Charsets.UTF_8.name());
	manifestFile.create(content, false, null);
	manifestFile.setCharset(Charsets.UTF_8.name(), null);

	IFolder src = project.getFolder("src");
	src.create(false, true, null);
	IFolder sub = src.getFolder("sub");
	sub.create(false, true, null);
	IFolder leaf = sub.getFolder("leaf");
	leaf.create(false, true, null);
	src.getFile("A.js").create(new ByteArrayInputStream(new byte[0]), false, null);
	src.getFile("B.js").create(new ByteArrayInputStream(new byte[0]), false, null);
	sub.getFile("B.js").create(new ByteArrayInputStream(new byte[0]), false, null);
	sub.getFile("C.js").create(new ByteArrayInputStream(new byte[0]), false, null);
	leaf.getFile("D.js").create(new ByteArrayInputStream(new byte[0]), false, null);
}
 
Example 8
Project: CustomWorldGen   File: ModelBakery.java   Source Code and License 6 votes vote down vote up
private ModelBlockDefinition loadModelBlockDefinition(ResourceLocation location, IResource resource)
{
    InputStream inputstream = null;
    ModelBlockDefinition lvt_4_1_;

    try
    {
        inputstream = resource.getInputStream();
        lvt_4_1_ = ModelBlockDefinition.parseFromReader(new InputStreamReader(inputstream, Charsets.UTF_8));
    }
    catch (Exception exception)
    {
        throw new RuntimeException("Encountered an exception when loading model definition of \'" + location + "\' from: \'" + resource.getResourceLocation() + "\' in resourcepack: \'" + resource.getResourcePackName() + "\'", exception);
    }
    finally
    {
        IOUtils.closeQuietly(inputstream);
    }

    return lvt_4_1_;
}
 
Example 9
Project: tac-kbp-eal   File: PerDocResultWriter.java   Source Code and License 6 votes vote down vote up
static void writeArgPerDoc(final List<EALScorer2015Style.ArgResult> perDocResults,
    final File outFile)
    throws IOException {
  Files.asCharSink(outFile, Charsets.UTF_8).write(
      String.format("%40s\t%10s\n", "Document", "Arg") +
          Joiner.on("\n").join(
              FluentIterable.from(perDocResults)
                  .transform(new Function<EALScorer2015Style.ArgResult, String>() {
                    @Override
                    public String apply(final EALScorer2015Style.ArgResult input) {
                      return String.format("%40s\t%10.2f",
                          input.docID(),
                          100.0 * input.scaledArgumentScore());
                    }
                  })));
}
 
Example 10
Project: cmakeify   File: TestCmakeify.java   Source Code and License 6 votes vote down vote up
@Test
public void testSQLite() throws IOException {
  File yaml = new File("test-files/testScript/cmakeify.yml");
  yaml.getParentFile().mkdirs();
  Files.write("targets: [android]\n" +
          "buildTarget: sqlite\n" +
          "android:\n" +
          "  ndk:\n" +
          "    runtimes: [c++, gnustl, stlport]\n" +
          "    platforms: [12, 21]\n" +
          "example: |\n" +
          "   #include <sqlite3.h>\n" +
          "   void test() {\n" +
          "     sqlite3_initialize();\n" +
          "   }",
      yaml, StandardCharsets.UTF_8);
  main("-wf", yaml.getParent(),
      "--host", "Linux",
      "--group-id", "my-group-id",
      "--artifact-id", "my-artifact-id",
      "--target-version", "my-target-version");
  File scriptFile = new File(".cmakeify/build.sh");
  String script = Joiner.on("\n").join(Files.readLines(scriptFile, Charsets.UTF_8));
  assertThat(script).contains("cmake-3.7.2-Linux-x86_64.tar.gz");
}
 
Example 11
Project: vrap   File: BaseUriReplacer.java   Source Code and License 6 votes vote down vote up
public StringWriter preprocess(Context ctx, final Path filePath, final Api api) throws IOException {
    final Integer port = ctx.getServerConfig().getPort();
    final StringWriter stringWriter = new StringWriter();
    final String baseUri = api.baseUri().value();
    final List<SecurityScheme> oauthSchemes = api.securitySchemes().stream().filter(securityScheme -> securityScheme.type().equals("OAuth 2.0")).collect(Collectors.toList());
    String content = new String(Files.readAllBytes(filePath), Charsets.UTF_8);

    ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
    final JsonNode file = mapper.readValue(filePath.toFile(), JsonNode.class);
    if (file.has("baseUri")) {
        content = content.replaceAll(baseUri, "http://localhost:" + port.toString() + "/api");
    }

    if (!oauthSchemes.isEmpty()) {
        for (SecurityScheme scheme : oauthSchemes) {
            content = content.replaceAll(scheme.settings().accessTokenUri().value(), "http://localhost:" + port.toString() + "/auth/" + scheme.name());
        }
    }

    return stringWriter.append(content);
}
 
Example 12
Project: QDrill   File: TestDistributedFragmentRun.java   Source Code and License 6 votes vote down vote up
@Test
public void oneBitOneExchangeTwoEntryRunLogical() throws Exception{
    RemoteServiceSet serviceSet = RemoteServiceSet.getLocalServiceSet();

    try(Drillbit bit1 = new Drillbit(CONFIG, serviceSet); DrillClient client = new DrillClient(CONFIG, serviceSet.getCoordinator());){
        bit1.run();
        client.connect();
        List<QueryDataBatch> results = client.runQuery(QueryType.LOGICAL, Files.toString(FileUtils.getResourceAsFile("/scan_screen_logical.json"), Charsets.UTF_8));
        int count = 0;
        for(QueryDataBatch b : results){
            count += b.getHeader().getRowCount();
            b.release();
        }
        assertEquals(100, count);
    }


}
 
Example 13
Project: BUbiNG   File: VisitStateSetTest.java   Source Code and License 6 votes vote down vote up
@Test
public void testResize() {
	final VisitState[] visitState = new VisitState[2000];
	for(int i = visitState.length; i-- != 0;) visitState[i] = new VisitState(null, Integer.toString(i).getBytes(Charsets.ISO_8859_1));

	VisitStateSet s = new VisitStateSet();
	for(int i = 2000; i-- != 0;) assertTrue(s.add(visitState[i]));
	assertEquals(2000, s.size());
	for(int i = 2000; i-- != 0;) assertFalse(s.add(new VisitState(null, Integer.toString(i).getBytes(Charsets.ISO_8859_1))));
	for(int i = 1000; i-- != 0;) assertTrue(s.remove(visitState[i]));

	for(int i = 1000; i-- != 0;) assertFalse(s.remove(visitState[i]));
	for(int i = 1000; i-- != 0;) assertNull(s.get(Integer.toString(i).getBytes(Charsets.ISO_8859_1)));
	for(int i = 2000; i-- != 1000;) assertSame(visitState[i], s.get(Integer.toString(i).getBytes(Charsets.ISO_8859_1)));
	assertEquals(1000, s.size());
	assertFalse(s.isEmpty());
	s.clear();
	assertEquals(0, s.size());
	assertTrue(s.isEmpty());
	s.clear();
	assertEquals(0, s.size());
	assertTrue(s.isEmpty());
}
 
Example 14
Project: uavstack   File: TailFile.java   Source Code and License 6 votes vote down vote up
private String readLine() throws IOException {

        ByteArrayDataOutput out = ByteStreams.newDataOutput(300);
        int i = 0;
        int c;
        while ((c = raf.read()) != -1) {
            i++;
            out.write((byte) c);
            if (c == LINE_SEP.charAt(0)) {
                break;
            }
        }
        if (i == 0) {
            return null;
        }
        return new String(out.toByteArray(), Charsets.UTF_8);
    }
 
Example 15
Project: tools   File: FileUtil.java   Source Code and License 6 votes vote down vote up
static String readLocalAuth(String filePath) {
    final Path path = Paths.get(filePath);
    try {
        if (!Files.exists(path)) {
            LOGGER.info("authFile not found, creating it");
            final UUID uuid = UUID.randomUUID();
            final Path temp = path.getParent().resolve(path.getFileName().toString() + ".temp");
            createFileWithPermissions(temp);
            final String written = uuid.toString();
            Files.write(temp, written.getBytes(Charsets.UTF_8));
            Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE);
            return written;
        } else {
            final List<String> lines = Files.readAllLines(path);
            Preconditions.checkState(lines.size() == 1);
            final String ret = lines.get(0);
            final UUID ignored = UUID.fromString(ret);//check if this throws an exception, that might mean that
            return ret;
        }
    } catch (IOException e) {
        throw new RuntimeException("unable to read password file at " + filePath, e);
    }
}
 
Example 16
Project: hadoop-oss   File: ZKUtil.java   Source Code and License 6 votes vote down vote up
/**
 * Parse a comma-separated list of authentication mechanisms. Each
 * such mechanism should be of the form 'scheme:auth' -- the same
 * syntax used for the 'addAuth' command in the ZK CLI.
 * 
 * @param authString the comma-separated auth mechanisms
 * @return a list of parsed authentications
 * @throws {@link BadAuthFormatException} if the auth format is invalid
 */
public static List<ZKAuthInfo> parseAuth(String authString) throws
    BadAuthFormatException{
  List<ZKAuthInfo> ret = Lists.newArrayList();
  if (authString == null) {
    return ret;
  }
  
  List<String> authComps = Lists.newArrayList(
      Splitter.on(',').omitEmptyStrings().trimResults()
      .split(authString));
  
  for (String comp : authComps) {
    String parts[] = comp.split(":", 2);
    if (parts.length != 2) {
      throw new BadAuthFormatException(
          "Auth '" + comp + "' not of expected form scheme:auth");
    }
    ret.add(new ZKAuthInfo(parts[0],
        parts[1].getBytes(Charsets.UTF_8)));
  }
  return ret;
}
 
Example 17
Project: ditb   File: MemoryBoundedLogMessageBuffer.java   Source Code and License 6 votes vote down vote up
/**
 * Dump the contents of the buffer to the given stream.
 */
public synchronized void dumpTo(PrintWriter out) {
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

  for (LogMessage msg : messages) {
    out.write(df.format(new Date(msg.timestamp)));
    out.write(" ");
    out.println(new String(msg.message, Charsets.UTF_8));
  }
}
 
Example 18
Project: javaide   File: AvdManager.java   Source Code and License 6 votes vote down vote up
/**
 * Writes a .ini file from a set of properties, using UTF-8 encoding.
 * The keys are sorted.
 * The file should be read back later by {@link #parseIniFile(IAbstractFile, ILogger)}.
 *
 * @param iniFile The file to generate.
 * @param values The properties to place in the ini file.
 * @param addEncoding When true, add a property {@link #AVD_INI_ENCODING} indicating the
 *                    encoding used to write the file.
 * @throws IOException if {@link FileWriter} fails to open, write or close the file.
 */
private static void writeIniFile(File iniFile, Map<String, String> values, boolean addEncoding)
        throws IOException {

    Charset charset = Charsets.ISO_8859_1;
    OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(iniFile), charset);

    if (addEncoding) {
        // Write down the charset used in case we want to use it later.
        writer.write(String.format("%1$s=%2$s\n", AVD_INI_ENCODING, charset.name()));
    }

    ArrayList<String> keys = new ArrayList<String>(values.keySet());
    Collections.sort(keys);

    for (String key : keys) {
        String value = values.get(key);
        writer.write(String.format("%1$s=%2$s\n", key, value));
    }
    writer.close();
}
 
Example 19
Project: sonar-css-plugin   File: LessMetricsVisitorTest.java   Source Code and License 6 votes vote down vote up
private void setUp(String fileName) {
  File moduleBaseDir = new File("src/test/resources/metrics/");
  context = SensorContextTester.create(moduleBaseDir);

  DefaultInputFile inputFile = new DefaultInputFile("moduleKey", fileName)
    .setModuleBaseDir(moduleBaseDir.toPath())
    .setLanguage("less")
    .setType(InputFile.Type.MAIN);

  context.fileSystem().add(inputFile);
  context.fileSystem().setEncoding(Charsets.UTF_8);

  LessMetricsVisitor metricsVisitor = new LessMetricsVisitor(context, mock(NoSonarFilter.class));

  TreeVisitorContext treeVisitorContext = mock(TreeVisitorContext.class);
  when(treeVisitorContext.getFile()).thenReturn(inputFile.file());
  when(treeVisitorContext.getTopTree()).thenReturn(LessParser.createParser(Charsets.UTF_8).parse(inputFile.file()));

  metricsVisitor.scanTree(treeVisitorContext);
}
 
Example 20
Project: flume-release-1.7.0   File: TestResettableFileInputStream.java   Source Code and License 6 votes vote down vote up
/**
 * Ensure a reset() brings us back to the default mark (beginning of file)
 * @throws IOException
 */
@Test
public void testReset() throws IOException {
  String output = singleLineFileInit(file, Charsets.UTF_8);

  PositionTracker tracker = new DurablePositionTracker(meta, file.getPath());
  ResettableInputStream in = new ResettableFileInputStream(file, tracker);

  String result1 = readLine(in, output.length());
  assertEquals(output, result1);

  in.reset();
  String result2 = readLine(in, output.length());
  assertEquals(output, result2);

  String result3 = readLine(in, output.length());
  assertNull("Should be null: " + result3, result3);

  in.close();
}
 
Example 21
Project: tac-kbp-eal   File: GoldArgumentCountsInspector.java   Source Code and License 6 votes vote down vote up
@Override
public void finish() throws IOException {
  outputDirectory.mkdirs();
  log.info("Writing gold counts to {}", outputDirectory);
  Files.asCharSink(new File(outputDirectory, "README.goldCounts.txt"), Charsets.UTF_8).write(
      Resources.asCharSource(
          Resources.getResource(GoldArgumentCountsInspector.class, "/README.scorer.txt"),
          Charsets.UTF_8)
          .read());
  writeTsv(eventTypeCounts.build(), new File(outputDirectory, "eventTypeCounts.tsv"));
  writeTsv(eventArgumentCounts.build(), new File(outputDirectory, "eventArgumentTypeCounts.tsv"));
  writeTsv(genreCounts.build(), new File(outputDirectory, "genreCounts.tsv"));
  writeTsv(argumentMentionTypeCounts.build(), new File(outputDirectory, "mentionTypeCounts.tsv"));
  genreExtractor.logDocIdsWithUndeterminedGenre(
      Files.asCharSink(new File(outputDirectory, "undetermined_genre.txt"), Charsets.UTF_8));
}
 
Example 22
Project: SurvivalMMO   File: Packet.java   Source Code and License 5 votes vote down vote up
public static String readUTF8(ByteBuf in) {
    ByteBuf buffer = in.alloc().buffer();
    byte b;
    while (in.readableBytes() > 0 && (b = in.readByte()) != 0) {
        buffer.writeByte(b);
    }

    return buffer.toString(Charsets.UTF_8);
}
 
Example 23
Project: googles-monorepo-demo   File: PackageSanityTests.java   Source Code and License 5 votes vote down vote up
public PackageSanityTests() {
  setDefault(BaseEncoding.class, BaseEncoding.base64());
  setDefault(int.class, 32);
  setDefault(String.class, "abcd");
  setDefault(Method.class, AbstractPackageSanityTests.class.getDeclaredMethods()[0]);
  setDefault(MapMode.class, MapMode.READ_ONLY);
  setDefault(CharsetEncoder.class, Charsets.UTF_8.newEncoder());
}
 
Example 24
Project: googles-monorepo-demo   File: SourceSinkFactories.java   Source Code and License 5 votes vote down vote up
public static CharSinkFactory asCharSinkFactory(final ByteSinkFactory factory) {
  checkNotNull(factory);
  return new CharSinkFactory() {
    @Override
    public CharSink createSink() throws IOException {
      return factory.createSink().asCharSink(Charsets.UTF_8);
    }

    @Override
    public String getSinkContents() throws IOException {
      return new String(factory.getSinkContents(), Charsets.UTF_8);
    }

    @Override
    public String getExpected(String data) {
      /*
       * Get what the byte sink factory would expect for no written bytes, then append expected
       * string to that.
       */
      byte[] factoryExpectedForNothing = factory.getExpected(new byte[0]);
      return new String(factoryExpectedForNothing, Charsets.UTF_8) + checkNotNull(data);
    }

    @Override
    public void tearDown() throws IOException {
      factory.tearDown();
    }
  };
}
 
Example 25
Project: flume-release-1.7.0   File: TestTaildirEventReader.java   Source Code and License 5 votes vote down vote up
@Test
// Test a basic case where a commit is missed.
public void testBasicCommitFailure() throws IOException {
  File f1 = new File(tmpDir, "file1");
  Files.write("file1line1\nfile1line2\nfile1line3\nfile1line4\n" +
              "file1line5\nfile1line6\nfile1line7\nfile1line8\n" +
              "file1line9\nfile1line10\nfile1line11\nfile1line12\n",
              f1, Charsets.UTF_8);

  ReliableTaildirEventReader reader = getReader();
  List<String> out1 = null;
  for (TailFile tf : reader.getTailFiles().values()) {
    out1 = bodiesAsStrings(reader.readEvents(tf, 4));
  }
  assertTrue(out1.contains("file1line1"));
  assertTrue(out1.contains("file1line2"));
  assertTrue(out1.contains("file1line3"));
  assertTrue(out1.contains("file1line4"));

  List<String> out2 = bodiesAsStrings(reader.readEvents(4));
  assertTrue(out2.contains("file1line1"));
  assertTrue(out2.contains("file1line2"));
  assertTrue(out2.contains("file1line3"));
  assertTrue(out2.contains("file1line4"));

  reader.commit();
  List<String> out3 = bodiesAsStrings(reader.readEvents(4));
  assertTrue(out3.contains("file1line5"));
  assertTrue(out3.contains("file1line6"));
  assertTrue(out3.contains("file1line7"));
  assertTrue(out3.contains("file1line8"));

  reader.commit();
  List<String> out4 = bodiesAsStrings(reader.readEvents(4));
  assertEquals(4, out4.size());
  assertTrue(out4.contains("file1line9"));
  assertTrue(out4.contains("file1line10"));
  assertTrue(out4.contains("file1line11"));
  assertTrue(out4.contains("file1line12"));
}
 
Example 26
Project: googles-monorepo-demo   File: SourceSinkFactories.java   Source Code and License 5 votes vote down vote up
@Override
public String getSinkContents() throws IOException {
  Path file = getPath();
  try (Reader reader = java.nio.file.Files.newBufferedReader(file, Charsets.UTF_8)) {
    StringBuilder builder = new StringBuilder();
    for (int c = reader.read(); c != -1; c = reader.read()) {
      builder.append((char) c);
    }
    return builder.toString();
  }
}
 
Example 27
Project: googles-monorepo-demo   File: FunnelsTest.java   Source Code and License 5 votes vote down vote up
public void testEquals() {
   new EqualsTester()
     .addEqualityGroup(Funnels.byteArrayFunnel())
     .addEqualityGroup(Funnels.integerFunnel())
     .addEqualityGroup(Funnels.longFunnel())
     .addEqualityGroup(Funnels.unencodedCharsFunnel())
     .addEqualityGroup(Funnels.stringFunnel(Charsets.UTF_8))
     .addEqualityGroup(Funnels.stringFunnel(Charsets.US_ASCII))
     .addEqualityGroup(Funnels.sequentialFunnel(Funnels.integerFunnel()),
         SerializableTester.reserialize(Funnels.sequentialFunnel(
             Funnels.integerFunnel())))
     .addEqualityGroup(Funnels.sequentialFunnel(Funnels.longFunnel()))
     .testEquals();
}
 
Example 28
Project: guava-mock   File: PackageSanityTests.java   Source Code and License 5 votes vote down vote up
public PackageSanityTests() {
  setDefault(BaseEncoding.class, BaseEncoding.base64());
  setDefault(int.class, 32);
  setDefault(String.class, "abcd");
  setDefault(Method.class, AbstractPackageSanityTests.class.getDeclaredMethods()[0]);
  setDefault(MapMode.class, MapMode.READ_ONLY);
  setDefault(CharsetEncoder.class, Charsets.UTF_8.newEncoder());
}
 
Example 29
Project: dremio-oss   File: Distributions.java   Source Code and License 5 votes vote down vote up
public static synchronized Distributions getDefaultDistributions()
{
    if (DEFAULT_DISTRIBUTIONS == null) {
        try {
            URL resource = Resources.getResource("dists.dss");
            checkState(resource != null, "Distribution file 'dists.dss' not found");
            DEFAULT_DISTRIBUTIONS = new Distributions(loadDistribution(Resources.asCharSource(resource, Charsets.UTF_8)));
        }
        catch (IOException e) {
            throw Throwables.propagate(e);
        }
    }
    return DEFAULT_DISTRIBUTIONS;
}
 
Example 30
Project: okwallet   File: WalletUtils.java   Source Code and License 5 votes vote down vote up
public static Wallet restorePrivateKeysFromBase58(final InputStream is,
        final NetworkParameters expectedNetworkParameters) throws IOException {
    final BufferedReader keyReader = new BufferedReader(new InputStreamReader(is, Charsets.UTF_8));

    // create non-HD wallet
    final KeyChainGroup group = new KeyChainGroup(expectedNetworkParameters);
    group.importKeys(WalletUtils.readKeys(keyReader, expectedNetworkParameters));
    return new Wallet(expectedNetworkParameters, group);
}
 
Example 31
Project: MooProject   File: NetworkBus.java   Source Code and License 5 votes vote down vote up
/**
 * Processes the packet (last step before sending)
 *
 * @param channel   The channel to send the packet to
 * @param packet    The packet to be sent
 * @param callbacks Callback after receiving a respond
 */
public synchronized void processOut(Channel channel, AbstractPacket packet, Consumer<AbstractPacket>... callbacks) {
    if(channel == null) {
        return;
    }

    // send time and identifier
    packet.setStamp(System.currentTimeMillis());
    if(packet.getQueryUid() == null) {
        packet.setQueryUid(UUID.nameUUIDFromBytes(("Time:" + System.nanoTime()).getBytes(Charsets.UTF_8)));
    }
    channel.writeAndFlush(packet);

    // releases the packetbuf
    try {
        packet.getBuf().release();
    }
    catch(Exception e) {
        //
    }

    // callbacks
    if(callbacks.length != 0) {
        handle.getCallbacks().put(packet.getQueryUid(), new ArrayList<>(Arrays.asList(callbacks)));
    }

    // call handler event
    handle.callEvent(adapter -> adapter.onPacketSend(packet));

    // packet content
    /*String content = (packet instanceof PacketRespond ? " {" + packet.toString().split("\"payload\": ")[1] : "");
    String id = (packet.getQueryUid() + "").substring(0, 2);
    id = ConsoleColor.translateLowSpectrum('&', "&" + (id.substring(0, 1))) + id + ConsoleColor.RESET;

    handle.getLogger().info("[Outgoing " + id + "] '" + packet.getName() + "'" + content);*/
}
 
Example 32
Project: open-kilda   File: FileUtil.java   Source Code and License 5 votes vote down vote up
/**
 * @return the actual number, or -1 if there was an exception.
 */
public int numLines() {
    int result = -1;

    try {
        result = Files.readLines(getFile(), Charsets.UTF_8).size();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;
}
 
Example 33
Project: okwallet   File: CryptoTest.java   Source Code and License 5 votes vote down vote up
private String readBackupFromResource(final String filename) throws IOException {
    final BufferedReader reader = new BufferedReader(
            new InputStreamReader(getClass().getResourceAsStream(filename), Charsets.UTF_8));
    final StringBuilder backup = new StringBuilder();
    Io.copy(reader, backup);
    reader.close();

    return backup.toString();
}
 
Example 34
Project: flume-release-1.7.0   File: TestSpoolingFileLineReader.java   Source Code and License 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testDestinationExistsAndDifferentFile() throws IOException {
  File f1 = new File(tmpDir.getAbsolutePath() + "/file1");
  File f1Completed =
      new File(tmpDir.getAbsolutePath() + "/file1" + completedSuffix);

  Files.write("file1line1\nfile1line2\n", f1, Charsets.UTF_8);
  Files.write("file1line1\nfile1XXXe2\n", f1Completed, Charsets.UTF_8);

  ReliableSpoolingFileEventReader parser = getParser();

  List<String> out = Lists.newArrayList();

  for (int i = 0; i < 2; i++) {
    out.add(bodyAsString(parser.readEvent()));
    parser.commit();
  }

  File f2 = new File(tmpDir.getAbsolutePath() + "/file2");
  Files.write("file2line1\nfile2line2\n", f2, Charsets.UTF_8);

  for (int i = 0; i < 2; i++) {
    out.add(bodyAsString(parser.readEvent()));
    parser.commit();
  }
  // Not reached
}
 
Example 35
Project: guava-mock   File: FilesTest.java   Source Code and License 5 votes vote down vote up
public void testReadFile_withCorrectSize() throws IOException {
  File asciiFile = getTestFile("ascii.txt");

  Closer closer = Closer.create();
  try {
    InputStream in = closer.register(new FileInputStream(asciiFile));
    byte[] bytes = Files.readFile(in, asciiFile.length());
    assertTrue(Arrays.equals(ASCII.getBytes(Charsets.US_ASCII), bytes));
  } catch (Throwable e) {
    throw closer.rethrow(e);
  } finally {
    closer.close();
  }
}
 
Example 36
Project: flume-release-1.7.0   File: TestSearchAndReplaceInterceptor.java   Source Code and License 5 votes vote down vote up
private void testSearchReplace(Context context, String input, String output)
    throws Exception {
  Interceptor.Builder builder = InterceptorBuilderFactory.newInstance(
      InterceptorType.SEARCH_REPLACE.toString());
  builder.configure(context);
  Interceptor interceptor = builder.build();

  Event event = EventBuilder.withBody(input, Charsets.UTF_8);
  event = interceptor.intercept(event);
  String val = new String(event.getBody(), Charsets.UTF_8);
  assertEquals(output, val);
  logger.info(val);
}
 
Example 37
Project: n4js   File: FileBasedProjectModelSetup.java   Source Code and License 5 votes vote down vote up
private void createArchive(URI baseDir) throws IOException {
	File directory = new File(java.net.URI.create(baseDir.toString()));
	File lib = new File(directory, "lib");
	assertTrue(lib.mkdir());
	File nfar = new File(lib, host.archiveProjectId + ".nfar");
	final ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(nfar));
	zipOutputStream.putNextEntry(new ZipEntry("src/A.js"));
	zipOutputStream.putNextEntry(new ZipEntry("src/B.js"));
	zipOutputStream.putNextEntry(new ZipEntry("src/sub/B.js"));
	zipOutputStream.putNextEntry(new ZipEntry("src/sub/C.js"));
	zipOutputStream.putNextEntry(new ZipEntry("src/sub/leaf/D.js"));

	zipOutputStream.putNextEntry(new ZipEntry(IN4JSProject.N4MF_MANIFEST));
	// this will close the stream
	CharStreams.write("ProjectId: " + host.archiveProjectId + "\n" +
			"ProjectType: library\n" +
			"ProjectVersion: 0.0.1-SNAPSHOT\n" +
			"VendorId: org.eclipse.n4js\n" +
			"VendorName: \"Eclipse N4JS Project\"\n" +
			"Output: \"src-gen\"\n" +
			"Sources {\n" +
			"	source {" +
			"		\"src\"\n" +
			"	}\n" +
			"}",
			CharStreams.newWriterSupplier(new OutputSupplier<ZipOutputStream>() {
				@Override
				public ZipOutputStream getOutput() throws IOException {
					return zipOutputStream;
				}
			}, Charsets.UTF_8));
	host.setArchiveFileURI(URI.createURI(nfar.toURI().toString()));
}
 
Example 38
Project: QDrill   File: TestReverseImplicitCast.java   Source Code and License 5 votes vote down vote up
@Test
public void twoWayCast(@Injectable final DrillbitContext bitContext,
                       @Injectable UserServer.UserClientConnection connection) throws Throwable {

  // Function checks for casting from Float, Double to Decimal data types
  try (RemoteServiceSet serviceSet = RemoteServiceSet.getLocalServiceSet();
       Drillbit bit = new Drillbit(CONFIG, serviceSet);
       DrillClient client = new DrillClient(CONFIG, serviceSet.getCoordinator())) {

    // run query.
    bit.run();
    client.connect();
    List<QueryDataBatch> results = client.runQuery(org.apache.drill.exec.proto.UserBitShared.QueryType.PHYSICAL,
        Files.toString(FileUtils.getResourceAsFile("/functions/cast/two_way_implicit_cast.json"), Charsets.UTF_8));

    RecordBatchLoader batchLoader = new RecordBatchLoader(bit.getContext().getAllocator());

    QueryDataBatch batch = results.get(0);
    assertTrue(batchLoader.load(batch.getHeader().getDef(), batch.getData()));

    Iterator<VectorWrapper<?>> itr = batchLoader.iterator();

    ValueVector.Accessor intAccessor1 = itr.next().getValueVector().getAccessor();
    ValueVector.Accessor varcharAccessor1 = itr.next().getValueVector().getAccessor();

    for (int i = 0; i < intAccessor1.getValueCount(); i++) {
      System.out.println(intAccessor1.getObject(i));
      assertEquals(intAccessor1.getObject(i), 10);
      System.out.println(varcharAccessor1.getObject(i));
      assertEquals(varcharAccessor1.getObject(i).toString(), "101");
    }

    batchLoader.clear();
    for (QueryDataBatch result : results) {
      result.release();
    }
  }
}
 
Example 39
Project: QDrill   File: ValueHolderHelper.java   Source Code and License 5 votes vote down vote up
public static VarCharHolder getVarCharHolder(BufferAllocator a, String s){
  VarCharHolder vch = new VarCharHolder();

  byte[] b = s.getBytes(Charsets.UTF_8);
  vch.start = 0;
  vch.end = b.length;
  vch.buffer = a.buffer(b.length); //
  vch.buffer.setBytes(0, b);
  return vch;
}
 
Example 40
Project: dotwebstack-framework   File: FileConfigurationBackendTest.java   Source Code and License 5 votes vote down vote up
@Test
public void loadResources_LoadsDefaultElmoFile_WhenElmoFileIsPresent() throws Exception {
  // Arrange
  Resource resource = mock(Resource.class);
  when(resource.getInputStream()).thenReturn(new ByteArrayInputStream(
      "@prefix dbeerpedia: <http://dbeerpedia.org#> .".getBytes(Charsets.UTF_8)));
  when(resource.getFilename()).thenReturn("config.trig");
  when(((ResourcePatternResolver) resourceLoader).getResources(any())).thenReturn(
      new Resource[] {resource});
  when(elmoConfigurationResource.getInputStream()).thenReturn(new ByteArrayInputStream(
      "@prefix dbeerpedia: <http://dbeerpedia.org#> .".getBytes(Charsets.UTF_8)));
  when(elmoConfigurationResource.getFilename()).thenReturn("elmo.trig");

  // Act
  backend.loadResources();

  // Assert
  verify(elmoConfigurationResource, atLeastOnce()).getInputStream();
  ArgumentCaptor<InputStream> captor = ArgumentCaptor.forClass(InputStream.class);
  verify(repositoryConnection, times(2)).add(captor.capture(), any(), any());
  List<InputStream> inputStreams = captor.getAllValues();
  List<String> fileContents = inputStreams.stream().map(stream -> {
    try {
      return CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
    } catch (IOException e) {
      fail(e.getMessage());
      return null;
    }
  }).collect(Collectors.toList());
  assertThat(fileContents, hasItems("@prefix dbeerpedia: <http://dbeerpedia.org#> .",
      "@prefix dbeerpedia: <http://dbeerpedia.org#> ."));
}
 
Example 41
Project: n4js   File: OutputStreamPrinterThread.java   Source Code and License 5 votes vote down vote up
@Override
public String toString() {
	try {
		return baos.toString(Charsets.UTF_8.name());
	} catch (UnsupportedEncodingException e) {
		return Throwables.getStackTraceAsString(e);
	}
}
 
Example 42
Project: tac-kbp-eal   File: CorpusScorer.java   Source Code and License 5 votes vote down vote up
static void trueMain(Parameters params) throws IOException {
  log.info(params.dump());
  final File outputDir = params.getCreatableDirectory("com.bbn.tac.eal.outputDir");
  final File queryFile = params.getExistingFile("com.bbn.tac.eal.queryFile");
  final File queryResponseAssessmentsFile =
      params.getExistingFile("com.bbn.tac.eal.queryAssessmentsFile");
  final CorpusQueryAssessments queryAssessments =
      SingleFileQueryAssessmentsLoader.create().loadFrom(
          Files.asCharSource(queryResponseAssessmentsFile, Charsets.UTF_8));
  final CorpusQuerySet2016 queries = DefaultCorpusQueryLoader.create().loadQueries(
      Files.asCharSource(queryFile, Charsets.UTF_8));
  final boolean ignoreJustifications = params.getBoolean("com.bbn.tac.eal.ignoreJustifications");
  final boolean allowUnassessed = params.getBoolean("com.bbn.tac.eal.allowUnassessed");
  final boolean skipQueriesWithNoCorrectAnswers = params.getBoolean("com.bbn.tac.eal.skipQueriesWithNoCorrectAnswers");
  final CorpusQueryAssessments justifiedForScoring;
  if (ignoreJustifications) {
    justifiedForScoring = queryAssessments.withNeutralizedJustifications();
  } else {
    justifiedForScoring = queryAssessments;
  }
  final CorpusQueryAssessments assessedForScoring;
  if(allowUnassessed) {
    assessedForScoring = justifiedForScoring.filterForAssessment(ImmutableSet.copyOf(QueryAssessment2016.values()));
  } else {
    assessedForScoring = justifiedForScoring;
  }
  log.info("Scoring output will be written to {}", outputDir);

  final ImmutableSet<Symbol> systemsToScore =
      ImmutableSet.copyOf(params.getSymbolSet("com.bbn.tac.eal.systemsToScore"));
  log.info("loaded {} assessed queries", justifiedForScoring.assessments().size());
  for (final Symbol system : systemsToScore) {
    log.info("Processing {}", system);
    score(queries, assessedForScoring, system, skipQueriesWithNoCorrectAnswers,
        new File(outputDir, system.asString()));
  }
}
 
Example 43
Project: sonar-onec   File: Assertions.java   Source Code and License 5 votes vote down vote up
public static ParserAssert assertThat(LexerlessGrammarBuilder b, GrammarRuleKey rule) {
    return new ParserAssert(new ActionParser<Tree>(
            Charsets.UTF_8,
            b,
            OneCGrammar.class,
            new TreeFactory(),
            new OneCNodeBuilder(),
            rule));
}
 
Example 44
Project: QDrill   File: TestOptiqPlans.java   Source Code and License 5 votes vote down vote up
@Test
public void testJoinPlan() throws Exception {
  final RemoteServiceSet serviceSet = RemoteServiceSet.getLocalServiceSet();

  try (final Drillbit bit1 = new Drillbit(config, serviceSet);
      final DrillClient client = new DrillClient(config, serviceSet.getCoordinator());) {
    bit1.run();
    client.connect();
    final List<QueryDataBatch> results = client.runQuery(org.apache.drill.exec.proto.UserBitShared.QueryType.PHYSICAL,
        Resources.toString(Resources.getResource("physical_join.json"), Charsets.UTF_8));
    final RecordBatchLoader loader = new RecordBatchLoader(bit1.getContext().getAllocator());
    for (final QueryDataBatch b : results) {
      System.out.println(String.format("Got %d results", b.getHeader().getRowCount()));
      loader.load(b.getHeader().getDef(), b.getData());
      for (final VectorWrapper<?> vw : loader) {
        System.out.println(vw.getValueVector().getField().toExpr());
        final ValueVector vv = vw.getValueVector();
        for (int i = 0; i < vv.getAccessor().getValueCount(); i++) {
          final Object o = vv.getAccessor().getObject(i);
          System.out.println(vv.getAccessor().getObject(i));
        }
      }
      loader.clear();
      b.release();
    }
    client.close();
  }
}
 
Example 45
Project: flume-release-1.7.0   File: TestResettableFileInputStream.java   Source Code and License 5 votes vote down vote up
private void generateUtf8NonUnicodeSequence(OutputStream out)
    throws IOException {
  out.write("NonUnicode: (".getBytes(Charsets.UTF_8));
  // This is a valid 5-octet sequence but is not Unicode
  out.write(new byte[]{(byte) 0xf8, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1,
      (byte) 0xa1});
  out.write(")\n".getBytes(Charsets.UTF_8));
}
 
Example 46
Project: guava-mock   File: ByteStreamsTest.java   Source Code and License 5 votes vote down vote up
public void testNewDataInput_readLine() {
  ByteArrayDataInput in = ByteStreams.newDataInput(
      "This is a line\r\nThis too\rand this\nand also this".getBytes(Charsets.UTF_8));
  assertEquals("This is a line", in.readLine());
  assertEquals("This too", in.readLine());
  assertEquals("and this", in.readLine());
  assertEquals("and also this", in.readLine());
}
 
Example 47
Project: guava-mock   File: ByteStreamsTest.java   Source Code and License 5 votes vote down vote up
public void testNewDataInput_readUTF() {
  byte[] data = new byte[17];
  data[1] = 15;
  System.arraycopy("Kilroy was here".getBytes(Charsets.UTF_8), 0, data, 2, 15);
  ByteArrayDataInput in = ByteStreams.newDataInput(data);
  assertEquals("Kilroy was here", in.readUTF());
}
 
Example 48
Project: hadoop   File: TestDFSHAAdminMiniCluster.java   Source Code and License 5 votes vote down vote up
private int runTool(String ... args) throws Exception {
  errOutBytes.reset();
  LOG.info("Running: DFSHAAdmin " + Joiner.on(" ").join(args));
  int ret = tool.run(args);
  errOutput = new String(errOutBytes.toByteArray(), Charsets.UTF_8);
  LOG.info("Output:\n" + errOutput);
  return ret;
}
 
Example 49
Project: VirtualTool   File: Tools.java   Source Code and License 5 votes vote down vote up
public static DataContainer deSerializeJson(String json) {
    try (StringReader reader = new StringReader(new String(Base64.getDecoder().decode(json), Charsets.UTF_8))) {
        return DataTranslators.CONFIGURATION_NODE.translate(HoconConfigurationLoader.builder().setSource(() -> new BufferedReader(reader)).build().load()).getContainer();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 50
Project: JavaNRPC   File: EtcdClient.java   Source Code and License 5 votes vote down vote up
private EtcdResult put(String key, List<BasicNameValuePair> data, Map<String, String> params, int[] expectedHttpStatusCodes,
            int... expectedErrorCodes) throws EtcdClientException {
    URI uri = buildUriWithKeyAndParams(key, params);
    HttpPut httpPut = new HttpPut(uri);

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(data, Charsets.UTF_8);
    httpPut.setEntity(entity);

    return syncExecute(httpPut, expectedHttpStatusCodes, expectedErrorCodes);
}
 
Example 51
Project: Re-Collector   File: ChunkProcessorTest.java   Source Code and License 5 votes vote down vote up
private ChunkProcessor setupProcessor(Path logFile, Buffer buffer) {
    final LinkedBlockingQueue<FileChunk> chunkQueue = new LinkedBlockingQueue<>();

    final MessageBuilder messageBuilder = new MessageBuilder().input("input-id").outputs(new HashSet<String>()).source("test");
    final ChunkProcessor processor = new ChunkProcessor(buffer, messageBuilder, chunkQueue, new NewlineChunkSplitter(), Charsets.UTF_8);

    return processor;
}
 
Example 52
Project: dremio-oss   File: TestDecimal.java   Source Code and License 5 votes vote down vote up
@Test
@Ignore("decimal")
public void testSimpleDecimalMathFunc() throws Exception {

  try (ClusterCoordinator clusterCoordinator = LocalClusterCoordinator.newRunningCoordinator();
       SabotNode bit = new SabotNode(DEFAULT_SABOT_CONFIG, clusterCoordinator, CLASSPATH_SCAN_RESULT);
       DremioClient client = new DremioClient(DEFAULT_SABOT_CONFIG, clusterCoordinator)) {

    // run query.
    bit.run();
    client.connect();
    List<QueryDataBatch> results = client.runQuery(com.dremio.exec.proto.UserBitShared.QueryType.PHYSICAL,
        Files.toString(FileUtils.getResourceAsFile("/decimal/simple_decimal_math.json"), Charsets.UTF_8)
            .replace("#{TEST_FILE}", "/input_simple_decimal.json")
    );

    RecordBatchLoader batchLoader = new RecordBatchLoader(bit.getContext().getAllocator());

    QueryDataBatch batch = results.get(0);
    assertTrue(batchLoader.load(batch.getHeader().getDef(), batch.getData()));

    Iterator<VectorWrapper<?>> itr = batchLoader.iterator();

    // Check the output of decimal18
    ValueVector.Accessor dec18Accessor = itr.next().getValueVector().getAccessor();

    assertEquals(6, dec18Accessor.getValueCount());

    batchLoader.clear();
    for (QueryDataBatch result : results) {
      result.release();
    }
  }
}
 
Example 53
Project: googles-monorepo-demo   File: ArbitraryInstancesTest.java   Source Code and License 5 votes vote down vote up
public void testGet_primitives() {
  assertNull(ArbitraryInstances.get(void.class));
  assertNull(ArbitraryInstances.get(Void.class));
  assertEquals(Boolean.FALSE, ArbitraryInstances.get(boolean.class));
  assertEquals(Boolean.FALSE, ArbitraryInstances.get(Boolean.class));
  assertEquals(Character.valueOf('\0'), ArbitraryInstances.get(char.class));
  assertEquals(Character.valueOf('\0'), ArbitraryInstances.get(Character.class));
  assertEquals(Byte.valueOf((byte) 0), ArbitraryInstances.get(byte.class));
  assertEquals(Byte.valueOf((byte) 0), ArbitraryInstances.get(Byte.class));
  assertEquals(Short.valueOf((short) 0), ArbitraryInstances.get(short.class));
  assertEquals(Short.valueOf((short) 0), ArbitraryInstances.get(Short.class));
  assertEquals(Integer.valueOf(0), ArbitraryInstances.get(int.class));
  assertEquals(Integer.valueOf(0), ArbitraryInstances.get(Integer.class));
  assertEquals(Long.valueOf(0), ArbitraryInstances.get(long.class));
  assertEquals(Long.valueOf(0), ArbitraryInstances.get(Long.class));
  assertEquals(Float.valueOf(0), ArbitraryInstances.get(float.class));
  assertEquals(Float.valueOf(0), ArbitraryInstances.get(Float.class));
  assertEquals(Double.valueOf(0), ArbitraryInstances.get(double.class));
  assertEquals(Double.valueOf(0), ArbitraryInstances.get(Double.class));
  assertEquals(UnsignedInteger.ZERO, ArbitraryInstances.get(UnsignedInteger.class));
  assertEquals(UnsignedLong.ZERO, ArbitraryInstances.get(UnsignedLong.class));
  assertEquals(0, ArbitraryInstances.get(BigDecimal.class).intValue());
  assertEquals(0, ArbitraryInstances.get(BigInteger.class).intValue());
  assertEquals("", ArbitraryInstances.get(String.class));
  assertEquals("", ArbitraryInstances.get(CharSequence.class));
  assertEquals(TimeUnit.SECONDS, ArbitraryInstances.get(TimeUnit.class));
  assertNotNull(ArbitraryInstances.get(Object.class));
  assertEquals(0, ArbitraryInstances.get(Number.class));
  assertEquals(Charsets.UTF_8, ArbitraryInstances.get(Charset.class));
}
 
Example 54
Project: QDrill   File: TestQueriesOnLargeFile.java   Source Code and License 5 votes vote down vote up
@Test
public void testMergingReceiver() throws Exception {
  String plan = Files.toString(FileUtils.getResourceAsFile("/largefiles/merging_receiver_large_data.json"),
      Charsets.UTF_8).replace("#{TEST_FILE}", escapeJsonString(dataFile.getPath()));
  List<QueryDataBatch> results = testPhysicalWithResults(plan);

  int recordsInOutput = 0;
  for(QueryDataBatch batch : results) {
    recordsInOutput += batch.getHeader().getDef().getRecordCount();
    batch.release();
  }

  assertTrue(String.format("Number of records in output is wrong: expected=%d, actual=%s",
      NUM_RECORDS, recordsInOutput), NUM_RECORDS == recordsInOutput);
}
 
Example 55
Project: Equella   File: SrwServiceImpl.java   Source Code and License 5 votes vote down vote up
@Inject
public void setXsltService(XsltService xsltService)
{
	this.xsltService = xsltService;
	try
	{
		this.xsltService
			.cacheXslt(Resources.toString(getClass().getResource("MARC21slim2MODS3-3.xsl"), Charsets.UTF_8));
	}
	catch( Exception e )
	{
		throw new RuntimeException(e);
	}
}
 
Example 56
Project: guava-mock   File: ByteStreamsTest.java   Source Code and License 5 votes vote down vote up
public void testNewDataOutput_writeUTF() {
  ByteArrayDataOutput out = ByteStreams.newDataOutput();
  out.writeUTF("r\u00C9sum\u00C9");
  byte[] expected ="r\u00C9sum\u00C9".getBytes(Charsets.UTF_8);
  byte[] actual = out.toByteArray();
  // writeUTF writes the length of the string in 2 bytes
  assertEquals(0, actual[0]);
  assertEquals(expected.length, actual[1]);
  assertEquals(expected, Arrays.copyOfRange(actual, 2, actual.length));
}
 
Example 57
Project: dotwebstack-framework   File: FileConfigurationBackendTest.java   Source Code and License 5 votes vote down vote up
public void loadPrefixes_CombinePrefixesWithConfiguration_WhenLoadResources() throws Exception {
  // Arrange
  Resource backendResource = mock(Resource.class);
  when(backendResource.getFilename()).thenReturn("backend.trig");
  when(backendResource.getInputStream()).thenReturn(new ByteArrayInputStream(
      new String("@prefix dbeerpedia: <http://dbeerpedia.org#> .").getBytes(Charsets.UTF_8)));
  Resource resource = mock(Resource.class);
  when(resource.getInputStream()).thenReturn(new ByteArrayInputStream(
      "@prefix dbeerpedia: <http://dbeerpedia.org#> .".getBytes(Charsets.UTF_8)));
  when(resource.getFilename()).thenReturn("temp.trig");
  when(prefixesResource.getFilename()).thenReturn("_prefixes.trig");
  when(prefixesResource.getInputStream()).thenReturn(
      new ByteArrayInputStream(new String("@prefix dbeerpedia: <http://dbeerpedia.org#> .\n"
          + "@prefix elmo: <http://dotwebstack.org/def/elmo#> .\n"
          + "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n"
          + "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n").getBytes(Charsets.UTF_8)));
  when(backendResource.getFilename()).thenReturn("backend.trig");
  when(backendResource.getInputStream()).thenReturn(new ByteArrayInputStream(
      new String("GRAPH dbeerpedia:Theatre {\n" + "  dbeerpedia:Backend a elmo:SparqlBackend;\n"
          + "    elmo:endpoint \"http://localhost:8900/sparql\"^^xsd:anyURI;\n" + "  .\n"
          + "}").getBytes(Charsets.UTF_8)));
  when(((ResourcePatternResolver) resourceLoader).getResources(any())).thenReturn(
      new Resource[] {prefixesResource, backendResource, resource, elmoShapesResource});

  // Act / Assert
  backend.loadResources();
}
 
Example 58
Project: crawling-framework   File: HttpSourceTestCSVUtils.java   Source Code and License 5 votes vote down vote up
public static HttpSourceTest mapCsvRowToHttpSourceTest(String[] row, Map<String, Integer> columnIndexes) {
    HttpSourceTest hst = new HttpSourceTest();
    hst.setUrl(Strings.emptyToNull(row[columnIndexes.get("url")]));
    hst.setSource(Strings.emptyToNull(row[columnIndexes.get("source")]));
    hst.setHtml(new String(BaseEncoding.base64().decode(row[columnIndexes.get("html")]), Charsets.UTF_8));
    hst.setUrlAccepted(Boolean.parseBoolean(row[columnIndexes.get("url_accepted")]));
    hst.setTitle(Strings.emptyToNull(row[columnIndexes.get("title")]));
    hst.setText(Strings.emptyToNull(row[columnIndexes.get("text")]));
    hst.setDate(Strings.emptyToNull(row[columnIndexes.get("date")]));
    return hst;
}
 
Example 59
Project: Backmemed   File: PacketBuffer.java   Source Code and License 5 votes vote down vote up
public PacketBuffer writeString(String string)
{
    byte[] abyte = string.getBytes(Charsets.UTF_8);

    if (abyte.length > 32767)
    {
        throw new EncoderException("String too big (was " + abyte.length + " bytes encoded, max " + 32767 + ")");
    }
    else
    {
        this.writeVarIntToBuffer(abyte.length);
        this.writeBytes(abyte);
        return this;
    }
}
 
Example 60
Project: FlexMC   File: MessageClientLoginListener.java   Source Code and License 5 votes vote down vote up
private String tryLogin( ConnectionHandler connectionHandler, MessageC00LoginStart message ) {
    if ( connectionHandler.getVersion() > 338 ) {
        return "Outdated Server. Please use 1.12.1";
    }
    if ( connectionHandler.getVersion() < 338 ) {
        return "Outdated Client. Please use 1.12.1";
    }
    if ( connectionHandler.getVerifySession() != null ) {
        return "Already logging in";
    }
    if ( !Flex.getServer().isRunning() || Flex.getServer().getWorld() == null || !Flex.getServer().getWorld().isGenerated() ) {
        return "Server ist still starting up";
    }
    AsyncPlayerPreLoginEvent event = EventFactory.call( new AsyncPlayerPreLoginEvent( message.getName(), connectionHandler.getSocketAddress().getAddress(), UUID.randomUUID() ) );

    if( event.getLoginResult() != AsyncPlayerPreLoginEvent.Result.ALLOWED ) {
        return event.getKickMessage() == null ? "null" : event.getKickMessage();
    }

    if ( Flex.getServer().getConfig().isVerifyUsers() ) {
        VerifySession verifySession = new VerifySession( AuthHelper.nextToken(), message.getName() );
        connectionHandler.setVerifySession( verifySession );
        MessageS01EncryptionRequest encryptionRequest = new MessageS01EncryptionRequest( "", Flex.getServer().getKeyPair().getPublic().getEncoded(), verifySession.getToken() );
        connectionHandler.sendMessage( encryptionRequest );
        verifySession.getState().incrementAndGet();
    } else {
        connectionHandler.loginSuccess( message.getName(), UUID.nameUUIDFromBytes( ( "OfflinePlayer:" + message.getName() ).getBytes( Charsets.UTF_8 ) ) );
    }
    return null;
}