Watch, Follow, &
Connect with Us

Embarcadero Blogs

Latest Posts


Random Thoughts on the Passing Scene #151


posted @ Fri, 19 Mar 2010 00:22:03 +0000 by Nick Hodges


Random Thoughts on the Passing Scene #150

  • David I is doing another webinar:  RAD/AJAX-based Web Application Development using RAD Studio 2010.  He is giving it three different times on Thursday, March 18, 2010, so no matter where you are in the world, you should be able to tune in.
  • Anders Ohlsson has wrapped up the Haiti auction, and when all is said and done, you all will have helped to contribute over US$26,000 to Haitian relief.  That’s amazing.  A huge thank you to all that bid and bought items, and to those that help spread the word.  You all were very generous and frequently paid quite a bit more than fair market value.  ;-)  Thanks, also, to folks like Anders Hejlsberg, Danny Thorpe, Chuck Jazdzewski, Zack Urlocker, Gary Whizin and others that made the effort to sign some of the items and increase their value.  A huge thank you to Anders who did the lion’s share of the work.  All in all, it was a great thing you all did.   Very cool.
  • Just two items today, as I wanted to get the David I seminar posted…..

posted @ Wed, 17 Mar 2010 16:59:39 +0000 by Nick Hodges


Fun With Testing DateUtils.pas #2

First of all, thanks for the help improving the CreateRandomDate function.  I confess that I didn’t spend enough time thinking on it, and I’ll also confess that you guys are way smarter than I am.  ;-)  I’ll post the “updated” version for your perusal in a separate post. 

In addition, you all were right – that code formatting sucks.   The plug-in I got for Live Writer was not very configurable.  I am now using John Kasters “YAPP” tool, and it all looks a lot better. 

Okay, back to the show…..

So to get going with unit testing DateUtils.pas, I naturally simply “plugged in” to our existing RTL unit test framework.  We have an existing RTL set of unit tests for running DUnit tests on the RTL.  I simply added the unit UnitTest.DateUtils.pas to the project, created the new class:


TDateUtilsTests = class(TTestCase)
end;

and I was in business.  From there, it’s merely a matter of declaring published methods that run the DUnit tests.

So, I started right at the top with DateOf and TimeOf. So, what to test? Well, the most obvious thing: Does DateOf actually return the date portion of a given TDateTime?  Well, lets create a TDateTime with a random time, then, lets create a TDate with the same date but no time at all, and see if DateOf can do it’s magic?


procedure TDateUtilsTests.Test_DateOf;
var
  TestDate: TDateTime;
  Expected: TDateTime;
  Result  : TDateTime;
begin
  TestDate := EncodeDateTime(1945, 12, 1, 1, 46, 13, 112);
  Expected := EncodeDate(1945, 12, 1);
  Result   := DateOf(TestDate);
  CheckTrue(SameDate(Result, Expected), 'Test date and Expected date were not the same.'
             + ' DateOf function failed. Test #1');
end;

So this is a pretty straightforward test – you create two dates, and see if they are the same after the call to DateOf.  Simple.

What if you, say, increment the time part by one millisecond.  Come on, that can’t hurt anything right?  Better make sure:


  // This test will fail if it gets run at 23:59.999 at night.
  // I'm willing to gamble that this will never happen.
  TestDate := Now;
  Expected := IncMillisecond(TestDate);
  Result   := DateOf(TestDate);
  CheckTrue(SameDate(Result, Expected), 'Test date and Expected date were not the same.'
          + ' DateOf function failed. Test #2');

Okay, those are some “positive test cases”.  (I have a bunch more different ones along these lines….)  What about testing the negative case?  That is, test where we know that the two dates should be different after the call, and we test to make sure that they are, indeed different.


  TestDate := Now;
  Expected := DateOf(IncDay(TestDate));
  Result   := DateOf(TestDate);
  CheckFalse(SameDate(Result, Expected), 'Test date and Expected date should have'
	+ ' been different but they weren''t.  Test #2');

I have a similar set of tests for TimeOf.  These are pretty basic, but that is where you start, right?  With the basics?  From there, I wrote tests that change only the milliseconds, the seconds, the minutes and the hours.  All should never allow the DateOf function to return anything other than the date.  For TimeOf, I do the same – change the year, month, and date and make sure the time is the same.  Then I purposefully change the time and make sure that the function actually does change the time. 

Now, some of you are going to chastise me for using other DateUtils.pas functions to write tests.  Two schools of thought on that.  One says that you should never rely on anything outside of the actually call being tested.  Another says to use those library functions because they’ll get tested all the more when used in other tests. I’m going to be following the latter philosophy, and as well see in a later post, this way of doing things actually will reveal a pretty significant bug in a routine that was used to test another routine.


posted @ Tue, 16 Mar 2010 18:26:50 +0000 by Nick Hodges


Specifying JDBC connection properties for Blackfish SQL

Recently, I needed to set the port for JDBC connection property for one of the Blackfish SQL databases we use on the Embarcadero Developer Network. After looking at the documentation online, I couldn’t find a way to set it in the connection string. (I’m not an expert on JDBC, but the port specification syntax appears to vary by vendor.)

