javamanager/debugapi/tsrc/src.s60/testjavadiagnostic.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Tests for Java Debug API
       
    15 *
       
    16 */
       
    17 
       
    18 #include <memory>
       
    19 #include <flogger.h>
       
    20 
       
    21 #include "TestHarness.h"
       
    22 #include "javadiagnostic.h"
       
    23 #include "javaredirector.h"
       
    24 #include "diagnosticimpl.h"
       
    25 
       
    26 #include "logger.h"
       
    27 
       
    28 using namespace java::debug;
       
    29 using namespace java::util;
       
    30 
       
    31 _LIT8(KText1,"");
       
    32 _LIT8(KText2," ");
       
    33 _LIT8(KText3,"\n");
       
    34 _LIT8(KText4,"hello world\r\n");
       
    35 _LIT8(KText5,"this is\r\n multiline\r\n example string with\t tabs\r\n");
       
    36 _LIT8(KText6,"very long string                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      indeed\r\n");
       
    37 _LIT8(KText7,"0123456789abcdefghijklmnopqrstyvwxyz\r\n");
       
    38 _LIT8(KText8,"*-.,/\"%&()=?\r\n");
       
    39 
       
    40 class TestListener: public DiagnosticListener
       
    41 {
       
    42 public:
       
    43     TestListener() : mSystemOutCount(0), mSystemErrCount(0), mLogCount(0)
       
    44     {
       
    45         mData = HBufC8::New(2000);
       
    46     };
       
    47     ~TestListener()
       
    48     {
       
    49         delete mData;
       
    50     };
       
    51 
       
    52     virtual void systemOut(const TDesC8& aData)
       
    53     {
       
    54         *mData = aData;
       
    55         mSystemOutCount++;
       
    56     };
       
    57     virtual void systemErr(const TDesC8& aData)
       
    58     {
       
    59         *mData = aData;
       
    60         mSystemErrCount++;
       
    61     };
       
    62     virtual void log(const TDesC8 &aData)
       
    63     {
       
    64         *mData = aData;
       
    65         mLogCount++;
       
    66     };
       
    67 
       
    68     bool operator==(const TDesC8& aRhs) const
       
    69     {
       
    70         if (aRhs.Compare(mData->Des()) == 0)
       
    71             return true;
       
    72         return false;
       
    73     };
       
    74 
       
    75     TInt mSystemOutCount;
       
    76     TInt mSystemErrCount;
       
    77     TInt mLogCount;
       
    78 
       
    79 private:
       
    80     HBufC8* mData;
       
    81 };
       
    82 
       
    83 class LoggingListener : public TestListener
       
    84 {
       
    85 public:
       
    86     virtual void systemOut(const TDesC8& aData)
       
    87     {
       
    88         flog(aData);
       
    89         TestListener::systemOut(aData);
       
    90     };
       
    91     virtual void systemErr(const TDesC8& aData)
       
    92     {
       
    93         flog(aData);
       
    94         TestListener::systemErr(aData);
       
    95     };
       
    96     virtual void log(const TDesC8& aData)
       
    97     {
       
    98         flog(aData);
       
    99         TestListener::log(aData);
       
   100     };
       
   101 
       
   102 protected:
       
   103     virtual void flog(const TDesC8& aData)
       
   104     {
       
   105         _LIT(KLogDirectory, "debug");   // create e.g. c:\logs\java to get logs
       
   106         _LIT(KLogFileName, "javadiagnostic.txt");
       
   107         RFileLogger::Write(KLogDirectory, KLogFileName, EFileLoggingModeAppendRaw, aData);
       
   108     };
       
   109 };
       
   110 
       
   111 class DiagnosticImplTester : public DiagnosticImpl
       
   112 {
       
   113 public:
       
   114     using DiagnosticImpl::DoCancel;
       
   115     using DiagnosticImpl::RunError;
       
   116 };
       
   117 
       
   118 TEST_GROUP(TestJavaDiagnostic)
       
   119 {
       
   120     TEST_SETUP()
       
   121     {
       
   122     }
       
   123 
       
   124     TEST_TEARDOWN()
       
   125     {
       
   126     }
       
   127 };
       
   128 
       
   129 
       
   130 TEST(TestJavaDiagnostic, createInstance)
       
   131 {
       
   132     std::auto_ptr<JavaDiagnostic> jd(JavaDiagnostic::createInstance());
       
   133     CHECK(jd.get());
       
   134 }
       
   135 
       
   136 TEST(TestJavaDiagnostic, setRemoveListener)
       
   137 {
       
   138     std::auto_ptr<JavaDiagnostic> jd(JavaDiagnostic::createInstance());
       
   139     CHECK(jd.get());
       
   140 
       
   141     TestListener listener;
       
   142     TInt rc = jd->setDiagnosticListener(listener);
       
   143     CHECK(rc == KErrNone);
       
   144     jd->removeDiagnosticListener();
       
   145 }
       
   146 
       
   147 TEST(TestJavaDiagnostic, setRemoveListenerTwice)
       
   148 {
       
   149     std::auto_ptr<JavaDiagnostic> jd(JavaDiagnostic::createInstance());
       
   150     CHECK(jd.get());
       
   151 
       
   152     TestListener listener;
       
   153     TInt rc = jd->setDiagnosticListener(listener);
       
   154     CHECK(rc == KErrNone);
       
   155     TestListener listener2;
       
   156     rc = jd->setDiagnosticListener(listener2);
       
   157     CHECK(rc == KErrAlreadyExists);
       
   158 
       
   159     jd->removeDiagnosticListener();
       
   160     jd->removeDiagnosticListener();
       
   161 }
       
   162 
       
   163 TEST(TestJavaDiagnostic, noRemoveListener)
       
   164 {
       
   165     std::auto_ptr<JavaDiagnostic> jd(JavaDiagnostic::createInstance());
       
   166     CHECK(jd.get());
       
   167 
       
   168     TestListener listener;
       
   169     jd->setDiagnosticListener(listener);
       
   170 }
       
   171 
       
   172 TEST(TestJavaDiagnostic, noSetListener)
       
   173 {
       
   174     std::auto_ptr<JavaDiagnostic> jd(JavaDiagnostic::createInstance());
       
   175     CHECK(jd.get());
       
   176 
       
   177     jd->removeDiagnosticListener();
       
   178 }
       
   179 
       
   180 TEST(TestJavaDiagnostic, serverNotRunning)
       
   181 {
       
   182     Redirector r;
       
   183     TInt rc = r.connect();
       
   184     CHECK(rc != KErrNone);
       
   185 
       
   186     rc = r.systemOut(KText1);
       
   187     CHECK(rc != KErrNone);
       
   188     rc = r.systemErr(KText1);
       
   189     CHECK(rc != KErrNone);
       
   190     rc = Redirector::log(KText1);
       
   191     CHECK(rc != KErrNone);
       
   192 
       
   193     r.close();
       
   194 }
       
   195 
       
   196 TEST(TestJavaDiagnostic, redirectorNotConnected)
       
   197 {
       
   198     Redirector r;
       
   199 
       
   200     TInt rc = r.systemOut(KText1);
       
   201     CHECK(rc != KErrNone);
       
   202     rc = r.systemErr(KText1);
       
   203     CHECK(rc != KErrNone);
       
   204     rc = Redirector::log(KText1);
       
   205     CHECK(rc != KErrNone);
       
   206 
       
   207     r.close();
       
   208 }
       
   209 
       
   210 TEST(TestJavaDiagnostic, alreadyRunning)
       
   211 {
       
   212     std::auto_ptr<JavaDiagnostic> jd1(JavaDiagnostic::createInstance());
       
   213     CHECK(jd1.get());
       
   214 
       
   215     TestListener listener;
       
   216     TInt rc = jd1->setDiagnosticListener(listener);
       
   217     CHECK(rc == KErrNone);
       
   218 
       
   219     std::auto_ptr<JavaDiagnostic> jd2(JavaDiagnostic::createInstance());
       
   220     CHECK(jd2.get());
       
   221 
       
   222     rc = jd2->setDiagnosticListener(listener);
       
   223     CHECK(rc != KErrNone);
       
   224 }
       
   225 
       
   226 
       
   227 TEST(TestJavaDiagnostic, systemOut)
       
   228 {
       
   229     std::auto_ptr<JavaDiagnostic> jd(JavaDiagnostic::createInstance());
       
   230     CHECK(jd.get());
       
   231 
       
   232     TestListener listener;
       
   233     jd->setDiagnosticListener(listener);
       
   234 
       
   235     Redirector r;
       
   236     TInt rc = r.connect();
       
   237     CHECK(rc == KErrNone);
       
   238 
       
   239     rc = r.systemOut(KText1);
       
   240     CHECK(rc == KErrNone);
       
   241     CHECK(listener == KText1);
       
   242     CHECK(listener.mSystemOutCount == 1);
       
   243 
       
   244     rc = r.systemOut(KText2);
       
   245     CHECK(rc == KErrNone);
       
   246     CHECK(listener == KText2);
       
   247     CHECK(listener.mSystemOutCount == 2);
       
   248 
       
   249     rc = r.systemOut(KText3);
       
   250     CHECK(rc == KErrNone);
       
   251     CHECK(listener == KText3);
       
   252     CHECK(listener.mSystemOutCount == 3);
       
   253 
       
   254     rc = r.systemOut(KText4);
       
   255     CHECK(rc == KErrNone);
       
   256     CHECK(listener == KText4);
       
   257     CHECK(listener.mSystemOutCount == 4);
       
   258 
       
   259     rc = r.systemOut(KText5);
       
   260     CHECK(rc == KErrNone);
       
   261     CHECK(listener == KText5);
       
   262     CHECK(listener.mSystemOutCount == 5);
       
   263 
       
   264     rc = r.systemOut(KText6);
       
   265     CHECK(rc == KErrNone);
       
   266     CHECK(listener == KText6);
       
   267     CHECK(listener.mSystemOutCount == 6);
       
   268 
       
   269     rc = r.systemOut(KText7);
       
   270     CHECK(rc == KErrNone);
       
   271     CHECK(listener == KText7);
       
   272     CHECK(listener.mSystemOutCount == 7);
       
   273 
       
   274     rc = r.systemOut(KText8);
       
   275     CHECK(rc == KErrNone);
       
   276     CHECK(listener == KText8);
       
   277     CHECK(listener.mSystemOutCount == 8);
       
   278 
       
   279     r.close();
       
   280     jd->removeDiagnosticListener();
       
   281 }
       
   282 
       
   283 TEST(TestJavaDiagnostic, systemErr)
       
   284 {
       
   285     std::auto_ptr<JavaDiagnostic> jd(JavaDiagnostic::createInstance());
       
   286     CHECK(jd.get());
       
   287 
       
   288     TestListener listener;
       
   289     jd->setDiagnosticListener(listener);
       
   290 
       
   291     Redirector r;
       
   292     TInt rc = r.connect();
       
   293     CHECK(rc == KErrNone);
       
   294 
       
   295     rc = r.systemErr(KText1);
       
   296     CHECK(rc == KErrNone);
       
   297     CHECK(listener == KText1);
       
   298     CHECK(listener.mSystemErrCount == 1);
       
   299 
       
   300     rc = r.systemErr(KText2);
       
   301     CHECK(rc == KErrNone);
       
   302     CHECK(listener == KText2);
       
   303     CHECK(listener.mSystemErrCount == 2);
       
   304 
       
   305     rc = r.systemErr(KText3);
       
   306     CHECK(rc == KErrNone);
       
   307     CHECK(listener == KText3);
       
   308     CHECK(listener.mSystemErrCount == 3);
       
   309 
       
   310     rc = r.systemErr(KText4);
       
   311     CHECK(rc == KErrNone);
       
   312     CHECK(listener == KText4);
       
   313     CHECK(listener.mSystemErrCount == 4);
       
   314 
       
   315     rc = r.systemErr(KText5);
       
   316     CHECK(rc == KErrNone);
       
   317     CHECK(listener == KText5);
       
   318     CHECK(listener.mSystemErrCount == 5);
       
   319 
       
   320     rc = r.systemErr(KText6);
       
   321     CHECK(rc == KErrNone);
       
   322     CHECK(listener == KText6);
       
   323     CHECK(listener.mSystemErrCount == 6);
       
   324 
       
   325     rc = r.systemErr(KText7);
       
   326     CHECK(rc == KErrNone);
       
   327     CHECK(listener == KText7);
       
   328     CHECK(listener.mSystemErrCount == 7);
       
   329 
       
   330     rc = r.systemErr(KText8);
       
   331     CHECK(rc == KErrNone);
       
   332     CHECK(listener == KText8);
       
   333     CHECK(listener.mSystemErrCount == 8);
       
   334 
       
   335     jd->removeDiagnosticListener();
       
   336 }
       
   337 
       
   338 TEST(TestJavaDiagnostic, log)
       
   339 {
       
   340     std::auto_ptr<JavaDiagnostic> jd(JavaDiagnostic::createInstance());
       
   341     CHECK(jd.get());
       
   342 
       
   343     LoggingListener listener;
       
   344     jd->setDiagnosticListener(listener);
       
   345 
       
   346     TInt rc = Redirector::log(KText1);
       
   347     CHECK(rc == KErrNone);
       
   348     CHECK(listener == KText1);
       
   349     CHECK(listener.mLogCount == 1);
       
   350 
       
   351     rc = Redirector::log(KText2);
       
   352     CHECK(rc == KErrNone);
       
   353     CHECK(listener == KText2);
       
   354     CHECK(listener.mLogCount == 2);
       
   355 
       
   356     rc = Redirector::log(KText3);
       
   357     CHECK(rc == KErrNone);
       
   358     CHECK(listener == KText3);
       
   359     CHECK(listener.mLogCount == 3);
       
   360 
       
   361     rc = Redirector::log(KText4);
       
   362     CHECK(rc == KErrNone);
       
   363     CHECK(listener == KText4);
       
   364     CHECK(listener.mLogCount == 4);
       
   365 
       
   366     rc = Redirector::log(KText5);
       
   367     CHECK(rc == KErrNone);
       
   368     CHECK(listener == KText5);
       
   369     CHECK(listener.mLogCount == 5);
       
   370 
       
   371     rc = Redirector::log(KText6);
       
   372     CHECK(rc == KErrNone);
       
   373     CHECK(listener == KText6);
       
   374     CHECK(listener.mLogCount == 6);
       
   375 
       
   376     rc = Redirector::log(KText7);
       
   377     CHECK(rc == KErrNone);
       
   378     CHECK(listener == KText7);
       
   379     CHECK(listener.mLogCount == 7);
       
   380 
       
   381     rc = Redirector::log(KText8);
       
   382     CHECK(rc == KErrNone);
       
   383     CHECK(listener == KText8);
       
   384     CHECK(listener.mLogCount == 8);
       
   385 
       
   386     jd->removeDiagnosticListener();
       
   387 }
       
   388 
       
   389 
       
   390 TEST(TestJavaDiagnostic, loadTest)
       
   391 {
       
   392     std::auto_ptr<JavaDiagnostic> jd(JavaDiagnostic::createInstance());
       
   393     CHECK(jd.get());
       
   394 
       
   395     TestListener listener;
       
   396     jd->setDiagnosticListener(listener);
       
   397 
       
   398     Redirector r;
       
   399     TInt rc = r.connect();
       
   400     CHECK(rc == KErrNone);
       
   401 
       
   402     for (int i = 0; i< 1000; i++)
       
   403     {
       
   404         rc = r.systemOut(KText1);
       
   405         CHECK(rc == KErrNone);
       
   406         rc = r.systemErr(KText1);
       
   407         CHECK(rc == KErrNone);
       
   408         rc = Redirector::log(KText1);
       
   409         CHECK(rc == KErrNone);
       
   410     }
       
   411     CHECK(listener.mSystemOutCount == 1000);
       
   412     CHECK(listener.mSystemErrCount == 1000);
       
   413     CHECK(listener.mLogCount == 1000);
       
   414 
       
   415     r.close();
       
   416     jd->removeDiagnosticListener();
       
   417 }
       
   418 
       
   419 
       
   420 TEST(TestJavaDiagnostic, logger)
       
   421 {
       
   422     std::auto_ptr<JavaDiagnostic> jd(JavaDiagnostic::createInstance());
       
   423     CHECK(jd.get());
       
   424 
       
   425     LoggingListener listener;
       
   426     jd->setDiagnosticListener(listener);
       
   427 
       
   428     ELOG(EDebugApi, "error level text");
       
   429     CHECK(listener.mLogCount == 1);
       
   430 
       
   431     WLOG(EDebugApi, "warning level text");
       
   432     CHECK(listener.mLogCount == 2);
       
   433 
       
   434     LOG(EDebugApi, EInfo, "info level text");
       
   435     CHECK(listener.mLogCount == 2);
       
   436 
       
   437     jd->removeDiagnosticListener();
       
   438 }
       
   439 
       
   440 TEST(TestJavaDiagnostic, unusedMethods)
       
   441 {
       
   442     std::auto_ptr<DiagnosticImplTester> d((DiagnosticImplTester*)JavaDiagnostic::createInstance());
       
   443 
       
   444     d->DoCancel();
       
   445     TInt rc = d->RunError(KErrGeneral);
       
   446     CHECK(rc == KErrNone);
       
   447 }