Skip to content

Commit

Permalink
Refactor/cleanup TestShortString
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaDemianenko committed Sep 27, 2016
1 parent 0524f41 commit b3ba3bd
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 327 deletions.
Expand Up @@ -29,7 +29,6 @@
import org.neo4j.kernel.impl.AbstractNeo4jTestCase;
import org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine;
import org.neo4j.kernel.impl.store.PropertyStore;
import org.neo4j.kernel.impl.store.TestShortString;
import org.neo4j.test.rule.DatabaseRule;
import org.neo4j.test.rule.GraphTransactionRule;
import org.neo4j.test.rule.ImpermanentDatabaseRule;
Expand All @@ -42,7 +41,7 @@
import static org.neo4j.test.mockito.matcher.Neo4jMatchers.hasProperty;
import static org.neo4j.test.mockito.matcher.Neo4jMatchers.inTx;

public class TestShortStringProperties extends TestShortString
public class TestShortStringProperties
{
@ClassRule
public static DatabaseRule graphdb = new ImpermanentDatabaseRule();
Expand All @@ -55,7 +54,7 @@ public void commit()
tx.success();
}

public void newTx()
private void newTx()
{
tx.success();
tx.begin();
Expand Down Expand Up @@ -182,10 +181,122 @@ public void canRemoveShortStringProperty() throws Exception
assertThat( node, inTx( db, not( hasProperty( "key" ) ) ) );
}

// === reuse the test cases from the encoding ===
@Test
public void canEncodeEmptyString() throws Exception
{
assertCanEncode( "" );
}

@Test
public void canEncodeReallyLongString() throws Exception
{
assertCanEncode( " " ); // 20 spaces
assertCanEncode( " " ); // 16 spaces
}

@Test
public void canEncodeFifteenSpaces() throws Exception
{
assertCanEncode( " " );
}

@Test
public void canEncodeNumericalString() throws Exception
{
assertCanEncode( "0123456789+,'.-" );
assertCanEncode( " ,'.-0123456789" );
assertCanEncode( "+ '.0123456789-" );
assertCanEncode( "+, 0123456789.-" );
assertCanEncode( "+,0123456789' -" );
assertCanEncode( "+0123456789,'. " );
// IP(v4) numbers
assertCanEncode( "192.168.0.1" );
assertCanEncode( "127.0.0.1" );
assertCanEncode( "255.255.255.255" );
}

@Test
public void canEncodeTooLongStringsWithCharsInDifferentTables()
throws Exception
{
assertCanEncode( "____________+" );
assertCanEncode( "_____+_____" );
assertCanEncode( "____+____" );
assertCanEncode( "HELLO world" );
assertCanEncode( "Hello_World" );
}

@Test
public void canEncodeUpToNineEuropeanChars() throws Exception
{
// Shorter than 10 chars
assertCanEncode( "fågel" ); // "bird" in Swedish
assertCanEncode( "påfågel" ); // "peacock" in Swedish
assertCanEncode( "påfågelö" ); // "peacock island" in Swedish
assertCanEncode( "påfågelön" ); // "the peacock island" in Swedish
// 10 chars
assertCanEncode( "påfågelöar" ); // "peacock islands" in Swedish
}

@Test
public void canEncodeEuropeanCharsWithPunctuation() throws Exception
{
assertCanEncode( "qHm7 pp3" );
assertCanEncode( "UKKY3t.gk" );
}

@Test
public void canEncodeAlphanumerical() throws Exception
{
assertCanEncode( "1234567890" ); // Just a sanity check
assertCanEncodeInBothCasings( "HelloWor1d" ); // There is a number there
assertCanEncode( " " ); // Alphanum is the first that can encode 10 spaces
assertCanEncode( "_ _ _ _ _ " ); // The only available punctuation
assertCanEncode( "H3Lo_ or1D" ); // Mixed case + punctuation
assertCanEncode( "q1w2e3r4t+" ); // + is not in the charset
}

