<commit_before>/*! ******************************************************************************
 *
 * Pentaho Data Integration
 *
 * Copyright (C) 2002-2013 by Pentaho : http://www.pentaho.com
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.pentaho.di.version;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import org.junit.Before;
import org.junit.Test;

public class BuildVersionUnitTest {
  private EnvironmentVariableGetter mockEnvironmentVariableGetter;
  private ManifestGetter mockManifestGetter;

  private void setNewMocks() {
    mockEnvironmentVariableGetter = mock( EnvironmentVariableGetter.class );
    mockManifestGetter = mock( ManifestGetter.class );
    BuildVersion.environmentVariableGetter = mockEnvironmentVariableGetter;
    BuildVersion.manifestGetter = mockManifestGetter;
  }

  @Before
  public void setup() {
    // This is to clear state between each run, init mocks
    setNewMocks();
    BuildVersion.refreshInstance();
    setNewMocks();
  }

  private void initManifestGetter( String version, String revision, String date, String user ) {
    Manifest manifest = mock( Manifest.class );
    try {
      when( mockManifestGetter.getManifest() ).thenReturn( manifest );
    } catch ( Exception e ) {
      // wont happen
    }
    Attributes attributes = mock( Attributes.class );
    when( manifest.getMainAttributes() ).thenReturn( attributes );
    when( manifest.getMainAttributes().getValue( Attributes.Name.IMPLEMENTATION_VERSION ) ).thenReturn( version );
    when( manifest.getMainAttributes().getValue( Attributes.Name.SPECIFICATION_VERSION ) ).thenReturn( revision );
    when( manifest.getMainAttributes().getValue( "Compile-Timestamp" ) ).thenReturn( date );
    when( manifest.getMainAttributes().getValue( "Compile-User" ) ).thenReturn( user );
  }

  private void initEnvironmentVariableGetter( String version, String revision, String date, String user ) {
    try {
      when( mockEnvironmentVariableGetter.getEnvVarible( BuildVersion.KETTLE_BUILD_VERSION ) ).thenReturn( version );
      when( mockEnvironmentVariableGetter.getEnvVarible( BuildVersion.KETTLE_BUILD_REVISION ) ).thenReturn( revision );
      when( mockEnvironmentVariableGetter.getEnvVarible( BuildVersion.KETTLE_BUILD_DATE ) ).thenReturn( date );
      when( mockEnvironmentVariableGetter.getEnvVarible( BuildVersion.KETTLE_BUILD_USER ) ).thenReturn( user );
    } catch ( Exception e ) {
      // wont happen
    }
  }

  @Test
  public void testGetInstance() {
    initManifestGetter( null, null, null, null ); // it's possible to have a manifest w/ no version, which causes an NPE
                                                  // later...
    BuildVersion.refreshInstance();
    BuildVersion version = BuildVersion.getInstance();
    if ( version == null || version.getVersion() == null || version.getVersion().isEmpty() ) {
      fail( "Unable to retrieve BuildVersion" );
    }

    initManifestGetter( "version", null, null, null );
    BuildVersion.refreshInstance();
    version = BuildVersion.getInstance();
    if ( version == null || version.getVersion().isEmpty() ) {
      fail( "Unable to retrieve BuildVersion" );
    }
    BuildVersion version2 = BuildVersion.getInstance();
    if ( version2 != version ) {
      fail( "Build version is required to be singleton" );
    }
  }

  @Test
  public void testGetBuildDate() {
    initManifestGetter( null, null, new Date().toString(), null );
    BuildVersion.refreshInstance();
    BuildVersion version = BuildVersion.getInstance();
    String buildDate = version.getBuildDate();
    if ( buildDate == null || buildDate.isEmpty() ) {
      fail( "Unable to retrieve build date" );
    }

  }

  @Test
  public void testGetBuildDateAsLocalDate() {
    SimpleDateFormat sdf = new SimpleDateFormat( BuildVersion.JAR_BUILD_DATE_FORMAT );
    initManifestGetter( null, null, sdf.format( new Date() ), null );
    BuildVersion.refreshInstance();
    BuildVersion version = BuildVersion.getInstance();
    Date buildDate = version.getBuildDateAsLocalDate();
    if ( buildDate == null ) {
      fail( "Unable to retrieve build date as Local date" );
    }
  }
<commit_msg>Fix the unit test to also check the environment setters for null<commit_after>/*! ******************************************************************************
 *
 * Pentaho Data Integration
 *
 * Copyright (C) 2002-2013 by Pentaho : http://www.pentaho.com
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.pentaho.di.version;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import org.junit.Before;
import org.junit.Test;

public class BuildVersionUnitTest {
  private EnvironmentVariableGetter mockEnvironmentVariableGetter;
  private ManifestGetter mockManifestGetter;

  private void setNewMocks() {
    mockEnvironmentVariableGetter = mock( EnvironmentVariableGetter.class );
    mockManifestGetter = mock( ManifestGetter.class );
    BuildVersion.environmentVariableGetter = mockEnvironmentVariableGetter;
    BuildVersion.manifestGetter = mockManifestGetter;
  }

  @Before
  public void setup() {
    // This is to clear state between each run, init mocks
    setNewMocks();
    BuildVersion.refreshInstance();
    setNewMocks();
  }

  private void initManifestGetter( String version, String revision, String date, String user ) {
    Manifest manifest = mock( Manifest.class );
    try {
      when( mockManifestGetter.getManifest() ).thenReturn( manifest );
    } catch ( Exception e ) {
      // wont happen
    }
    Attributes attributes = mock( Attributes.class );
    when( manifest.getMainAttributes() ).thenReturn( attributes );
    when( manifest.getMainAttributes().getValue( Attributes.Name.IMPLEMENTATION_VERSION ) ).thenReturn( version );
    when( manifest.getMainAttributes().getValue( Attributes.Name.SPECIFICATION_VERSION ) ).thenReturn( revision );
    when( manifest.getMainAttributes().getValue( "Compile-Timestamp" ) ).thenReturn( date );
    when( manifest.getMainAttributes().getValue( "Compile-User" ) ).thenReturn( user );
  }

  private void initEnvironmentVariableGetter( String version, String revision, String date, String user ) {
    try {
      when( mockEnvironmentVariableGetter.getEnvVarible( BuildVersion.KETTLE_BUILD_VERSION ) ).thenReturn( version );
      when( mockEnvironmentVariableGetter.getEnvVarible( BuildVersion.KETTLE_BUILD_REVISION ) ).thenReturn( revision );
      when( mockEnvironmentVariableGetter.getEnvVarible( BuildVersion.KETTLE_BUILD_DATE ) ).thenReturn( date );
      when( mockEnvironmentVariableGetter.getEnvVarible( BuildVersion.KETTLE_BUILD_USER ) ).thenReturn( user );
    } catch ( Exception e ) {
      // wont happen
    }
  }

  @Test
  public void testGetInstance() {

    initManifestGetter( null, null, null, null ); // it's possible to have a manifest w/ no version, which causes an NPE
    BuildVersion.refreshInstance();
    BuildVersion version = BuildVersion.getInstance();
    if ( version == null || version.getVersion() == null || version.getVersion().isEmpty() ) {
      fail( "Unable to retrieve BuildVersion" );
    }

    initManifestGetter( "version", null, null, null );
    BuildVersion.refreshInstance();
    version = BuildVersion.getInstance();
    if ( version == null || version.getVersion().isEmpty() ) {
      fail( "Unable to retrieve BuildVersion" );
    }
    BuildVersion version2 = BuildVersion.getInstance();
    if ( version2 != version ) {
      fail( "Build version is required to be singleton" );
    }

    initManifestGetter( null, null, null, null ); // it's possible to have a manifest w/ no version, which causes an NPE
    initEnvironmentVariableGetter( null, null, null, null ); // it's possible to have a manifest w/ no version, which
                                                             // causes an NPE
    BuildVersion.refreshInstance();
    version = BuildVersion.getInstance();
    if ( version == null || version.getVersion() == null || version.getVersion().isEmpty() ) {
      fail( "Unable to retrieve BuildVersion" );
    }

    initEnvironmentVariableGetter( "version", null, null, null ); // it's possible to have a manifest w/ no version,
                                                                  // which causes an NPE
    BuildVersion.refreshInstance();
    version = BuildVersion.getInstance();
    if ( version == null || version.getVersion().isEmpty() ) {
      fail( "Unable to retrieve BuildVersion" );
    }
    version2 = BuildVersion.getInstance();
    if ( version2 != version ) {
      fail( "Build version is required to be singleton" );
    }

  }

  @Test
  public void testGetBuildDate() {
    initManifestGetter( null, null, new Date().toString(), null );
    BuildVersion.refreshInstance();
    BuildVersion version = BuildVersion.getInstance();
    String buildDate = version.getBuildDate();
    if ( buildDate == null || buildDate.isEmpty() ) {
      fail( "Unable to retrieve build date" );
    }

  }

  @Test
  public void testGetBuildDateAsLocalDate() {
    SimpleDateFormat sdf = new SimpleDateFormat( BuildVersion.JAR_BUILD_DATE_FORMAT );
    initManifestGetter( null, null, sdf.format( new Date() ), null );
    BuildVersion.refreshInstance();
    BuildVersion version = BuildVersion.getInstance();
    Date buildDate = version.getBuildDateAsLocalDate();
    if ( buildDate == null ) {
      fail( "Unable to retrieve build date as Local date" );
    }
  }

{'fraction_non_alphanumeric': 0.13993416955786853, 'fraction_numerical': 0.0026688017080330933, 'mean_word_length': 3.7981220657276995, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5526158', 'n_tokens_mistral': 2891, 'n_tokens_neox': 2882, 'n_words': 851}