So, I asked Adrian Andrei, and he referred to the source to determine the appropriate syntax.

You can specify some additional connection-specific properties as a name/value after the base connection string, like so:

jdbc:borland:dslocal:/acme/db/acme.jds;tempDir=/tmp;readonly=true;port=1234

In Blackfish SQL, the default port is 2508, so the following would set the default port:

jdbc:borland:dslocal:/acme/db/acme.jds;tempDir=/tmp;readonly=true;port=2508

This syntax should work in both JDBC URLs and programatically  if you are passing a Properties instance.


posted @ Tue, 16 Mar 2010 16:09:20 +0000 by John Kaster


Semana do DBA vídeos disponiveis para assistir online ou download

A todos aqueles que puderam participar da Semana do DBA online o nosso MUITO OBRIGADO! O evento foi um sucesso, obtendo mais de 1200 usuários conectados durante os 3 dias. Isso demonstra o alto nível das apresentações realizadas durante o evento. As apresentações tiveram como foco mostrar problemas atuais relacionados a bancos de dados e como [...]

posted @ Fri, 12 Mar 2010 14:00:42 +0000 by Andreano Lanusse (Portuguese)


Utilizando LINQ to Objetcs em Delphi Prism

Durante os últimos 10 anos as linguagens, frameworks e plataformas de desenvolvimento tiveram inúmeras evoluções, todos nós sabemos estas foram para melhor, mas em especial uma eu considero como uma das maiores inovações dos últimos 10 anos e se chama LINQ. LINQ é um conjunto de extensões para o .NET Framework que abrange operações de consulta [...]

posted @ Fri, 12 Mar 2010 10:47:34 +0000 by Andreano Lanusse (Portuguese)


Six months in a leaky boat

Yesterday I discovered a potential resource leak when using the dbExpress Ado.NET provider, which can cause connections to be created and never freed. This leak was discovered when I implemented and started consuming a helper method which allows me to easily determine whether a data reader contains a specified column.

class method DbUtils.ColumnExists(AReader: DbDataReader; AColumnName: String): Boolean;
begin
  var Table: DataTable := AReader.GetSchemaTable;
  Table.DefaultView.RowFilter := String.Format('ColumnName =''{0}''', AColumnName);
  Result := (table.DefaultView.Count > 0)
end;

The above code seemed pretty innocuous, but a bit of spelunking using Reflector discovered that the call to TAdoDbxDataReader.GetSchemaTable resulted in the associated TAdoDbxConnection cloning itself into a private variable, and this connection was never closed. So if this method is called multiple times using different connections, each call would result in an additional database connection being made which wasn’t being closed until the application exited. For the app that this bug was discovered in, this meant that an ever increasing number of connections were being established to an InterBase database, eventually resulting in stored procedure calls that never finished executing.

If you think you may be affected by this bug, please take a look at http://qc.embarcadero.com/wc/qcmain.aspx?d=82886, and vote/rate as you see fit. In addition to providing more details as to the cause of the leak, it also suggests the following possible workaround.

class method CloseConnection(AConnection: TAdoDbxConnection);
begin
  var Field: FieldInfo := AConnection.GetType.GetField('FClonedConnection',
    (BindingFlags.GetField or (BindingFlags.NonPublic or BindingFlags.Instance)));
  if (Field <> nil) then
  begin
    var Con: TAdoDbxConnection := (Field.GetValue(AConnection) as TAdoDbxConnection);
    if (Con <> nil) then
    begin
      Con.Close;
      Field.SetValue(AConnection, nil)
    end;
  end;
  AConnection.Close;
end;

Now, obviously this workaround is pretty brittle, and could easily break if the DBX team change their internal implementation of TAdoDbxConnection (especially if said change involved renaming FClonedConnection), but I have verified that it works against the release of DBX that currently ships with Delphi Prism. The DBX team have been made aware of this bug, so hopefully it will be resolved in a not too distant release, and give the above kludge a short life expectancy.


posted @ Thu, 11 Mar 2010 19:56:50 +0000 by David Clegg


Fun With Testing DateUtils.pas #1

Okay, so I’m a Development Manager. My job is to see to the health, welfare, productivity, effectiveness, and proper tasking of a big chunk of the RAD Studio development team. I share these duties with the excellent and capable Mike Devery.  I mainly manage the guys that work on the IDE, the RTL, and the frameworks.  I do things like make sure they are working on the right thing via our SCRUM interations, that they have good machines, nice chairs, vacations when they want them, the right keyboard, etc.  I manage the development process in that we on the “War Team” spend a lot of time triaging bugs, managing and defining requirements, tracking progress, finding better ways to write better code – you know, development manager stuff.

But, like all good development managers, my heart really is in coding.  And I don’t do much of that anymore.  So I’ve tried to keep my fingers in things by taking on some small development tasks where I can keep my skills up, stimulate my brain in that way, and not cause too much damage. 

What better way to do that than to write unit tests?  I’m a bit weird in that I actually like to write unit tests. I find them challenging and enjoy the “puzzle solving” aspect of it.  I like to try to find corner and edge cases where the tests might fail.  I like knowing that ever test I write means less work down the road because any regressions will be found sooner – hopefully immediately.  And I can write tests to my hearts content without worrying about breaking the product.  ;-)

