use of java.util.Formatter in project camel by apache.
the class MongoDbOperationsTest method testUpdateFromString.
@Test
public void testUpdateFromString() throws Exception {
// Prepare test
assertEquals(0, testCollection.count());
for (int i = 1; i <= 100; i++) {
String body = null;
try (Formatter f = new Formatter()) {
if (i % 2 == 0) {
body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\"}", i).toString();
} else {
body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\", \"extraField\": true}", i).toString();
}
f.close();
}
template.requestBody("direct:insert", body);
}
assertEquals(100L, testCollection.count());
// Testing the update logic
Bson extraField = eq("extraField", true);
assertEquals("Number of records with 'extraField' flag on must equal 50", 50L, testCollection.count(extraField));
assertEquals("Number of records with 'scientist' field = Darwin on must equal 0", 0, testCollection.count(new Document("scientist", "Darwin")));
Bson updateObj = combine(set("scientist", "Darwin"), currentTimestamp("lastModified"));
String updates = "[" + extraField.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry()).toJson() + "," + updateObj.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry()).toJson() + "]";
Exchange resultExchange = template.request("direct:update", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(updates);
exchange.getIn().setHeader(MongoDbConstants.MULTIUPDATE, true);
}
});
Object result = resultExchange.getOut().getBody();
assertTrue(result instanceof UpdateResult);
assertEquals("Number of records updated header should equal 50", 50L, resultExchange.getOut().getHeader(MongoDbConstants.RECORDS_AFFECTED));
assertEquals("Number of records with 'scientist' field = Darwin on must equal 50 after update", 50, testCollection.count(new Document("scientist", "Darwin")));
}
use of java.util.Formatter in project camel by apache.
the class MongoDbOperationsTest method testColStats.
@Test
public void testColStats() throws Exception {
assertEquals(0, testCollection.count());
// Add some records to the collection (and do it via camel-mongodb)
for (int i = 1; i <= 100; i++) {
String body = null;
try (Formatter f = new Formatter()) {
body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\"}", i).toString();
f.close();
}
template.requestBody("direct:insert", body);
}
Object result = template.requestBody("direct:getColStats", "irrelevantBody");
assertTrue("Result is not of type Document", result instanceof Document);
assertTrue("The result should contain keys", Document.class.cast(result).keySet().size() > 0);
}
use of java.util.Formatter in project platform_frameworks_base by android.
the class MediaController method initControllerView.
private void initControllerView(View v) {
Resources res = mContext.getResources();
mPlayDescription = res.getText(com.android.internal.R.string.lockscreen_transport_play_description);
mPauseDescription = res.getText(com.android.internal.R.string.lockscreen_transport_pause_description);
mPauseButton = (ImageButton) v.findViewById(com.android.internal.R.id.pause);
if (mPauseButton != null) {
mPauseButton.requestFocus();
mPauseButton.setOnClickListener(mPauseListener);
}
mFfwdButton = (ImageButton) v.findViewById(com.android.internal.R.id.ffwd);
if (mFfwdButton != null) {
mFfwdButton.setOnClickListener(mFfwdListener);
if (!mFromXml) {
mFfwdButton.setVisibility(mUseFastForward ? View.VISIBLE : View.GONE);
}
}
mRewButton = (ImageButton) v.findViewById(com.android.internal.R.id.rew);
if (mRewButton != null) {
mRewButton.setOnClickListener(mRewListener);
if (!mFromXml) {
mRewButton.setVisibility(mUseFastForward ? View.VISIBLE : View.GONE);
}
}
// By default these are hidden. They will be enabled when setPrevNextListeners() is called
mNextButton = (ImageButton) v.findViewById(com.android.internal.R.id.next);
if (mNextButton != null && !mFromXml && !mListenersSet) {
mNextButton.setVisibility(View.GONE);
}
mPrevButton = (ImageButton) v.findViewById(com.android.internal.R.id.prev);
if (mPrevButton != null && !mFromXml && !mListenersSet) {
mPrevButton.setVisibility(View.GONE);
}
mProgress = (ProgressBar) v.findViewById(com.android.internal.R.id.mediacontroller_progress);
if (mProgress != null) {
if (mProgress instanceof SeekBar) {
SeekBar seeker = (SeekBar) mProgress;
seeker.setOnSeekBarChangeListener(mSeekListener);
}
mProgress.setMax(1000);
}
mEndTime = (TextView) v.findViewById(com.android.internal.R.id.time);
mCurrentTime = (TextView) v.findViewById(com.android.internal.R.id.time_current);
mFormatBuilder = new StringBuilder();
mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
installPrevNextListeners();
}
use of java.util.Formatter in project robovm by robovm.
the class OldFormattableTest method testFormatTo.
public void testFormatTo() {
Formatter fmt = new Formatter();
Mock_Formattable mf = new Mock_Formattable();
assertTrue(fmt.format("%1.1s", mf).toString().equals("single precision "));
assertTrue(fmt.format("%2.1s", mf).toString().equals("single precision single precision "));
assertTrue(fmt.format("%2.2s", mf).toString().equals("single precision single precision double precision "));
assertTrue(mf.isFormatToCalled());
}
use of java.util.Formatter in project robovm by robovm.
the class OldFormatterTest method test_Formattable.
public void test_Formattable() {
Formattable ones = new Formattable() {
public void formatTo(Formatter formatter, int flags, int width, int precision) throws IllegalFormatException {
try {
formatter.out().append("111");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
Formattable twos = new Formattable() {
public void formatTo(Formatter formatter, int flags, int width, int precision) throws IllegalFormatException {
try {
formatter.out().append("222");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
assertEquals("aaa 111?", new Formatter().format("aaa %s?", ones).toString());
assertEquals("aaa 111 bbb 222?", new Formatter().format("aaa %s bbb %s?", ones, twos).toString());
}
Aggregations