Search in sources :

Example 46 with Formatter

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")));
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Formatter(java.util.Formatter) Document(org.bson.Document) UpdateResult(com.mongodb.client.result.UpdateResult) Bson(org.bson.conversions.Bson) Test(org.junit.Test)

Example 47 with Formatter

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);
}
Also used : Formatter(java.util.Formatter) Document(org.bson.Document) Test(org.junit.Test)

Example 48 with Formatter

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();
}
Also used : Formatter(java.util.Formatter) Resources(android.content.res.Resources)

Example 49 with Formatter

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());
}
Also used : Formatter(java.util.Formatter)

Example 50 with Formatter

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());
}
Also used : Formatter(java.util.Formatter) IOException(java.io.IOException) Formattable(java.util.Formattable)

Aggregations

Formatter (java.util.Formatter)558 ArrayList (java.util.ArrayList)26 File (java.io.File)25 IOException (java.io.IOException)25 Date (java.util.Date)22 Test (org.junit.Test)19 HashMap (java.util.HashMap)16 Map (java.util.Map)16 AlertDialog (android.app.AlertDialog)14 DialogInterface (android.content.DialogInterface)14 MessageDigest (java.security.MessageDigest)14 PrintWriter (java.io.PrintWriter)13 Justif (aQute.lib.justif.Justif)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 Locale (java.util.Locale)11 BigInteger (java.math.BigInteger)10 PrintStream (java.io.PrintStream)9 Calendar (java.util.Calendar)7 LayoutBuilder (android.text.StaticLayoutTest.LayoutBuilder)6 View (android.view.View)6