So I started in on DateUtils.pas.  This is a pretty cool unit with a lot of good functionality, and it’s ripe for expanding on our unit tests.  It was written a while ago, and its unit test coverage wasn’t where it should be.  So in my “spare time” (hehe….) I’ve been writing unit tests for the routines in that unit.  It’s been pretty fun, and I think, too, that it’s been illustrative of how beneficial unit testing can be.  So I thought I’d write a series of blog posts about it, and this is the first one. 

So, one of the first things I realized was that I needed to be able to generate dates.  Now, I realized that you don’t want that many non-deterministic tests (or maybe you don’t want any at all – it depends).  But I need to be sure that many of the DateUtils.pas routines can pass with any date.  So I wrote the following routine to generate a legitimate but random date:


/// <summary>
///   This creates a random, valid date from year 1 to aYearRange
/// </summary>
function CreateRandomDate(aMakeItLeapYear: Boolean = False; aYearRange: Word = 2500): TDateTime;
var
  AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word;
begin
  AYear := Random(aYearRange) + 1;
  if (not IsLeapYear(AYear)) and (aMakeItLeapYear) then
  begin
    repeat
      Inc(AYear);
    until IsLeapYear(AYear);
  end;
  AMonth := Random(MonthsPerYear) + 1;
  if IsLeapYear(AYear) and (AMonth = 2) then
  begin
    ADay := Random(29) + 1;
  end else begin
    ADay := Random(28) + 1;
  end;
  AHour := Random(HoursPerDay) - 1;
  AMinute := Random(MinsPerHour) - 1;
  ASecond := Random(SecsPerMin) - 1;
  AMilliSecond := Random(MSecsPerSec);
  Result := EncodeDateTime(AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond);
end;

Now I’ll bet that you guys can come up with a better algorithm, but this works just fine for testing purposes and is pretty clear in what it does.  I use it to test, say, 1000 random dates against a routine that takes a Date as a parameter.  (By the way, it uses some constants from SysUtils and from DateUtils.)  I mix that in with tests that used constant dates every time.  I debated whether to do the random date thing (if a test fails, you can’t necessarily reproduce it), but I decided in favor of it because I’ll make sure all the tests clearly report the date that was failing, and because I wanted to test to make sure that any date would be handled correctly, and you simply can’t do that with a limited, fixed set of dates.  Constantly running a large set of random dates is as close as you can come to testing “every” date.

Another thing that I knew I’d need was to generate to do date testing is valid “Leap Days’”, that is, a valid February 29 date.  When you unit test, you are constantly looking for corner cases, and Leap Days are a corner case for dates.  Naturally, I’ll utilize CreateRandomDate to help out:


function GetRandomLeapDay: TDate;
begin
  Result := DateOf(CreateRandomDate(True));
  Result := RecodeDate(Result, YearOf(Result), 2, 29);
end;

So, now I can generate random dates, and easily create corner case LeapDays. 

I’ll probably also eventually add a GenerateRandomTime routine as well. 

That’s it for now – next time I’ll talk about the basics of how I got tests up and running. By the way, I am using DUnit.  We use DUnit extensively internally.


posted @ Wed, 10 Mar 2010 17:22:21 +0000 by Nick Hodges


Random Thoughts on the Passing Scene #149

  • If you are a registered user of Delphi 2010, you can now download a digital copy of Marco Cantu’s new book, Delphi 2010 Handbook. (From Marco’s Site: “The book covers all the new features of Delphi 2010 for Win32, from Extended RTTI to new IDE features, from Windows 7 support to the improved DataSnap architecture. This is a brand new book, there is no overlapping material with the Delphi 2007 Handbook and Delphi 2009 Handbook (which you can consider buying along with this book in printed or electronic format).”) I think you all know by now how great Marco’s stuff is.  You can also order a hard-copy of the book as well from Marco’s site.  (Marco now is using CreateSpace as his publishing center.  The CreateSpace guys are actually here in our current building – I play basketball with a couple of the guys that work there….)
  • Anders apparently can’t control himself and has put a few more items up for auction, including an autographed by Allen Bauer copy of Delphi 1.
  • Michael Swindell sent me this link today:  The Secret Origin of Windows or as he called it “How Turbo Pascal Shaped Windows 1.0”.  Read and enjoy.

posted @ Tue, 09 Mar 2010 21:42:28 +0000 by Nick Hodges


Two more RAD Studio/Delphi/C++Builder/Delphi Prism webinars this month

March has been a big month for development webinars on EDN.  We have two more webinars to go before we are finished: 

The other webinars that have recently taken place include:

Check out the RAD Studio in Action page for all the white papers, videos, articles, and webinar replay links.  You will learn from some of the top programming experts including Brian Long,  Bob Swart and Marco Cantu.  You’ll find the page at http://www.embarcadero.com/rad-in-action.


posted @ Tue, 09 Mar 2010 16:00:59 +0000 by David Intersimone


Server Response from: BLOGS1