@Test
public void canEncodeHighUnicode() throws Exception
{
assertCanEncode( "\u02FF" );
assertCanEncode( "hello\u02FF" );
}

@Test
public void canEncodeLatin1SpecialChars() throws Exception
{
assertCanEncode( "#$#$#$#" );
assertCanEncode( "$hello#" );
}

@Test
public void canEncodeTooLongLatin1String() throws Exception
{
assertCanEncode( "#$#$#$#$" );
}

@Test
public void canEncodeLowercaseAndUppercaseStringsUpTo12Chars() throws Exception
{
assertCanEncodeInBothCasings( "hello world" );
assertCanEncode( "hello_world" );
assertCanEncode( "_hello_world" );
assertCanEncode( "hello::world" );
assertCanEncode( "hello//world" );
assertCanEncode( "hello world" );
assertCanEncode( "http://ok" );
assertCanEncode( "::::::::" );
assertCanEncode( " _.-:/ _.-:/" );
}

private void assertCanEncodeInBothCasings( String string )
{
assertCanEncode( string.toLowerCase() );
assertCanEncode( string.toUpperCase() );
}

@Override
protected void assertCanEncode( String string )
private void assertCanEncode( String string )
{
encode( string, true );
}
Expand Down
Expand Up @@ -21,9 +21,9 @@

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import org.neo4j.kernel.impl.store.TestShortString.Charset;
import org.neo4j.kernel.impl.store.record.PropertyBlock;

import static org.junit.Assert.assertEquals;
Expand All @@ -36,10 +36,12 @@ public class TestLongerShortString

@Test
public void testMasks() throws Exception {
assertEquals(0,1 & LongerShortString.invertedBitMask(LongerShortString.NUMERICAL));
assertEquals(0,2 & LongerShortString.invertedBitMask(LongerShortString.DATE));
assertEquals(LongerShortString.NUMERICAL.bitMask(),3 & LongerShortString.invertedBitMask(LongerShortString.DATE));
assertEquals(0, (LongerShortString.NUMERICAL.bitMask()|LongerShortString.NUMERICAL.bitMask()) & LongerShortString.invertedBitMask(LongerShortString.NUMERICAL, LongerShortString.DATE));
assertEquals( 0, 1 & LongerShortString.invertedBitMask( LongerShortString.NUMERICAL ) );
assertEquals( 0, 2 & LongerShortString.invertedBitMask( LongerShortString.DATE ) );
assertEquals( LongerShortString.NUMERICAL.bitMask(),
3 & LongerShortString.invertedBitMask( LongerShortString.DATE ) );
assertEquals( 0, (LongerShortString.NUMERICAL.bitMask() | LongerShortString.NUMERICAL.bitMask()) &
LongerShortString.invertedBitMask( LongerShortString.NUMERICAL, LongerShortString.DATE ) );
}

@Test
Expand Down Expand Up @@ -67,9 +69,9 @@ public void testRandomStrings() throws Exception
{
for ( int i = 0; i < 1000; i++ )
{
for ( Charset charset : Charset.values() )
for ( TestStringCharset charset : TestStringCharset.values() )
{
List<String> list = TestShortString.randomStrings( 100, charset, 30 );
List<String> list = randomStrings( 100, charset, 30 );
for ( String string : list )
{
PropertyBlock record = new PropertyBlock();
Expand Down Expand Up @@ -130,6 +132,16 @@ public void canEncodeUUIDString() throws Exception
assertCanEncodeAndDecodeToSame( "81fe144f-484b-4a34-8e36-17a021540318" );
}

private static List<String> randomStrings( int count, TestStringCharset charset, int maxLen )
{
List<String> result = new ArrayList<>( count );
for ( int i = 0; i < count; i++ )
{
result.add( charset.randomString( maxLen ) );
}
return result;
}

private void assertCanEncodeAndDecodeToSame( String string )
{
assertCanEncodeAndDecodeToSame( string, DEFAULT_PAYLOAD_SIZE );
Expand Down

0 comments on commit b3ba3bd

Please sign in to comment.